Skip to content

Self-improving AI Employees for everyone

feat(agent): Context journal and tool-based Memory, on a single execution path

trohitgd0e1b816f88c
diff --git a/RFC-0002-AGENT-CONTEXT-AND-MEMORY.md b/RFC-0002-AGENT-CONTEXT-AND-MEMORY.md
new file mode 100644
index 00000000..f23c0ed4
--- /dev/null
+++ b/RFC-0002-AGENT-CONTEXT-AND-MEMORY.md
@@ -0,0 +1,357 @@
+# RFC-0002 — Agent Context V2 and Tool-Based Memory
+
+Status: Implementing
+Graph version: 2
+Compatibility boundary: new workflow generations only
+
+## 1. Summary
+
+OpenCompany separates two concepts that were previously conflated:
+
+- **Context** is the exact, backend-owned execution journal used to reconstruct
+ the next provider request.
+- **Memory** is an explicitly invoked tool used to store and retrieve durable
+ facts, decisions, and user preferences.
+
+The visible canvas nodes are declarative UI and policy surfaces. They never own
+the journal, provider bindings, tenant scope, mutation rules, compaction
+algorithm, or durable Memory items. Those responsibilities live in backend
+services and are reached through the plugin and tool registries.
+
+Each agent that declares the `requiresContext` capability has exactly one
+system-managed Context companion. A Context owns multiple isolated threads:
+
+1. an explicit chat/session ID selects a persistent session thread;
+2. otherwise a delegated task ID selects a task thread;
+3. otherwise the execution ID selects an execution thread.
+
+The same Memory tool may be shared by multiple agents. Contexts are never
+shared.
+
+## 2. Decisions
+
+- Reuse `MessageWireV2`, including ordered blocks, raw malformed tool
+ arguments, signed/thought blocks, and provider continuation state.
+- Preserve existing Temporal V1 histories. Context V2 is selected only by a
+ new graph/generation and uses new workflow/activity type names.
+- Keep Gemini on `generate_content`; an Interactions migration is out of
+ scope.
+- Do not reintroduce LangChain compatibility.
+- Do not put runtime state in node parameters, node outputs, workflow JSON,
+ status caches, ordinary WebSocket events, CloudEvents, or Temporal Event
+ History.
+- Use canonical IDs assigned by the existing workflow canonicalizer:
+ `<workflow_id>:context:<ordinal>`.
+- Memory survives Workflow Reset by default. Each Memory node may opt into
+ `reset_policy=clear`.
+- Provider/runtime fidelity is explicit. No integration claims access to
+ hidden state a provider does not expose.
+
+## 3. Plugin and UI contract
+
+The backend NodeSpec is the source of truth.
+
+### 3.1 Context node
+
+The `context` plugin is a passive, system-managed configuration node:
+
+```text
+context.output-context -> agent.input-context
+```
+
+Its parameters are policy only:
+
+```text
+compaction_mode: auto | native | portable | disabled
+trigger_ratio: float = 0.8
+context_window_override?: integer
+exact_tail_retention_count: integer
+```
+
+Its NodeSpec advertises `isContextPanel` and `systemManaged`. The frontend
+renders the panel and calls backend handlers; it does not create journals,
+choose thread IDs, calculate pressure, compact messages, or mutate epochs.
+
+### 3.2 Memory node
+
+`simpleMemory` remains the public workflow type but is a normal `ToolNode`:
+
+```text
+simpleMemory.output-tool -> agent.input-tools
+```
+
+The LLM-visible name is `memory`. Persisted configuration is limited to
+operator policy such as `reset_policy`. Model arguments use a separate,
+plugin-locked `ToolInput`; model arguments cannot override backend scope or
+configuration.
+
+The frontend Memory panel is a client of authorized backend CRUD/search
+handlers. It does not hold items or implement mutation semantics.
+
+## 4. Public backend contracts
+
+```text
+AgentContextRef {
+ workflow_id,
+ context_node_id,
+ generation,
+ thread_id,
+ epoch,
+ revision
+}
+
+AgentContextEvent {
+ sequence,
+ event_type,
+ message_wire_v2?,
+ payload_ref?,
+ operation_id,
+ provider,
+ previous_hash,
+ payload_hash
+}
+
+AgentContextCheckpoint {
+ provider,
+ strategy,
+ covers_through_sequence,
+ replay_payload_ref,
+ active_token_count,
+ source_revision,
+ source_hash
+}
+```
+
+`AgentContextStore` exposes:
+
+```text
+resolve_thread
+load_active
+append_transition
+prepare_compaction
+commit_checkpoint
+start_epoch
+fork_provider
+archive
+purge
+```
+
+It persists normalized thread, event, checkpoint, provider-binding,
+compaction-attempt, and optional hash-addressed blob records. Append and
+checkpoint activation are transactional, operation-ID idempotent, hash
+chained, revision guarded, and epoch fenced. `RuntimeMutation` stores only
+committed identifiers and hashes.
+
+## 5. Fidelity
+
+`provider_replayable` means the retained state can construct the provider's
+next public API request without losing exposed continuation data.
+
+| Runtime | Fidelity |
+|---|---|
+| OpenAI native | provider-replayable Responses output and encrypted reasoning |
+| Anthropic native | provider-replayable ordered blocks and signatures |
+| Gemini native | provider-replayable `generate_content` parts/thought signatures |
+| OpenAI-compatible | provider-replayable only where capability verification passes; otherwise portable |
+| Claude Code | exact observable stream plus explicit provider UUID binding |
+| Vertex | exact observable events plus interaction/environment binding |
+| Codex CLI | observable-only, non-resumable |
+| RLM | observable-only, non-resumable |
+
+No row or UI label implies retention of hidden provider/runtime state.
+
+## 6. Native transition order
+
+`run_native_agent_loop` accepts an optional bound Context transition sink.
+For every iteration it commits:
+
+1. effective request snapshot, including resolved messages/system instruction,
+ provider/model/settings, compiled tool definitions, attachments, and
+ dynamic tool surface;
+2. complete assistant `MessageWireV2`, before any requested tool executes;
+3. each tool result, validation error, execution error, or ambiguous outcome,
+ before another provider request;
+4. final response and usage through the same journal.
+
+Sink failure stops execution at the durability boundary. Progress broadcast
+failure remains observational.
+
+## 7. Usage and pressure
+
+Two counters are never conflated:
+
+- **Lifetime usage/cost** is append-only billing across provider and
+ compaction calls.
+- **Active context pressure** is retained next-request input plus requested
+ output headroom.
+
+Provider context adapters implement:
+
+```text
+capabilities(model)
+measure_active_context(rendered_request)
+compact(committed_prefix, policy)
+validate_replay(candidate)
+```
+
+Central compaction orchestration branches on capabilities, not provider names.
+
+## 8. Compaction invariants
+
+- Only committed prefixes are compacted.
+- A boundary never splits an assistant tool call from its tool results.
+- Final-only responses are eligible.
+- The active checkpoint plus exact event tail is the source; startup Markdown
+ is never a source.
+- A candidate must serialize for the provider, cover the claimed sequence, and
+ reduce active tokens before activation.
+- Activation is compare-and-swap against checkpoint and source hash.
+- Concurrent writes remain an exact uncompacted tail.
+- Raw journal events are immutable.
+- Failure records the attempt and preserves the prior active checkpoint and
+ pressure state.
+
+OpenAI and Anthropic may use verified native compaction. Gemini and unsupported
+providers use portable structured checkpoints. A provider switch starts a new
+epoch with a portable handoff while archiving opaque provider state.
+
+## 9. Memory tool contract
+
+One multi-operation schema is exposed:
+
+```text
+remember(content, title?, category?, tags?, expires_at?)
+recall(query, categories?, tags?, limit?, cursor?)
+list(categories?, tags?, limit?, cursor?)
+get(memory_id)
+update(memory_id, expected_version, patch)
+forget(memory_id, expected_version)
+```
+
+The backend derives the namespace from authenticated owner, workflow, and
+Memory node. Namespace fields are absent from `ToolInput`. Items use optimistic
+versions. Mutation receipts make retries idempotent. Expired items are omitted.
+SQL/FTS is authoritative and remains usable when embedding generation fails;
+embedding projections are rebuildable accelerators.
+
+Memory does not automatically inject prompts, recall vectors, persist
+transcripts, own provider identity, or compact Context.
+
+## 10. Temporal V2
+
+New identities isolate deterministic histories:
+
+```text
+AgentWorkflowV2
+agent.prepare_context.v2
+agent.execute_llm_step.v2
+agent.append_context.v2
+agent.compact_context.v2
+```
+
+The child workflow carries only `AgentContextRef`, operation IDs/hashes,
+iteration state, and pending tool identities. Activities load payloads from
+the Context store. Provider messages and large tool results do not enter Event
+History. The LLM activity retains the existing one-attempt retry policy;
+database commits retry idempotently. An ambiguous provider outcome is recorded
+and surfaced rather than silently rebilled.
+
+`AgentWorkflowV2` uses Continue-As-New with a bounded Context reference and
+pending state. Existing V1 native histories keep their recorded command
+sequence. Pre-native histories still require Reset.
+
+## 11. Graph normalization and lifecycle
+
+`normalize_workflow_graph` is the single versioned pipeline used by
+save/get/import/deploy and persisted-graph loading.
+
+For each legacy `simpleMemory -> input-memory` edge it:
+
+1. creates one Context for the destination agent;
+2. canonicalizes IDs;
+3. connects `output-context -> input-context`;
+4. reconnects Simple Memory through `output-tool -> input-tools`;
+5. stores recognized Markdown as an immutable `legacy_partial` artifact;
+6. moves recoverable Claude/Vertex bindings into Context;
+7. never converts transcript text into durable Memory items;
+8. reports that process-local vectors cannot be recovered.
+
+State import receipts are idempotent and committed before legacy topology is
+retired. APIs return the authoritative normalized graph and aliases; the
+frontend replaces its draft rather than reproducing migration rules.
+
+Backend lifecycle rules:
+
+- create/copy/hot-spawn/Agent-Builder add a fresh Context companion;
+- copying never copies a journal;
+- deleting an agent archives its Context;
+- deleting a required system edge is rejected or repaired;
+- a Context cannot attach to two agents;
+- capability discovery uses `requiresContext`, never renderer kind or a
+ hardcoded agent type list.
+
+## 12. Reset
+
+- Clear Context archives the current epoch, fences late writes, clears active
+ replay/bindings, and preserves Memory.
+- Clear Memory removes items only.
+- Workflow Reset always rotates/archives Context and execution state, then
+ applies each Memory node's `reset_policy`.
+- Todos retain their independent reset hook.
+- Permanent Context deletion is an explicit purge.
+
+## 13. Security and observability
+
+Ordinary broadcasts contain metadata only:
+
+```text
+context.updated
+context.compacted
+context.epoch.started
+memory.changed
+```
+
+Authorized, paginated backend handlers fetch journal or Memory content on
+demand. Provider bindings, signatures, recalled secrets, raw journal payloads,
+and Memory contents are excluded from workflow exports, logs, CloudEvents,
+status caches, generation `runtime_data`, and Temporal payloads. Server-side
+sanitization is authoritative; the client sanitizer remains defense in depth
+for legacy fields.
+
+## 14. Rollout
+
+1. Contracts, storage, node plugins, graph version.
+2. Context topology behind a generation cutover; optional shadow dual-write.
+3. Native transition sink and restart hash comparison.
+4. Context-backed in-process generations and checkpoint compaction.
+5. Temporal V2 and replay/Continue-As-New fixtures.
+6. Vertex, Claude Code, Codex, and RLM fidelity adapters.
+7. `ToolInput` and durable Memory; then remove automatic legacy injection.
+8. Automatic graph/state normalization.
+9. Retire V1 only when deployment telemetry reports no active V1 histories.
+
+There is no fixed compatibility-release deadline.
+
+## 15. Acceptance gates
+
+Tests cover exact ordering/hashes, provider serialization, malformed arguments,
+restart reconstruction, isolated thread kinds, concurrent append/CAS behavior,
+crash boundaries, final-only compaction, rollback, checkpoint-plus-tail replay,
+V1 and V2 Temporal replay, Continue-As-New, bounded payloads, provider binding
+semantics, explicit CLI fidelity, Memory isolation/idempotency/expiry/versioning
+and fallback search, Reset policies, lifecycle pairing/migration idempotency,
+authorized pagination, and export/log/event redaction.
+
+Release gates are the full backend suite, frontend Vitest, TypeScript
+typecheck, lint, and production build.
+
+## 16. Primary references
+
+- OpenAI conversation state:
+ <https://developers.openai.com/api/docs/guides/conversation-state>
+- OpenAI compaction:
+ <https://developers.openai.com/api/docs/guides/compaction>
+- Anthropic compaction:
+ <https://platform.claude.com/docs/en/build-with-claude/compaction>
+- Temporal Continue-As-New:
+ <https://docs.temporal.io/develop/python/continue-as-new>
diff --git a/client/package.json b/client/package.json
index ea247dc1..27dd7eb8 100644
--- a/client/package.json
+++ b/client/package.json
@@ -22,9 +22,6 @@
"@hookform/resolvers": "^5.2.2",
"@lobehub/icons": "^2.48.0",
"@lukemorales/query-key-factory": "^1.3.4",
- "@radix-ui/react-collapsible": "^1.1.12",
- "@radix-ui/react-dialog": "^1.1.15",
- "@radix-ui/react-slot": "^1.2.4",
"@tailwindcss/typography": "^0.5.19",
"@tanstack/query-sync-storage-persister": "^5.100.10",
"@tanstack/react-query": "^5.100.10",
@@ -36,7 +33,6 @@
"@types/react-dom": "^19.2.3",
"@uiw/react-json-view": "2.0.0-alpha.41",
"@vitejs/plugin-react": "^5.2.0",
- "autoprefixer": "^10.5.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
@@ -48,7 +44,6 @@
"idb-keyval": "^6.2.2",
"lucide-react": "^1.14.0",
"partysocket": "1.1.18",
- "postcss": "^8.5.18",
"prismjs": "^1.30.0",
"qrcode.react": "^4.2.0",
"radix-ui": "^1.4.3",
@@ -86,7 +81,6 @@
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.2",
"@testing-library/user-event": "^14.6.1",
- "@typescript-eslint/parser": "^8.65.0",
"@vitest/coverage-v8": "^4.1.8",
"babel-plugin-react-compiler": "1.0.0",
"jsdom": "^25.0.1",
diff --git a/client/src/Dashboard.tsx b/client/src/Dashboard.tsx
index 0512edfa..3dbd430e 100644
--- a/client/src/Dashboard.tsx
+++ b/client/src/Dashboard.tsx
@@ -54,7 +54,10 @@ import { useReactFlowNodes } from './hooks/useReactFlowNodes';
import { useAutoSkillEdges } from './hooks/useAutoSkillEdges';
import { useWorkflowOpsListener } from './hooks/useWorkflowOpsListener';
import { useCopyPaste } from './hooks/useCopyPaste';
-import { useWebSocket } from './contexts/WebSocketContext';
+import {
+ useWebSocket,
+ type WorkflowStartResult,
+} from './contexts/WebSocketContext';
import { useNodeStatusStore } from './stores/nodeStatusStore';
import {
sanitizeNodesForComparison,
@@ -316,6 +319,48 @@ const DashboardContent: React.FC = () => {
}));
}, [nodes, getAllNodeParameters, saveNodeParameters]);
+ const applyAuthoritativeStartGraph = React.useCallback((
+ workflowId: string,
+ result: WorkflowStartResult,
+ ) => {
+ if (
+ !result.graph
+ || !Array.isArray(result.graph.nodes)
+ || !Array.isArray(result.graph.edges)
+ ) {
+ return;
+ }
+
+ // Start admits and normalizes the graph on the backend. Once it succeeds,
+ // replace both UI copies with that exact topology. Aliases only preserve
+ // the user's selected-node identity across canonical ID assignment.
+ const store = useAppStore.getState();
+ const workflow = store.currentWorkflow;
+ if (!workflow || workflow.id !== workflowId) return;
+
+ const aliases = result.aliases ?? {};
+ const selectedId = store.selectedNode
+ ? aliases[store.selectedNode.id] ?? store.selectedNode.id
+ : null;
+ const authoritativeNodes = (result.graph.nodes as Node[]).map((node) => ({
+ ...node,
+ selected: selectedId !== null && node.id === selectedId,
+ }));
+ const authoritativeEdges = result.graph.edges as Edge[];
+ const canonicalSelection = selectedId
+ ? authoritativeNodes.find((node) => node.id === selectedId) ?? null
+ : null;
+
+ store.setCurrentWorkflow({
+ ...workflow,
+ nodes: authoritativeNodes,
+ edges: authoritativeEdges,
+ });
+ store.setSelectedNode(canonicalSelection);
+ setNodes(authoritativeNodes);
+ setEdges(authoritativeEdges);
+ }, [setEdges, setNodes]);
+
// Toggle disabled state on selected nodes
const toggleDisableSelected = React.useCallback(() => {
setNodes(nds => nds.map(node => {
@@ -505,7 +550,14 @@ const DashboardContent: React.FC = () => {
// canvas rendered the fallback default node on first drop.
const [specsKey, setSpecsKey] = React.useState(() => cachedNodeSpecTypesKey());
React.useEffect(() => {
- if (!isReady || hasPrefetchedSpecs.current) return;
+ if (!isReady) {
+ // A backend restart can add or change NodeSpecs while this editor tab
+ // remains open. Re-arm the catalogue check so reconnect runs the
+ // revision-based cache invalidation and restores new icons/handles.
+ hasPrefetchedSpecs.current = false;
+ return;
+ }
+ if (hasPrefetchedSpecs.current) return;
if (!featureFlags.nodeSpecBackend) {
// When the backend is disabled, mark ready so the legacy fallback
// dispatch runs without waiting on a never-completing prefetch.
@@ -827,7 +879,14 @@ const DashboardContent: React.FC = () => {
})),
});
- await startWorkflow(currentWorkflow.id, nodes, edges, 'default', workflowControl.revision);
+ const result = await startWorkflow(
+ currentWorkflow.id,
+ nodes,
+ edges,
+ 'default',
+ workflowControl.revision,
+ );
+ applyAuthoritativeStartGraph(currentWorkflow.id, result);
} catch (error: any) {
console.error('[Dashboard] Deployment error:', error);
alert(`Deployment error: ${error.message}`);
@@ -844,17 +903,23 @@ const DashboardContent: React.FC = () => {
try {
const status = await getWorkflowControlStatus(workflow.id);
if (!status.can_start) return;
- await startWorkflow(
+ const result = await startWorkflow(
workflow.id,
workflow.nodes,
workflow.edges,
'default',
status.revision,
);
+ applyAuthoritativeStartGraph(workflow.id, result);
} catch {
toast.error('Could not start the example — press Start on the toolbar.');
}
- }, [getWorkflowControlStatus, openExampleAndChat, startWorkflow]);
+ }, [
+ applyAuthoritativeStartGraph,
+ getWorkflowControlStatus,
+ openExampleAndChat,
+ startWorkflow,
+ ]);
const handlePauseWorkflow = async () => {
const workflowId = currentWorkflow?.id;
@@ -888,7 +953,10 @@ const DashboardContent: React.FC = () => {
if (!currentWorkflow?.nodes.length) return {};
const nodeIds = currentWorkflow.nodes.map(n => n.id);
try {
- const allParams = await getAllNodeParameters(nodeIds);
+ const allParams = await getAllNodeParameters(nodeIds, {
+ purpose: 'export',
+ workflowId: currentWorkflow.id,
+ });
const result: Record<string, Record<string, any>> = {};
for (const [nodeId, np] of Object.entries(allParams)) {
if (np?.parameters && Object.keys(np.parameters).length > 0) {
diff --git a/client/src/adapters/__tests__/nodeSpecToDescription.test.ts b/client/src/adapters/__tests__/nodeSpecToDescription.test.ts
index 984a6dae..d4d6e89a 100644
--- a/client/src/adapters/__tests__/nodeSpecToDescription.test.ts
+++ b/client/src/adapters/__tests__/nodeSpecToDescription.test.ts
@@ -229,7 +229,7 @@ describe('nodeSpecToDescription contract', () => {
]);
});
- it('always emits a single main input and output handle', () => {
+ it('falls back to a single main input and output without declared handles', () => {
const def = nodeSpecToDescription({
type: 'handleNode',
displayName: 'Handle Node',
@@ -241,6 +241,70 @@ describe('nodeSpecToDescription contract', () => {
expect(def.outputs).toEqual(['main']);
});
+ it('retains declared handles, roles, labels, positions, and connection ids', () => {
+ const handles = [
+ {
+ name: 'input-context',
+ kind: 'input' as const,
+ position: 'left' as const,
+ offset: '50%',
+ label: 'Context',
+ role: 'context',
+ },
+ {
+ name: 'input-tools',
+ kind: 'input' as const,
+ position: 'bottom' as const,
+ offset: '75%',
+ label: 'Tools',
+ role: 'tools',
+ },
+ {
+ name: 'output-main',
+ kind: 'output' as const,
+ position: 'right' as const,
+ label: 'Output',
+ role: 'main',
+ },
+ ];
+ const def = nodeSpecToDescription({
+ type: 'contextAgent',
+ displayName: 'Context Agent',
+ icon: '🧠',
+ group: ['agent'],
+ version: 2,
+ handles,
+ });
+
+ expect(def.inputs).toEqual(['input-context', 'input-tools']);
+ expect(def.outputs).toEqual(['output-main']);
+ expect(def.handles).toEqual(handles);
+ expect(def.handles).not.toBe(handles);
+ });
+
+ it('retains Context V2 capability and panel hints', () => {
+ const def = nodeSpecToDescription({
+ type: 'contextNode',
+ displayName: 'Context',
+ icon: '🧠',
+ group: ['context'],
+ version: 2,
+ uiHints: {
+ requiresContext: false,
+ systemManaged: true,
+ isContextPanel: true,
+ isMemoryToolPanel: false,
+ },
+ });
+
+ expect(def.uiHints).toMatchObject({
+ requiresContext: false,
+ systemManaged: true,
+ isContextPanel: true,
+ isMemoryToolPanel: false,
+ });
+ });
+
it('preserves uiHints object as-is on the description', () => {
const def = nodeSpecToDescription({
type: 'hintsNode',
diff --git a/client/src/adapters/nodeSpecToDescription.ts b/client/src/adapters/nodeSpecToDescription.ts
index 8b012470..920cda84 100644
--- a/client/src/adapters/nodeSpecToDescription.ts
+++ b/client/src/adapters/nodeSpecToDescription.ts
@@ -14,20 +14,19 @@
*/
import type {
+ INodeHandleDescription,
INodeProperties,
INodeTypeDescription,
+ INodeUIHints,
NodeConnectionType,
} from '../types/INodeProperties';
/** One React Flow handle on a node. Wire mirror of
* server/models/node_metadata.NodeHandle (Wave 10.A). */
-export interface NodeSpecHandle {
- name: string;
- kind: 'input' | 'output';
- position: 'top' | 'bottom' | 'left' | 'right';
- offset?: string;
- label?: string;
- role?: string;
+export type NodeSpecHandle = INodeHandleDescription;
+
+export interface NodeSpecUIHints extends INodeUIHints {
+ [key: string]: unknown;
}
/** Wire shape emitted by GET /api/schemas/nodes/{type}/spec.json. */
@@ -44,7 +43,7 @@ export interface NodeSpec {
/** JSON Schema 7 document describing runtime output (Wave 3 contract). */
outputs?: JsonSchema;
credentials?: string[];
- uiHints?: Record<string, unknown>;
+ uiHints?: NodeSpecUIHints;
// Wave 10.A — full visual contract (every field is optional on the wire;
// the backend emits each one only when seeded on that node).
color?: string;
@@ -319,6 +318,16 @@ function defaultHandles(): NodeConnectionType[] {
return ['main' as NodeConnectionType];
}
+function connectionNames(
+ handles: NodeSpecHandle[] | undefined,
+ kind: NodeSpecHandle['kind'],
+): NodeConnectionType[] {
+ if (!handles || handles.length === 0) return defaultHandles();
+ return handles
+ .filter((handle) => handle.kind === kind)
+ .map((handle) => handle.name as NodeConnectionType);
+}
+
/**
* Top-level conversion. Adapter is pure — no side effects, no throws.
* Missing fields become sensible defaults so the parameter panel always
@@ -348,10 +357,11 @@ export function nodeSpecToDescription(spec: NodeSpec): INodeTypeDescription {
defaults: {
name: spec.displayName,
},
- inputs: defaultHandles(),
- outputs: defaultHandles(),
+ inputs: connectionNames(spec.handles, 'input'),
+ outputs: connectionNames(spec.handles, 'output'),
+ handles: spec.handles?.map((handle) => ({ ...handle })),
properties,
credentials: spec.credentials?.map(name => ({ name })),
- uiHints: spec.uiHints as INodeTypeDescription['uiHints'],
+ uiHints: spec.uiHints,
};
}
diff --git a/client/src/components/SquareNode.tsx b/client/src/components/SquareNode.tsx
index e080a795..a1439ce7 100644
--- a/client/src/components/SquareNode.tsx
+++ b/client/src/components/SquareNode.tsx
@@ -6,10 +6,11 @@ import { useAppStore } from '../store/useAppStore';
import { useAppTheme } from '../hooks/useAppTheme';
import EditableNodeLabel from './ui/EditableNodeLabel';
import { useWebSocket, useWhatsAppStatus, useNodeStatus } from '../contexts/WebSocketContext';
-import { getCachedNodeSpec, isNodeInBackendGroup, resolveNodeDescription, useNodeSpec } from '../lib/nodeSpec';
+import { isNodeInBackendGroup, resolveNodeDescription, useNodeSpec } from '../lib/nodeSpec';
import { NodeIcon } from '../assets/icons';
import { AI_MODEL_PROVIDER_MAP } from '../lib/aiModelProviders';
import { useProviderStored } from '../hooks/useCatalogueQuery';
+import type { NodeSpecHandle } from '../adapters/nodeSpecToDescription';
// Nodes with 'tool' in their group can connect to AI Agent/Zeenie tool handles
const hasToolGroup = (definition: any): boolean => {
@@ -30,6 +31,13 @@ const CREDENTIAL_TO_PROVIDER: Record<string, string> = {
googleAiApi: 'gemini',
};
+const REACT_POSITION: Record<NodeSpecHandle['position'], Position> = {
+ top: Position.Top,
+ bottom: Position.Bottom,
+ left: Position.Left,
+ right: Position.Right,
+};
+
const SquareNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectable, selected }) => {
const theme = useAppTheme();
// Slice selectors so an unrelated store mutation (sidebar toggle,
@@ -100,6 +108,7 @@ const SquareNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectab
// Wave 6 Phase 3e: backend NodeSpec -> legacy fallback. Flag off or
// cache cold -> returns local nodeDefinitions entry unchanged.
const definition = resolveNodeDescription(type || '');
+ const spec = useNodeSpec(type);
// Wave 10.E: backend group membership with bundled-definition fallback
const isAndroidNode = inGroup(type, 'android');
@@ -107,6 +116,49 @@ const SquareNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectab
// Check if this node can be used as a tool: any Android service node
// can connect to Android Toolkit, plus any node carrying the 'tool' group.
const isToolCapable = isAndroidNode || hasToolGroup(definition);
+ const renderedHandles = useMemo<NodeSpecHandle[]>(() => {
+ if (spec?.handles?.length) return spec.handles;
+
+ // Cold-cache / legacy fallback. Once a NodeSpec arrives its declared
+ // topology replaces this list wholesale, including Context V2 handles.
+ const fallback: NodeSpecHandle[] = [];
+ if (!spec?.hideInputHandle) {
+ fallback.push({
+ name: 'input-main',
+ kind: 'input',
+ position: 'left',
+ offset: '50%',
+ label: 'Service Input',
+ role: 'main',
+ });
+ }
+ if (!spec?.hideOutputHandle) {
+ fallback.push({
+ name: 'output-main',
+ kind: 'output',
+ position: 'right',
+ offset: '50%',
+ label: 'Service Output',
+ role: 'main',
+ });
+ }
+ if (isToolCapable) {
+ fallback.push({
+ name: 'output-tool',
+ kind: 'output',
+ position: 'top',
+ offset: '50%',
+ label: 'Tool Output',
+ role: 'tools',
+ });
+ }
+ return fallback;
+ }, [
+ spec?.handles,
+ spec?.hideInputHandle,
+ spec?.hideOutputHandle,
+ isToolCapable,
+ ]);
// Android connection status from WebSocket (real-time updates)
// Service nodes need a paired device to execute, not just relay connection
@@ -245,8 +297,7 @@ const SquareNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectab
// NodeSpec cache so the icon populates when prefetch lands without
// waiting for a parent re-render. <NodeIcon> resolves the ref and
// tints lucide icons with the node's brand color via currentColor.
- const iconSpec = useNodeSpec(type);
- const iconRef = (iconSpec?.icon as string | undefined) ?? (definition?.icon as string | undefined);
+ const iconRef = (spec?.icon as string | undefined) ?? (definition?.icon as string | undefined);
// Get the node color from definition or use default
const nodeColor = definition?.defaults?.color || 'var(--node-model)';
@@ -351,86 +402,41 @@ const SquareNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectab
title={getStatusTitle()}
/>
- {/* Square Input Handle (gated by spec.hideInputHandle, mirrors
- the hideOutputHandle pattern below — auto-derived True for
- usable_as_tool=True nodes via BaseNode.__init_subclass__). */}
- {!getCachedNodeSpec(type || '')?.hideInputHandle && (
- <Handle
- id="input-main"
- type="target"
- position={Position.Left}
- isConnectable={isConnectable}
- className="sq-node-handle in"
- style={{
- position: 'absolute',
- left: '-6px',
- top: '50%',
- transform: 'translateY(-50%)',
- width: theme.nodeSize.handle,
- height: theme.nodeSize.handle,
- zIndex: 20
- }}
- title="Service Input"
- />
- )}
-
- {/* Square Output Handle (Wave 10.E: spec.hideOutputHandle replaces the
- local NO_OUTPUT_NODE_TYPES list) */}
- {!getCachedNodeSpec(type || '')?.hideOutputHandle && (
- <Handle
- id="output-main"
- type="source"
- position={Position.Right}
- isConnectable={isConnectable}
- className="sq-node-handle out"
- style={{
- '--node-color': isConfigured ? nodeColor : theme.colors.textSecondary,
- position: 'absolute',
- right: '-6px',
- top: '50%',
- transform: 'translateY(-50%)',
- width: theme.nodeSize.handle,
- height: theme.nodeSize.handle,
- zIndex: 20
- } as NodeStyle}
- title="Service Output"
- />
- )}
-
- {/* Top Tool Output Handle.
- Wave 10.E: color + label come from the node's own spec. The
- `nodeColor` carries the spec's brand color (Android green for
- Android services, dracula accents elsewhere). The tooltip
- reads from the spec's top-position output handle when one is
- declared there; otherwise falls back to a generic label.
- Visual styles (background, border, radius) owned by
- `.sq-node-handle.out` in base.css + per-theme overrides — we
- pass `--node-color` for the CSS var only. */}
- {isToolCapable && (() => {
- const spec = getCachedNodeSpec(type || '');
- const topOut = spec?.handles?.find(h => h.kind === 'output' && h.position === 'top');
- const tooltip = topOut?.label ?? 'Tool Output';
+ {/* NodeSpec owns the complete topology. This renderer deliberately
+ knows no handle ids: Context, Memory-as-tool and future plugin
+ handles use the same positioning contract as main I/O. */}
+ {renderedHandles.map((handle) => {
+ const placement: React.CSSProperties =
+ handle.position === 'left'
+ ? { left: '-6px', top: handle.offset || '50%', transform: 'translateY(-50%)' }
+ : handle.position === 'right'
+ ? { right: '-6px', top: handle.offset || '50%', transform: 'translateY(-50%)' }
+ : handle.position === 'top'
+ ? { top: '-6px', left: handle.offset || '50%', transform: 'translateX(-50%)' }
+ : { bottom: '-6px', left: handle.offset || '50%', transform: 'translateX(-50%)' };
return (
<Handle
- id="output-tool"
- type="source"
- position={Position.Top}
+ key={handle.name}
+ id={handle.name}
+ type={handle.kind === 'input' ? 'target' : 'source'}
+ position={REACT_POSITION[handle.position]}
isConnectable={isConnectable}
- className="sq-node-handle out"
+ className={`sq-node-handle ${handle.kind === 'input' ? 'in' : 'out'}`}
style={{
- '--node-color': nodeColor,
+ '--node-color':
+ handle.kind === 'output' && handle.role === 'main' && !isConfigured
+ ? theme.colors.textSecondary
+ : nodeColor,
position: 'absolute',
- top: '-6px',
- left: '50%',
- transform: 'translateX(-50%)',
+ ...placement,
width: theme.nodeSize.handle,
height: theme.nodeSize.handle,
zIndex: 20,
} as NodeStyle}
- title={tooltip}
+ title={handle.label || handle.name}
/>
);
- })()}
+ })}
{/* Output Data Indicator - shows when node has execution output */}
{executionStatus === 'success' && nodeStatus?.data && (
@@ -475,4 +481,4 @@ const SquareNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectab
);
};
-export default memo(SquareNode, nodePropsEqual);
\ No newline at end of file
+export default memo(SquareNode, nodePropsEqual);
diff --git a/client/src/components/TriggerNode.tsx b/client/src/components/TriggerNode.tsx
index 1b1c624e..a1d858d0 100644
--- a/client/src/components/TriggerNode.tsx
+++ b/client/src/components/TriggerNode.tsx
@@ -16,6 +16,14 @@ import { NodeIcon } from '../assets/icons';
import { useAppTheme } from '../hooks/useAppTheme';
import { useWhatsAppStatus, useNodeStatus } from '../contexts/WebSocketContext';
import EditableNodeLabel from './ui/EditableNodeLabel';
+import type { NodeSpecHandle } from '../adapters/nodeSpecToDescription';
+
+const REACT_POSITION: Record<NodeSpecHandle['position'], Position> = {
+ top: Position.Top,
+ bottom: Position.Bottom,
+ left: Position.Left,
+ right: Position.Right,
+};
const TriggerNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectable, selected }) => {
const theme = useAppTheme();
@@ -114,6 +122,15 @@ const TriggerNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnecta
// <NodeIcon> resolves the ref and tints lucide icons via currentColor.
const iconSpec = useNodeSpec(type);
const iconRef = (iconSpec?.icon as string | undefined) ?? (definition?.icon as string | undefined);
+ const outputHandles: NodeSpecHandle[] = iconSpec?.handles
+ ?.filter((handle) => handle.kind === 'output')
+ ?? [{
+ name: 'output-main',
+ kind: 'output',
+ position: 'right',
+ label: 'Trigger Output',
+ role: 'main',
+ }];
// Get the node color from definition or use default trigger color
const nodeColor = definition?.defaults?.color || 'var(--node-trigger)';
@@ -251,24 +268,36 @@ const TriggerNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnecta
<span style={{ lineHeight: 1, color: theme.isDarkMode ? theme.colors.background : '#1a1d21' }}>{isPaused ? 'Ⅱ' : '⚡'}</span>
</div>
- {/* Output Handle (right side) — CSS owns bg/border via .sq-node-handle.out */}
- <Handle
- id="output-main"
- type="source"
- position={Position.Right}
- isConnectable={isConnectable}
- className="sq-node-handle out"
- style={{
- position: 'absolute',
- right: '-6px',
- top: '50%',
- transform: 'translateY(-50%)',
- width: theme.nodeSize.handle,
- height: theme.nodeSize.handle,
- zIndex: 20,
- }}
- title="Trigger Output"
- />
+ {/* Backend NodeSpec owns every trigger output, including the
+ canonical output-tool endpoint on dual-purpose triggers. */}
+ {outputHandles.map((handle) => {
+ const placement: React.CSSProperties =
+ handle.position === 'left'
+ ? { left: '-6px', top: handle.offset || '50%', transform: 'translateY(-50%)' }
+ : handle.position === 'right'
+ ? { right: '-6px', top: handle.offset || '50%', transform: 'translateY(-50%)' }
+ : handle.position === 'top'
+ ? { top: '-6px', left: handle.offset || '50%', transform: 'translateX(-50%)' }
+ : { bottom: '-6px', left: handle.offset || '50%', transform: 'translateX(-50%)' };
+ return (
+ <Handle
+ key={handle.name}
+ id={handle.name}
+ type="source"
+ position={REACT_POSITION[handle.position]}
+ isConnectable={isConnectable}
+ className="sq-node-handle out"
+ style={{
+ position: 'absolute',
+ ...placement,
+ width: theme.nodeSize.handle,
+ height: theme.nodeSize.handle,
+ zIndex: 20,
+ }}
+ title={handle.label || handle.name}
+ />
+ );
+ })}
{/* Output Data Indicator (bespoke; keeps inline style) */}
{executionStatus === 'success' && nodeStatus?.data && (
diff --git a/client/src/components/parameterPanel/ContextPanel.tsx b/client/src/components/parameterPanel/ContextPanel.tsx
new file mode 100644
index 00000000..2935b47b
--- /dev/null
+++ b/client/src/components/parameterPanel/ContextPanel.tsx
@@ -0,0 +1,424 @@
+import React, { useMemo, useState } from 'react';
+import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
+import {
+ Archive,
+ ChevronLeft,
+ ChevronRight,
+ Download,
+ GitFork,
+ Loader2,
+ RefreshCw,
+ ShieldCheck,
+} from 'lucide-react';
+import { toast } from 'sonner';
+
+import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
+import { Badge } from '@/components/ui/badge';
+import { Button } from '@/components/ui/button';
+import { Progress } from '@/components/ui/progress';
+import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
+import { useWebSocket } from '@/contexts/WebSocketContext';
+
+const PAGE_SIZE = 50;
+
+export interface AgentContextEventView {
+ sequence: number;
+ event_type: string;
+ operation_id?: string;
+ provider?: string;
+ payload_ref?: string;
+ payload_hash?: string;
+ message_wire_v2?: unknown;
+ payload?: unknown;
+}
+
+interface ContextCheckpointView {
+ provider?: string;
+ strategy?: string;
+ covers_through_sequence?: number;
+ active_token_count?: number;
+ source_revision?: number;
+ source_hash?: string;
+}
+
+interface ContextSnapshot {
+ thread_id?: string;
+ epoch?: number;
+ revision?: number;
+ provider?: string;
+ fidelity?: string;
+ resumable?: boolean;
+ active_token_count?: number;
+ context_window?: number;
+ pressure_ratio?: number;
+ provider_binding_status?: string;
+ checkpoints?: ContextCheckpointView[];
+ events?: AgentContextEventView[];
+ active_replay?: unknown;
+ next_cursor?: string | null;
+}
+
+interface ContextResponse extends Partial<ContextSnapshot> {
+ success?: boolean;
+ context?: ContextSnapshot;
+ error?: string;
+}
+
+interface ContextPanelProps {
+ nodeId: string;
+ workflowId?: string;
+}
+
+const contextQueryKey = (
+ workflowId: string | undefined,
+ nodeId: string,
+ cursor: string | null,
+ view: string,
+) => ['agentContext', workflowId ?? '', nodeId, cursor ?? '', view] as const;
+
+function responseSnapshot(response: ContextResponse | undefined): ContextSnapshot {
+ if (!response) return {};
+ return response.context ?? response;
+}
+
+function exportDownload(response: unknown, nodeId: string): void {
+ if (!response || typeof window === 'undefined') return;
+ const envelope = response as {
+ filename?: string;
+ content?: string;
+ export?: unknown;
+ };
+ const body =
+ typeof envelope.content === 'string'
+ ? envelope.content
+ : JSON.stringify(envelope.export ?? response, null, 2);
+ const blob = new Blob([body], { type: 'application/json' });
+ const href = URL.createObjectURL(blob);
+ const anchor = document.createElement('a');
+ anchor.href = href;
+ anchor.download = envelope.filename || `context-${nodeId}.json`;
+ anchor.click();
+ URL.revokeObjectURL(href);
+}
+
+/** Authorized, query-backed Context inspector.
+ *
+ * Raw journal/replay payloads are fetched only while this panel is open.
+ * It never reads transcript data from workflow params, node status, or
+ * websocket broadcasts.
+ */
+const ContextPanel: React.FC<ContextPanelProps> = ({ nodeId, workflowId }) => {
+ const { sendRequest } = useWebSocket();
+ const queryClient = useQueryClient();
+ const [view, setView] = useState<'journal' | 'active'>('journal');
+ const [cursor, setCursor] = useState<string | null>(null);
+ const [cursorHistory, setCursorHistory] = useState<Array<string | null>>([]);
+
+ const queryKey = contextQueryKey(workflowId, nodeId, cursor, view);
+ const contextQuery = useQuery<ContextResponse, Error>({
+ queryKey,
+ queryFn: () =>
+ sendRequest<ContextResponse>('get_agent_context', {
+ workflow_id: workflowId,
+ context_node_id: nodeId,
+ cursor,
+ limit: PAGE_SIZE,
+ view,
+ }),
+ enabled: !!workflowId && !!nodeId,
+ });
+ const snapshot = responseSnapshot(contextQuery.data);
+
+ const invalidateContext = async () => {
+ await queryClient.invalidateQueries({
+ queryKey: ['agentContext', workflowId ?? '', nodeId],
+ });
+ };
+ const clearMutation = useMutation({
+ mutationFn: () =>
+ sendRequest('clear_agent_context', {
+ workflow_id: workflowId,
+ context_node_id: nodeId,
+ }),
+ onSuccess: async () => {
+ setCursor(null);
+ setCursorHistory([]);
+ await invalidateContext();
+ toast.success('Context epoch cleared');
+ },
+ onError: () => toast.error('Failed to clear Context'),
+ });
+ const forkMutation = useMutation({
+ mutationFn: () =>
+ // No `provider`: the backend derives the binding from the thread.
+ // Echoing it from this snapshot would let a stale render fork the
+ // epoch onto a provider the thread has already moved off.
+ sendRequest('fork_agent_context', {
+ workflow_id: workflowId,
+ context_node_id: nodeId,
+ }),
+ onSuccess: async () => {
+ await invalidateContext();
+ toast.success('Provider context forked');
+ },
+ onError: () => toast.error('Failed to fork Context'),
+ });
+ const exportMutation = useMutation({
+ mutationFn: () =>
+ sendRequest('export_agent_context', {
+ workflow_id: workflowId,
+ context_node_id: nodeId,
+ thread_id: snapshot.thread_id,
+ epoch: snapshot.epoch,
+ }),
+ onSuccess: (response) => {
+ exportDownload(response, nodeId);
+ toast.success('Context export prepared');
+ },
+ onError: () => toast.error('Failed to export Context'),
+ });
+
+ const pressurePercent = useMemo(() => {
+ if (typeof snapshot.pressure_ratio === 'number') {
+ return Math.max(0, Math.min(100, snapshot.pressure_ratio * 100));
+ }
+ if (snapshot.active_token_count && snapshot.context_window) {
+ return Math.max(
+ 0,
+ Math.min(100, (snapshot.active_token_count / snapshot.context_window) * 100),
+ );
+ }
+ return 0;
+ }, [
+ snapshot.active_token_count,
+ snapshot.context_window,
+ snapshot.pressure_ratio,
+ ]);
+
+ if (!workflowId) {
+ return (
+ <div className="p-6">
+ <Alert variant="info">
+ <ShieldCheck />
+ <AlertTitle>Context is workflow-scoped</AlertTitle>
+ <AlertDescription>Save the workflow before inspecting its Context.</AlertDescription>
+ </Alert>
+ </div>
+ );
+ }
+
+ return (
+ <div className="flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto p-6">
+ <div className="flex flex-wrap items-start justify-between gap-3">
+ <div>
+ <h3 className="text-base font-semibold text-foreground">Agent Context</h3>
+ <p className="text-xs text-muted-foreground">
+ Exact observable journal and active replay for this Context node.
+ </p>
+ </div>
+ <div className="flex flex-wrap gap-2">
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => void contextQuery.refetch()}
+ disabled={contextQuery.isFetching}
+ >
+ <RefreshCw className={contextQuery.isFetching ? 'animate-spin' : ''} />
+ Refresh
+ </Button>
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => forkMutation.mutate()}
+ disabled={forkMutation.isPending}
+ >
+ <GitFork />
+ Fork
+ </Button>
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => exportMutation.mutate()}
+ disabled={exportMutation.isPending}
+ >
+ <Download />
+ Export
+ </Button>
+ <Button
+ variant="destructive"
+ size="sm"
+ onClick={() => clearMutation.mutate()}
+ disabled={clearMutation.isPending}
+ >
+ <Archive />
+ Clear epoch
+ </Button>
+ </div>
+ </div>
+
+ <div className="grid grid-cols-2 gap-3 lg:grid-cols-4">
+ <div className="rounded-md border border-border bg-card p-3">
+ <div className="text-xs text-muted-foreground">Thread / epoch</div>
+ <div className="truncate text-sm font-medium">
+ {snapshot.thread_id || '—'} / {snapshot.epoch ?? '—'}
+ </div>
+ <div className="text-xs text-muted-foreground">
+ Revision {snapshot.revision ?? '—'}
+ </div>
+ </div>
+ <div className="rounded-md border border-border bg-card p-3">
+ <div className="text-xs text-muted-foreground">Replay fidelity</div>
+ <div className="mt-1 flex flex-wrap gap-1">
+ <Badge variant={snapshot.resumable === false ? 'warning' : 'success'}>
+ {snapshot.fidelity || 'unknown'}
+ </Badge>
+ {snapshot.resumable === false && <Badge variant="outline">non-resumable</Badge>}
+ </div>
+ </div>
+ <div className="rounded-md border border-border bg-card p-3">
+ <div className="text-xs text-muted-foreground">Provider</div>
+ <div className="text-sm font-medium">{snapshot.provider || '—'}</div>
+ <div className="text-xs text-muted-foreground">
+ Binding {snapshot.provider_binding_status || 'unbound'}
+ </div>
+ </div>
+ <div className="rounded-md border border-border bg-card p-3">
+ <div className="text-xs text-muted-foreground">Active pressure</div>
+ <div className="mb-2 text-sm font-medium">
+ {snapshot.active_token_count ?? 0}
+ {snapshot.context_window ? ` / ${snapshot.context_window}` : ' tokens'}
+ </div>
+ <Progress value={pressurePercent} />
+ </div>
+ </div>
+
+ {(snapshot.checkpoints?.length ?? 0) > 0 && (
+ <div className="rounded-md border border-border bg-card p-3">
+ <div className="mb-2 text-sm font-medium">Checkpoints</div>
+ <div className="flex flex-col gap-2">
+ {snapshot.checkpoints!.map((checkpoint, index) => (
+ <div
+ key={`${checkpoint.source_hash || checkpoint.covers_through_sequence || index}`}
+ className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground"
+ >
+ <Badge variant="outline">{checkpoint.strategy || 'portable'}</Badge>
+ <span>{checkpoint.provider || 'unknown provider'}</span>
+ <span>through #{checkpoint.covers_through_sequence ?? '—'}</span>
+ <span>{checkpoint.active_token_count ?? 0} active tokens</span>
+ </div>
+ ))}
+ </div>
+ </div>
+ )}
+
+ <Tabs
+ value={view}
+ onValueChange={(next) => {
+ setView(next as 'journal' | 'active');
+ setCursor(null);
+ setCursorHistory([]);
+ }}
+ className="min-h-0 flex-1"
+ >
+ <TabsList>
+ <TabsTrigger value="journal">Raw journal</TabsTrigger>
+ <TabsTrigger value="active">Active replay</TabsTrigger>
+ </TabsList>
+ <TabsContent value="journal" className="mt-2">
+ {contextQuery.isLoading ? (
+ <Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
+ ) : contextQuery.error ? (
+ <Alert variant="destructive">
+ <AlertTitle>Context unavailable</AlertTitle>
+ <AlertDescription>{contextQuery.error.message}</AlertDescription>
+ </Alert>
+ ) : (snapshot.events?.length ?? 0) === 0 ? (
+ <div className="rounded-md border border-dashed border-border p-8 text-center text-sm text-muted-foreground">
+ No committed Context events yet.
+ </div>
+ ) : (
+ <div className="flex flex-col gap-2">
+ {snapshot.events!.map((event) => (
+ <details
+ key={`${event.sequence}:${event.operation_id || event.event_type}`}
+ className="rounded-md border border-border bg-card p-3"
+ >
+ <summary className="cursor-pointer text-sm">
+ <span className="font-mono text-xs text-muted-foreground">
+ #{event.sequence}
+ </span>{' '}
+ <span className="font-medium">{event.event_type}</span>
+ {event.provider && (
+ <Badge variant="outline" className="ml-2">
+ {event.provider}
+ </Badge>
+ )}
+ </summary>
+ <pre className="mt-3 max-h-80 overflow-auto whitespace-pre-wrap break-words rounded bg-muted p-3 text-xs">
+ {JSON.stringify(
+ event.message_wire_v2 ??
+ event.payload ?? {
+ payload_ref: event.payload_ref,
+ payload_hash: event.payload_hash,
+ operation_id: event.operation_id,
+ },
+ null,
+ 2,
+ )}
+ </pre>
+ </details>
+ ))}
+ </div>
+ )}
+ </TabsContent>
+ <TabsContent value="active" className="mt-2">
+ {contextQuery.isLoading ? (
+ <Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
+ ) : (
+ <pre className="max-h-[32rem] overflow-auto whitespace-pre-wrap break-words rounded-md border border-border bg-muted p-4 text-xs">
+ {snapshot.active_replay
+ ? JSON.stringify(snapshot.active_replay, null, 2)
+ : 'No active replay is available.'}
+ </pre>
+ )}
+ </TabsContent>
+ </Tabs>
+
+ {view === 'journal' && (
+ <div className="flex items-center justify-between">
+ <Button
+ variant="outline"
+ size="sm"
+ disabled={cursorHistory.length === 0}
+ onClick={() => {
+ setCursorHistory((history) => {
+ const next = [...history];
+ setCursor(next.pop() ?? null);
+ return next;
+ });
+ }}
+ >
+ <ChevronLeft />
+ Previous
+ </Button>
+ <span className="text-xs text-muted-foreground">Up to {PAGE_SIZE} events per page</span>
+ <Button
+ variant="outline"
+ size="sm"
+ disabled={!snapshot.next_cursor}
+ onClick={() => {
+ if (!snapshot.next_cursor) return;
+ setCursorHistory((history) => [...history, cursor]);
+ setCursor(snapshot.next_cursor);
+ }}
+ >
+ Next
+ <ChevronRight />
+ </Button>
+ </div>
+ )}
+ </div>
+ );
+};
+
+export default ContextPanel;
diff --git a/client/src/components/parameterPanel/MemoryToolPanel.tsx b/client/src/components/parameterPanel/MemoryToolPanel.tsx
new file mode 100644
index 00000000..60e9b134
--- /dev/null
+++ b/client/src/components/parameterPanel/MemoryToolPanel.tsx
@@ -0,0 +1,490 @@
+import React, { useEffect, useState } from 'react';
+import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
+import {
+ ChevronLeft,
+ ChevronRight,
+ Database,
+ Loader2,
+ Plus,
+ Save,
+ Search,
+ Trash2,
+} from 'lucide-react';
+import { toast } from 'sonner';
+
+import {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+} from '@/components/ui/alert-dialog';
+import { Badge } from '@/components/ui/badge';
+import { Button } from '@/components/ui/button';
+import { Input } from '@/components/ui/input';
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from '@/components/ui/select';
+import { Textarea } from '@/components/ui/textarea';
+import { useWebSocket } from '@/contexts/WebSocketContext';
+
+const PAGE_SIZE = 25;
+
+interface MemoryItemView {
+ /** Store views use `id`; API adapters may expose the public `memory_id`. */
+ id?: string;
+ memory_id?: string;
+ version: number;
+ title?: string | null;
+ content: string;
+ category?: string | null;
+ tags?: string[];
+ expires_at?: string | null;
+ created_at?: string;
+ updated_at?: string;
+ indexing_state?: string;
+}
+
+interface MemoryListResponse {
+ success?: boolean;
+ items?: MemoryItemView[];
+ next_cursor?: string | null;
+ indexing_state?: string;
+ error?: string;
+}
+
+interface MemoryItemResponse {
+ success?: boolean;
+ item?: MemoryItemView;
+ memory?: MemoryItemView;
+ error?: string;
+}
+
+interface MemoryToolPanelProps {
+ nodeId: string;
+ workflowId?: string;
+ parameters: Record<string, unknown>;
+ onParameterChange: (name: string, value: unknown) => void;
+}
+
+interface MemoryDraft {
+ title: string;
+ content: string;
+ category: string;
+ tags: string;
+}
+
+const EMPTY_DRAFT: MemoryDraft = {
+ title: '',
+ content: '',
+ category: '',
+ tags: '',
+};
+
+const memoryListPrefix = (workflowId: string | undefined, nodeId: string) =>
+ ['memoryItems', workflowId ?? '', nodeId] as const;
+
+const memoryItemId = (item: MemoryItemView | undefined): string =>
+ item?.memory_id || item?.id || '';
+
+const MemoryToolPanel: React.FC<MemoryToolPanelProps> = ({
+ nodeId,
+ workflowId,
+ parameters,
+ onParameterChange,
+}) => {
+ const { sendRequest } = useWebSocket();
+ const queryClient = useQueryClient();
+ const [searchInput, setSearchInput] = useState('');
+ const [query, setQuery] = useState('');
+ const [cursor, setCursor] = useState<string | null>(null);
+ const [cursorHistory, setCursorHistory] = useState<Array<string | null>>([]);
+ const [selectedId, setSelectedId] = useState<string | null>(null);
+ const [draft, setDraft] = useState<MemoryDraft>(EMPTY_DRAFT);
+ const [showClearDialog, setShowClearDialog] = useState(false);
+
+ const listPrefix = memoryListPrefix(workflowId, nodeId);
+ const listQuery = useQuery<MemoryListResponse, Error>({
+ queryKey: [...listPrefix, query, cursor ?? ''],
+ queryFn: () =>
+ sendRequest<MemoryListResponse>('list_memory_items', {
+ workflow_id: workflowId,
+ memory_node_id: nodeId,
+ query: query || undefined,
+ cursor,
+ limit: PAGE_SIZE,
+ }),
+ enabled: !!workflowId && !!nodeId,
+ });
+ const itemQuery = useQuery<MemoryItemResponse, Error>({
+ queryKey: ['memoryItem', workflowId ?? '', nodeId, selectedId ?? 'new'],
+ queryFn: () =>
+ sendRequest<MemoryItemResponse>('get_memory_item', {
+ workflow_id: workflowId,
+ memory_node_id: nodeId,
+ memory_id: selectedId,
+ }),
+ enabled: !!workflowId && !!selectedId,
+ });
+
+ const selected = itemQuery.data?.item ?? itemQuery.data?.memory;
+ useEffect(() => {
+ if (!selected) return;
+ setDraft({
+ title: selected.title || '',
+ content: selected.content,
+ category: selected.category || '',
+ tags: (selected.tags || []).join(', '),
+ });
+ }, [selected]);
+
+ const invalidateItems = async () => {
+ await queryClient.invalidateQueries({ queryKey: listPrefix });
+ if (selectedId) {
+ await queryClient.invalidateQueries({
+ queryKey: ['memoryItem', workflowId ?? '', nodeId, selectedId],
+ });
+ }
+ };
+ const rememberMutation = useMutation({
+ mutationFn: () =>
+ sendRequest<MemoryItemResponse>('remember_memory', {
+ workflow_id: workflowId,
+ memory_node_id: nodeId,
+ content: draft.content,
+ title: draft.title || undefined,
+ category: draft.category || undefined,
+ tags: draft.tags
+ .split(',')
+ .map((tag) => tag.trim())
+ .filter(Boolean),
+ }),
+ onSuccess: async (response) => {
+ const remembered = response.item ?? response.memory;
+ setSelectedId(memoryItemId(remembered) || null);
+ await invalidateItems();
+ toast.success('Memory saved');
+ },
+ onError: () => toast.error('Failed to save Memory'),
+ });
+ const updateMutation = useMutation({
+ mutationFn: () =>
+ sendRequest<MemoryItemResponse>('update_memory_item', {
+ workflow_id: workflowId,
+ memory_node_id: nodeId,
+ memory_id: memoryItemId(selected),
+ expected_version: selected?.version,
+ patch: {
+ title: draft.title || null,
+ content: draft.content,
+ category: draft.category || null,
+ tags: draft.tags
+ .split(',')
+ .map((tag) => tag.trim())
+ .filter(Boolean),
+ },
+ }),
+ onSuccess: async () => {
+ await invalidateItems();
+ toast.success('Memory updated');
+ },
+ onError: () => toast.error('Memory changed elsewhere; refresh and retry'),
+ });
+ const forgetMutation = useMutation({
+ mutationFn: () =>
+ sendRequest('forget_memory_item', {
+ workflow_id: workflowId,
+ memory_node_id: nodeId,
+ memory_id: memoryItemId(selected),
+ expected_version: selected?.version,
+ }),
+ onSuccess: async () => {
+ setSelectedId(null);
+ setDraft(EMPTY_DRAFT);
+ await invalidateItems();
+ toast.success('Memory forgotten');
+ },
+ onError: () => toast.error('Failed to forget Memory'),
+ });
+ const clearMutation = useMutation({
+ mutationFn: () =>
+ sendRequest('clear_memory_items', {
+ workflow_id: workflowId,
+ memory_node_id: nodeId,
+ }),
+ onSuccess: async () => {
+ setSelectedId(null);
+ setDraft(EMPTY_DRAFT);
+ setShowClearDialog(false);
+ await invalidateItems();
+ toast.success('All Memory items cleared');
+ },
+ onError: () => toast.error('Failed to clear Memory items'),
+ });
+
+ const items = listQuery.data?.items ?? [];
+ const isSaving = rememberMutation.isPending || updateMutation.isPending;
+ const canSave = draft.content.trim().length > 0 && !isSaving;
+
+ return (
+ <div className="grid min-h-0 flex-1 grid-cols-1 overflow-hidden lg:grid-cols-[minmax(240px,0.8fr)_minmax(320px,1.2fr)]">
+ <section className="flex min-h-0 flex-col gap-3 border-r border-border p-4">
+ <div className="flex items-start justify-between gap-2">
+ <div>
+ <h3 className="text-base font-semibold">Memory</h3>
+ <p className="text-xs text-muted-foreground">
+ Explicit durable items exposed through the <code>memory</code> tool.
+ </p>
+ </div>
+ <Badge variant="outline">
+ <Database />
+ {listQuery.data?.indexing_state || 'lexical ready'}
+ </Badge>
+ </div>
+
+ <div className="flex gap-2">
+ <div className="relative flex-1">
+ <Search className="absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
+ <Input
+ className="pl-8"
+ value={searchInput}
+ onChange={(event) => setSearchInput(event.target.value)}
+ onKeyDown={(event) => {
+ if (event.key === 'Enter') {
+ setQuery(searchInput.trim());
+ setCursor(null);
+ setCursorHistory([]);
+ }
+ }}
+ placeholder="Search memories"
+ />
+ </div>
+ <Button
+ variant="outline"
+ size="icon"
+ aria-label="Search Memory"
+ onClick={() => {
+ setQuery(searchInput.trim());
+ setCursor(null);
+ setCursorHistory([]);
+ }}
+ >
+ <Search />
+ </Button>
+ <Button
+ size="icon"
+ aria-label="New Memory"
+ onClick={() => {
+ setSelectedId(null);
+ setDraft(EMPTY_DRAFT);
+ }}
+ >
+ <Plus />
+ </Button>
+ </div>
+
+ <div className="min-h-0 flex-1 overflow-y-auto">
+ {listQuery.isLoading ? (
+ <Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
+ ) : items.length === 0 ? (
+ <div className="rounded-md border border-dashed border-border p-6 text-center text-sm text-muted-foreground">
+ No Memory items match this query.
+ </div>
+ ) : (
+ <div className="flex flex-col gap-2">
+ {items.map((item) => (
+ <button
+ key={memoryItemId(item)}
+ type="button"
+ className={`row rounded-md border p-3 text-left ${
+ selectedId === memoryItemId(item)
+ ? 'border-primary bg-primary/5'
+ : 'border-border bg-card'
+ }`}
+ onClick={() => setSelectedId(memoryItemId(item))}
+ >
+ <div className="flex items-center justify-between gap-2">
+ <span className="truncate text-sm font-medium">
+ {item.title || item.content.slice(0, 60)}
+ </span>
+ <span className="text-[10px] text-muted-foreground">v{item.version}</span>
+ </div>
+ <p className="mt-1 line-clamp-2 text-xs text-muted-foreground">
+ {item.content}
+ </p>
+ <div className="mt-2 flex flex-wrap gap-1">
+ {item.category && <Badge variant="secondary">{item.category}</Badge>}
+ {(item.tags || []).slice(0, 3).map((tag) => (
+ <Badge key={tag} variant="outline">
+ {tag}
+ </Badge>
+ ))}
+ </div>
+ </button>
+ ))}
+ </div>
+ )}
+ </div>
+
+ <div className="flex items-center justify-between">
+ <Button
+ variant="outline"
+ size="icon-sm"
+ aria-label="Previous Memory page"
+ disabled={cursorHistory.length === 0}
+ onClick={() => {
+ setCursorHistory((history) => {
+ const next = [...history];
+ setCursor(next.pop() ?? null);
+ return next;
+ });
+ }}
+ >
+ <ChevronLeft />
+ </Button>
+ <span className="text-xs text-muted-foreground">{PAGE_SIZE} per page</span>
+ <Button
+ variant="outline"
+ size="icon-sm"
+ aria-label="Next Memory page"
+ disabled={!listQuery.data?.next_cursor}
+ onClick={() => {
+ if (!listQuery.data?.next_cursor) return;
+ setCursorHistory((history) => [...history, cursor]);
+ setCursor(listQuery.data.next_cursor);
+ }}
+ >
+ <ChevronRight />
+ </Button>
+ </div>
+ </section>
+
+ <section className="min-h-0 overflow-y-auto p-4">
+ <div className="mb-4 grid gap-3 rounded-md border border-border bg-card p-3 sm:grid-cols-2">
+ <label className="text-xs font-medium text-muted-foreground">
+ Workflow Reset policy
+ <Select
+ value={String(parameters.reset_policy || 'preserve')}
+ onValueChange={(value) => onParameterChange('reset_policy', value)}
+ >
+ <SelectTrigger className="mt-1">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="preserve">Preserve items</SelectItem>
+ <SelectItem value="clear">Clear items</SelectItem>
+ </SelectContent>
+ </Select>
+ </label>
+ <div className="flex items-end justify-end">
+ <Button
+ variant="destructive"
+ size="sm"
+ onClick={() => setShowClearDialog(true)}
+ >
+ <Trash2 />
+ Clear all items
+ </Button>
+ </div>
+ </div>
+
+ {selectedId && itemQuery.isLoading ? (
+ <Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
+ ) : (
+ <div className="flex flex-col gap-3">
+ <div className="flex items-center justify-between gap-2">
+ <h4 className="font-medium">{selected ? 'Edit Memory item' : 'Remember something'}</h4>
+ {selected && (
+ <Badge variant="outline">
+ {selected.indexing_state || 'indexed'} · v{selected.version}
+ </Badge>
+ )}
+ </div>
+ <Input
+ value={draft.title}
+ onChange={(event) =>
+ setDraft((current) => ({ ...current, title: event.target.value }))
+ }
+ placeholder="Title (optional)"
+ />
+ <div className="grid gap-3 sm:grid-cols-2">
+ <Input
+ value={draft.category}
+ onChange={(event) =>
+ setDraft((current) => ({ ...current, category: event.target.value }))
+ }
+ placeholder="Category"
+ />
+ <Input
+ value={draft.tags}
+ onChange={(event) =>
+ setDraft((current) => ({ ...current, tags: event.target.value }))
+ }
+ placeholder="Tags, comma separated"
+ />
+ </div>
+ <Textarea
+ rows={12}
+ value={draft.content}
+ onChange={(event) =>
+ setDraft((current) => ({ ...current, content: event.target.value }))
+ }
+ placeholder="Durable fact, preference, decision, or note"
+ />
+ <div className="flex justify-end gap-2">
+ {selected && (
+ <Button
+ variant="destructive"
+ onClick={() => forgetMutation.mutate()}
+ disabled={forgetMutation.isPending}
+ >
+ <Trash2 />
+ Forget
+ </Button>
+ )}
+ <Button
+ onClick={() =>
+ selected ? updateMutation.mutate() : rememberMutation.mutate()
+ }
+ disabled={!canSave}
+ >
+ {isSaving ? <Loader2 className="animate-spin" /> : <Save />}
+ {selected ? 'Update' : 'Remember'}
+ </Button>
+ </div>
+ </div>
+ )}
+ </section>
+
+ <AlertDialog open={showClearDialog} onOpenChange={setShowClearDialog}>
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>Clear all durable Memory items?</AlertDialogTitle>
+ <AlertDialogDescription>
+ This is a human-only bulk action. It does not clear Agent Context or Todos.
+ </AlertDialogDescription>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel disabled={clearMutation.isPending}>Cancel</AlertDialogCancel>
+ <AlertDialogAction
+ variant="destructive"
+ disabled={clearMutation.isPending}
+ onClick={() => clearMutation.mutate()}
+ >
+ Clear Memory
+ </AlertDialogAction>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
+ </div>
+ );
+};
+
+export default MemoryToolPanel;
diff --git a/client/src/components/parameterPanel/MiddleSection.tsx b/client/src/components/parameterPanel/MiddleSection.tsx
index 6959caa2..7fd9f75f 100644
--- a/client/src/components/parameterPanel/MiddleSection.tsx
+++ b/client/src/components/parameterPanel/MiddleSection.tsx
@@ -33,6 +33,8 @@ import TaskManagerPanel from './TaskManagerPanel';
import TeamMonitorPanel from './TeamMonitorPanel';
import ProcessManagerPanel from './ProcessManagerPanel';
import GalleryPanel from './GalleryPanel';
+import ContextPanel from './ContextPanel';
+import MemoryToolPanel from './MemoryToolPanel';
import { useAppStore } from '../../store/useAppStore';
import { useNodeStatus, useWebSocket, CompactionStats } from '../../contexts/WebSocketContext';
import { useUserSettingsQuery } from '../../hooks/useUserSettingsQuery';
@@ -188,8 +190,13 @@ const MiddleSection: React.FC<MiddleSectionProps> = ({
// node's own module emits.
const hints = nodeDefinition.uiHints ?? {};
const isMasterSkillNode = hints.isMasterSkillEditor === true;
- const isMemoryNode = hints.isMemoryPanel === true;
- const needsCodeEditorLayout = hints.hasCodeEditor === true;
+ const isContextNode = hints.isContextPanel === true;
+ const isMemoryToolNode = hints.isMemoryToolPanel === true;
+ // Legacy combined Markdown/transcript panel remains available while
+ // normalize_workflow_graph upgrades input-memory graphs.
+ const isMemoryNode = hints.isMemoryPanel === true && !isMemoryToolNode;
+ const needsCodeEditorLayout =
+ hints.hasCodeEditor === true && !isContextNode && !isMemoryToolNode;
const isCodeExecutorNode = needsCodeEditorLayout && !isMemoryNode && !isMasterSkillNode;
// No seedable skills today besides masterSkill (handled via its own
// editor). The reset-skill branch used to fire for SKILL_NODE_TYPES
@@ -540,7 +547,7 @@ const MiddleSection: React.FC<MiddleSectionProps> = ({
<div className="relative flex h-full flex-1 flex-col overflow-hidden">
{/* Description - hide for code editor nodes (Python, Skill), masterSkill,
and the todo editor (each renders its own full-panel header). */}
- {!needsCodeEditorLayout && !isMasterSkillNode && !isTodoEditorNode && !isTaskManagerNode && !isMonitorNode && !isProcessManagerNode && !isGalleryNode && (
+ {!needsCodeEditorLayout && !isMasterSkillNode && !isContextNode && !isMemoryToolNode && !isTodoEditorNode && !isTaskManagerNode && !isMonitorNode && !isProcessManagerNode && !isGalleryNode && (
<div className="shrink-0 border-b border-border-default bg-bg-panel px-6 pt-4 pb-2">
<p className="m-0 text-base leading-[1.5] text-fg-muted">
{nodeDefinition.description}
@@ -588,6 +595,18 @@ const MiddleSection: React.FC<MiddleSectionProps> = ({
parameters={parameters}
onParameterChange={onParameterChange}
/>
+ ) : isContextNode ? (
+ <ContextPanel
+ nodeId={nodeId}
+ workflowId={currentWorkflow?.id}
+ />
+ ) : isMemoryToolNode ? (
+ <MemoryToolPanel
+ nodeId={nodeId}
+ workflowId={currentWorkflow?.id}
+ parameters={parameters}
+ onParameterChange={onParameterChange}
+ />
) : (
<>
{/* Parameters */}
diff --git a/client/src/components/parameterPanel/__tests__/ContextMemoryPanels.test.tsx b/client/src/components/parameterPanel/__tests__/ContextMemoryPanels.test.tsx
new file mode 100644
index 00000000..8e5bb6ce
--- /dev/null
+++ b/client/src/components/parameterPanel/__tests__/ContextMemoryPanels.test.tsx
@@ -0,0 +1,137 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { screen, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+
+import { renderWithProviders as render } from '../../../test/providers';
+import ContextPanel from '../ContextPanel';
+import MemoryToolPanel from '../MemoryToolPanel';
+
+const sendRequest = vi.fn();
+
+vi.mock('../../../contexts/WebSocketContext', () => ({
+ useWebSocket: () => ({ sendRequest }),
+}));
+
+beforeEach(() => {
+ sendRequest.mockReset();
+});
+
+describe('ContextPanel', () => {
+ it('loads authorized journal content through get_agent_context', async () => {
+ sendRequest.mockImplementation(async (operation: string) => {
+ if (operation === 'get_agent_context') {
+ return {
+ context: {
+ thread_id: 'session:abc',
+ epoch: 3,
+ revision: 9,
+ provider: 'openai',
+ fidelity: 'provider_replayable',
+ resumable: true,
+ active_token_count: 800,
+ context_window: 10_000,
+ provider_binding_status: 'bound',
+ events: [
+ {
+ sequence: 7,
+ event_type: 'assistant_message',
+ message_wire_v2: { role: 'assistant', blocks: [{ type: 'text', text: 'hello' }] },
+ },
+ ],
+ },
+ };
+ }
+ return {};
+ });
+
+ render(<ContextPanel nodeId="ctx-1" workflowId="wf-1" />);
+
+ expect(await screen.findByText('session:abc / 3')).toBeInTheDocument();
+ expect(screen.getByText('assistant_message')).toBeInTheDocument();
+ expect(sendRequest).toHaveBeenCalledWith(
+ 'get_agent_context',
+ expect.objectContaining({
+ workflow_id: 'wf-1',
+ context_node_id: 'ctx-1',
+ view: 'journal',
+ limit: 50,
+ }),
+ );
+ });
+
+ it('invokes backend epoch clearing without synthesizing local Context', async () => {
+ sendRequest.mockImplementation(async (operation: string) => {
+ if (operation === 'get_agent_context') return { context: { events: [] } };
+ if (operation === 'clear_agent_context') return { success: true };
+ return {};
+ });
+ const user = userEvent.setup();
+ render(<ContextPanel nodeId="ctx-1" workflowId="wf-1" />);
+
+ await screen.findByText('No committed Context events yet.');
+ await user.click(screen.getByRole('button', { name: /Clear epoch/i }));
+
+ await waitFor(() =>
+ expect(sendRequest).toHaveBeenCalledWith('clear_agent_context', {
+ workflow_id: 'wf-1',
+ context_node_id: 'ctx-1',
+ }),
+ );
+ });
+});
+
+describe('MemoryToolPanel', () => {
+ it('lists and remembers items through the backend Memory operations', async () => {
+ sendRequest.mockImplementation(async (operation: string) => {
+ if (operation === 'list_memory_items') {
+ return { items: [], indexing_state: 'lexical_ready' };
+ }
+ if (operation === 'remember_memory') {
+ return {
+ item: {
+ id: 'mem-1',
+ version: 1,
+ content: 'The launch is Tuesday',
+ },
+ };
+ }
+ if (operation === 'get_memory_item') {
+ return {
+ item: {
+ id: 'mem-1',
+ version: 1,
+ content: 'The launch is Tuesday',
+ },
+ };
+ }
+ return {};
+ });
+ const user = userEvent.setup();
+ render(
+ <MemoryToolPanel
+ nodeId="memory-1"
+ workflowId="wf-1"
+ parameters={{ reset_policy: 'preserve' }}
+ onParameterChange={vi.fn()}
+ />,
+ );
+
+ await screen.findByText('No Memory items match this query.');
+ await user.type(
+ screen.getByPlaceholderText('Durable fact, preference, decision, or note'),
+ 'The launch is Tuesday',
+ );
+ await user.click(screen.getByRole('button', { name: 'Remember' }));
+
+ await waitFor(() =>
+ expect(sendRequest).toHaveBeenCalledWith(
+ 'remember_memory',
+ expect.objectContaining({
+ workflow_id: 'wf-1',
+ memory_node_id: 'memory-1',
+ content: 'The launch is Tuesday',
+ }),
+ ),
+ );
+ });
+});
diff --git a/client/src/components/parameterPanel/__tests__/InputSection.test.tsx b/client/src/components/parameterPanel/__tests__/InputSection.test.tsx
index e10200ec..82db8eb7 100644
--- a/client/src/components/parameterPanel/__tests__/InputSection.test.tsx
+++ b/client/src/components/parameterPanel/__tests__/InputSection.test.tsx
@@ -4,7 +4,8 @@
* Locks in the connection-discovery contract:
* - Empty state when no edges target the node
* - Direct edges to main handles populate connected nodes
- * - Config handles (input-memory, input-tools, input-skill) are SKIPPED
+ * - Config handles (input-context, legacy input-memory, input-tools,
+ * input-skill) are SKIPPED
* for agent nodes (those have a dedicated UI in MiddleSection)
* - input-main / input-chat / input-task / input-teammates are NEVER skipped
* - Config nodes (memory/tool group) inherit their parent node's main inputs
@@ -196,6 +197,19 @@ describe('InputSection -- config handle filtering for agent nodes', () => {
});
});
+ it('SKIPS input-context edges on Context V2 agents', async () => {
+ setWorkflow(
+ [
+ { id: 'ctx', type: 'context', data: { label: 'Context' }, position: { x: 0, y: 0 } },
+ { id: 'agent', type: 'aiAgent', data: { label: 'Agent' }, position: { x: 100, y: 0 } },
+ ],
+ [{ id: 'e1', source: 'ctx', target: 'agent', sourceHandle: 'output-context', targetHandle: 'input-context' }],
+ );
+
+ render(<InputSection nodeId="agent" />);
+ await waitFor(() => expect(screen.queryByText('Context')).not.toBeInTheDocument());
+ });
+
it('SKIPS input-tools edges on agent nodes', async () => {
setWorkflow(
[
diff --git a/client/src/components/ui/ComponentPalette.tsx b/client/src/components/ui/ComponentPalette.tsx
index 851ac2cd..7d5b9f6b 100644
--- a/client/src/components/ui/ComponentPalette.tsx
+++ b/client/src/components/ui/ComponentPalette.tsx
@@ -53,6 +53,10 @@ const ComponentPalette: React.FC<ComponentPaletteProps> = ({
const definitions = listCachedNodeSpecs().map(nodeSpecToDescription);
const filteredDefinitions = definitions.filter((definition) => {
+ // Context companions and other lifecycle-owned nodes are rendered when
+ // present in a workflow, but can only be created by graph operations.
+ if (definition.uiHints?.systemManaged) return false;
+
// Backend allowlist (server/config/node_allowlist.json). Two-tier:
// 1. `disabled_groups` + `disabled_nodes` — absolute blocklist
// enforced in BOTH normal and dev mode. The node's group
@@ -243,4 +247,4 @@ const PaletteSkeleton: React.FC = () => (
</div>
);
-export default ComponentPalette;
\ No newline at end of file
+export default ComponentPalette;
diff --git a/client/src/contexts/WebSocketContext.tsx b/client/src/contexts/WebSocketContext.tsx
index 5f0f5648..952848a8 100644
--- a/client/src/contexts/WebSocketContext.tsx
+++ b/client/src/contexts/WebSocketContext.tsx
@@ -167,6 +167,23 @@ export interface WorkflowControlStatus {
error?: string | null;
}
+export interface WorkflowStartGraph {
+ graphVersion?: number;
+ nodes: any[];
+ edges: any[];
+}
+
+/**
+ * Successful Start responses carry the backend-admitted graph in addition to
+ * the lifecycle snapshot. The graph is optional because timeout recovery can
+ * complete from a later status read, which has no graph payload.
+ */
+export interface WorkflowStartResult extends WorkflowControlStatus {
+ graph?: WorkflowStartGraph;
+ aliases?: Record<string, string>;
+ migration_warnings?: string[];
+}
+
export type WorkflowControlMutationAction = 'start' | 'pause' | 'resume' | 'reset';
export type WorkflowControlTransitionState = Extract<
WorkflowControlState,
@@ -465,7 +482,10 @@ interface WebSocketContextValue {
// Node Parameters
getNodeParameters: (nodeId: string) => Promise<NodeParameters | null>;
- getAllNodeParameters: (nodeIds: string[]) => Promise<Record<string, NodeParameters>>;
+ getAllNodeParameters: (
+ nodeIds: string[],
+ options?: { purpose?: 'export'; workflowId?: string },
+ ) => Promise<Record<string, NodeParameters>>;
saveNodeParameters: (nodeId: string, parameters: Record<string, any>, version?: number) => Promise<boolean>;
deleteNodeParameters: (nodeId: string) => Promise<boolean>;
@@ -483,7 +503,7 @@ interface WebSocketContextValue {
getDeploymentStatus: (workflowId?: string) => Promise<{ isRunning: boolean; activeRuns: number; settings?: any; workflow_id?: string }>;
cancelExecution: (workflowId: string, nodeId?: string) => Promise<any>;
getWorkflowStatus: (workflowId: string) => Promise<{ executing: boolean }>;
- startWorkflow: (workflowId: string, nodes: any[], edges: any[], sessionId?: string, expectedRevision?: number) => Promise<WorkflowControlStatus>;
+ startWorkflow: (workflowId: string, nodes: any[], edges: any[], sessionId?: string, expectedRevision?: number) => Promise<WorkflowStartResult>;
pauseWorkflow: (workflowId: string, expectedRevision: number) => Promise<WorkflowControlStatus>;
resumeWorkflow: (workflowId: string, expectedRevision: number) => Promise<WorkflowControlStatus>;
resetWorkflow: (workflowId: string, expectedRevision: number) => Promise<WorkflowControlStatus>;
@@ -2523,10 +2543,17 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
}
}, [sendRequest]);
- const getAllNodeParametersAsync = useCallback(async (nodeIds: string[]): Promise<Record<string, NodeParameters>> => {
+ const getAllNodeParametersAsync = useCallback(async (
+ nodeIds: string[],
+ options?: { purpose?: 'export'; workflowId?: string },
+ ): Promise<Record<string, NodeParameters>> => {
if (!nodeIds.length) return {};
try {
- const response = await sendRequest<any>('get_all_node_parameters', { node_ids: nodeIds });
+ const response = await sendRequest<any>('get_all_node_parameters', {
+ node_ids: nodeIds,
+ purpose: options?.purpose,
+ workflow_id: options?.workflowId,
+ });
const result: Record<string, NodeParameters> = {};
if (response.parameters) {
@@ -2536,8 +2563,11 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
version: data.version || 0,
};
}
- // Update local cache with all parameters
- setNodeParameters(prev => ({ ...prev, ...result }));
+ // Export responses are server-redacted and must not replace the
+ // editor's normal parameter cache.
+ if (options?.purpose !== 'export') {
+ setNodeParameters(prev => ({ ...prev, ...result }));
+ }
}
return result;
} catch (error) {
@@ -2774,7 +2804,7 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
type: WorkflowControlMutationRequest,
workflowId: string,
data: Record<string, any>,
- ): Promise<WorkflowControlStatus> => {
+ ): Promise<WorkflowStartResult> => {
if (workflowControlPendingRef.current.has(workflowId)) {
throw new Error('A workflow lifecycle change is already in progress');
}
@@ -2785,7 +2815,7 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
const sendMutationAttempt = async (
attemptData: Record<string, any>,
- ): Promise<WorkflowControlStatus> => {
+ ): Promise<WorkflowStartResult> => {
const response = await sendRequest<any>(type, {
...attemptData,
workflow_id: workflowId,
@@ -2802,7 +2832,21 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
? applyWorkflowControlStatus(workflowId, snapshot)
: undefined;
assertWorkflowControlMutationSucceeded(response);
- return acceptedSnapshot ?? applyWorkflowControlStatus(workflowId, response);
+ const acceptedStatus = acceptedSnapshot
+ ?? applyWorkflowControlStatus(workflowId, response);
+ if (type !== 'start_workflow') return acceptedStatus;
+
+ // Lifecycle status is merged independently, but Start also returns the
+ // exact graph admitted by backend normalization. Preserve that transport
+ // metadata for the UI caller even when the status was nested.
+ return {
+ ...acceptedStatus,
+ ...(response?.graph ? { graph: response.graph } : {}),
+ ...(response?.aliases ? { aliases: response.aliases } : {}),
+ ...(response?.migration_warnings
+ ? { migration_warnings: response.migration_warnings }
+ : {}),
+ };
};
try {
@@ -2859,7 +2903,12 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
}, [applyWorkflowControlStatus, requestWorkflowControlStatus, sendRequest]);
const graphEnvelope = (nodes: any[], edges: any[]) => ({
- nodes: nodes.map((node) => ({ id: node.id, type: node.type || '', data: node.data || {} })),
+ nodes: nodes.map((node) => ({
+ id: node.id,
+ type: node.type || '',
+ position: node.position,
+ data: node.data || {},
+ })),
edges: edges.map((edge) => ({
id: edge.id, source: edge.source, target: edge.target,
sourceHandle: edge.sourceHandle || undefined,
@@ -2867,7 +2916,13 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi
})),
});
- const startWorkflowAsync = useCallback((workflowId: string, nodes: any[], edges: any[], sessionId?: string, expectedRevision = 0) =>
+ const startWorkflowAsync = useCallback((
+ workflowId: string,
+ nodes: any[],
+ edges: any[],
+ sessionId?: string,
+ expectedRevision = 0,
+ ): Promise<WorkflowStartResult> =>
controlMutation('start_workflow', workflowId, {
...graphEnvelope(nodes, edges), session_id: sessionId || 'default', expected_revision: expectedRevision,
}), [controlMutation]);
diff --git a/client/src/contexts/__tests__/workflowControlStatus.test.ts b/client/src/contexts/__tests__/workflowControlStatus.test.ts
index 5c250f0c..d4b95639 100644
--- a/client/src/contexts/__tests__/workflowControlStatus.test.ts
+++ b/client/src/contexts/__tests__/workflowControlStatus.test.ts
@@ -10,6 +10,7 @@ import {
type WorkflowControlMutationAction,
type WorkflowControlState,
type WorkflowControlStatus,
+ type WorkflowStartResult,
} from '../WebSocketContext';
const status = (
@@ -99,6 +100,33 @@ describe('workflow control mutation responses', () => {
expect(assertWorkflowControlMutationSucceeded(response)).toBe(response);
});
+ it('preserves a successful Start graph and aliases in the typed snapshot', () => {
+ const response = {
+ success: true,
+ workflow_id: 'workflow-1',
+ generation: 2,
+ state: 'running',
+ revision: 3,
+ graph: {
+ graphVersion: 2,
+ nodes: [{ id: 'workflow-1:context:1', type: 'context' }],
+ edges: [{
+ id: 'context-edge',
+ source: 'workflow-1:context:1',
+ target: 'workflow-1:agent:1',
+ }],
+ },
+ aliases: {
+ 'legacy-agent': 'workflow-1:agent:1',
+ },
+ };
+
+ const snapshot: WorkflowStartResult = normalizeWorkflowControlStatus(response);
+
+ expect(snapshot.graph).toEqual(response.graph);
+ expect(snapshot.aliases).toEqual(response.aliases);
+ });
+
it('extracts the authoritative snapshot carried by a success:false response', () => {
const current = status({ state: 'running', revision: 3 });
const response = {
diff --git a/client/src/hooks/useReactFlowNodes.ts b/client/src/hooks/useReactFlowNodes.ts
index 795169e5..9ef0a547 100644
--- a/client/src/hooks/useReactFlowNodes.ts
+++ b/client/src/hooks/useReactFlowNodes.ts
@@ -14,6 +14,24 @@ export const useReactFlowNodes = ({ setNodes, setEdges, clearNodeStatus }: UseRe
const selectedNode = useAppStore((s) => s.selectedNode);
const setSelectedNode = useAppStore((s) => s.setSelectedNode);
+ const connectionTypeFor = (
+ role: string | undefined,
+ handleName: string,
+ ): NodeConnectionType => {
+ const normalized = role === 'tools' ? 'tool' : role;
+ const known = new Set<NodeConnectionType>([
+ 'main', 'trigger', 'ai', 'file', 'binary', 'webhook',
+ 'context', 'memory', 'tool', 'skill', 'task', 'teammates',
+ ]);
+ if (known.has(normalized as NodeConnectionType)) {
+ return normalized as NodeConnectionType;
+ }
+ const fromName = handleName.replace(/^(input|output)-/, '');
+ return known.has(fromName as NodeConnectionType)
+ ? (fromName as NodeConnectionType)
+ : 'main';
+ };
+
// Helper function to get node inputs/outputs for both enhanced and legacy nodes
const getNodeInputs = (nodeType: string): INodeInputDefinition[] => {
const definition = resolveNodeDescription(nodeType);
@@ -25,12 +43,15 @@ export const useReactFlowNodes = ({ setNodes, setEdges, clearNodeStatus }: UseRe
}
// Legacy nodes: array of strings - convert to input objects
- return (definition.inputs as string[]).map((input, index) => ({
- name: `input_${index}`,
- displayName: 'Input',
- type: (input as NodeConnectionType) || 'main',
+ return (definition.inputs as string[]).map((input) => {
+ const handle = definition.handles?.find((candidate) => candidate.name === input);
+ return {
+ name: input,
+ displayName: handle?.label || 'Input',
+ type: connectionTypeFor(handle?.role, input),
description: 'Node input connection'
- }));
+ };
+ });
};
const getNodeOutputs = (nodeType: string): INodeOutputDefinition[] => {
@@ -43,12 +64,15 @@ export const useReactFlowNodes = ({ setNodes, setEdges, clearNodeStatus }: UseRe
}
// Legacy nodes: array of strings - convert to output objects
- return (definition.outputs as string[]).map((output, index) => ({
- name: `output_${index}`,
- displayName: 'Output',
- type: (output as NodeConnectionType) || 'main',
+ return (definition.outputs as string[]).map((output) => {
+ const handle = definition.handles?.find((candidate) => candidate.name === output);
+ return {
+ name: output,
+ displayName: handle?.label || 'Output',
+ type: connectionTypeFor(handle?.role, output),
description: 'Node output connection'
- }));
+ };
+ });
};
// Validate connection compatibility
@@ -113,13 +137,13 @@ export const useReactFlowNodes = ({ setNodes, setEdges, clearNodeStatus }: UseRe
if (outputType === 'main' || inputType === 'main') return true;
// Specific compatibility rules
- const compatibilityMatrix: Record<NodeConnectionType, NodeConnectionType[]> = {
+ const compatibilityMatrix: Partial<Record<NodeConnectionType, NodeConnectionType[]>> = {
'main': ['main', 'trigger', 'ai', 'file', 'binary', 'webhook'], // Main accepts everything
'trigger': ['main', 'trigger'], // Triggers only connect to main or other triggers
'ai': ['main', 'ai'], // AI outputs connect to main or other AI inputs
'file': ['main', 'file', 'binary'], // Files can connect to binary
'binary': ['main', 'file', 'binary'], // Binary connects to file/binary
- 'webhook': ['main', 'webhook'] // Webhooks connect to main or other webhooks
+ 'webhook': ['main', 'webhook'], // Webhooks connect to main or other webhooks
};
return compatibilityMatrix[outputType]?.includes(inputType) ?? false;
diff --git a/client/src/services/workflowApi.ts b/client/src/services/workflowApi.ts
index d91643f8..b067895a 100644
--- a/client/src/services/workflowApi.ts
+++ b/client/src/services/workflowApi.ts
@@ -21,6 +21,7 @@ export interface WorkflowData {
name: string;
slug: string;
data: {
+ graphVersion?: number;
nodes: any[];
edges: any[];
};
@@ -35,6 +36,13 @@ export interface SaveWorkflowResult {
slug?: string;
name?: string;
nodeIdAliases?: Record<string, string>;
+ /** Authoritative, normalized graph. Context companions and migrations are
+ * created server-side; clients replace their edit buffer with this graph. */
+ data?: {
+ graphVersion?: number;
+ nodes: any[];
+ edges: any[];
+ };
}
export const workflowApi = {
@@ -56,6 +64,7 @@ export const workflowApi = {
slug: result.slug,
name: result.name,
nodeIdAliases: result.node_id_aliases ?? {},
+ data: result.data,
};
} catch (error) {
console.error('Failed to save workflow:', error);
diff --git a/client/src/store/__tests__/openExampleAndChat.test.ts b/client/src/store/__tests__/openExampleAndChat.test.ts
index b491816d..8be53529 100644
--- a/client/src/store/__tests__/openExampleAndChat.test.ts
+++ b/client/src/store/__tests__/openExampleAndChat.test.ts
@@ -178,3 +178,93 @@ describe('useAppStore.openExampleAndChat', () => {
expect(state.chatFocusRequest).toBe(0);
});
});
+
+describe('useAppStore authoritative normalized graph handoff', () => {
+ it('replaces the edit buffer with save response data and reconciles selection', async () => {
+ const draftAgent = {
+ id: 'draft-agent',
+ type: 'aiAgent',
+ data: { label: 'Agent' },
+ position: { x: 100, y: 100 },
+ };
+ useAppStore.setState({
+ currentWorkflow: {
+ id: 'wf-1',
+ name: 'Context flow',
+ slug: 'Context_flow_1',
+ nodes: [draftAgent],
+ edges: [],
+ createdAt: new Date(),
+ lastModified: new Date(),
+ },
+ selectedNode: draftAgent,
+ hasUnsavedChanges: true,
+ });
+ const normalizedAgent = { ...draftAgent, id: 'wf-1:aiAgent:1' };
+ const contextNode = {
+ id: 'wf-1:context:1',
+ type: 'context',
+ data: { systemManaged: true },
+ position: { x: -120, y: 100 },
+ };
+ const contextEdge = {
+ id: 'wf-1:edge:context:1',
+ source: contextNode.id,
+ target: normalizedAgent.id,
+ sourceHandle: 'output-context',
+ targetHandle: 'input-context',
+ };
+ saveWorkflowMock.mockResolvedValue({
+ success: true,
+ id: 'wf-1',
+ slug: 'Context_flow_1',
+ nodeIdAliases: { 'draft-agent': normalizedAgent.id },
+ data: {
+ graphVersion: 2,
+ nodes: [normalizedAgent, contextNode],
+ edges: [contextEdge],
+ },
+ });
+
+ await useAppStore.getState().saveWorkflow();
+
+ const state = useAppStore.getState();
+ expect(state.currentWorkflow?.nodes).toEqual([normalizedAgent, contextNode]);
+ expect(state.currentWorkflow?.edges).toEqual([contextEdge]);
+ expect(state.selectedNode).toEqual(normalizedAgent);
+ expect(state.hasUnsavedChanges).toBe(false);
+ });
+
+ it('uses aliases only as a compatibility fallback when save data is absent', async () => {
+ const draftAgent = {
+ id: 'draft-agent',
+ type: 'aiAgent',
+ data: {},
+ position: { x: 0, y: 0 },
+ };
+ useAppStore.setState({
+ currentWorkflow: {
+ id: 'wf-1',
+ name: 'Legacy save',
+ slug: '',
+ nodes: [draftAgent],
+ edges: [],
+ createdAt: new Date(),
+ lastModified: new Date(),
+ },
+ selectedNode: draftAgent,
+ });
+ saveWorkflowMock.mockResolvedValue({
+ success: true,
+ id: 'wf-1',
+ nodeIdAliases: { 'draft-agent': 'wf-1:aiAgent:1' },
+ });
+
+ await useAppStore.getState().saveWorkflow();
+
+ expect(useAppStore.getState().currentWorkflow?.nodes[0].id).toBe(
+ 'wf-1:aiAgent:1',
+ );
+ expect(useAppStore.getState().selectedNode?.id).toBe('wf-1:aiAgent:1');
+ });
+});
diff --git a/client/src/store/useAppStore.ts b/client/src/store/useAppStore.ts
index e1d4b7f2..e89b21db 100644
--- a/client/src/store/useAppStore.ts
+++ b/client/src/store/useAppStore.ts
@@ -245,6 +245,8 @@ export const useAppStore = create<AppStore>((set, get) => ({
...newWorkflow,
id: result.id,
slug: result.slug ?? newWorkflow.slug,
+ nodes: migrateNodes(result.data?.nodes ?? newWorkflow.nodes),
+ edges: result.data?.edges ?? newWorkflow.edges,
},
hasUnsavedChanges: false,
selectedNode: null,
@@ -272,15 +274,27 @@ export const useAppStore = create<AppStore>((set, get) => ({
}
const aliases = result.nodeIdAliases ?? {};
- const nodes = currentWorkflow.nodes.map(node => {
- const canonicalId = aliases[node.id];
- return canonicalId ? { ...node, id: canonicalId } : node;
- });
- const edges = currentWorkflow.edges.map(edge => ({
- ...edge,
- source: aliases[edge.source] ?? edge.source,
- target: aliases[edge.target] ?? edge.target,
- }));
+ // Server normalization is authoritative: it creates Context companions,
+ // repairs protected edges, applies migrations, and returns canonical ids
+ // atomically. Alias rewriting is only for older servers that omit data.
+ const nodes = result.data?.nodes
+ ? migrateNodes(result.data.nodes as Node[])
+ : currentWorkflow.nodes.map(node => {
+ const canonicalId = aliases[node.id];
+ return canonicalId ? { ...node, id: canonicalId } : node;
+ });
+ const edges = result.data?.edges
+ ? (result.data.edges as Edge[])
+ : currentWorkflow.edges.map(edge => ({
+ ...edge,
+ source: aliases[edge.source] ?? edge.source,
+ target: aliases[edge.target] ?? edge.target,
+ }));
+ const selected = get().selectedNode;
+ const selectedId = selected ? aliases[selected.id] ?? selected.id : null;
+ const normalizedSelection = selectedId
+ ? nodes.find((node) => node.id === selectedId) ?? null
+ : null;
set({
currentWorkflow: {
@@ -291,9 +305,7 @@ export const useAppStore = create<AppStore>((set, get) => ({
slug: result.slug ?? currentWorkflow.slug,
lastModified: new Date(),
},
- selectedNode: get().selectedNode && aliases[get().selectedNode!.id]
- ? { ...get().selectedNode!, id: aliases[get().selectedNode!.id] }
- : get().selectedNode,
+ selectedNode: normalizedSelection,
hasUnsavedChanges: false,
});
invalidateWorkflowsList();
@@ -359,14 +371,25 @@ export const useAppStore = create<AppStore>((set, get) => ({
};
// Save migrated workflow to database (sanitize node.data)
- await workflowApi.saveWorkflow(
+ const result = await workflowApi.saveWorkflow(
migratedWorkflow.id,
migratedWorkflow.name,
{ nodes: sanitizeNodes(migratedWorkflow.nodes), edges: migratedWorkflow.edges }
);
+ if (!result) return;
set({
- currentWorkflow: migratedWorkflow,
+ currentWorkflow: {
+ ...migratedWorkflow,
+ id: result.id,
+ slug: result.slug ?? migratedWorkflow.slug,
+ nodes: result.data?.nodes
+ ? migrateNodes(result.data.nodes as Node[])
+ : migratedWorkflow.nodes,
+ edges: result.data?.edges
+ ? (result.data.edges as Edge[])
+ : migratedWorkflow.edges,
+ },
hasUnsavedChanges: false
});
invalidateWorkflowsList();
diff --git a/client/src/types/INodeProperties.ts b/client/src/types/INodeProperties.ts
index 573472a9..2fec235a 100644
--- a/client/src/types/INodeProperties.ts
+++ b/client/src/types/INodeProperties.ts
@@ -34,7 +34,28 @@ export type NodeConnectionType =
| 'ai' // AI service responses
| 'file' // File data
| 'binary' // Binary data specific
- | 'webhook'; // Webhook data
+ | 'webhook' // Webhook data
+ | 'context' // Durable agent context
+ | 'memory' // Explicit memory tool
+ | 'tool'
+ | 'skill'
+ | 'task'
+ | 'teammates';
+
+/** Lossless mirror of one backend NodeSpec handle.
+ *
+ * `inputs` / `outputs` remain available for legacy consumers, while this
+ * field preserves the React Flow handle id, placement, label, offset and
+ * semantic role declared by the backend.
+ */
+export interface INodeHandleDescription {
+ name: string;
+ kind: 'input' | 'output';
+ position: 'top' | 'bottom' | 'left' | 'right';
+ offset?: string;
+ label?: string;
+ role?: string;
+}
// Enhanced output definition with data structure info
export interface INodeOutputDefinition {
@@ -187,6 +208,14 @@ export interface INodeOperationDefinition {
* exactly one panel; defaults to `false` (the panel renders normally).
*/
export interface INodeUIHints {
+ /** Graph capability: this node must own one system-managed Context node. */
+ requiresContext?: boolean;
+ /** The graph lifecycle owns this node; it is not directly palette-created. */
+ systemManaged?: boolean;
+ /** MiddleSection: render the authorized Context inspector. */
+ isContextPanel?: boolean;
+ /** MiddleSection: render explicit durable Memory item controls. */
+ isMemoryToolPanel?: boolean;
/** ParameterPanel: skip the Input section (e.g. start, skill, monitor). */
hideInputSection?: boolean;
/** ParameterPanel: skip the Output section (e.g. start, skill). */
@@ -261,6 +290,8 @@ export interface INodeTypeDescription {
};
inputs: string[] | INodeInputDefinition[];
outputs: string[] | INodeOutputDefinition[];
+ /** Exact backend-declared handle topology. */
+ handles?: INodeHandleDescription[];
properties: INodeProperties[];
credentials?: INodeCredentialDescription[];
resources?: INodeResourceDefinition[];
diff --git a/client/src/utils/__tests__/workflowExport.test.ts b/client/src/utils/__tests__/workflowExport.test.ts
new file mode 100644
index 00000000..553d9490
--- /dev/null
+++ b/client/src/utils/__tests__/workflowExport.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it } from 'vitest';
+import type { Node } from 'reactflow';
+
+import { sanitizeNodes } from '../workflowExport';
+
+
+describe('sanitizeNodes', () => {
+ it('preserves backend-owned Context identity and strips runtime payloads', () => {
+ const nodes: Node[] = [
+ {
+ id: 'ctx',
+ type: 'context',
+ position: { x: 10, y: 20 },
+ data: {
+ label: 'Context',
+ systemManaged: true,
+ agentNodeId: 'agent',
+ contextJournal: [{ role: 'user', content: 'secret' }],
+ providerBindings: { session: 'secret' },
+ },
+ },
+ ];
+
+ expect(sanitizeNodes(nodes)[0].data).toEqual({
+ label: 'Context',
+ systemManaged: true,
+ agentNodeId: 'agent',
+ });
+ });
+});
diff --git a/client/src/utils/workflowExport.ts b/client/src/utils/workflowExport.ts
index 7c53e7b3..2dc67a48 100644
--- a/client/src/utils/workflowExport.ts
+++ b/client/src/utils/workflowExport.ts
@@ -16,7 +16,15 @@ const APP_VERSION = typeof __APP_VERSION__ !== 'undefined' ? __APP_VERSION__ : '
// Fields allowed in node.data for export/save - everything else is parameter data
// that belongs in the DB and should not leak into exported JSON files.
-export const UI_DATA_FIELDS = new Set(['label', 'disabled', 'condition']);
+export const UI_DATA_FIELDS = new Set([
+ 'label',
+ 'disabled',
+ 'condition',
+ // Backend-owned Context companion identity. The client only preserves
+ // these values; normalization and lifecycle decisions remain server-side.
+ 'systemManaged',
+ 'agentNodeId',
+]);
/**
* Declarative manifest of what an exported workflow needs to run on the
diff --git a/client/vite.config.js b/client/vite.config.js
index 31baf8c5..b0651803 100644
--- a/client/vite.config.js
+++ b/client/vite.config.js
@@ -215,12 +215,7 @@ export default defineConfig(({ mode }) => {
'@hookform/resolvers',
],
'vendor-flow': ['reactflow'],
- 'vendor-radix': [
- 'radix-ui',
- '@radix-ui/react-collapsible',
- '@radix-ui/react-dialog',
- '@radix-ui/react-slot',
- ],
+ 'vendor-radix': ['radix-ui'],
'vendor-icons': ['lucide-react', '@lobehub/icons'],
'vendor-query': [
'@tanstack/react-query',
diff --git a/package.json b/package.json
index 3004a731..03c7eb44 100644
--- a/package.json
+++ b/package.json
@@ -101,13 +101,7 @@
"preuninstall": "node scripts/preinstall.js"
},
"dependencies": {
- "@react-three/drei": "^10.7.7",
- "@react-three/fiber": "^9.6.1",
- "@types/three": "^0.184.1",
- "cross-env": "^10.1.0",
- "framer-motion": "^12.43.0",
- "lucide-react": "^1.27.0",
- "three": "^0.184.0"
+ "cross-env": "^10.1.0"
},
"devDependencies": {
"typescript": "7.0.2"
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4e0c797e..de51e70f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -26,27 +26,9 @@ importers:
.:
dependencies:
- '@react-three/drei':
- specifier: ^10.7.7
- '@react-three/fiber':
- specifier: ^9.6.1
- '@types/three':
- specifier: ^0.184.1
- version: 0.184.1
cross-env:
specifier: ^10.1.0
version: 10.1.0
- framer-motion:
- specifier: ^12.43.0
- lucide-react:
- specifier: ^1.27.0
- version: 1.27.0([email protected])
- three:
- specifier: ^0.184.0
- version: 0.184.0
devDependencies:
typescript:
specifier: 7.0.2
@@ -69,15 +51,6 @@ importers:
'@lukemorales/query-key-factory':
specifier: ^1.3.4
version: 1.3.4(@tanstack/[email protected])(@tanstack/[email protected]([email protected]))
- '@radix-ui/react-collapsible':
- specifier: ^1.1.12
- '@radix-ui/react-dialog':
- specifier: ^1.1.15
- '@radix-ui/react-slot':
- specifier: ^1.2.4
- version: 1.2.4(@types/[email protected])([email protected])
'@tailwindcss/typography':
specifier: ^0.5.19
version: 0.5.19([email protected])
@@ -111,9 +84,6 @@ importers:
'@vitejs/plugin-react':
specifier: ^5.2.0
- autoprefixer:
- specifier: ^10.5.0
- version: 10.5.0([email protected])
class-variance-authority:
specifier: ^0.7.1
version: 0.7.1
@@ -147,9 +117,6 @@ importers:
partysocket:
specifier: 1.1.18
version: 1.1.18([email protected])
- postcss:
- specifier: ^8.5.18
- version: 8.5.23
prismjs:
specifier: ^1.30.0
version: 1.30.0
@@ -229,9 +196,6 @@ importers:
'@testing-library/user-event':
specifier: ^14.6.1
version: 14.6.1(@testing-library/[email protected])
- '@typescript-eslint/parser':
- specifier: ^8.65.0
'@vitest/coverage-v8':
specifier: ^4.1.8
version: 4.1.8([email protected])
@@ -575,9 +539,6 @@ packages:
resolution: {integrity: sha512-Vd/9EVDiu6PPJt9yAh6roZP6El1xHrdvIVGjyBsHR0RYwNHgL7FJPyIIW4fANJNG6FtyZfvlRPpFI4ZM/lubvw==}
engines: {node: '>=18'}
- '@dimforge/[email protected]':
- resolution: {integrity: sha512-uekIGetywIgopfD97oDL5PfeezkFpNhwlzlaEYNOA0N6ghdsOvh/HYjSMek5Q2O1PYvRSDFcqFVJl4r4ZBwOow==}
-
'@dnd-kit/[email protected]':
resolution: {integrity: sha512-2P+YgaXF+gRsIihwwY1gCsQSYnu9Zyj2py8kY5fFvUM1qm2WA2u639R6YNVfU4GWr+ZM5mqEsfHZZLoRONbemw==}
peerDependencies:
@@ -1044,9 +1005,6 @@ packages:
'@types/react': '>=16'
react: '>=16'
- '@mediapipe/[email protected]':
- resolution: {integrity: sha512-CZWV/q6TTe8ta61cZXjfnnHsfWIdFhms03M9T7Cnd5y2mdpylJM0rF1qRq+wsQVRMLz1OYPVEBU9ph2Bx8cxrg==}
-
'@mermaid-js/[email protected]':
resolution: {integrity: sha512-VuHdsYMK1bT6X2JbcAaWAhugTRvRBRyuZgd+c22swUeI9g/ntaxF7CY7dYarhZovofCbUNO0G7JesfmNtjYOCw==}
@@ -1060,11 +1018,6 @@ packages:
'@cfworker/json-schema':
optional: true
- '@monogrid/[email protected]':
- resolution: {integrity: sha512-2Z0FATFHaoYJ8b+Y4y4Hgfn3FRFwuU5zRrk+9dFWp4uGAdHGqVEdP7HP+gLA3X469KXHmfupJaUbKo1b/aDKIg==}
- peerDependencies:
- three: '>= 0.159.0'
-
resolution: {integrity: sha512-pRLMNKTSGRoLq+KnEB/7OY5vijw1XmcheAAOiv6pj7W1FG32kAGqj1C/RK/cqxRGr1Fh+zBi8sDur8kj3EQv6A==}
engines: {node: '>=18'}
@@ -1888,42 +1841,6 @@ packages:
react: '>=16.9.0'
react-dom: '>=16.9.0'
- '@react-three/[email protected]':
- resolution: {integrity: sha512-ff+J5iloR0k4tC++QtD/j9u3w5fzfgFAWDtAGQah9pF2B1YgOq/5JxqY0/aVoQG5r3xSZz0cv5tk2YuBob4xEQ==}
- peerDependencies:
- '@react-three/fiber': ^9.0.0
- react: ^19
- react-dom: ^19
- three: '>=0.159'
- peerDependenciesMeta:
- react-dom:
- optional: true
-
- '@react-three/[email protected]':
- resolution: {integrity: sha512-zF0rsKcVYpcJwbFEnv2HkHX9cvOEgsfQo/X8lwmR2dn13S4qEQJXir9fxf5js2LQFoXqxOY7MDkOkYx2uZ4gSg==}
- peerDependencies:
- expo: '>=43.0'
- expo-asset: '>=8.4'
- expo-file-system: '>=11.0'
- expo-gl: '>=11.0'
- react: '>=19 <19.3'
- react-dom: '>=19 <19.3'
- react-native: '>=0.78'
- three: '>=0.156'
- peerDependenciesMeta:
- expo:
- optional: true
- expo-asset:
- optional: true
- expo-file-system:
- optional: true
- expo-gl:
- optional: true
- react-dom:
- optional: true
- react-native:
- optional: true
-
'@reactflow/[email protected]':
resolution: {integrity: sha512-Gewd7blEVT5Lh6jqrvOgd4G6Qk17eGKQfsDXgyRSqM+CTwDqRldG2LsWN4sNeno6sbqVIC2fZ+rAUBFA9ZEUDA==}
peerDependencies:
@@ -2289,9 +2206,6 @@ packages:
'@ts-morph/[email protected]':
resolution: {integrity: sha512-Wf29UqxWDpc+i61k3oIOzcUfQt79PIT9y/MWfAGlrkjg6lBC1hwDECLXPVJAhWjiGbfBCxZd65F/LIZF3+jeJQ==}
- '@tweenjs/[email protected]':
- resolution: {integrity: sha512-vJmvvwFxYuGnF2axRtPYocag6Clbb5YS7kLL+SO/TeVFzHqDIWrNKYtcsPMibjDx9O+bu+psAy9NKfWklassUA==}
-
resolution: {integrity: sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw==}
@@ -2415,9 +2329,6 @@ packages:
resolution: {integrity: sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==}
- '@types/[email protected]':
- resolution: {integrity: sha512-AX22jp8Y7wwaBgAixaSvkoG4M/+PlAcm3Qs4OW8yT9DM4xUpWKeFhLueTAyZF39pviAdcDdeJoACapiAceqNcw==}
-
resolution: {integrity: sha512-xJBAbDifo5hpffDBuHl0Y8ywswbiAp/Wi7Y/GtAgSlZyIABppyurxVueOPE8LUQOxdlgi6Zqce7uoEpqNTeiUw==}
@@ -2472,9 +2383,6 @@ packages:
resolution: {integrity: sha512-8oljBDGun9cIsZRJR6fkihn0TSXJI0UDOOhncYaERq6M0JMDoPLxyscwruJcb4GKS6dvK/d8xebYBg27h/duaQ==}
- '@types/[email protected]':
- resolution: {integrity: sha512-ieXiYmgSRXUDeOntE1InxjWyvEelZGP63M+cGuquuRLuIKKT1osnkXjxev9B7d1nXSug5vpunx+gNlbVxMlC9A==}
-
resolution: {integrity: sha512-dISoDXWWQwUquiKsyZ4Ng+HX2KsPL7LyHKHQwgGFEA3IaKac4Obd+h2a/a6waisAoepJlBcx9paWqjA8/HVjCw==}
@@ -2492,11 +2400,6 @@ packages:
peerDependencies:
'@types/react': ^19.2.0
- '@types/[email protected]':
- resolution: {integrity: sha512-HHM3nxyUZ3zAylX8ZEyrDNd2XZOnQ0D5XfunJF5FLQnZbHHYq4UWvW1QfelQNXv1ICNkwYhfxjwfnqivYB6bFg==}
- peerDependencies:
- '@types/react': '*'
-
resolution: {integrity: sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w==}
@@ -2509,15 +2412,9 @@ packages:
resolution: {integrity: sha512-GGmQVGpQWUe5qglJozEjZV/5dyxbOOZ0LHe/lqyWssB88Y4svNfst0uqBVscdDeIKl5Jy5+aPSvy7mI9tYRguw==}
- '@types/[email protected]':
- resolution: {integrity: sha512-jIBvWWShCvlBqBNIZt0KAshWpvSjhkwkEu4ZUcASoAvhmrgAUI2t1dXrjSL4xXVLB4FznPrIsX3nKXFl/Dt4vA==}
-
resolution: {integrity: sha512-xMAgYwceFhRA2zY+XbEA7mxYbA093wdiW8Vu6gZPGWy9cmOyU9XesH1tNcEWsKFd5Vzrqx5T3D38PWx1FIIXkA==}
- '@types/[email protected]':
- resolution: {integrity: sha512-6q4VdiqVsrTRqmk62/BnlcAvIrnDM0zf2ZDVKI5kZiniWrSaOHaQzmbp+BNzoggc/8tgW412pL//wZIxu2PPTA==}
-
resolution: {integrity: sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==}
@@ -2530,9 +2427,6 @@ packages:
resolution: {integrity: sha512-lrpDziQipxCEeK5kWxvljWYhUvOiB2A9izZd9B2AFarYAkqZshb4lPbRs7zKEic6eGtH8V/2qJW+dPp9OtF6bw==}
- '@types/[email protected]':
- resolution: {integrity: sha512-h8fgEd/DpoS9CBrjEQXR+dIDraopAEfu4wYVNY2tEPwk60stPWhvZMf4Foo5FakuQ7HFZoa8WceaWFervK2Ovg==}
-
'@typescript-eslint/[email protected]':
resolution: {integrity: sha512-IEgob78X12rHpUmtcwFsXhZdVGJtwTVP8FiCLZkR6GlYVrl2PcuB+KhCE5BlVC/eQpQnu8WXRtkHZuPar+gCRA==}
engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0}
@@ -2887,13 +2781,6 @@ packages:
resolution: {integrity: sha512-0bDNnY/u6pPwHDMoF0FieU354oBi0a8rD9FcsLwzcGWbc8KS8KPIi7y+s13OlVY+gMWc/9xEMUgNE6Qm8ZllYQ==}
engines: {node: '>=4'}
- resolution: {integrity: sha512-FMhOoZV4+qR6aTUALKX2rEqGG+oyATvwBt9IIzVR5rMa2HRWPkxf+P+PAJLD1I/H5/II+HuZcBJYEFBpq39ong==}
- engines: {node: ^10 || ^12 || >=14}
- hasBin: true
- peerDependencies:
- postcss: ^8.5.18
-
resolution: {integrity: sha512-Cg7TFGpIr01vOQNODXOOaGz2NpCU5gl8x1qJFbb6hbZxR7XrcE2vtbAsTAbJ7/xwJtUuJEw8K8Zr/AE0LHlesg==}
engines: {node: '>=10', npm: '>=6'}
@@ -2923,17 +2810,11 @@ packages:
resolution: {integrity: sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==}
engines: {node: 18 || 20 || >=22}
- resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==}
-
resolution: {integrity: sha512-Asa2krT+XTPZINCS+2QcyS8WTkObE77RwkydwF7h6DmnKqbvlalz93m/dnphUyCa6SWSP51VgtEUf2FN+gelFQ==}
engines: {node: '>=6.0.0'}
hasBin: true
- resolution: {integrity: sha512-RKshQI1R3YQ+n9YJz2QQ147P66ELpa1FQEg20Dk8oW9t2KgLbpDLLp9aGZ7y8WHSshDknG0bknqGw5/tyCs5tw==}
-
resolution: {integrity: sha512-2cGmJupaNgg+QUwVLAucDuWuoMZ6EX9iHDRswZ5lsNYEmwPaRknMPCLZz07yTzVq/83p4o/wzbDZbBrTvGGTIw==}
engines: {node: '>=18'}
@@ -2951,9 +2832,6 @@ packages:
engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7}
hasBin: true
- resolution: {integrity: sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==}
-
resolution: {integrity: sha512-tjwM5exMg6BGRI+kNmTntNsvdZS1X8BFYS6tnJ2hdH0kVxM6/eVZ2xy+FqStSWvYmtfFMDLIxurorHwDKfDz5Q==}
engines: {node: '>=18'}
@@ -2974,12 +2852,6 @@ packages:
resolution: {integrity: sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==}
engines: {node: '>=6'}
- resolution: {integrity: sha512-xkxfpG2ECZ6Ww5/9+kf4mfg1VEYAoe9aDSY+IwF0UEs7qEzwy0aVRfs2grImIECs/PoBtWFrh7RXsQkwG922JA==}
- engines: {node: '>=22.0.0', npm: '>=10.5.1'}
- peerDependencies:
- three: '>=0.126.1'
-
resolution: {integrity: sha512-hVLMUZFgR4JJ6ACt1uEESvQN1/dBVqPAKY0hgrV70eN3391K6juAfTjKZLKvOMsx8PxA7gsY1/tLMMTcfFLLpw==}
@@ -3154,11 +3026,6 @@ packages:
engines: {node: '>=20'}
hasBin: true
- resolution: {integrity: sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==}
- engines: {node: '>=10.14', npm: '>=6', yarn: '>=1'}
- hasBin: true
-
resolution: {integrity: sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==}
engines: {node: '>= 8'}
@@ -3409,9 +3276,6 @@ packages:
resolution: {integrity: sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==}
engines: {node: '>=6'}
- resolution: {integrity: sha512-bqerEP1Ese6nt3rFkwPnGbsUF9a4q+gMmpTVVOEzoCyeCc+y7/RvJnQZJx1JwhgQI5Ntg0Kgat8Uu7XpBqnz1w==}
-
resolution: {integrity: sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==}
engines: {node: '>=8'}
@@ -3439,9 +3303,6 @@ packages:
resolution: {integrity: sha512-nI4U3TottKAcAD9LLud4Cb7b2QztQMUEfHbvhTH09bqXTxnSie8WnjPALV/WMCrJZ6UV/qHJ6L03OqO3LcdYZw==}
engines: {node: '>=12'}
- resolution: {integrity: sha512-m6WCKt/erDXcw+70IJXnG7M3awwQPAsZvJGX5zY7beBqpELw6RDGkYVU0W43AFxye4pDZ5i2Lbyc/NNGqwjUVQ==}
-
resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==}
engines: {node: '>= 0.4'}
@@ -3700,12 +3561,6 @@ packages:
resolution: {integrity: sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==}
engines: {node: ^12.20 || >= 14.13}
- resolution: {integrity: sha512-3JyEFWGjFn7zHmoa9+zG1BmW7X2okcmAB+0Cnu9UFbVs/jCBnl2A8o065ZlXiw145K3eBM3uLuzrYXC0RK7eDg==}
-
- resolution: {integrity: sha512-tbZNuJrLwGUp3zshBtdy4W+ORxZuIh8a5ilyIEQDC5rY1f3U20JMry0Ll3WBzU58EZKsEuJFXhb5gwv8CsPvgA==}
-
resolution: {integrity: sha512-d+l3qxjSesT4V7v2fh+QnmFnUWv9lSpjarhShNTgBOfA0ttejbQUAlHLitbjkoRiDulW0OPoQPYIGhIC8ohejg==}
engines: {node: '>=18'}
@@ -3760,9 +3615,6 @@ packages:
resolution: {integrity: sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==}
engines: {node: '>= 0.6'}
- resolution: {integrity: sha512-1X1NTtiJphryn/uLQz3whtY6jK3fTqoE3ohKs0tT+Ujr1W59oopxmoEh7Lu5p6vBaPbgoM0bzveAW4Qi5RyWDQ==}
-
resolution: {integrity: sha512-1eaL3RvR/kAlbG7UYcpMptEyzPoENO0c6w7ZnB3/hh2vSAz/6uGAFn6fdoqTBguNstf3MsFhJHsD/0DHiclG+g==}
peerDependencies:
@@ -3854,9 +3706,6 @@ packages:
resolution: {integrity: sha512-c/c15i26VrJ4IRt5Z89DnIzCGDn9EcebibhAOjw5ibqEHsE1wLUgkPn9RDmNcUKyU87GeaL633nyJ+pplFR2ZQ==}
engines: {node: '>=18'}
- resolution: {integrity: sha512-b/ZCF6amfAUb7dJM/MxRs7AetQEahYzJ8PtgfrmEdtw6uyGOr+ZSGtgjFm6mfsBkxJ4d2W7kg+Nlqzqvn3Bc0w==}
-
resolution: {integrity: sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==}
engines: {node: '>= 0.4'}
@@ -3942,9 +3791,6 @@ packages:
resolution: {integrity: sha512-6pEjquH3rqaI6cYAXYPcz9MS4rY6R4ngRgrgfDshRptUZIc3lw0MCIJIGDj9++mfySOuPTHB4nrSW99BCvOPIA==}
- resolution: {integrity: sha512-VSIRpLfRwlAAdGL4wiTucx2ScRipo0ed1FBatWkyt832jC4CReKstga6yIhYVwGu9LOBjuX9wzmRMeQdBJtzEA==}
-
resolution: {integrity: sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==}
@@ -3996,9 +3842,6 @@ packages:
resolution: {integrity: sha512-yjD9nARJ/jb1g+CvD0tlhUHOrJ9Sy0P8T9MF3YaLlHnSRpwPfpTX0XIvpmw3gAJUmEu3FiICLBDPXVwyEvrleg==}
- resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==}
-
resolution: {integrity: sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==}
engines: {node: '>= 4'}
@@ -4007,9 +3850,6 @@ packages:
resolution: {integrity: sha512-Hs59xBNfUIunMFgWAbGX5cq6893IbWg4KnrjbYwX3tx0ztorVgTDA6B2sxf8ejHJ4wz8BqGUMYlnzNBer5NvGg==}
engines: {node: '>= 4'}
- resolution: {integrity: sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==}
-
resolution: {integrity: sha512-d/+XTN3zfODyjr89gM3mPq1WNX2B8pYsu7eORitdwyA2sBubnTl3laYlBk4sXY5FUa5qTZGBDPJICVbvqzjlbw==}
@@ -4132,9 +3972,6 @@ packages:
resolution: {integrity: sha512-bCYeRA2rVibKZd+s2625gGnGF/t7DSqDs4dP7CrLA1m7jKWz6pps0LpYLJN8Q64HtmPKJ1hrN3nzPNKFEKOUiQ==}
- resolution: {integrity: sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==}
-
resolution: {integrity: sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==}
@@ -4185,11 +4022,6 @@ packages:
resolution: {integrity: sha512-HGYWWS/ehqTV3xN10i23tkPkpH46MLCIMFNCaaKNavAXTF1RkqxawEPtnjnGZ6XKSInBKkiOA5BKS+aZiY3AvA==}
engines: {node: '>=8'}
- resolution: {integrity: sha512-KLViCmWx94zOvpLwSlsx6yOCeMhZYaxrJV87Po5k/FoZzcPSahvK5qJ7fYhS61sZi5ikmh2S3Hz55A2l3U69ng==}
- peerDependencies:
- react: ^19.0.0
-
resolution: {integrity: sha512-AC/7JofJvZGrrneWNaEnJeOLUx+JlGt7tNa0wZiRPT4MY1wmfKjt2+6O2p2uz2+skll8OZZmJMNqeke7kKbNgQ==}
hasBin: true
@@ -4288,9 +4120,6 @@ packages:
resolution: {integrity: sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==}
engines: {node: '>= 0.8.0'}
- resolution: {integrity: sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==}
-
resolution: {integrity: sha512-YK7/ClTt4kAK0vo6w3X+Pnm0D2cf2vPHbhOXdoNti1Ga0al1P4TBZhwjATvjNwLEBCnKvjJc2jQgHXH0NEwlAg==}
engines: {node: '>= 12.0.0'}
@@ -4418,21 +4247,10 @@ packages:
peerDependencies:
react: ^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0
- resolution: {integrity: sha512-rJicGl/3Fly/E0rOH1YmPZ6e49JCnKknh1ox1vpHnkfjujAkKA6sqUZvH3MTAaXXjgexyUwgNwTJzTtYuAFYJw==}
- peerDependencies:
- react: ^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0
-
resolution: {integrity: sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ==}
hasBin: true
- resolution: {integrity: sha512-tRvbDF0Pgqz+9XUa4jjfgAQ8/aPKmQdWXilFu2tMy4GWj4NOsx99HlULO4IeREfbO3a0sA145DZYyvXPkybm0g==}
- peerDependencies:
- '@types/three': '>=0.134.0'
- three: '>=0.134.0'
-
resolution: {integrity: sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==}
@@ -4540,14 +4358,6 @@ packages:
resolution: {integrity: sha512-pTMbcf3rWdtLiYGpmoTjHEpeY8seiy6sR+9nD7LOs8KfUbHE4lOUAprTRqRAcWSQ6MQpdX+YEsxShtGsINtPtw==}
- resolution: {integrity: sha512-/TQj+JdZkeSUOl5Mk2J7eLcYTLiQm2IDzmlSvYm7ov15anEcDJ92GHqqazxTSreeNgfnYu24kiEvvv0WlbCdFQ==}
- peerDependencies:
- three: '>=0.137'
-
- resolution: {integrity: sha512-oRFNWJRDA/WTrVj7NWvqa5HqE1t9MYDj2VaWirQCzCCrAd2GHrqR/sQezCxiWATPNlKTcRaPRHPJwIRoPBAp5g==}
-
resolution: {integrity: sha512-RDBrHEMSxVFLg6xvnXmb1Ayr2WzLAWjeSATAoxwKYJV94TeNavgoIdA0a9ytzDSVzBy2YKFK+emCPOEibLeCrg==}
@@ -4943,16 +4753,10 @@ packages:
resolution: {integrity: sha512-orRsuYpJVw8LdAwqqLykBj9ecS5/cRHlI5+nvTo8LcCKmzDmqVORXtOIYEEQuL9D4BxtA1lm5isAqzQZCoQ6Eg==}
engines: {node: '>=4'}
- resolution: {integrity: sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==}
-
resolution: {integrity: sha512-g50586zr4bZmwFiTlflMu8E0bDTb5I5gertgwAKmsdUlTQIhZtunzUlD1WSzwcVWPoAVpsrA6vlfCD7oXvRwgg==}
engines: {node: ^10 || ^12 || >=14}
- resolution: {integrity: sha512-choctRBIV9EMT9WGAZHn3V7t0Z2pMQyl0EZE6pFc/6ml3ssw7Dlf/oAOvFwjm1HVsqfQN8GfeFyJ+d8tRzqueQ==}
-
resolution: {integrity: sha512-dM0jVuXJPsDN6DvRpea484tCUaMiXWjuCn++HGTqUWzGDjv5tZkEZldAJ/UMlqRYGFrD/etByo4/xOuC/snX2A==}
engines: {node: '>=20'}
@@ -4973,9 +4777,6 @@ packages:
resolution: {integrity: sha512-DEvV2ZF2r2/63V+tK8hQvrR2ZGn10srHbXviTlcv7Kpzw8jWiNTqbVgjO3IY8RxrrOUF8VPMQQFysYYYv0YZxw==}
engines: {node: '>=6'}
- resolution: {integrity: sha512-bN+0ehEnrXfxV2ZQvU2PetO0n4gqBD4ulq3MI1WOPLgr7/Mg9yRQkX5+0v1vagr74ZTsl7XtzlaYDo2EuCeYJw==}
-
resolution: {integrity: sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q==}
engines: {node: '>= 6'}
@@ -5405,15 +5206,6 @@ packages:
'@types/react':
optional: true
- resolution: {integrity: sha512-KrvcAo13I/60HpwGO5jpW7E9DfusKyLPLvuHlUyP5zqnmAPhNc6qTRjUQrdTADl0lpPpDVU2/Gg51UlOGHXbdg==}
- peerDependencies:
- react: '>=16.13'
- react-dom: '>=16.13'
- peerDependenciesMeta:
- react-dom:
- optional: true
-
resolution: {integrity: sha512-xNF5zDGEEIMB7cKwcen/pLig0YDf6OnfFrVgKFa7sHPf9fRem0CaLshyObbBcP88jzn0enavL39EgplgdyT21g==}
peerDependencies:
@@ -5733,15 +5525,6 @@ packages:
resolution: {integrity: sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==}
- resolution: {integrity: sha512-g5O9B0hm9CvnM36+v7SFl39T7hmAlv541tU81ME8YeSb3i1CIP5/QdDeSB3A0la0bKNHpxpwxOVRo2wFTYEosQ==}
- peerDependencies:
- '@types/three': '*'
- three: '*'
-
- resolution: {integrity: sha512-hNKz8phvYLPEcRkeG1rsGmV5ChMjKDAWU7/OJJdDErPBNChQXxCo3WZurGpnWc6gZhAzEPFad1aVgyOANH1sMw==}
-
resolution: {integrity: sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==}
engines: {node: '>= 0.8'}
@@ -5818,11 +5601,6 @@ packages:
resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==}
engines: {node: '>= 0.4'}
- resolution: {integrity: sha512-aqldKgX9aZqpoDp3e8/BZ8Dm7x1pJl+qI3ZKxDN0i/IQTWUwBx/ManmlVJ3wowqbno6c2bmiIfs+Um6LbsjJyQ==}
- peerDependencies:
- react: '>=17.0'
-
resolution: {integrity: sha512-2CC6CiKQtEwaEeNiqWTAw9PGykW8SR5zZX8MZk6TeAvEAnVS7Visz8WzphqgtQ8v2xz/4Q5K+j+SeMaKXeeQIA==}
peerDependencies:
@@ -5848,19 +5626,6 @@ packages:
resolution: {integrity: sha512-uxc/zpqFg6x7C8vOE7lh6Lbda8eEL9zmVm/PLeTPBRhh1xCgdWaQ+J1CUieGpIfm2HdtsUpRv+HshiasBMcc6A==}
engines: {node: '>=6'}
- resolution: {integrity: sha512-4G5lBaF+g2auKX3P0yqx+MJC6oVt6sB5k+CchS6Ob0qvH0YIhuUk1eYr7ktsIpY+albCqE80/FVQGV190PmiAg==}
- peerDependencies:
- three: '>= 0.159.0'
-
- resolution: {integrity: sha512-XyGQrFmNQ5O/IoKm556ftwKsBg11TIb301MB5dWNicziQBEs2g3gtOYIf7pFiLa0zI2gUwhtCjv9fmjnxKZ1Cg==}
- peerDependencies:
- three: '>=0.128.0'
-
- resolution: {integrity: sha512-wtTRjG92pM5eUg/KuUnHsqSAlPM296brTOcLgMRqEeylYTh/CdtvKUvCyyCQTzFuStieWxvZb8mVTMvdPyUpxg==}
-
resolution: {integrity: sha512-B71/4oyj61iNH0KeCamLuE2rmKuTO5byTOSVwECM5FA7TiAiAW+UqTKZ9ERueC4qvgSttUhdmq1mXC3kJqGX7A==}
engines: {node: '>=12.22'}
@@ -5926,19 +5691,6 @@ packages:
resolution: {integrity: sha512-kRj8B+YHZCc9kQYdWfJB2/oUl9rA99qbowYYBtr4ui4mZyAQ2JpvVBd/6U2YloATfqBhBTSMhTpgBHtU0Mf3Rg==}
- resolution: {integrity: sha512-Ry3jRhic9pzcY4JduSvRRyDmVOSqEW19gT4vtK+aCiPNVcDlmkxvGG0YbFd36RTDq1wExOupXnvNF/j1oiHHDA==}
- peerDependencies:
- three: '>=0.125.0'
-
- resolution: {integrity: sha512-WsePbcX8RtfidRfsxK1eCZCjF81ZDzAKHH/evLs0hdV2wpoCb0vArGZHdzdOJrSS3k4zfdtbKDaBh8+phkrYnw==}
- peerDependencies:
- three: '>=0.125.0'
-
- resolution: {integrity: sha512-W1CpvTHykaPH5brv5VHLfQo9D1OYuo0cSBEUQFFT/nBUzM8iD6Lq2/tgG/f1OelbAS1WtaTPQzE5uM49egnngw==}
-
resolution: {integrity: sha512-tmMpK00BjZiUyVyvrBK7knerNgmgvcV/KLVyuma/SC+TQN167GrMRciANTz09+k3zW8L8t60jWO1GpfkZdjTaw==}
@@ -5974,9 +5726,6 @@ packages:
engines: {node: '>=18.0.0'}
hasBin: true
- resolution: {integrity: sha512-lR5VHmkPhzdhrM092lI2nACsLO4QubF0/yoOhzX7c+wIpbN1GjHNzCc91QlpxBi+cnx8vVJ+Ur6vL5cEoQPFpQ==}
-
resolution: {integrity: sha512-7bziOlRqH0hJx80h/3mbicLW7o8qLsH5+RaLR2t+OHM3D0JlWGODQKQ4cxbK7WlvmUxpcj6Kgu6EKqjrGFe3QQ==}
@@ -6107,10 +5856,6 @@ packages:
resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==}
- resolution: {integrity: sha512-6Z7Ma2aVEWisaL6TvBCy7P8rm2LQoPv6dJ7ecIaIixHcwfbJ0x7mWdbcwlIM5IGQxPZSFYeqRCqlOOeKoJYMkw==}
- engines: {node: '>= 4'}
-
resolution: {integrity: sha512-vzi9uRZ926x4XV73S/4qQaTwPXM2JBj6/6lI/byHH1jOpCzb0zDbfytgA9LcN/hzb2l7WQSQnxITOVx5un/wGw==}
hasBin: true
@@ -6227,12 +5972,6 @@ packages:
resolution: {integrity: sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==}
engines: {node: '>= 8'}
- resolution: {integrity: sha512-LkBXKjU5r9vAW7Gcu3T5u+5cvSvh5WwINdr0C+9jpzVB41cjQAP5ePArDtk/WHYdVj0GefCgM73BA7FlIiNtdg==}
-
- resolution: {integrity: sha512-9Z0JcMTFxeE+b2x1LJTdnaT8rT8aEp7MVxkNwoycNmJWwPdzoXzMh0BjJSh/AEFP+KPYZUli814h8bJZFIZ2jA==}
-
resolution: {integrity: sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==}
engines: {node: '>=12'}
@@ -6401,24 +6140,6 @@ packages:
use-sync-external-store:
optional: true
- resolution: {integrity: sha512-/8tAspM5LMPr28b3fwLYrtdj77ECpfZviaP75CMTnwO8ISyaE4GDIG/9rDDYq/cH9D2Xw2A2RXglLInmVBQB/g==}
- engines: {node: '>=12.20.0'}
- peerDependencies:
- '@types/react': '>=18.0.0'
- immer: '>=9.0.6'
- react: '>=18.0.0'
- use-sync-external-store: '>=1.2.0'
- peerDependenciesMeta:
- '@types/react':
- optional: true
- immer:
- optional: true
- react:
- optional: true
- use-sync-external-store:
- optional: true
-
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
@@ -6812,8 +6533,6 @@ snapshots:
'@csstools/[email protected]': {}
- '@dimforge/[email protected]': {}
-
dependencies:
react: 19.2.6
@@ -7359,8 +7078,6 @@ snapshots:
'@types/react': 19.2.14
react: 19.2.6
- '@mediapipe/[email protected]': {}
-
'@mermaid-js/[email protected]':
dependencies:
'@chevrotain/types': 11.1.2
@@ -7387,11 +7104,6 @@ snapshots:
transitivePeerDependencies:
- supports-color
- dependencies:
- promise-worker-transferable: 1.0.4
- three: 0.184.0
-
dependencies:
'@open-draft/deferred-promise': 2.2.0
@@ -8276,59 +7988,6 @@ snapshots:
react: 19.2.6
react-dom: 19.2.6([email protected])
- dependencies:
- '@babel/runtime': 7.29.7
- '@mediapipe/tasks-vision': 0.10.17
- '@monogrid/gainmap-js': 3.4.0([email protected])
- '@use-gesture/react': 10.3.1([email protected])
- camera-controls: 3.1.2([email protected])
- cross-env: 7.0.3
- detect-gpu: 5.0.70
- glsl-noise: 0.0.0
- hls.js: 1.6.16
- maath: 0.10.8(@types/[email protected])([email protected])
- meshline: 3.3.1([email protected])
- react: 19.2.6
- stats-gl: 2.4.2(@types/[email protected])([email protected])
- stats.js: 0.17.0
- suspend-react: 0.1.3([email protected])
- three: 0.184.0
- three-mesh-bvh: 0.8.3([email protected])
- three-stdlib: 2.36.1([email protected])
- troika-three-text: 0.52.5([email protected])
- use-sync-external-store: 1.6.0([email protected])
- utility-types: 3.11.0
- optionalDependencies:
- react-dom: 19.2.6([email protected])
- transitivePeerDependencies:
- - '@types/react'
- - '@types/three'
- - immer
-
- dependencies:
- '@babel/runtime': 7.29.7
- '@types/webxr': 0.5.24
- base64-js: 1.5.1
- buffer: 6.0.3
- its-fine: 2.0.0(@types/[email protected])([email protected])
- react: 19.2.6
- scheduler: 0.27.0
- suspend-react: 0.1.3([email protected])
- three: 0.184.0
- use-sync-external-store: 1.6.0([email protected])
- optionalDependencies:
- react-dom: 19.2.6([email protected])
- transitivePeerDependencies:
- - '@types/react'
- - immer
-
dependencies:
@@ -8682,8 +8341,6 @@ snapshots:
minimatch: 10.2.5
path-browserify: 1.0.1
- '@tweenjs/[email protected]': {}
-
'@types/[email protected]': {}
@@ -8844,8 +8501,6 @@ snapshots:
'@types/[email protected]': {}
- '@types/[email protected]': {}
-
'@types/[email protected]': {}
@@ -8901,8 +8556,6 @@ snapshots:
dependencies:
undici-types: 7.16.0
- '@types/[email protected]': {}
-
'@types/[email protected]': {}
'@types/[email protected]': {}
@@ -8915,10 +8568,6 @@ snapshots:
dependencies:
'@types/react': 19.2.14
- dependencies:
- '@types/react': 19.2.14
-
dependencies:
csstype: 3.2.3
@@ -8936,19 +8585,8 @@ snapshots:
dependencies:
'@types/node': 24.12.3
- '@types/[email protected]': {}
-
'@types/[email protected]': {}
- '@types/[email protected]':
- dependencies:
- '@dimforge/rapier3d-compat': 0.12.0
- '@tweenjs/tween.js': 23.1.3
- '@types/stats.js': 0.17.4
- '@types/webxr': 0.5.24
- fflate: 0.8.3
- meshoptimizer: 1.1.1
-
'@types/[email protected]': {}
'@types/[email protected]': {}
@@ -8957,8 +8595,6 @@ snapshots:
'@types/[email protected]': {}
- '@types/[email protected]': {}
-
dependencies:
'@eslint-community/regexpp': 4.12.2
@@ -9363,15 +8999,6 @@ snapshots:
- dependencies:
- browserslist: 4.28.2
- caniuse-lite: 1.0.30001792
- fraction.js: 5.3.4
- picocolors: 1.1.1
- postcss: 8.5.23
- postcss-value-parser: 4.2.0
-
dependencies:
'@babel/runtime': 7.29.2
@@ -9410,14 +9037,8 @@ snapshots:
-
- dependencies:
- require-from-string: 2.0.2
-
dependencies:
bytes: 3.1.2
@@ -9448,11 +9069,6 @@ snapshots:
node-releases: 2.0.38
update-browserslist-db: 1.2.3([email protected])
- dependencies:
- base64-js: 1.5.1
- ieee754: 1.2.1
-
dependencies:
run-applescript: 7.1.0
@@ -9471,10 +9087,6 @@ snapshots:
- dependencies:
- three: 0.184.0
-
@@ -9622,10 +9234,6 @@ snapshots:
'@epic-web/invariant': 1.0.0
cross-spawn: 7.0.6
- dependencies:
- cross-spawn: 7.0.6
-
dependencies:
path-key: 3.1.1
@@ -9877,10 +9485,6 @@ snapshots:
- dependencies:
- webgl-constants: 1.1.1
-
@@ -9901,8 +9505,6 @@ snapshots:
-
dependencies:
call-bind-apply-helpers: 1.0.2
@@ -10254,10 +9856,6 @@ snapshots:
node-domexception: 1.0.0
web-streams-polyfill: 3.3.3
-
-
dependencies:
is-unicode-supported: 2.1.0
@@ -10317,8 +9915,6 @@ snapshots:
-
dependencies:
motion-dom: 12.43.0
@@ -10399,8 +9995,6 @@ snapshots:
-
@@ -10577,8 +10171,6 @@ snapshots:
dependencies:
hermes-estree: 0.25.1
-
dependencies:
react-is: 16.13.1
@@ -10631,14 +10223,10 @@ snapshots:
-
-
@@ -10721,8 +10309,6 @@ snapshots:
-
@@ -10758,13 +10344,6 @@ snapshots:
html-escaper: 2.0.2
istanbul-lib-report: 3.0.1
- dependencies:
- '@types/react-reconciler': 0.28.9(@types/[email protected])
- react: 19.2.6
- transitivePeerDependencies:
- - '@types/react'
-
@@ -10875,10 +10454,6 @@ snapshots:
prelude-ls: 1.2.1
type-check: 0.4.0
- dependencies:
- immediate: 3.0.6
-
optional: true
@@ -10985,17 +10560,8 @@ snapshots:
dependencies:
react: 19.2.6
- dependencies:
- react: 19.2.6
-
- dependencies:
- '@types/three': 0.184.1
- three: 0.184.0
-
dependencies:
'@jridgewell/sourcemap-codec': 1.5.5
@@ -11239,12 +10805,6 @@ snapshots:
ts-dedent: 2.2.0
uuid: 13.0.2
- dependencies:
- three: 0.184.0
-
-
dependencies:
decode-named-character-reference: 1.3.0
@@ -11796,16 +11356,12 @@ snapshots:
cssesc: 3.0.0
util-deprecate: 1.0.2
-
dependencies:
nanoid: 3.3.16
picocolors: 1.1.1
source-map-js: 1.2.1
-
@@ -11822,11 +11378,6 @@ snapshots:
- dependencies:
- is-promise: 2.2.2
- lie: 3.3.0
-
dependencies:
kleur: 3.0.3
@@ -12412,12 +11963,6 @@ snapshots:
optionalDependencies:
'@types/react': 19.2.14
- dependencies:
- react: 19.2.6
- optionalDependencies:
- react-dom: 19.2.6([email protected])
-
dependencies:
react: 19.2.6
@@ -12880,13 +12425,6 @@ snapshots:
- dependencies:
- '@types/three': 0.184.1
- three: 0.184.0
-
-
@@ -12956,10 +12494,6 @@ snapshots:
- dependencies:
- react: 19.2.6
-
dependencies:
dequal: 2.0.3
@@ -12978,22 +12512,6 @@ snapshots:
- dependencies:
- three: 0.184.0
-
- dependencies:
- '@types/draco3d': 1.4.10
- '@types/offscreencanvas': 2019.7.3
- '@types/webxr': 0.5.24
- draco3d: 1.5.7
- fflate: 0.6.11
- potpack: 1.0.2
- three: 0.184.0
-
-
@@ -13047,20 +12565,6 @@ snapshots:
- dependencies:
- bidi-js: 1.0.3
- three: 0.184.0
- troika-three-utils: 0.52.5([email protected])
- troika-worker-utils: 0.52.0
- webgl-sdf-generator: 1.1.1
-
- dependencies:
- three: 0.184.0
-
-
@@ -13093,14 +12597,6 @@ snapshots:
optionalDependencies:
fsevents: 2.3.3
- dependencies:
- transitivePeerDependencies:
- - '@types/react'
- - immer
- - react
-
@@ -13255,8 +12751,6 @@ snapshots:
-
@@ -13332,10 +12826,6 @@ snapshots:
-
-
@@ -13457,11 +12947,4 @@ snapshots:
react: 19.2.6
use-sync-external-store: 1.6.0([email protected])
- optionalDependencies:
- '@types/react': 19.2.14
- immer: 10.2.0
- react: 19.2.6
- use-sync-external-store: 1.6.0([email protected])
-
diff --git a/server/config/llm_defaults.json b/server/config/llm_defaults.json
index 8db389e0..9653970f 100644
--- a/server/config/llm_defaults.json
+++ b/server/config/llm_defaults.json
@@ -81,6 +81,23 @@
"_default": 200000
},
"thinking_type": "budget",
+ "_capability_note": "Two request-shape breaks landed together in the Claude 4.7+ generations: `temperature`/`top_p`/`top_k` are rejected (400 `temperature is deprecated for this model`), and the `{type: enabled, budget_tokens: N}` thinking form is rejected in favour of `{type: adaptive}`. Both lists are prefix-matched by AnthropicProvider._model_policy. claude-opus-4-6 / claude-sonnet-4-6 are deliberately absent: adaptive is recommended there but budget_tokens still works, so they keep the legacy shape until Anthropic removes it.",
+ "adaptive_thinking_models": [
+ "claude-fable-5",
+ "claude-mythos-5",
+ "claude-opus-5",
+ "claude-opus-4-8",
+ "claude-opus-4-7",
+ "claude-sonnet-5"
+ ],
+ "sampling_params_removed": [
+ "claude-fable-5",
+ "claude-mythos-5",
+ "claude-opus-5",
+ "claude-opus-4-8",
+ "claude-opus-4-7",
+ "claude-sonnet-5"
+ ],
"temperature_range": [0.0, 1.0]
},
"gemini": {
diff --git a/server/config/model_registry.json b/server/config/model_registry.json
index 222ac06f..098dd38f 100644
--- a/server/config/model_registry.json
+++ b/server/config/model_registry.json
@@ -1,8 +1,156 @@
{
- "_generated": "2026-07-25T15:25:22.803545+00:00",
+ "_generated": "2026-08-03T06:03:03.772497+00:00",
"_source": "openrouter",
- "_model_count": 345,
+ "_model_count": 337,
"models": {
+ "deepseek/deepseek-v4-flash-latest": {
+ "id": "~deepseek/deepseek-v4-flash-latest",
+ "name": "DeepSeek V4 Flash Latest",
+ "provider": "deepseek",
+ "local_id": "deepseek-v4-flash-latest",
+ "context_length": 1048576,
+ "max_output_tokens": 65536,
+ "input_price_per_mtok": 0.09,
+ "output_price_per_mtok": 0.18,
+ "supports_thinking": false,
+ "thinking_type": "none",
+ "temperature_range": [
+ 0.0,
+ 2.0
+ ],
+ "is_reasoning_model": false,
+ "supported_parameters": [
+ "frequency_penalty",
+ "include_reasoning",
+ "logit_bias",
+ "logprobs",
+ "max_tokens",
+ "min_p",
+ "presence_penalty",
+ "reasoning",
+ "reasoning_effort",
+ "repetition_penalty",
+ "response_format",
+ "seed",
+ "stop",
+ "structured_outputs",
+ "temperature",
+ "tool_choice",
+ "tools",
+ "top_a",
+ "top_k",
+ "top_logprobs",
+ "top_p"
+ ]
+ },
+ "deepseek/deepseek-v4-flash-0731": {
+ "id": "deepseek/deepseek-v4-flash-0731",
+ "name": "DeepSeek: DeepSeek V4 Flash 0731",
+ "provider": "deepseek",
+ "local_id": "deepseek-v4-flash-0731",
+ "context_length": 1048576,
+ "max_output_tokens": 65536,
+ "input_price_per_mtok": 0.09,
+ "output_price_per_mtok": 0.18,
+ "supports_thinking": false,
+ "thinking_type": "none",
+ "temperature_range": [
+ 0.0,
+ 2.0
+ ],
+ "is_reasoning_model": false,
+ "supported_parameters": [
+ "frequency_penalty",
+ "include_reasoning",
+ "logit_bias",
+ "logprobs",
+ "max_tokens",
+ "min_p",
+ "presence_penalty",
+ "reasoning",
+ "reasoning_effort",
+ "repetition_penalty",
+ "response_format",
+ "seed",
+ "stop",
+ "structured_outputs",
+ "temperature",
+ "tool_choice",
+ "tools",
+ "top_a",
+ "top_k",
+ "top_logprobs",
+ "top_p"
+ ]
+ },
+ "thinkingmachines/inkling-small": {
+ "id": "thinkingmachines/inkling-small",
+ "name": "Thinking Machines: Inkling Small",
+ "provider": "thinkingmachines",
+ "local_id": "inkling-small",
+ "context_length": 524288,
+ "max_output_tokens": 0,
+ "input_price_per_mtok": 0.5,
+ "output_price_per_mtok": 1.2,
+ "supports_thinking": false,
+ "thinking_type": "none",
+ "temperature_range": [
+ 0.0,
+ 2.0
+ ],
+ "is_reasoning_model": false,
+ "supported_parameters": [
+ "frequency_penalty",
+ "include_reasoning",
+ "logit_bias",
+ "logprobs",
+ "max_tokens",
+ "min_p",
+ "presence_penalty",
+ "reasoning",
+ "reasoning_effort",
+ "repetition_penalty",
+ "seed",
+ "stop",
+ "temperature",
+ "tool_choice",
+ "tools",
+ "top_k",
+ "top_logprobs",
+ "top_p"
+ ]
+ },
+ "qwen/qwen3.7-flash": {
+ "id": "qwen/qwen3.7-flash",
+ "name": "Qwen: Qwen3.7 Flash",
+ "provider": "qwen",
+ "local_id": "qwen3.7-flash",
+ "context_length": 1000000,
+ "max_output_tokens": 65536,
+ "input_price_per_mtok": 0.03,
+ "output_price_per_mtok": 0.13,
+ "supports_thinking": false,
+ "thinking_type": "none",
+ "temperature_range": [
+ 0.0,
+ 2.0
+ ],
+ "is_reasoning_model": false,
+ "supported_parameters": [
+ "include_reasoning",
+ "logprobs",
+ "max_tokens",
+ "presence_penalty",
+ "reasoning",
+ "response_format",
+ "seed",
+ "temperature",
+ "tool_choice",
+ "tools",
+ "top_logprobs",
+ "top_p"
+ ]
+ },
"anthropic/claude-opus-5-fast": {
"id": "anthropic/claude-opus-5-fast",
"name": "Claude Opus 5 (Fast)",
@@ -104,8 +252,8 @@
"local_id": "laguna-s-2.1",
"context_length": 1048576,
"max_output_tokens": 131072,
- "input_price_per_mtok": 0.1,
- "output_price_per_mtok": 0.2,
+ "input_price_per_mtok": 0.09,
+ "output_price_per_mtok": 0.18,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -117,7 +265,6 @@
"include_reasoning",
"max_tokens",
"reasoning",
- "stop",
"temperature",
"tool_choice",
"tools"
@@ -270,6 +417,7 @@
"reasoning",
"reasoning_effort",
"repetition_penalty",
+ "response_format",
"seed",
"stop",
"temperature",
@@ -340,15 +488,24 @@
"supported_parameters": [
"frequency_penalty",
"include_reasoning",
+ "logit_bias",
+ "logprobs",
"max_tokens",
+ "min_p",
"presence_penalty",
"reasoning",
"reasoning_effort",
+ "repetition_penalty",
"response_format",
+ "seed",
"stop",
"structured_outputs",
+ "temperature",
"tool_choice",
- "tools"
+ "tools",
+ "top_k",
+ "top_logprobs",
+ "top_p"
]
},
"meta/muse-spark-1.1": {
@@ -451,8 +608,8 @@
"local_id": "gpt-5.6-luna-pro",
"context_length": 1050000,
"max_output_tokens": 128000,
- "input_price_per_mtok": 1.0,
- "output_price_per_mtok": 6.0,
+ "input_price_per_mtok": 0.1,
+ "output_price_per_mtok": 0.6,
"supports_thinking": true,
"thinking_type": "effort",
"temperature_range": [
@@ -480,8 +637,8 @@
"local_id": "gpt-5.6-luna",
"context_length": 1050000,
"max_output_tokens": 128000,
- "input_price_per_mtok": 1.0,
- "output_price_per_mtok": 6.0,
+ "input_price_per_mtok": 0.1,
+ "output_price_per_mtok": 0.6,
"supports_thinking": true,
"thinking_type": "effort",
"temperature_range": [
@@ -509,8 +666,8 @@
"local_id": "gpt-5.6-terra-pro",
"context_length": 1050000,
"max_output_tokens": 128000,
- "input_price_per_mtok": 2.5,
- "output_price_per_mtok": 15.0,
+ "input_price_per_mtok": 1.0,
+ "output_price_per_mtok": 6.0,
"supports_thinking": true,
"thinking_type": "effort",
"temperature_range": [
@@ -538,8 +695,8 @@
"local_id": "gpt-5.6-terra",
"context_length": 1050000,
"max_output_tokens": 128000,
- "input_price_per_mtok": 2.5,
- "output_price_per_mtok": 15.0,
+ "input_price_per_mtok": 1.0,
+ "output_price_per_mtok": 6.0,
"supports_thinking": true,
"thinking_type": "effort",
"temperature_range": [
@@ -866,7 +1023,7 @@
"provider": "gemini",
"local_id": "gemini-3.1-flash-lite-image",
"context_length": 65536,
- "max_output_tokens": 66000,
+ "max_output_tokens": 65536,
"input_price_per_mtok": 0.25,
"output_price_per_mtok": 1.5,
"supports_thinking": true,
@@ -1039,9 +1196,9 @@
"provider": "z-ai",
"local_id": "glm-5.2",
"context_length": 1048576,
- "max_output_tokens": 131072,
- "input_price_per_mtok": 0.7126,
- "output_price_per_mtok": 2.2396,
+ "max_output_tokens": 262144,
+ "input_price_per_mtok": 1.19,
+ "output_price_per_mtok": 3.74,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -1098,7 +1255,7 @@
"local_id": "kimi-k2.7-code",
"context_length": 262144,
"max_output_tokens": 262144,
- "input_price_per_mtok": 0.78,
+ "input_price_per_mtok": 0.73,
"output_price_per_mtok": 3.5,
"supports_thinking": false,
"thinking_type": "none",
@@ -1316,7 +1473,7 @@
"provider": "qwen",
"local_id": "qwen3.7-plus",
"context_length": 1000000,
- "max_output_tokens": 65536,
+ "max_output_tokens": 131072,
"input_price_per_mtok": 0.32,
"output_price_per_mtok": 1.28,
"supports_thinking": false,
@@ -1327,6 +1484,7 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"logprobs",
"max_tokens",
@@ -1334,10 +1492,12 @@
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -1485,7 +1645,7 @@
"provider": "qwen",
"local_id": "qwen3.7-max",
"context_length": 1000000,
- "max_output_tokens": 65536,
+ "max_output_tokens": 131072,
"input_price_per_mtok": 1.475,
"output_price_per_mtok": 4.425,
"supports_thinking": false,
@@ -1496,6 +1656,7 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"logprobs",
"max_tokens",
@@ -1503,10 +1664,12 @@
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -1852,56 +2015,6 @@
"top_p"
]
},
- "poolside/laguna-m.1": {
- "id": "poolside/laguna-m.1",
- "name": "Poolside: Laguna M.1",
- "provider": "poolside",
- "local_id": "laguna-m.1",
- "context_length": 262144,
- "max_output_tokens": 32768,
- "input_price_per_mtok": 0.2,
- "output_price_per_mtok": 0.4,
- "supports_thinking": false,
- "thinking_type": "none",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "include_reasoning",
- "max_tokens",
- "reasoning",
- "temperature",
- "tool_choice",
- "tools"
- ]
- },
- "poolside/laguna-m.1:free": {
- "id": "poolside/laguna-m.1:free",
- "name": "Poolside: Laguna M.1 (free)",
- "provider": "poolside",
- "local_id": "laguna-m.1:free",
- "context_length": 262144,
- "max_output_tokens": 32768,
- "input_price_per_mtok": 0.0,
- "output_price_per_mtok": 0.0,
- "supports_thinking": false,
- "thinking_type": "none",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "include_reasoning",
- "max_tokens",
- "reasoning",
- "temperature",
- "tool_choice",
- "tools"
- ]
- },
"anthropic/claude-haiku-latest": {
"id": "~anthropic/claude-haiku-latest",
"name": "Anthropic Claude Haiku Latest",
@@ -1999,9 +2112,9 @@
"provider": "moonshotai",
"local_id": "kimi-latest",
"context_length": 1048576,
- "max_output_tokens": 0,
- "input_price_per_mtok": 3.0,
- "output_price_per_mtok": 15.0,
+ "max_output_tokens": 1048576,
+ "input_price_per_mtok": 2.9,
+ "output_price_per_mtok": 14.0,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -2012,15 +2125,24 @@
"supported_parameters": [
"frequency_penalty",
"include_reasoning",
+ "logit_bias",
+ "logprobs",
"max_tokens",
+ "min_p",
"presence_penalty",
"reasoning",
"reasoning_effort",
+ "repetition_penalty",
"response_format",
+ "seed",
"stop",
"structured_outputs",
+ "temperature",
"tool_choice",
- "tools"
+ "tools",
+ "top_k",
+ "top_logprobs",
+ "top_p"
]
},
"gemini/gemini-flash-latest": {
@@ -2130,6 +2252,7 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"logprobs",
"max_tokens",
@@ -2137,10 +2260,12 @@
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -2162,6 +2287,7 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"logprobs",
"max_tokens",
@@ -2169,10 +2295,12 @@
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -2222,8 +2350,8 @@
"local_id": "qwen3.6-max-preview",
"context_length": 262144,
"max_output_tokens": 65536,
- "input_price_per_mtok": 1.04,
- "output_price_per_mtok": 6.24,
+ "input_price_per_mtok": 1.027,
+ "output_price_per_mtok": 6.162,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -2232,6 +2360,7 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"logprobs",
"max_tokens",
@@ -2239,10 +2368,12 @@
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -2253,9 +2384,9 @@
"provider": "qwen",
"local_id": "qwen3.6-27b",
"context_length": 262144,
- "max_output_tokens": 131072,
- "input_price_per_mtok": 0.289,
- "output_price_per_mtok": 2.4,
+ "max_output_tokens": 65536,
+ "input_price_per_mtok": 0.3,
+ "output_price_per_mtok": 2.0,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -2387,9 +2518,9 @@
"provider": "deepseek",
"local_id": "deepseek-v4-flash",
"context_length": 1048576,
- "max_output_tokens": 0,
- "input_price_per_mtok": 0.0938,
- "output_price_per_mtok": 0.1876,
+ "max_output_tokens": 393216,
+ "input_price_per_mtok": 0.14,
+ "output_price_per_mtok": 0.28,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -2415,6 +2546,7 @@
"temperature",
"tool_choice",
"tools",
+ "top_a",
"top_k",
"top_logprobs",
"top_p"
@@ -2471,18 +2603,14 @@
],
"is_reasoning_model": false,
"supported_parameters": [
- "frequency_penalty",
"include_reasoning",
"max_tokens",
- "presence_penalty",
"reasoning",
"reasoning_effort",
"seed",
- "stop",
"temperature",
"tool_choice",
"tools",
- "top_k",
"top_p"
]
},
@@ -2684,8 +2812,8 @@
"local_id": "kimi-k2.6",
"context_length": 262144,
"max_output_tokens": 262144,
- "input_price_per_mtok": 0.646,
- "output_price_per_mtok": 2.72,
+ "input_price_per_mtok": 0.6,
+ "output_price_per_mtok": 3.41,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -2790,9 +2918,9 @@
"provider": "gemini",
"local_id": "gemma-4-26b-a4b-it",
"context_length": 262144,
- "max_output_tokens": 262144,
- "input_price_per_mtok": 0.12,
- "output_price_per_mtok": 0.35,
+ "max_output_tokens": 16384,
+ "input_price_per_mtok": 0.07,
+ "output_price_per_mtok": 0.34,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -2865,8 +2993,8 @@
"local_id": "gemma-4-31b-it",
"context_length": 262144,
"max_output_tokens": 262144,
- "input_price_per_mtok": 0.14,
- "output_price_per_mtok": 0.4,
+ "input_price_per_mtok": 0.1,
+ "output_price_per_mtok": 0.34,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -2942,6 +3070,7 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"logprobs",
"max_tokens",
@@ -2949,10 +3078,12 @@
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -3674,7 +3805,7 @@
"name": "Google: Nano Banana 2 (Gemini 3.1 Flash Image Preview)",
"provider": "gemini",
"local_id": "gemini-3.1-flash-image-preview",
- "context_length": 131072,
+ "context_length": 65536,
"max_output_tokens": 65536,
"input_price_per_mtok": 0.5,
"output_price_per_mtok": 3.0,
@@ -3780,8 +3911,8 @@
"local_id": "qwen3.5-122b-a10b",
"context_length": 262144,
"max_output_tokens": 65536,
- "input_price_per_mtok": 0.26,
- "output_price_per_mtok": 2.08,
+ "input_price_per_mtok": 0.4,
+ "output_price_per_mtok": 3.2,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -3828,16 +3959,19 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"max_tokens",
"presence_penalty",
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_p"
]
},
@@ -4008,6 +4142,7 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"logprobs",
"max_tokens",
@@ -4015,10 +4150,12 @@
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -4144,7 +4281,7 @@
"provider": "qwen",
"local_id": "qwen3-max-thinking",
"context_length": 262144,
- "max_output_tokens": 32768,
+ "max_output_tokens": 65536,
"input_price_per_mtok": 0.78,
"output_price_per_mtok": 3.9,
"supports_thinking": false,
@@ -4155,6 +4292,7 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"logprobs",
"max_tokens",
@@ -4162,10 +4300,12 @@
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -4210,7 +4350,7 @@
"local_id": "qwen3-coder-next",
"context_length": 262144,
"max_output_tokens": 262144,
- "input_price_per_mtok": 0.11,
+ "input_price_per_mtok": 0.12,
"output_price_per_mtok": 0.8,
"supports_thinking": false,
"thinking_type": "none",
@@ -4537,7 +4677,6 @@
"supported_parameters": [
"include_reasoning",
"max_completion_tokens",
- "max_tokens",
"reasoning",
"reasoning_effort",
"response_format",
@@ -4715,7 +4854,7 @@
"provider": "nvidia",
"local_id": "nemotron-3-nano-30b-a3b",
"context_length": 262144,
- "max_output_tokens": 228000,
+ "max_output_tokens": 262144,
"input_price_per_mtok": 0.05,
"output_price_per_mtok": 0.2,
"supports_thinking": false,
@@ -4857,36 +4996,6 @@
"tools"
]
},
- "mistralai/devstral-2512": {
- "id": "mistralai/devstral-2512",
- "name": "Mistral: Devstral 2 2512",
- "provider": "mistralai",
- "local_id": "devstral-2512",
- "context_length": 262144,
- "max_output_tokens": 0,
- "input_price_per_mtok": 0.4,
- "output_price_per_mtok": 2.0,
- "supports_thinking": false,
- "thinking_type": "none",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "frequency_penalty",
- "max_tokens",
- "presence_penalty",
- "response_format",
- "seed",
- "stop",
- "structured_outputs",
- "temperature",
- "tool_choice",
- "tools",
- "top_p"
- ]
- },
"relace/relace-search": {
"id": "relace/relace-search",
"name": "Relace: Relace Search",
@@ -4984,7 +5093,6 @@
"supported_parameters": [
"include_reasoning",
"max_completion_tokens",
- "max_tokens",
"reasoning",
"reasoning_effort",
"response_format",
@@ -5070,10 +5178,8 @@
"is_reasoning_model": false,
"supported_parameters": [
"frequency_penalty",
- "logprobs",
"max_tokens",
"presence_penalty",
- "repetition_penalty",
"response_format",
"seed",
"stop",
@@ -5081,7 +5187,6 @@
"temperature",
"tool_choice",
"tools",
- "top_logprobs",
"top_p"
]
},
@@ -5337,32 +5442,6 @@
"tools"
]
},
- "openai/gpt-5.1-chat": {
- "id": "openai/gpt-5.1-chat",
- "name": "OpenAI: GPT-5.1 Chat",
- "provider": "openai",
- "local_id": "gpt-5.1-chat",
- "context_length": 128000,
- "max_output_tokens": 16384,
- "input_price_per_mtok": 1.25,
- "output_price_per_mtok": 10.0,
- "supports_thinking": true,
- "thinking_type": "effort",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "max_completion_tokens",
- "max_tokens",
- "response_format",
- "seed",
- "structured_outputs",
- "tool_choice",
- "tools"
- ]
- },
"openai/gpt-5.1-codex": {
"id": "openai/gpt-5.1-codex",
"name": "OpenAI: GPT-5.1-Codex",
@@ -5382,7 +5461,6 @@
"supported_parameters": [
"include_reasoning",
"max_completion_tokens",
- "max_tokens",
"reasoning",
"reasoning_effort",
"response_format",
@@ -5398,7 +5476,7 @@
"provider": "openai",
"local_id": "gpt-5.1-codex-mini",
"context_length": 400000,
- "max_output_tokens": 100000,
+ "max_output_tokens": 128000,
"input_price_per_mtok": 0.25,
"output_price_per_mtok": 2.0,
"supports_thinking": true,
@@ -5411,7 +5489,6 @@
"supported_parameters": [
"include_reasoning",
"max_completion_tokens",
- "max_tokens",
"reasoning",
"reasoning_effort",
"response_format",
@@ -5651,15 +5728,18 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"logprobs",
"max_tokens",
"presence_penalty",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -5768,40 +5848,8 @@
"local_id": "qwen3-vl-8b-thinking",
"context_length": 131072,
"max_output_tokens": 32768,
- "input_price_per_mtok": 0.117,
- "output_price_per_mtok": 1.365,
- "supports_thinking": false,
- "thinking_type": "none",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "include_reasoning",
- "logprobs",
- "max_tokens",
- "presence_penalty",
- "reasoning",
- "response_format",
- "seed",
- "structured_outputs",
- "temperature",
- "tool_choice",
- "tools",
- "top_logprobs",
- "top_p"
- ]
- },
- "qwen/qwen3-vl-8b-instruct": {
- "id": "qwen/qwen3-vl-8b-instruct",
- "name": "Qwen: Qwen3 VL 8B Instruct",
- "provider": "qwen",
- "local_id": "qwen3-vl-8b-instruct",
- "context_length": 262144,
- "max_output_tokens": 32768,
- "input_price_per_mtok": 0.117,
- "output_price_per_mtok": 0.455,
+ "input_price_per_mtok": 0.18,
+ "output_price_per_mtok": 2.1,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -5809,45 +5857,9 @@
2.0
],
"is_reasoning_model": false,
- "supported_parameters": [
- "frequency_penalty",
- "logit_bias",
- "logprobs",
- "max_tokens",
- "presence_penalty",
- "repetition_penalty",
- "response_format",
- "seed",
- "stop",
- "structured_outputs",
- "temperature",
- "tool_choice",
- "tools",
- "top_k",
- "top_logprobs",
- "top_p"
- ]
- },
- "openai/gpt-5-image": {
- "id": "openai/gpt-5-image",
- "name": "OpenAI: GPT-5 Image",
- "provider": "openai",
- "local_id": "gpt-5-image",
- "context_length": 400000,
- "max_output_tokens": 128000,
- "input_price_per_mtok": 10.0,
- "output_price_per_mtok": 10.0,
- "supports_thinking": true,
- "thinking_type": "effort",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
"supported_parameters": [
"frequency_penalty",
"include_reasoning",
- "logit_bias",
"logprobs",
"max_tokens",
"presence_penalty",
@@ -5857,34 +5869,36 @@
"stop",
"structured_outputs",
"temperature",
+ "tool_choice",
+ "tools",
+ "top_k",
"top_logprobs",
"top_p"
]
},
- "openai/o3-deep-research": {
- "id": "openai/o3-deep-research",
- "name": "OpenAI: o3 Deep Research",
- "provider": "openai",
- "local_id": "o3-deep-research",
- "context_length": 200000,
- "max_output_tokens": 100000,
- "input_price_per_mtok": 10.0,
- "output_price_per_mtok": 40.0,
- "supports_thinking": true,
- "thinking_type": "effort",
+ "qwen/qwen3-vl-8b-instruct": {
+ "id": "qwen/qwen3-vl-8b-instruct",
+ "name": "Qwen: Qwen3 VL 8B Instruct",
+ "provider": "qwen",
+ "local_id": "qwen3-vl-8b-instruct",
+ "context_length": 262144,
+ "max_output_tokens": 32768,
+ "input_price_per_mtok": 0.117,
+ "output_price_per_mtok": 0.455,
+ "supports_thinking": false,
+ "thinking_type": "none",
"temperature_range": [
- 1.0,
- 1.0
+ 0.0,
+ 2.0
],
- "is_reasoning_model": true,
+ "is_reasoning_model": false,
"supported_parameters": [
"frequency_penalty",
- "include_reasoning",
"logit_bias",
"logprobs",
"max_tokens",
"presence_penalty",
- "reasoning",
+ "repetition_penalty",
"response_format",
"seed",
"stop",
@@ -5892,26 +5906,27 @@
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
},
- "openai/o4-mini-deep-research": {
- "id": "openai/o4-mini-deep-research",
- "name": "OpenAI: o4 Mini Deep Research",
+ "openai/gpt-5-image": {
+ "id": "openai/gpt-5-image",
+ "name": "OpenAI: GPT-5 Image",
"provider": "openai",
- "local_id": "o4-mini-deep-research",
- "context_length": 200000,
- "max_output_tokens": 100000,
- "input_price_per_mtok": 2.0,
- "output_price_per_mtok": 8.0,
+ "local_id": "gpt-5-image",
+ "context_length": 400000,
+ "max_output_tokens": 128000,
+ "input_price_per_mtok": 10.0,
+ "output_price_per_mtok": 10.0,
"supports_thinking": true,
"thinking_type": "effort",
"temperature_range": [
- 1.0,
- 1.0
+ 0.0,
+ 2.0
],
- "is_reasoning_model": true,
+ "is_reasoning_model": false,
"supported_parameters": [
"frequency_penalty",
"include_reasoning",
@@ -5925,8 +5940,6 @@
"stop",
"structured_outputs",
"temperature",
- "tool_choice",
- "tools",
"top_logprobs",
"top_p"
]
@@ -5964,8 +5977,8 @@
"local_id": "qwen3-vl-30b-a3b-thinking",
"context_length": 262144,
"max_output_tokens": 32768,
- "input_price_per_mtok": 0.13,
- "output_price_per_mtok": 1.56,
+ "input_price_per_mtok": 0.2,
+ "output_price_per_mtok": 2.4,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -5982,6 +5995,7 @@
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
@@ -5997,9 +6011,9 @@
"provider": "qwen",
"local_id": "qwen3-vl-30b-a3b-instruct",
"context_length": 262144,
- "max_output_tokens": 16384,
- "input_price_per_mtok": 0.15,
- "output_price_per_mtok": 0.6,
+ "max_output_tokens": 32768,
+ "input_price_per_mtok": 0.13,
+ "output_price_per_mtok": 0.52,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -6222,8 +6236,8 @@
"local_id": "qwen3-vl-235b-a22b-thinking",
"context_length": 131072,
"max_output_tokens": 32768,
- "input_price_per_mtok": 0.26,
- "output_price_per_mtok": 2.6,
+ "input_price_per_mtok": 0.98,
+ "output_price_per_mtok": 3.95,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -6293,7 +6307,7 @@
"provider": "qwen",
"local_id": "qwen3-max",
"context_length": 262144,
- "max_output_tokens": 32768,
+ "max_output_tokens": 65536,
"input_price_per_mtok": 0.78,
"output_price_per_mtok": 3.9,
"supports_thinking": false,
@@ -6304,15 +6318,18 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"logprobs",
"max_tokens",
"presence_penalty",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -6334,46 +6351,22 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"logprobs",
"max_tokens",
"presence_penalty",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
},
- "openai/gpt-5-codex": {
- "id": "openai/gpt-5-codex",
- "name": "OpenAI: GPT-5 Codex",
- "provider": "openai",
- "local_id": "gpt-5-codex",
- "context_length": 400000,
- "max_output_tokens": 128000,
- "input_price_per_mtok": 1.25,
- "output_price_per_mtok": 10.0,
- "supports_thinking": true,
- "thinking_type": "effort",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "include_reasoning",
- "max_tokens",
- "reasoning",
- "response_format",
- "seed",
- "structured_outputs",
- "tool_choice",
- "tools"
- ]
- },
"deepseek/deepseek-v3.1-terminus": {
"id": "deepseek/deepseek-v3.1-terminus",
"name": "DeepSeek: DeepSeek V3.1 Terminus",
@@ -6427,14 +6420,17 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"logprobs",
"max_tokens",
"presence_penalty",
"response_format",
"seed",
+ "stop",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -6446,8 +6442,8 @@
"local_id": "qwen3-next-80b-a3b-thinking",
"context_length": 262144,
"max_output_tokens": 32768,
- "input_price_per_mtok": 0.0975,
- "output_price_per_mtok": 0.78,
+ "input_price_per_mtok": 0.15,
+ "output_price_per_mtok": 1.2,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -6529,15 +6525,18 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"logprobs",
"max_tokens",
"presence_penalty",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -6549,8 +6548,8 @@
"local_id": "qwen-plus-2025-07-28:thinking",
"context_length": 1000000,
"max_output_tokens": 32768,
- "input_price_per_mtok": 0.26,
- "output_price_per_mtok": 0.78,
+ "input_price_per_mtok": 0.4,
+ "output_price_per_mtok": 1.2,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -6559,16 +6558,21 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
+ "logprobs",
"max_tokens",
"presence_penalty",
"reasoning",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
+ "top_logprobs",
"top_p"
]
},
@@ -6640,8 +6644,8 @@
"local_id": "qwen3-30b-a3b-thinking-2507",
"context_length": 81920,
"max_output_tokens": 32768,
- "input_price_per_mtok": 0.13,
- "output_price_per_mtok": 1.56,
+ "input_price_per_mtok": 0.2,
+ "output_price_per_mtok": 2.4,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -6650,15 +6654,18 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"max_tokens",
"presence_penalty",
"reasoning",
"response_format",
"seed",
+ "stop",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_p"
]
},
@@ -6847,29 +6854,6 @@
"top_p"
]
},
- "openai/gpt-5-chat": {
- "id": "openai/gpt-5-chat",
- "name": "OpenAI: GPT-5 Chat",
- "provider": "openai",
- "local_id": "gpt-5-chat",
- "context_length": 128000,
- "max_output_tokens": 16384,
- "input_price_per_mtok": 1.25,
- "output_price_per_mtok": 10.0,
- "supports_thinking": true,
- "thinking_type": "effort",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "max_tokens",
- "response_format",
- "seed",
- "structured_outputs"
- ]
- },
"openai/gpt-5": {
"id": "openai/gpt-5",
"name": "OpenAI: GPT-5",
@@ -7141,9 +7125,9 @@
"provider": "qwen",
"local_id": "qwen3-coder-30b-a3b-instruct",
"context_length": 262144,
- "max_output_tokens": 32768,
+ "max_output_tokens": 262144,
"input_price_per_mtok": 0.07,
- "output_price_per_mtok": 0.27,
+ "output_price_per_mtok": 0.28,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -7190,7 +7174,6 @@
"logit_bias",
"logprobs",
"max_tokens",
- "min_p",
"presence_penalty",
"repetition_penalty",
"response_format",
@@ -7271,9 +7254,9 @@
"provider": "qwen",
"local_id": "qwen3-235b-a22b-thinking-2507",
"context_length": 262144,
- "max_output_tokens": 32768,
- "input_price_per_mtok": 0.3,
- "output_price_per_mtok": 3.0,
+ "max_output_tokens": 0,
+ "input_price_per_mtok": 0.23,
+ "output_price_per_mtok": 2.3,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -7406,9 +7389,9 @@
"provider": "qwen",
"local_id": "qwen3-235b-a22b-2507",
"context_length": 262144,
- "max_output_tokens": 16384,
- "input_price_per_mtok": 0.09,
- "output_price_per_mtok": 0.55,
+ "max_output_tokens": 32768,
+ "input_price_per_mtok": 0.1495,
+ "output_price_per_mtok": 0.598,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -7605,9 +7588,9 @@
"provider": "mistralai",
"local_id": "mistral-small-3.2-24b-instruct",
"context_length": 256000,
- "max_output_tokens": 0,
- "input_price_per_mtok": 0.1,
- "output_price_per_mtok": 0.3,
+ "max_output_tokens": 16384,
+ "input_price_per_mtok": 0.075,
+ "output_price_per_mtok": 0.2,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -8036,9 +8019,9 @@
"provider": "qwen",
"local_id": "qwen3-30b-a3b",
"context_length": 131072,
- "max_output_tokens": 8192,
- "input_price_per_mtok": 0.13,
- "output_price_per_mtok": 0.52,
+ "max_output_tokens": 16384,
+ "input_price_per_mtok": 0.12,
+ "output_price_per_mtok": 0.5,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -8050,7 +8033,6 @@
"frequency_penalty",
"include_reasoning",
"logit_bias",
- "logprobs",
"max_tokens",
"min_p",
"presence_penalty",
@@ -8059,12 +8041,10 @@
"response_format",
"seed",
"stop",
- "structured_outputs",
"temperature",
"tool_choice",
"tools",
"top_k",
- "top_logprobs",
"top_p"
]
},
@@ -8085,15 +8065,18 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"max_tokens",
"presence_penalty",
"reasoning",
"response_format",
"seed",
+ "stop",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_p"
]
},
@@ -8190,15 +8173,18 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"include_reasoning",
"max_tokens",
"presence_penalty",
"reasoning",
"response_format",
"seed",
+ "stop",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_p"
]
},
@@ -8624,52 +8610,6 @@
"top_p"
]
},
- "openai/gpt-4o-mini-search-preview": {
- "id": "openai/gpt-4o-mini-search-preview",
- "name": "OpenAI: GPT-4o-mini Search Preview",
- "provider": "openai",
- "local_id": "gpt-4o-mini-search-preview",
- "context_length": 128000,
- "max_output_tokens": 16384,
- "input_price_per_mtok": 0.15,
- "output_price_per_mtok": 0.6,
- "supports_thinking": false,
- "thinking_type": "none",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "max_tokens",
- "response_format",
- "structured_outputs",
- "web_search_options"
- ]
- },
- "openai/gpt-4o-search-preview": {
- "id": "openai/gpt-4o-search-preview",
- "name": "OpenAI: GPT-4o Search Preview",
- "provider": "openai",
- "local_id": "gpt-4o-search-preview",
- "context_length": 128000,
- "max_output_tokens": 16384,
- "input_price_per_mtok": 2.5,
- "output_price_per_mtok": 10.0,
- "supports_thinking": false,
- "thinking_type": "none",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "max_tokens",
- "response_format",
- "structured_outputs",
- "web_search_options"
- ]
- },
"rekaai/reka-flash-3": {
"id": "rekaai/reka-flash-3",
"name": "Reka Flash 3",
@@ -8983,15 +8923,18 @@
],
"is_reasoning_model": false,
"supported_parameters": [
+ "frequency_penalty",
"logprobs",
"max_tokens",
"presence_penalty",
"response_format",
"seed",
+ "stop",
"structured_outputs",
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -9207,8 +9150,8 @@
"local_id": "deepseek-chat",
"context_length": 163840,
"max_output_tokens": 16000,
- "input_price_per_mtok": 0.2002,
- "output_price_per_mtok": 0.8001,
+ "input_price_per_mtok": 0.2574,
+ "output_price_per_mtok": 1.0287,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -9532,8 +9475,8 @@
"name": "TheDrummer: UnslopNemo 12B",
"provider": "thedrummer",
"local_id": "unslopnemo-12b",
- "context_length": 32768,
- "max_output_tokens": 32768,
+ "context_length": 1024000,
+ "max_output_tokens": 1024000,
"input_price_per_mtok": 0.4,
"output_price_per_mtok": 0.4,
"supports_thinking": false,
@@ -9545,6 +9488,7 @@
"is_reasoning_model": false,
"supported_parameters": [
"frequency_penalty",
+ "logit_bias",
"logprobs",
"max_tokens",
"presence_penalty",
@@ -9556,6 +9500,7 @@
"temperature",
"tool_choice",
"tools",
+ "top_k",
"top_logprobs",
"top_p"
]
@@ -9602,8 +9547,8 @@
"local_id": "qwen-2.5-7b-instruct",
"context_length": 32768,
"max_output_tokens": 32768,
- "input_price_per_mtok": 0.04,
- "output_price_per_mtok": 0.1,
+ "input_price_per_mtok": 0.1,
+ "output_price_per_mtok": 0.2,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -9614,7 +9559,6 @@
"supported_parameters": [
"frequency_penalty",
"logit_bias",
- "logprobs",
"max_tokens",
"min_p",
"presence_penalty",
@@ -9627,53 +9571,6 @@
"tool_choice",
"tools",
"top_k",
- "top_logprobs",
- "top_p"
- ]
- },
- "inflection/inflection-3-pi": {
- "id": "inflection/inflection-3-pi",
- "name": "Inflection: Inflection 3 Pi",
- "provider": "inflection",
- "local_id": "inflection-3-pi",
- "context_length": 8000,
- "max_output_tokens": 1024,
- "input_price_per_mtok": 2.5,
- "output_price_per_mtok": 10.0,
- "supports_thinking": false,
- "thinking_type": "none",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "max_tokens",
- "stop",
- "temperature",
- "top_p"
- ]
- },
- "inflection/inflection-3-productivity": {
- "id": "inflection/inflection-3-productivity",
- "name": "Inflection: Inflection 3 Productivity",
- "provider": "inflection",
- "local_id": "inflection-3-productivity",
- "context_length": 8000,
- "max_output_tokens": 1024,
- "input_price_per_mtok": 2.5,
- "output_price_per_mtok": 10.0,
- "supports_thinking": false,
- "thinking_type": "none",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "max_tokens",
- "stop",
- "temperature",
"top_p"
]
},
@@ -9986,6 +9883,7 @@
"supported_parameters": [
"frequency_penalty",
"logit_bias",
+ "logprobs",
"max_tokens",
"min_p",
"presence_penalty",
@@ -9996,6 +9894,7 @@
"structured_outputs",
"temperature",
"top_k",
+ "top_logprobs",
"top_p"
]
},
diff --git a/server/constants.py b/server/constants.py
index 93e20d16..b488f9f2 100644
--- a/server/constants.py
+++ b/server/constants.py
@@ -48,16 +48,21 @@ AI_AGENT_TYPES: FrozenSet[str] = frozenset(
"ai_employee",
"rlm_agent",
"claude_code_agent",
+ "codex_agent",
"vertex_managed_agent",
]
)
AI_MEMORY_TYPES: FrozenSet[str] = frozenset(
[
+ # V2 exposes this node through input-tools, while immutable V1
+ # generations still recognize their recorded input-memory topology.
"simpleMemory",
]
)
+AI_CONTEXT_TYPES: FrozenSet[str] = frozenset(["context"])
+
# Tool node types (connect to AI Agent's input-tools handle)
AI_TOOL_TYPES: FrozenSet[str] = frozenset(
[
@@ -66,6 +71,7 @@ AI_TOOL_TYPES: FrozenSet[str] = frozenset(
"duckduckgoSearch",
"writeTodos",
"processManager",
+ "simpleMemory",
]
)
@@ -89,7 +95,8 @@ AI_MODEL_TYPES: FrozenSet[str] = AI_AGENT_TYPES | AI_CHAT_MODEL_TYPES
# (input-memory, input-tools, input-model, input-skill).
# They don't execute independently - they're used by their parent nodes.
CONFIG_NODE_TYPES: FrozenSet[str] = (
- AI_MEMORY_TYPES # Memory nodes (connect to input-memory)
+ AI_CONTEXT_TYPES # Context nodes (connect to input-context)
+ | AI_MEMORY_TYPES # Legacy V1 memory / V2 Memory tool config nodes
| AI_TOOL_TYPES # Tool nodes (connect to AI Agent's input-tools)
| AI_CHAT_MODEL_TYPES # Model config nodes (connect to input-model)
| SKILL_NODE_TYPES # Skill nodes (connect to Zeenie's input-skill)
diff --git a/server/core/database.py b/server/core/database.py
index 79d66f2d..a364d719 100644
--- a/server/core/database.py
+++ b/server/core/database.py
@@ -2,6 +2,7 @@
import json
import inspect
+import secrets
from copy import deepcopy
from datetime import datetime, timedelta, timezone
from typing import Awaitable, Callable, Dict, Any, List, Optional, Tuple, Union
@@ -42,6 +43,15 @@ from models.database import (
ProxyProviderConfig,
ProxyRoutingRule,
WorkflowControlExecution,
+ WorkflowContextArchiveOutbox,
+)
+from models.agent_context import ( # noqa: F401 - registers SQLModel tables
+ AgentContextBlobRecord,
+ AgentContextCheckpointRecord,
+ AgentContextCompactionAttemptRecord,
+ AgentContextEventRecord,
+ AgentContextProviderBindingRecord,
+ AgentContextThreadRecord,
)
from models.cache import CacheEntry # SQLite-backed cache for Redis alternative
from core.logging import get_logger
@@ -61,6 +71,12 @@ ParameterMutator = Callable[
]
+def _workflow_context_node_ids(data: Any) -> set[str]:
+ if not isinstance(data, dict):
+ return set()
+ return {str(node.get("id")) for node in data.get("nodes") or [] if isinstance(node, dict) and node.get("type") == "context" and node.get("id")}
+
+
class Database:
"""Async database service with SQLModel."""
@@ -580,6 +596,7 @@ class Database:
workflow = selected.scalar_one_or_none()
if workflow is None:
return {"found": False}
+ previous_context_ids = _workflow_context_node_ids(workflow.data)
transformed = mutator(deepcopy(workflow.data or {}))
if isinstance(transformed, tuple):
new_data, metadata = transformed
@@ -587,6 +604,16 @@ class Database:
new_data, metadata = transformed, {}
if not isinstance(new_data, dict):
raise TypeError("workflow data mutator must return a dict")
+ for context_node_id in sorted(
+ previous_context_ids - _workflow_context_node_ids(new_data)
+ ):
+ session.add(
+ WorkflowContextArchiveOutbox(
+ id=f"ctxarc-{secrets.token_hex(16)}",
+ workflow_id=workflow_id,
+ context_node_id=context_node_id,
+ )
+ )
workflow.data = deepcopy(new_data)
workflow.updated_at = datetime.now(timezone.utc)
return {"found": True, **deepcopy(metadata or {})}
@@ -670,6 +697,7 @@ class Database:
slug: str,
data: Dict[str, Any],
description: Optional[str] = None,
+ context_id_aliases: Optional[Dict[str, str]] = None,
) -> bool:
"""Save or update workflow.
@@ -686,6 +714,8 @@ class Database:
stmt = select(Workflow).where(Workflow.id == workflow_id)
result = await session.execute(stmt)
existing = result.scalar_one_or_none()
+ previous_context_ids = _workflow_context_node_ids(existing.data if existing is not None else {})
+ next_context_ids = _workflow_context_node_ids(data)
if existing:
existing.name = name
@@ -696,6 +726,24 @@ class Database:
existing = Workflow(id=workflow_id, name=name, slug=slug, description=description, data=data)
session.add(existing)
+ aliases = context_id_aliases or {}
+ canonicalized_context_ids = {
+ old_id
+ for old_id in previous_context_ids
+ if aliases.get(old_id) in next_context_ids
+ }
+ for context_node_id in sorted(
+ previous_context_ids
+ - next_context_ids
+ - canonicalized_context_ids
+ ):
+ session.add(
+ WorkflowContextArchiveOutbox(
+ id=f"ctxarc-{secrets.token_hex(16)}",
+ workflow_id=workflow_id,
+ context_node_id=context_node_id,
+ )
+ )
await session.commit()
return True
@@ -769,7 +817,7 @@ class Database:
return False
async def delete_workflow(self, workflow_id: str) -> bool:
- """Delete workflow."""
+ """Delete a workflow and atomically enqueue its Context archives."""
try:
async with self.get_session() as session:
stmt = select(Workflow).where(Workflow.id == workflow_id)
@@ -777,6 +825,14 @@ class Database:
workflow = result.scalar_one_or_none()
if workflow:
+ for context_node_id in sorted(_workflow_context_node_ids(workflow.data)):
+ session.add(
+ WorkflowContextArchiveOutbox(
+ id=f"ctxarc-{secrets.token_hex(16)}",
+ workflow_id=workflow_id,
+ context_node_id=context_node_id,
+ )
+ )
await session.delete(workflow)
await session.commit()
@@ -786,6 +842,72 @@ class Database:
logger.error("Failed to delete workflow", workflow_id=workflow_id, error=str(e))
return False
+ async def list_workflow_context_archive_outbox(
+ self,
+ workflow_id: str,
+ ) -> List[Dict[str, str]]:
+ """List pending archive identities without loading Context payloads."""
+
+ try:
+ async with self.get_session() as session:
+ result = await session.execute(
+ select(WorkflowContextArchiveOutbox)
+ .where(
+ WorkflowContextArchiveOutbox.workflow_id == workflow_id,
+ WorkflowContextArchiveOutbox.status == "pending",
+ )
+ .order_by(
+ WorkflowContextArchiveOutbox.created_at,
+ WorkflowContextArchiveOutbox.id,
+ )
+ )
+ return [
+ {
+ "id": row.id,
+ "workflow_id": row.workflow_id,
+ "context_node_id": row.context_node_id,
+ }
+ for row in result.scalars().all()
+ ]
+ except Exception as e:
+ logger.error(
+ "Failed to list workflow Context archive outbox",
+ workflow_id=workflow_id,
+ error=str(e),
+ )
+ return []
+
+ async def complete_workflow_context_archive_outbox(
+ self,
+ *,
+ outbox_id: str,
+ workflow_id: str,
+ ) -> bool:
+ """Idempotently mark one successfully archived Context intent."""
+
+ try:
+ async with self.get_session() as session:
+ row = await session.get(
+ WorkflowContextArchiveOutbox,
+ outbox_id,
+ )
+ if row is None or row.workflow_id != workflow_id:
+ return False
+ if row.status == "completed":
+ return True
+ row.status = "completed"
+ row.completed_at = datetime.now(timezone.utc)
+ await session.commit()
+ return True
+ except Exception as e:
+ logger.error(
+ "Failed to complete workflow Context archive outbox",
+ workflow_id=workflow_id,
+ outbox_id=outbox_id,
+ error=str(e),
+ )
+ return False
+
# ============================================================================
# Executions
# ============================================================================
@@ -2574,6 +2696,7 @@ class Database:
async def create_workflow_control(self, control: WorkflowControlExecution) -> WorkflowControlExecution:
from models.database import WorkflowRunDataScope
+ from services.workflow_sanitizer import sanitize_runtime_payload
async with self.get_session() as session:
session.add(control)
@@ -2596,8 +2719,8 @@ class Database:
runtime_nodes = {
str(node["id"]): {
"type": node.get("type"),
- "canvas_data": dict(node.get("data") or {}),
- "parameters": parameters_by_id.get(str(node["id"]), {}),
+ "canvas_data": sanitize_runtime_payload(dict(node.get("data") or {})),
+ "parameters": sanitize_runtime_payload(parameters_by_id.get(str(node["id"]), {})),
}
for node in graph_nodes
}
@@ -2615,7 +2738,7 @@ class Database:
node_data={
str(node.get("id")): {
"type": node.get("type"),
- "data": node.get("data", {}),
+ "data": sanitize_runtime_payload(node.get("data", {})),
}
for node in control.graph_snapshot.get("nodes", [])
if node.get("id")
diff --git a/server/models/agent_context.py b/server/models/agent_context.py
new file mode 100644
index 00000000..eaf1cef2
--- /dev/null
+++ b/server/models/agent_context.py
@@ -0,0 +1,394 @@
+"""Durable Agent Context V2 contracts and normalized persistence records.
+
+The public models in this module are deliberately payload-reference oriented.
+Only :class:`AgentContextEvent` may expose an exact ``MessageWireV2`` value,
+and only when an authorized caller explicitly loads the journal. Workflow
+graphs, node parameters, Temporal commands, status events, and ordinary node
+outputs use :class:`AgentContextRef` instead.
+"""
+
+from __future__ import annotations
+
+from datetime import datetime, timezone
+from typing import Any, Literal, Optional
+
+from pydantic import BaseModel, ConfigDict, Field as PydanticField
+from sqlalchemy import (
+ Column,
+ DateTime,
+ Index,
+ JSON,
+ LargeBinary,
+ UniqueConstraint,
+)
+from sqlmodel import Field, SQLModel
+
+
+ContextThreadKind = Literal["session", "task", "execution"]
+ContextFidelity = Literal[
+ "provider_replayable",
+ "provider_bound",
+ "observable_only",
+]
+
+
+class AgentContextRef(BaseModel):
+ """Bounded reference carried by runtimes and Temporal histories."""
+
+ workflow_id: str
+ context_node_id: str
+ generation: int
+ thread_id: str
+ epoch: int
+ revision: int
+
+ model_config = ConfigDict(frozen=True)
+
+
+class AgentContextEvent(BaseModel):
+ """One exact, hash-chained observable transition in the raw journal."""
+
+ sequence: int = PydanticField(ge=1)
+ event_type: str
+ message_wire_v2: Optional[dict[str, Any]] = None
+ payload_ref: Optional[str] = None
+ operation_id: str
+ provider: Optional[str] = None
+ previous_hash: str
+ payload_hash: str
+
+ model_config = ConfigDict(frozen=True)
+
+
+class AgentContextCheckpoint(BaseModel):
+ """Provider-replayable checkpoint activated over a committed prefix."""
+
+ provider: str
+ strategy: str
+ covers_through_sequence: int = PydanticField(ge=0)
+ replay_payload_ref: str
+ active_token_count: int = PydanticField(ge=0)
+ source_revision: int = PydanticField(ge=0)
+ source_hash: str
+
+ model_config = ConfigDict(frozen=True)
+
+
+class AgentContextActiveState(BaseModel):
+ """Checkpoint plus the exact, still-uncompacted event tail."""
+
+ ref: AgentContextRef
+ checkpoint: Optional[AgentContextCheckpoint] = None
+ tail: list[AgentContextEvent] = PydanticField(default_factory=list)
+ active_token_count: int = PydanticField(default=0, ge=0)
+
+ model_config = ConfigDict(frozen=True)
+
+
+class AgentContextAppendResult(BaseModel):
+ ref: AgentContextRef
+ event: AgentContextEvent
+ applied: bool
+
+ model_config = ConfigDict(frozen=True)
+
+
+class AgentContextCompactionPlan(BaseModel):
+ """Lease returned to a compactor before it performs provider work."""
+
+ attempt_id: int
+ ref: AgentContextRef
+ provider: str
+ strategy: str
+ base_checkpoint: Optional[AgentContextCheckpoint] = None
+ committed_prefix: list[AgentContextEvent]
+ covers_through_sequence: int
+ source_revision: int
+ source_hash: str
+
+ model_config = ConfigDict(frozen=True)
+
+
+class AgentContextProviderBinding(BaseModel):
+ """Metadata-only view of an external provider continuation binding."""
+
+ provider: str
+ binding_type: str
+ binding_ref: str
+ epoch: int
+ fidelity: ContextFidelity
+
+ model_config = ConfigDict(frozen=True)
+
+
+class AgentContextThreadSummary(BaseModel):
+ """Metadata-only thread listing for an authorized Context panel."""
+
+ ref: AgentContextRef
+ thread_kind: ContextThreadKind
+ provider: Optional[str] = None
+ status: str
+ active_checkpoint_sequence: int = PydanticField(ge=0)
+ active_token_count: int = PydanticField(ge=0)
+ updated_at: datetime
+
+ model_config = ConfigDict(frozen=True)
+
+
+class AgentContextThreadRecord(SQLModel, table=True):
+ """Current control row for one isolated Context thread."""
+
+ __tablename__ = "agent_context_threads"
+ __table_args__ = (
+ UniqueConstraint(
+ "workflow_id",
+ "context_node_id",
+ "generation",
+ "thread_id",
+ name="uq_agent_context_thread",
+ ),
+ Index(
+ "ix_agent_context_node_generation",
+ "workflow_id",
+ "context_node_id",
+ "generation",
+ ),
+ )
+
+ id: Optional[int] = Field(default=None, primary_key=True)
+ workflow_id: str = Field(max_length=255)
+ context_node_id: str = Field(max_length=255)
+ generation: int = Field(index=True)
+ thread_id: str = Field(max_length=512)
+ thread_kind: str = Field(max_length=20)
+ epoch: int = Field(default=1)
+ revision: int = Field(default=0)
+ next_sequence: int = Field(default=1)
+ last_event_hash: str = Field(default="0" * 64, max_length=64)
+ active_checkpoint_id: Optional[int] = Field(default=None, index=True)
+ active_checkpoint_sequence: int = Field(default=0)
+ active_token_count: int = Field(default=0)
+ provider: Optional[str] = Field(default=None, max_length=100)
+ status: str = Field(default="active", index=True, max_length=20)
+ created_at: datetime = Field(
+ default_factory=lambda: datetime.now(timezone.utc),
+ sa_column=Column(
+ DateTime(timezone=True),
+ nullable=False,
+ ),
+ )
+ updated_at: datetime = Field(
+ default_factory=lambda: datetime.now(timezone.utc),
+ sa_column=Column(
+ DateTime(timezone=True),
+ nullable=False,
+ ),
+ )
+ archived_at: Optional[datetime] = Field(
+ default=None,
+ sa_column=Column(DateTime(timezone=True), nullable=True),
+ )
+
+
+class AgentContextEventRecord(SQLModel, table=True):
+ """Append-only exact journal entry."""
+
+ __tablename__ = "agent_context_events"
+ __table_args__ = (
+ UniqueConstraint(
+ "context_thread_id",
+ "sequence",
+ name="uq_agent_context_event_sequence",
+ ),
+ UniqueConstraint(
+ "context_thread_id",
+ "operation_id",
+ name="uq_agent_context_event_operation",
+ ),
+ Index(
+ "ix_agent_context_event_epoch_sequence",
+ "context_thread_id",
+ "epoch",
+ "sequence",
+ ),
+ )
+
+ id: Optional[int] = Field(default=None, primary_key=True)
+ context_thread_id: int = Field(
+ foreign_key="agent_context_threads.id",
+ index=True,
+ )
+ epoch: int = Field(index=True)
+ sequence: int
+ event_type: str = Field(max_length=100)
+ message_wire_v2: Optional[dict[str, Any]] = Field(
+ default=None,
+ sa_column=Column(JSON, nullable=True),
+ )
+ payload_ref: Optional[str] = Field(default=None, max_length=512)
+ operation_id: str = Field(max_length=512)
+ provider: Optional[str] = Field(default=None, max_length=100)
+ previous_hash: str = Field(max_length=64)
+ payload_hash: str = Field(max_length=64, index=True)
+ created_at: datetime = Field(
+ default_factory=lambda: datetime.now(timezone.utc),
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+
+
+class AgentContextCheckpointRecord(SQLModel, table=True):
+ """Immutable provider replay payload and its journal coverage."""
+
+ __tablename__ = "agent_context_checkpoints"
+ __table_args__ = (
+ UniqueConstraint(
+ "context_thread_id",
+ "operation_id",
+ name="uq_agent_context_checkpoint_operation",
+ ),
+ Index(
+ "ix_agent_context_checkpoint_epoch",
+ "context_thread_id",
+ "epoch",
+ "covers_through_sequence",
+ ),
+ )
+
+ id: Optional[int] = Field(default=None, primary_key=True)
+ context_thread_id: int = Field(
+ foreign_key="agent_context_threads.id",
+ index=True,
+ )
+ epoch: int = Field(index=True)
+ provider: str = Field(max_length=100)
+ strategy: str = Field(max_length=100)
+ covers_through_sequence: int
+ replay_payload_ref: str = Field(max_length=512)
+ active_token_count: int = Field(default=0)
+ source_revision: int
+ source_hash: str = Field(max_length=64)
+ operation_id: str = Field(max_length=512)
+ status: str = Field(default="active", index=True, max_length=20)
+ created_at: datetime = Field(
+ default_factory=lambda: datetime.now(timezone.utc),
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+
+
+class AgentContextProviderBindingRecord(SQLModel, table=True):
+ """Opaque external continuation identity stored by payload reference."""
+
+ __tablename__ = "agent_context_provider_bindings"
+ __table_args__ = (
+ UniqueConstraint(
+ "context_thread_id",
+ "operation_id",
+ name="uq_agent_context_binding_operation",
+ ),
+ Index(
+ "ix_agent_context_binding_lookup",
+ "context_thread_id",
+ "epoch",
+ "provider",
+ "binding_type",
+ ),
+ )
+
+ id: Optional[int] = Field(default=None, primary_key=True)
+ context_thread_id: int = Field(
+ foreign_key="agent_context_threads.id",
+ index=True,
+ )
+ epoch: int = Field(index=True)
+ provider: str = Field(max_length=100)
+ binding_type: str = Field(max_length=100)
+ binding_ref: str = Field(max_length=512)
+ binding_hash: str = Field(max_length=64)
+ fidelity: str = Field(default="provider_bound", max_length=32)
+ operation_id: str = Field(max_length=512)
+ status: str = Field(default="active", index=True, max_length=20)
+ created_at: datetime = Field(
+ default_factory=lambda: datetime.now(timezone.utc),
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+
+
+class AgentContextCompactionAttemptRecord(SQLModel, table=True):
+ """CAS lease separating expensive compaction from checkpoint activation."""
+
+ __tablename__ = "agent_context_compaction_attempts"
+ __table_args__ = (
+ UniqueConstraint(
+ "context_thread_id",
+ "operation_id",
+ name="uq_agent_context_compaction_operation",
+ ),
+ )
+
+ id: Optional[int] = Field(default=None, primary_key=True)
+ context_thread_id: int = Field(
+ foreign_key="agent_context_threads.id",
+ index=True,
+ )
+ epoch: int = Field(index=True)
+ provider: str = Field(max_length=100)
+ strategy: str = Field(max_length=100)
+ covers_through_sequence: int
+ base_checkpoint_id: Optional[int] = Field(default=None)
+ base_checkpoint_sequence: int = Field(default=0)
+ source_revision: int
+ source_hash: str = Field(max_length=64)
+ operation_id: str = Field(max_length=512)
+ status: str = Field(default="prepared", index=True, max_length=20)
+ checkpoint_id: Optional[int] = Field(default=None)
+ error_code: Optional[str] = Field(default=None, max_length=255)
+ created_at: datetime = Field(
+ default_factory=lambda: datetime.now(timezone.utc),
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+ completed_at: Optional[datetime] = Field(
+ default=None,
+ sa_column=Column(DateTime(timezone=True), nullable=True),
+ )
+
+
+class AgentContextBlobRecord(SQLModel, table=True):
+ """Optional hash-addressed storage for large or opaque exact payloads."""
+
+ __tablename__ = "agent_context_blobs"
+
+ payload_hash: str = Field(primary_key=True, max_length=64)
+ media_type: str = Field(default="application/json", max_length=100)
+ json_payload: Optional[Any] = Field(
+ default=None,
+ sa_column=Column(JSON, nullable=True),
+ )
+ binary_payload: Optional[bytes] = Field(
+ default=None,
+ sa_column=Column(LargeBinary, nullable=True),
+ )
+ byte_size: int = Field(default=0)
+ created_at: datetime = Field(
+ default_factory=lambda: datetime.now(timezone.utc),
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+
+
+__all__ = [
+ "AgentContextActiveState",
+ "AgentContextAppendResult",
+ "AgentContextBlobRecord",
+ "AgentContextCheckpoint",
+ "AgentContextCheckpointRecord",
+ "AgentContextCompactionAttemptRecord",
+ "AgentContextCompactionPlan",
+ "AgentContextEvent",
+ "AgentContextEventRecord",
+ "AgentContextProviderBinding",
+ "AgentContextProviderBindingRecord",
+ "AgentContextRef",
+ "AgentContextThreadRecord",
+ "AgentContextThreadSummary",
+ "ContextFidelity",
+ "ContextThreadKind",
+]
diff --git a/server/models/database.py b/server/models/database.py
index 93f6ab2a..1dea6856 100644
--- a/server/models/database.py
+++ b/server/models/database.py
@@ -47,6 +47,30 @@ class RuntimeMutation(SQLModel, table=True):
)
+class WorkflowContextArchiveOutbox(SQLModel, table=True):
+ """Durable Context archive intent committed with a workflow mutation.
+
+ The record contains identities and timestamps only. Exact Context events,
+ provider replay payloads, and Memory contents remain in their dedicated
+ stores and never enter this lifecycle outbox.
+ """
+
+ __tablename__ = "workflow_context_archive_outbox"
+
+ id: str = Field(primary_key=True, max_length=64)
+ workflow_id: str = Field(index=True, max_length=255)
+ context_node_id: str = Field(index=True, max_length=255)
+ status: str = Field(default="pending", index=True, max_length=20)
+ created_at: datetime = Field(
+ default_factory=lambda: datetime.now(timezone.utc),
+ sa_column=Column(DateTime(timezone=True), server_default=func.now()),
+ )
+ completed_at: Optional[datetime] = Field(
+ default=None,
+ sa_column=Column(DateTime(timezone=True), nullable=True),
+ )
+
+
class Workflow(SQLModel, table=True):
"""Workflow definitions.
diff --git a/server/nodes/agent/_handles.py b/server/nodes/agent/_handles.py
index 924037be..bab6856c 100644
--- a/server/nodes/agent/_handles.py
+++ b/server/nodes/agent/_handles.py
@@ -8,18 +8,25 @@ from __future__ import annotations
STD_SIZE = {"width": 300, "height": 200}
-STD_AGENT_HINTS = {**STD_SIZE, "hasSkills": True}
+STD_AGENT_HINTS = {
+ **STD_SIZE,
+ "hasSkills": True,
+ # An execution capability, not a rendering heuristic. Graph
+ # normalization and validation use this declaration to pair the agent
+ # with exactly one system-managed Context node.
+ "requiresContext": True,
+}
def std_agent_handles() -> tuple:
- """Skill + Tool + Memory + Task + Input + Output — shared by 18
+ """Skill + Tool + Context + Task + Input + Output — shared by 18
of the 20 agent variants. Returned as a tuple so the BaseNode
`handles` class attribute is immutable."""
return (
{"name": "input-skill", "kind": "input", "position": "bottom", "offset": "25%", "label": "Skill", "role": "skill"},
{"name": "input-tools", "kind": "input", "position": "bottom", "offset": "75%", "label": "Tool", "role": "tools"},
{"name": "input-main", "kind": "input", "position": "left", "offset": "25%", "label": "Input", "role": "main"},
- {"name": "input-memory", "kind": "input", "position": "left", "offset": "50%", "label": "Memory", "role": "memory"},
+ {"name": "input-context", "kind": "input", "position": "left", "offset": "50%", "label": "Context", "role": "context"},
{"name": "input-task", "kind": "input", "position": "left", "offset": "75%", "label": "Task", "role": "task"},
{"name": "output-main", "kind": "output", "position": "right", "offset": "50%", "label": "Output", "role": "main"},
{"name": "output-top", "kind": "output", "position": "top", "label": "Output", "role": "main"},
@@ -27,14 +34,14 @@ def std_agent_handles() -> tuple:
def team_lead_agent_handles() -> tuple:
- """Skill / Tool / Teammates bottom + Memory/Task/Input left + Outputs.
+ """Skill / Tool / Teammates bottom + Context/Task/Input left + Outputs.
Used by orchestrator_agent and ai_employee."""
return (
{"name": "input-skill", "kind": "input", "position": "bottom", "offset": "20%", "label": "Skill", "role": "skill"},
{"name": "input-tools", "kind": "input", "position": "bottom", "offset": "50%", "label": "Tool", "role": "tools"},
{"name": "input-teammates", "kind": "input", "position": "bottom", "offset": "80%", "label": "Team", "role": "teammates"},
{"name": "input-main", "kind": "input", "position": "left", "offset": "25%", "label": "Input", "role": "main"},
- {"name": "input-memory", "kind": "input", "position": "left", "offset": "50%", "label": "Memory", "role": "memory"},
+ {"name": "input-context", "kind": "input", "position": "left", "offset": "50%", "label": "Context", "role": "context"},
{"name": "input-task", "kind": "input", "position": "left", "offset": "75%", "label": "Task", "role": "task"},
{"name": "output-main", "kind": "output", "position": "right", "offset": "50%", "label": "Output", "role": "main"},
{"name": "output-top", "kind": "output", "position": "top", "label": "Output", "role": "main"},
diff --git a/server/nodes/agent/_specialized.py b/server/nodes/agent/_specialized.py
index b8a1fde9..1296b758 100644
--- a/server/nodes/agent/_specialized.py
+++ b/server/nodes/agent/_specialized.py
@@ -108,6 +108,7 @@ class SpecializedAgentBase(ActionNode, abstract=True):
"""Subclass and set type/display_name/icon/color/subtitle/description."""
component_kind = "agent"
+ requires_context = True
handles = std_agent_handles()
ui_hints = STD_AGENT_HINTS
annotations = {"destructive": False, "readonly": False, "open_world": True}
diff --git a/server/nodes/agent/ai_agent/__init__.py b/server/nodes/agent/ai_agent/__init__.py
index 9bfb2b23..88338d2a 100644
--- a/server/nodes/agent/ai_agent/__init__.py
+++ b/server/nodes/agent/ai_agent/__init__.py
@@ -117,6 +117,7 @@ class AIAgentNode(ActionNode):
group = ("agent",)
description = "AI agent with tool calling, memory, and iterative reasoning"
component_kind = "agent"
+ requires_context = True
tool_name = "delegate_to_ai_agent"
handles = std_agent_handles()
ui_hints = STD_AGENT_HINTS
diff --git a/server/nodes/agent/chat_agent/__init__.py b/server/nodes/agent/chat_agent/__init__.py
index c5105114..8c555643 100644
--- a/server/nodes/agent/chat_agent/__init__.py
+++ b/server/nodes/agent/chat_agent/__init__.py
@@ -108,6 +108,7 @@ class ChatAgentNode(ActionNode):
group = ("agent",)
description = "Conversational AI agent with skills and memory"
component_kind = "agent"
+ requires_context = True
tool_name = "delegate_to_chat_agent"
handles = std_agent_handles()
ui_hints = STD_AGENT_HINTS
diff --git a/server/nodes/agent/claude_code_agent/__init__.py b/server/nodes/agent/claude_code_agent/__init__.py
index 0e8efd78..622a1ca3 100644
--- a/server/nodes/agent/claude_code_agent/__init__.py
+++ b/server/nodes/agent/claude_code_agent/__init__.py
@@ -24,7 +24,14 @@ from typing import Any, List, Literal, Optional
from pydantic import BaseModel, ConfigDict, Field, field_validator
from core.logging import get_logger
-from services.plugin import ActionNode, NodeContext, NodeUserError, Operation, TaskQueue
+from services.plugin import (
+ ActionNode,
+ NodeContext,
+ NodeUserError,
+ Operation,
+ RetryPolicy,
+ TaskQueue,
+)
from services.plugin.edge_walker import collect_agent_connections
from .._handles import STD_AGENT_HINTS, std_agent_handles
@@ -235,11 +242,16 @@ class ClaudeCodeAgentNode(ActionNode):
group = ("agent",)
description = "Run N parallel Claude Code CLI sessions over a list of tasks. " "Each task is isolated in its own git worktree."
component_kind = "agent"
+ requires_context = True
tool_description = "ONE-SHOT delegation to Claude Code Agent. Call ONCE per task, returns task_id. Agentic coding with file reading, editing, and command execution - do NOT re-call."
handles = std_agent_handles()
ui_hints = STD_AGENT_HINTS
annotations = {"destructive": True, "readonly": False, "open_world": True}
task_queue = TaskQueue.AI_HEAVY
+ # A CLI/provider turn can be accepted before the worker observes its
+ # result. Automatic activity retry would repeat a paid, side-effecting
+ # operation with the same durable context identity.
+ retry_policy = RetryPolicy(maximum_attempts=1)
Params = ClaudeCodeAgentParams
Output = ClaudeCodeAgentOutput
@@ -255,7 +267,7 @@ class ClaudeCodeAgentNode(ActionNode):
) -> Any:
from services.cli_agent.service import get_ai_cli_service
from services.cli_agent.types import session_result_to_model
- from services.plugin.deps import get_database
+ from services.plugin.deps import get_ai_service, get_database
from services.status_broadcaster import get_status_broadcaster
start_time = time.time()
@@ -276,7 +288,7 @@ class ClaudeCodeAgentNode(ActionNode):
# read from `input_data` exactly the way `nodes/agent/_inline.py`
# does for the standard agent path.
database = get_database()
- memory_data, skill_data, tool_data, input_data, _ = await collect_agent_connections(
+ context_data, skill_data, tool_data, input_data, _ = await collect_agent_connections(
node_id,
ctx.raw,
database,
@@ -284,8 +296,10 @@ class ClaudeCodeAgentNode(ActionNode):
)
connected_skills = [s.get("skill_name") or s.get("label") for s in skill_data if s.get("skill_name") or s.get("label")]
- # Memory bridge: claude maintains its own session JSONL on disk
+ # Immutable V1 Memory bridge: claude maintains its own session JSONL
# under `<CLAUDE_CONFIG_DIR>/projects/<cwd-encoded>/<UUID>.jsonl`.
+ # Context V2 resolution, explicit UUID resume, raw-event journalling,
+ # and binding persistence live in AICliService.
# The project_key is derived from cwd (`[^a-zA-Z0-9.-] -> -`),
# so memory continuity needs only a STABLE cwd across runs.
# That's handled by AICliService passing memory_bound=True so
@@ -304,6 +318,10 @@ class ClaudeCodeAgentNode(ActionNode):
# First run: ``--continue`` with no prior session under the cwd
# is a benign no-op; claude starts fresh. Subsequent runs find
# and continue the prior JSONL.
+ from services.cli_agent.context_bridge import is_context
+
+ context_v2 = context_data if is_context(context_data) else None
+ memory_data = context_data if context_data and not context_v2 else None
continue_session = bool(memory_data)
if memory_data:
logger.info(
@@ -390,9 +408,11 @@ class ClaudeCodeAgentNode(ActionNode):
connected_skill_descriptors=skill_data,
connected_tools=tool_data,
connected_memory=memory_data,
+ connected_context=context_v2,
execution_id=ctx.execution_id,
allowed_credentials=params.allowed_credentials,
max_parallel=params.max_parallel,
+ ai_service=get_ai_service(),
)
elapsed = time.time() - start_time
@@ -417,7 +437,12 @@ class ClaudeCodeAgentNode(ActionNode):
workflow_id=workflow_id,
)
- task_models = [session_result_to_model(t).model_dump() for t in result.tasks]
+ task_models = [
+ session_result_to_model(t).model_dump() for t in result.tasks
+ ]
+ if context_v2 is not None:
+ for task_model in task_models:
+ task_model["session_id"] = None
# Legacy single-task convenience fields
legacy_response = None
@@ -425,7 +450,11 @@ class ClaudeCodeAgentNode(ActionNode):
legacy_cost = None
if len(result.tasks) == 1:
legacy_response = result.tasks[0].response
- legacy_session_id = result.tasks[0].session_id
+ legacy_session_id = (
+ result.tasks[0].session_id
+ if context_v2 is None
+ else None
+ )
legacy_cost = result.tasks[0].cost_usd
return {
diff --git a/server/nodes/agent/claude_code_agent/_pool.py b/server/nodes/agent/claude_code_agent/_pool.py
index 2a8d83d6..63591e9a 100644
--- a/server/nodes/agent/claude_code_agent/_pool.py
+++ b/server/nodes/agent/claude_code_agent/_pool.py
@@ -1,9 +1,10 @@
"""Long-lived ``claude`` subprocess pool — VSCode-extension pattern.
Keeps one warm ``claude --output-format stream-json --input-format
-stream-json --verbose --ide`` subprocess per ``simpleMemory.node_id``
-so successive turns can reuse the same process — same session UUID
-across turns, no respawn cost. Mirrors what Anthropic's official
+stream-json --verbose --ide`` subprocess per Context thread+epoch (or per
+``simpleMemory.node_id`` for immutable V1 generations) so successive turns
+can reuse the same process — same session UUID across turns, no respawn cost.
+Mirrors what Anthropic's official
VSCode extension does (verified from the on-disk extension source at
``$VSCODE_EXT_DIR/anthropic.claude-code-<ver>/extension.js`` line 156).
@@ -72,7 +73,7 @@ import time
import uuid
from dataclasses import dataclass, field
from pathlib import Path
-from typing import Any, Dict, List, Literal, Optional
+from typing import Any, Awaitable, Callable, Dict, List, Literal, Optional, Union
from core.logging import get_logger
from services.cli_agent.factory import create_cli_provider
@@ -81,6 +82,8 @@ from services.cli_agent.types import ClaudeTaskSpec
logger = get_logger(__name__)
+ClaudePoolKey = Union[str, tuple[str, str, int]]
+
# Defaults — keep aligned with the operator's mental model.
_DEFAULT_IDLE_TTL = 30 * 60.0
@@ -113,7 +116,9 @@ class PooledClaudeSession:
stream-json mode, NOT to the JSONL file.
"""
- memory_node_id: str
+ # V1 uses a Simple Memory node id. Context V2 uses the exact
+ # (context_node_id, thread_id, epoch) tuple.
+ memory_node_id: ClaudePoolKey
process: asyncio.subprocess.Process
cwd: Path
# ``current_session_uuid`` is "" until the first turn's ``system/init``
@@ -165,6 +170,10 @@ class PooledClaudeSession:
# warning level).
stdout_reader_task: Optional[asyncio.Task[None]] = None
stderr_drain_task: Optional[asyncio.Task[None]] = None
+ context_event_sink: Optional[
+ Callable[[Dict[str, Any]], Awaitable[None]]
+ ] = None
+ context_capture_error: Optional[str] = None
class ClaudeSessionPool:
@@ -190,7 +199,7 @@ class ClaudeSessionPool:
max_size: int = _DEFAULT_MAX_SIZE,
reaper_interval: float = _DEFAULT_REAPER_INTERVAL,
) -> None:
- self._pool: Dict[str, PooledClaudeSession] = {}
+ self._pool: Dict[ClaudePoolKey, PooledClaudeSession] = {}
self._idle_ttl = float(idle_ttl)
self._max_size = int(max_size)
self._reaper_interval = float(reaper_interval)
@@ -213,7 +222,10 @@ class ClaudeSessionPool:
name="ClaudeSessionPool.reaper",
)
- def peek(self, memory_node_id: str) -> Optional[PooledClaudeSession]:
+ def peek(
+ self,
+ memory_node_id: ClaudePoolKey,
+ ) -> Optional[PooledClaudeSession]:
"""Return the live session for ``memory_node_id`` or ``None``."""
session = self._pool.get(memory_node_id)
if session is None:
@@ -224,7 +236,7 @@ class ClaudeSessionPool:
async def acquire(
self,
- memory_node_id: str,
+ memory_node_id: ClaudePoolKey,
*,
spec: ClaudeTaskSpec,
cwd: Path,
@@ -236,6 +248,9 @@ class ClaudeSessionPool:
connected_skill_names: Optional[List[str]] = None,
workspace_dir: Optional[Path] = None,
workflow_id: Optional[str] = None,
+ context_event_sink: Optional[
+ Callable[[Dict[str, Any]], Awaitable[None]]
+ ] = None,
) -> PooledClaudeSession:
"""Return a live :class:`PooledClaudeSession`.
@@ -256,6 +271,19 @@ class ClaudeSessionPool:
# not wait for a busy per-memory lease here: a queued turn for M1
# must never prevent an unrelated M2 from being acquired.
async with self._pool_lock:
+ if isinstance(memory_node_id, tuple):
+ stale_keys = [
+ key
+ for key in self._pool
+ if isinstance(key, tuple)
+ and key[:2] == memory_node_id[:2]
+ and key[2] != memory_node_id[2]
+ ]
+ for stale_key in stale_keys:
+ await self._terminate_locked(
+ stale_key,
+ reason="epoch_changed",
+ )
existing = self._pool.get(memory_node_id)
crashed_uuid = ""
if existing is not None and existing.process.returncode is not None:
@@ -291,10 +319,12 @@ class ClaudeSessionPool:
connected_tool_names=connected_tool_names,
connected_skill_names=connected_skill_names,
workspace_dir=workspace_dir,
+ context_event_sink=context_event_sink,
)
if mcp_bearer_token:
session.batch_token = mcp_bearer_token
session.workspace_dir = workspace_dir
+ session.context_event_sink = context_event_sink
session.materialised_skills = frozenset(
connected_skill_names or []
)
@@ -350,19 +380,24 @@ class ClaudeSessionPool:
memory_node_id=memory_node_id,
mcp_bearer_token=mcp_bearer_token,
connected_skill_names=connected_skill_names,
+ context_event_sink=context_event_sink,
)
async def _prepare_warm_reuse(
self,
existing: PooledClaudeSession,
*,
- memory_node_id: str,
+ memory_node_id: ClaudePoolKey,
mcp_bearer_token: Optional[str],
connected_skill_names: Optional[List[str]],
+ context_event_sink: Optional[
+ Callable[[Dict[str, Any]], Awaitable[None]]
+ ],
) -> PooledClaudeSession:
"""Rebind one already-leased warm process for the next turn."""
try:
existing.last_used_at = time.monotonic()
+ existing.context_event_sink = context_event_sink
if (
existing.batch_token
and mcp_bearer_token
@@ -472,11 +507,42 @@ class ClaudeSessionPool:
if session.turn_lease.locked():
session.turn_lease.release()
- async def terminate(self, memory_node_id: str) -> None:
+ async def terminate(self, memory_node_id: ClaudePoolKey) -> None:
"""Force-drop a specific pooled session."""
async with self._pool_lock:
await self._terminate_locked(memory_node_id)
+ async def terminate_context(
+ self,
+ context_node_id: str,
+ *,
+ thread_id: Optional[str] = None,
+ keep_epoch: Optional[int] = None,
+ ) -> int:
+ """Terminate warm processes fenced by a Context lifecycle change.
+
+ Context clear/reset handlers can call this immediately after
+ ``start_epoch``. ``acquire`` independently performs the same fence,
+ so a missed best-effort lifecycle notification cannot reuse an old
+ epoch later.
+ """
+
+ async with self._pool_lock:
+ keys = [
+ key
+ for key in self._pool
+ if isinstance(key, tuple)
+ and key[0] == context_node_id
+ and (thread_id is None or key[1] == thread_id)
+ and (keep_epoch is None or key[2] != keep_epoch)
+ ]
+ for key in keys:
+ await self._terminate_locked(
+ key,
+ reason="context_epoch_changed",
+ )
+ return len(keys)
+
async def shutdown_all(self) -> None:
"""Terminate every pooled subprocess + stop the reaper.
@@ -526,6 +592,7 @@ class ClaudeSessionPool:
session.result_event.clear()
session.events_this_turn = []
+ session.context_capture_error = None
# Stream-json input shape — the VSCode extension's wire
# format. Newline-delimited; one JSON object per turn.
@@ -552,6 +619,14 @@ class ClaudeSessionPool:
session.result_event.wait(),
timeout=float(timeout_seconds),
)
+ if session.context_capture_error:
+ return self._build_result_from_events(
+ session=session,
+ events=session.events_this_turn,
+ prompt=prompt,
+ success=False,
+ error=session.context_capture_error,
+ )
except asyncio.TimeoutError:
logger.warning(
"[ClaudeSessionPool] turn timeout memory_node=%s " "prompt_len=%d",
@@ -601,7 +676,7 @@ class ClaudeSessionPool:
async def _spawn(
self,
*,
- memory_node_id: str,
+ memory_node_id: ClaudePoolKey,
spec: ClaudeTaskSpec,
cwd: Path,
env: Dict[str, str],
@@ -611,6 +686,9 @@ class ClaudeSessionPool:
connected_tool_names: Optional[List[str]],
connected_skill_names: Optional[List[str]] = None,
workspace_dir: Optional[Path] = None,
+ context_event_sink: Optional[
+ Callable[[Dict[str, Any]], Awaitable[None]]
+ ] = None,
) -> PooledClaudeSession:
"""Cold-spawn a new pooled claude subprocess. Caller holds pool_lock."""
# Materialise connected skills under
@@ -668,6 +746,7 @@ class ClaudeSessionPool:
memory_node_id=memory_node_id,
process=process,
cwd=cwd,
+ context_event_sink=context_event_sink,
)
# stdout reader — the single runtime contract in stream-json
@@ -689,11 +768,34 @@ class ClaudeSessionPool:
try:
event = json.loads(line)
except json.JSONDecodeError:
- logger.debug(
- "[ClaudeSessionPool] stdout non-JSON line " "memory_node=%s line=%r",
- memory_node_id,
- line[:200],
- )
+ if session.context_event_sink is not None:
+ try:
+ await session.context_event_sink(
+ {
+ "type": "stdout.non_json",
+ "raw": line,
+ }
+ )
+ except Exception as exc:
+ session.context_capture_error = (
+ "context_event_persistence_failed: "
+ f"{exc}"
+ )
+ session.result_event.set()
+ return
+ logger.debug(
+ "[ClaudeSessionPool] stdout non-JSON "
+ "memory_node=%s bytes=%d",
+ memory_node_id,
+ len(raw),
+ )
+ else:
+ logger.debug(
+ "[ClaudeSessionPool] stdout non-JSON line "
+ "memory_node=%s line=%r",
+ memory_node_id,
+ line[:200],
+ )
continue
try:
await self._handle_stream_event(session, event)
@@ -721,12 +823,33 @@ class ClaudeSessionPool:
return
line = raw.decode("utf-8", errors="replace").rstrip()
if line:
- logger.warning(
- "[ClaudeSessionPool] claude stderr " "memory_node=%s pid=%s: %s",
- memory_node_id,
- process.pid,
- line,
- )
+ if session.context_event_sink is not None:
+ try:
+ await session.context_event_sink(
+ {"type": "stderr", "raw": line}
+ )
+ except Exception as exc:
+ session.context_capture_error = (
+ "context_event_persistence_failed: "
+ f"{exc}"
+ )
+ session.result_event.set()
+ return
+ logger.warning(
+ "[ClaudeSessionPool] claude stderr "
+ "memory_node=%s pid=%s bytes=%d",
+ memory_node_id,
+ process.pid,
+ len(raw),
+ )
+ else:
+ logger.warning(
+ "[ClaudeSessionPool] claude stderr "
+ "memory_node=%s pid=%s: %s",
+ memory_node_id,
+ process.pid,
+ line,
+ )
except (asyncio.CancelledError, ConnectionError):
return
except Exception as exc: # pragma: no cover — defensive
@@ -769,13 +892,36 @@ class ClaudeSessionPool:
:class:`CompactionService` so the local-threshold path
doesn't double-fire on claude's native auto-compaction.
"""
+ # Context receives the unmodified stream object before the
+ # presentation result is reduced/truncated.
+ if session.context_event_sink is not None:
+ try:
+ await session.context_event_sink(dict(event))
+ except Exception as exc: # noqa: BLE001 - provider stays observable
+ session.context_capture_error = (
+ f"context_event_persistence_failed: {exc}"
+ )
+ logger.error(
+ "[ClaudeSessionPool] Context raw-event append failed "
+ "key=%s: %s",
+ session.memory_node_id,
+ exc,
+ exc_info=True,
+ )
+ session.result_event.set()
+ return
+
sid = event.get("session_id") or event.get("sessionId")
if sid and not session.current_session_uuid:
session.current_session_uuid = sid
session.events_this_turn.append(event)
if self._provider.is_final_event(event):
session.result_event.set()
- if event.get("type") == "system" and event.get("subtype") == "compact_boundary":
+ if (
+ session.context_event_sink is None
+ and event.get("type") == "system"
+ and event.get("subtype") == "compact_boundary"
+ ):
await self._record_native_compaction(session, event)
@staticmethod
@@ -860,7 +1006,7 @@ class ClaudeSessionPool:
async def _terminate_locked(
self,
- memory_node_id: str,
+ memory_node_id: ClaudePoolKey,
*,
reason: str = "explicit",
) -> None:
@@ -974,7 +1120,7 @@ class ClaudeSessionPool:
self,
kind: Literal["spawned", "cleared", "terminated", "usage"],
*,
- memory_node_id: str,
+ memory_node_id: ClaudePoolKey,
workflow_id: Optional[str] = None,
**payload: Any,
) -> None:
@@ -991,31 +1137,53 @@ class ClaudeSessionPool:
except Exception: # pragma: no cover
return
try:
+ context_scoped = isinstance(memory_node_id, tuple)
+ wire_node_id = (
+ memory_node_id[0]
+ if context_scoped
+ else memory_node_id
+ )
if kind == "spawned":
await broadcaster.broadcast_claude_session_spawned(
- memory_node_id,
- session_uuid=payload["session_uuid"],
+ wire_node_id,
+ session_uuid=(
+ "" if context_scoped else payload["session_uuid"]
+ ),
pid=payload["pid"],
workflow_id=workflow_id,
)
elif kind == "cleared":
await broadcaster.broadcast_claude_session_cleared(
- memory_node_id,
- old_session_uuid=payload["old_session_uuid"],
- new_session_uuid=payload["new_session_uuid"],
+ wire_node_id,
+ old_session_uuid=(
+ ""
+ if context_scoped
+ else payload["old_session_uuid"]
+ ),
+ new_session_uuid=(
+ ""
+ if context_scoped
+ else payload["new_session_uuid"]
+ ),
workflow_id=workflow_id,
)
elif kind == "terminated":
await broadcaster.broadcast_claude_session_terminated(
- memory_node_id,
+ wire_node_id,
reason=payload["reason"],
- session_uuid=payload.get("session_uuid"),
+ session_uuid=(
+ None
+ if context_scoped
+ else payload.get("session_uuid")
+ ),
workflow_id=workflow_id,
)
elif kind == "usage":
await broadcaster.broadcast_claude_session_usage(
- memory_node_id,
- session_uuid=payload["session_uuid"],
+ wire_node_id,
+ session_uuid=(
+ "" if context_scoped else payload["session_uuid"]
+ ),
total_cost_usd=payload.get("total_cost_usd"),
input_tokens=payload.get("input_tokens", 0),
output_tokens=payload.get("output_tokens", 0),
diff --git a/server/nodes/agent/codex_agent/__init__.py b/server/nodes/agent/codex_agent/__init__.py
index 0806c351..ad5bac5d 100644
--- a/server/nodes/agent/codex_agent/__init__.py
+++ b/server/nodes/agent/codex_agent/__init__.py
@@ -17,7 +17,13 @@ from typing import Any, List, Optional
from pydantic import BaseModel, ConfigDict, Field
from core.logging import get_logger
-from services.plugin import ActionNode, NodeContext, Operation, TaskQueue
+from services.plugin import (
+ ActionNode,
+ NodeContext,
+ Operation,
+ RetryPolicy,
+ TaskQueue,
+)
from services.plugin.edge_walker import collect_agent_connections
from .._handles import STD_AGENT_HINTS, std_agent_handles
@@ -82,10 +88,12 @@ class CodexAgentNode(ActionNode):
group = ("agent",)
description = "Run N parallel OpenAI Codex CLI sessions. Sandbox enforced " "by Codex itself; per-task git worktree isolation."
component_kind = "agent"
+ requires_context = True
handles = std_agent_handles()
ui_hints = STD_AGENT_HINTS
annotations = {"destructive": True, "readonly": False, "open_world": True}
task_queue = TaskQueue.AI_HEAVY
+ retry_policy = RetryPolicy(maximum_attempts=1)
Params = CodexAgentParams
Output = CodexAgentOutput
@@ -101,7 +109,7 @@ class CodexAgentNode(ActionNode):
) -> Any:
from services.cli_agent.service import get_ai_cli_service
from services.cli_agent.types import session_result_to_model
- from services.plugin.deps import get_database
+ from services.plugin.deps import get_ai_service, get_database
from services.status_broadcaster import get_status_broadcaster
start_time = time.time()
@@ -141,7 +149,7 @@ class CodexAgentNode(ActionNode):
tasks[i] = t.model_copy(update=changed)
database = get_database()
- _, skill_data, tool_data, _, _ = await collect_agent_connections(
+ context_data, skill_data, tool_data, _, _ = await collect_agent_connections(
node_id,
ctx.raw,
database,
@@ -169,9 +177,15 @@ class CodexAgentNode(ActionNode):
connected_skill_names=connected_skills,
connected_skill_descriptors=skill_data,
connected_tools=tool_data,
+ connected_context=(
+ context_data
+ if (context_data or {}).get("kind") == "context"
+ else None
+ ),
execution_id=ctx.execution_id,
allowed_credentials=params.allowed_credentials,
max_parallel=params.max_parallel,
+ ai_service=get_ai_service(),
)
elapsed = time.time() - start_time
@@ -196,7 +210,15 @@ class CodexAgentNode(ActionNode):
workflow_id=workflow_id,
)
- task_models = [session_result_to_model(t).model_dump() for t in result.tasks]
+ task_models = [
+ session_result_to_model(t).model_dump() for t in result.tasks
+ ]
+ if (context_data or {}).get("kind") == "context":
+ for task_model in task_models:
+ # Codex Context fidelity is observable-only/non-resumable.
+ # A provider-emitted identifier must not look like a
+ # continuation contract in ordinary node output.
+ task_model["session_id"] = None
legacy_response = result.tasks[0].response if len(result.tasks) == 1 else None
diff --git a/server/nodes/agent/rlm_agent/__init__.py b/server/nodes/agent/rlm_agent/__init__.py
index dc021fb7..6a08b8b1 100644
--- a/server/nodes/agent/rlm_agent/__init__.py
+++ b/server/nodes/agent/rlm_agent/__init__.py
@@ -10,7 +10,13 @@ from __future__ import annotations
from typing import Any
from core.logging import get_logger
-from services.plugin import ActionNode, NodeContext, Operation, TaskQueue
+from services.plugin import (
+ ActionNode,
+ NodeContext,
+ Operation,
+ RetryPolicy,
+ TaskQueue,
+)
from services.plugin.edge_walker import (
collect_agent_connections,
format_task_context,
@@ -29,11 +35,13 @@ class RLMAgentNode(ActionNode):
group = ("agent",)
description = "Recursive Language Model agent (REPL-based)"
component_kind = "agent"
+ requires_context = True
tool_description = "ONE-SHOT delegation to RLM Agent. Call ONCE per task, returns task_id. Uses recursive REPL-based reasoning with code execution - do NOT re-call."
handles = std_agent_handles()
ui_hints = STD_AGENT_HINTS
annotations = {"destructive": True, "readonly": False, "open_world": True}
task_queue = TaskQueue.AI_HEAVY
+ retry_policy = RetryPolicy(maximum_attempts=1)
Params = SpecializedAgentParams
Output = SpecializedAgentOutput
@@ -50,12 +58,18 @@ class RLMAgentNode(ActionNode):
payload = params.model_dump()
# 1. Edge-walk for memory / skill / tool / input / task connections.
- memory_data, skill_data, tool_data, input_data, task_data = await collect_agent_connections(
+ context_data, skill_data, tool_data, input_data, task_data = await collect_agent_connections(
node_id,
ctx.raw,
database,
log_prefix="[RLM Agent]",
)
+ context_v2 = (
+ context_data
+ if (context_data or {}).get("kind") == "context"
+ else None
+ )
+ memory_data = context_data if context_data and not context_v2 else None
# 2. Inject task-completion context into the prompt.
if task_data:
@@ -87,12 +101,14 @@ class RLMAgentNode(ActionNode):
node_id,
payload,
memory_data=memory_data,
+ context_data=context_v2,
skill_data=skill_data if skill_data else None,
tool_data=tool_data if tool_data else None,
broadcaster=get_status_broadcaster(),
workflow_id=workflow_id,
context=ctx.raw,
database=database,
+ ai_service=ai_service,
)
if response.get("success") is False:
raise RuntimeError(response.get("error") or "rlm_agent execution failed")
diff --git a/server/nodes/agent/vertex_managed_agent/__init__.py b/server/nodes/agent/vertex_managed_agent/__init__.py
index 96c40707..9cd238b0 100644
--- a/server/nodes/agent/vertex_managed_agent/__init__.py
+++ b/server/nodes/agent/vertex_managed_agent/__init__.py
@@ -9,9 +9,9 @@ Interactions API and bridges the OpenCompany canvas into it:
``execute_tool`` path (so tool nodes glow exactly like they do for
the local aiAgent loop), then answered via ``function_result`` inputs
on a chained follow-up create.
-- Conversation + sandbox continuity rides the connected simpleMemory
- node: ``vertex_interaction_id`` / ``vertex_environment_id`` are stored
- in its params and the turn is appended to ``memory_content``.
+- Conversation + sandbox continuity lives in the backend Context store as an
+ opaque interaction/environment provider binding. Immutable V1 generations
+ retain their recorded Simple Memory parameter bridge.
- Cloud-side tool usage (sandbox commands, google_search, ...) is
surfaced LIVE as dynamic ``vertexCloudTool`` canvas nodes via the
workflow-ops protocol (agentBuilder pattern) — see ``_ops.py``: each
@@ -30,7 +30,14 @@ from typing import Any, Dict, List, Literal, Optional, Tuple
from pydantic import BaseModel, ConfigDict, Field
from core.logging import get_logger
-from services.plugin import ActionNode, NodeContext, NodeUserError, Operation, TaskQueue
+from services.plugin import (
+ ActionNode,
+ NodeContext,
+ NodeUserError,
+ Operation,
+ RetryPolicy,
+ TaskQueue,
+)
from services.plugin.edge_walker import collect_agent_connections
from services.plugin.scaling import AI_START_TO_CLOSE
@@ -181,11 +188,13 @@ class VertexManagedAgentNode(ActionNode):
"function calling and shows cloud tool usage on the canvas."
)
component_kind = "agent"
+ requires_context = True
tool_name = "delegate_to_vertex_managed_agent"
handles = std_agent_handles()
ui_hints = STD_AGENT_HINTS
annotations = {"destructive": False, "readonly": False, "open_world": True}
task_queue = TaskQueue.AI_HEAVY
+ retry_policy = RetryPolicy(maximum_attempts=1)
# 30m: managed-agent sandbox turns (provision + code execution +
# multi-round tool bridging) routinely exceed the 10m action default.
start_to_close_timeout = AI_START_TO_CLOSE
@@ -224,9 +233,28 @@ class VertexManagedAgentNode(ActionNode):
await phase("initializing", message="Starting Vertex managed agent...")
- memory_data, _, tool_data, input_data, _ = await collect_agent_connections(
+ context_data, _, tool_data, input_data, _ = await collect_agent_connections(
node_id, ctx.raw, database, log_prefix="[Vertex Agent]"
)
+ from services.cli_agent.context_bridge import (
+ SpecializedAgentContextBridge,
+ is_context,
+ )
+
+ context_bridge: Optional[SpecializedAgentContextBridge] = None
+ memory_data = context_data
+ if is_context(context_data):
+ memory_data = None
+ context_bridge = await SpecializedAgentContextBridge.resolve(
+ database,
+ context_data,
+ provider="vertex",
+ fidelity="provider_bound",
+ resumable=True,
+ operation_prefix=(
+ f"vertex-context:{ctx.execution_id or 'run'}:{node_id}"
+ ),
+ )
prompt = params.prompt or self._prompt_from_input(input_data)
if not prompt:
@@ -234,6 +262,8 @@ class VertexManagedAgentNode(ActionNode):
"vertex_managed_agent: provide a prompt or connect an "
"input node that produced a message."
)
+ if context_bridge is not None:
+ prompt = context_bridge.augment_prompt(prompt)
api_key = resolve_api_key_from_context(ctx.raw)
client = build_genai_client(api_key, params.project_id, params.location)
@@ -245,9 +275,9 @@ class VertexManagedAgentNode(ActionNode):
# ---- live visibility state (shared with the post-turn sweep)
live_nodes: Dict[str, str] = {} # cloud_tool_key -> canvas node id
open_calls: Dict[str, Dict[str, Any]] = {} # step id -> {node_id, label, arguments}
- on_event = None
+ visibility_handler = None
if params.visualize_cloud_tools:
- on_event = self._make_live_handler(
+ visibility_handler = self._make_live_handler(
broadcaster=broadcaster,
workflow_id=workflow_id,
agent_node_id=node_id,
@@ -256,6 +286,20 @@ class VertexManagedAgentNode(ActionNode):
open_calls=open_calls,
)
+ on_event = visibility_handler
+ if context_bridge is not None:
+ async def context_on_event(event: Any) -> None:
+ # Persist the complete provider event before the UI-oriented
+ # live handler shapes/pulses it.
+ await context_bridge.append_observable(
+ "provider.stream",
+ self._jsonable(event),
+ )
+ if visibility_handler is not None:
+ await visibility_handler(event)
+
+ on_event = context_on_event
+
# ---- memory bridge: interaction/environment chain ids
prev_interaction_id: Optional[str] = None
environment: Any = "remote"
@@ -265,6 +309,15 @@ class VertexManagedAgentNode(ActionNode):
mem_params = await database.get_node_parameters(memory_node_id) or {}
prev_interaction_id = mem_params.get("vertex_interaction_id") or None
environment = mem_params.get("vertex_environment_id") or "remote"
+ elif context_bridge is not None:
+ await phase("loading_context")
+ binding = await context_bridge.load_binding(
+ "interaction_environment"
+ )
+ prev_interaction_id = (
+ str((binding or {}).get("interaction_id") or "") or None
+ )
+ environment = (binding or {}).get("environment_id") or "remote"
create_kwargs: Dict[str, Any] = {
"agent": params.agent,
@@ -297,11 +350,40 @@ class VertexManagedAgentNode(ActionNode):
create_kwargs["tools"] = declared_tools
async def run_turn(**kw: Any) -> Any:
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "provider.request",
+ {
+ "agent": params.agent,
+ "request": kw,
+ "has_system_instruction": bool(system_instruction),
+ "declared_tools": declared_tools,
+ },
+ )
try:
- return await stream_interaction(
+ result = await stream_interaction(
client, on_event=on_event, **create_kwargs, **kw
)
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "provider.response",
+ self._jsonable(result),
+ )
+ return result
except Exception as exc: # noqa: BLE001 — mapped below
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "provider.ambiguous_outcome",
+ {
+ "error_type": type(exc).__name__,
+ "error": str(exc),
+ },
+ )
+ raise NodeUserError(
+ "Vertex managed agent interaction failed "
+ f"({type(exc).__name__}); inspect the authorized "
+ "Context journal for provider details."
+ ) from exc
raise_as_user_error(exc, what="Vertex managed agent interaction")
# ---- turn 1 (with one stale-chain retry)
@@ -332,7 +414,11 @@ class VertexManagedAgentNode(ActionNode):
logger.warning(
"[Vertex Agent] unresumable interaction chain for %s (%s) — retrying fresh",
node_id,
- str(cause).replace("\n", " ")[:120],
+ (
+ type(cause).__name__
+ if context_bridge is not None
+ else str(cause).replace("\n", " ")[:120]
+ ),
)
if memory_node_id:
await self._save_chain_ids(
@@ -347,6 +433,16 @@ class VertexManagedAgentNode(ActionNode):
else None
),
)
+ elif context_bridge is not None:
+ await context_bridge.bind_provider(
+ "interaction_environment",
+ {
+ "interaction_id": None,
+ "environment_id": None,
+ "reset_reason": "unresumable_chain",
+ },
+ operation_suffix="binding-reset",
+ )
prev_interaction_id = None
environment = "remote"
interaction = await run_turn(input=prompt, environment="remote")
@@ -377,11 +473,15 @@ class VertexManagedAgentNode(ActionNode):
function_results = []
for call in results:
await phase("executing_tool", tool_name=call["name"])
- function_results.append(
- await self._execute_bridged_call(
- call, tool_configs, ctx, delegation_wait_seconds=wait
- )
+ function_result = await self._execute_bridged_call(
+ call, tool_configs, ctx, delegation_wait_seconds=wait
)
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "tool.result",
+ function_result,
+ )
+ function_results.append(function_result)
turn += 1
await broadcaster.broadcast_agent_progress(
node_id, workflow_id=workflow_id, iteration=turn, max_iterations=params.max_turns
@@ -458,6 +558,29 @@ class VertexManagedAgentNode(ActionNode):
else None
),
)
+ elif context_bridge is not None:
+ await phase("saving_context")
+ if status in _RESUMABLE_STATUSES:
+ await context_bridge.bind_provider(
+ "interaction_environment",
+ {
+ "interaction_id": getattr(interaction, "id", None),
+ "environment_id": environment_id,
+ "status": status,
+ },
+ operation_suffix="interaction-binding",
+ )
+ await context_bridge.append_observable(
+ "provider.final",
+ {
+ "status": status,
+ "turns": turn,
+ "response": response_text,
+ "usage": usage,
+ "warnings": run_warnings,
+ },
+ operation_suffix="final",
+ )
# ---- usage bookkeeping (tokens billed by Google; recorded for stats)
try:
@@ -498,8 +621,16 @@ class VertexManagedAgentNode(ActionNode):
return {
"response": response_text,
- "interaction_id": getattr(interaction, "id", None),
- "environment_id": environment_id,
+ # Provider bindings are fetched through the authorized Context
+ # API in V2; they never enter ordinary node outputs.
+ "interaction_id": (
+ getattr(interaction, "id", None)
+ if context_bridge is None
+ else None
+ ),
+ "environment_id": (
+ environment_id if context_bridge is None else None
+ ),
"status": status,
"agent": params.agent,
"provider": "gemini",
@@ -563,6 +694,7 @@ class VertexManagedAgentNode(ActionNode):
from constants import AI_AGENT_TYPES
from services.plugin.deps import get_ai_service
from services.plugin.tool import inline_schema_refs
+ from services.tool_identity import ensure_unique_tool_names
ai_service = get_ai_service()
declared: List[Dict[str, Any]] = []
@@ -582,22 +714,26 @@ class VertexManagedAgentNode(ActionNode):
schema = inline_schema_refs(structured.args_schema.model_json_schema())
else:
schema = {"type": "object", "properties": {}}
- # Deterministic de-dup: first node keeps the base name, later
- # nodes (edge order) get _2, _3, ... — declared list and
- # dispatch configs stay in sync. Dispatch routes by the
- # config's node_type/node_id, so a suffixed name is safe.
name = structured.name
if name in configs:
- n = 2
- while f"{name}_{n}" in configs:
- n += 1
- logger.warning(
- "[Vertex Agent] duplicate tool name %r (node %s) — declared as %r",
- name,
- (config or {}).get("node_id"),
- f"{name}_{n}",
+ # The provider-visible identity is part of the canonical tool
+ # contract. Never invent Vertex-only suffixes: that would hide
+ # an ambiguous topology and make dispatch differ from native,
+ # Temporal, CLI, and RLM agents.
+ ensure_unique_tool_names(
+ [
+ {
+ "name": name,
+ "node_id": configs[name].get("node_id") or "",
+ "label": configs[name].get("label") or name,
+ },
+ {
+ "name": name,
+ "node_id": (config or {}).get("node_id") or "",
+ "label": (config or {}).get("label") or name,
+ },
+ ]
)
- name = f"{name}_{n}"
declared.append(
{
"type": "function",
diff --git a/server/nodes/context/__init__.py b/server/nodes/context/__init__.py
new file mode 100644
index 00000000..f3558d38
--- /dev/null
+++ b/server/nodes/context/__init__.py
@@ -0,0 +1,219 @@
+"""Visible, system-managed Agent Context companion node.
+
+Only replay policy is workflow configuration. Journals, checkpoints,
+provider bindings, epochs, and payload references live exclusively in
+``AgentContextStore`` and never enter node parameters or normal outputs.
+"""
+
+from __future__ import annotations
+
+from typing import Literal, Optional
+
+from pydantic import BaseModel, ConfigDict, Field
+
+from services.plugin import ActionNode, NodeContext, Operation, TaskQueue
+
+
+class AgentContextParams(BaseModel):
+ compaction_mode: Literal[
+ "auto",
+ "native",
+ "portable",
+ "disabled",
+ ] = Field(
+ default="auto",
+ title="Compaction Mode",
+ description=(
+ "Prefer verified provider-native compaction in auto mode, "
+ "falling back to a portable structured checkpoint."
+ ),
+ )
+ trigger_ratio: float = Field(
+ default=0.8,
+ gt=0,
+ lt=1,
+ title="Context Pressure Trigger",
+ description=(
+ "Compact when retained next-request input plus output headroom "
+ "reaches this fraction of the context window."
+ ),
+ )
+ context_window_override: Optional[int] = Field(
+ default=None,
+ ge=1024,
+ title="Context Window Override",
+ description=(
+ "Optional model context-window override. Empty uses provider "
+ "capability discovery."
+ ),
+ )
+ exact_tail_retention_count: int = Field(
+ default=8,
+ ge=1,
+ le=1000,
+ title="Exact Tail Retention",
+ description=(
+ "Minimum number of recent exact transitions retained after a "
+ "checkpoint."
+ ),
+ )
+
+ model_config = ConfigDict(extra="forbid")
+
+
+class AgentContextOutput(BaseModel):
+ """Metadata-only execution result; no transcript or provider state."""
+
+ configured: bool = True
+ compaction_mode: str
+ trigger_ratio: float
+ exact_tail_retention_count: int
+
+ model_config = ConfigDict(extra="forbid")
+
+
+class AgentContextNode(ActionNode):
+ type = "context"
+ version = 2
+ display_name = "Context"
+ subtitle = "Exact Agent State"
+ group = ("memory",)
+ description = (
+ "System-managed exact agent journal, replay checkpoints, and "
+ "context-pressure policy"
+ )
+ component_kind = "model"
+ handles = (
+ {
+ "name": "output-context",
+ "kind": "output",
+ "position": "top",
+ "label": "Context",
+ "role": "context",
+ },
+ )
+ hide_input_handle = True
+ hide_output_handle = False
+ ui_hints = {
+ "isContextPanel": True,
+ "systemManaged": True,
+ "hideInputSection": True,
+ "hideOutputSection": True,
+ "hideRunButton": True,
+ }
+ annotations = {
+ "destructive": False,
+ "readonly": True,
+ "open_world": False,
+ }
+ task_queue = TaskQueue.DEFAULT
+
+ Params = AgentContextParams
+ Output = AgentContextOutput
+
+ @Operation("policy")
+ async def policy(
+ self,
+ ctx: NodeContext,
+ params: AgentContextParams,
+ ) -> AgentContextOutput:
+ """Return public policy only; durable state stays in the service."""
+
+ del ctx
+ return AgentContextOutput(
+ compaction_mode=params.compaction_mode,
+ trigger_ratio=params.trigger_ratio,
+ exact_tail_retention_count=params.exact_tail_retention_count,
+ )
+
+ @classmethod
+ async def reset_execution_state(
+ cls,
+ *,
+ node_id: str,
+ workflow_id: str,
+ execution_id: str,
+ generation: int,
+ graph: dict,
+ database,
+ ) -> dict:
+ """Rotate every active thread and fence late generation writes."""
+
+ del graph
+ if generation <= 0:
+ return {
+ "reset": False,
+ "rotated_threads": 0,
+ }
+ from services.agent_context import AgentContextStore
+ from services.agent_context.lifecycle import (
+ fence_context_provider_resources,
+ )
+
+ from ._events import dispatch_context_epoch_started
+
+ store = AgentContextStore(database)
+ threads = store.iter_threads(
+ workflow_id=str(workflow_id),
+ context_node_id=str(node_id),
+ generation=generation,
+ include_archived=False,
+ )
+ rotated = 0
+ async for thread in threads:
+ new_ref = await store.start_epoch(
+ thread.ref,
+ operation_id=(
+ f"workflow-reset:{execution_id}:{node_id}:"
+ f"{thread.ref.thread_id}:epoch-{thread.ref.epoch}"
+ ),
+ provider=thread.provider,
+ )
+ await fence_context_provider_resources(
+ context_node_id=new_ref.context_node_id,
+ thread_id=new_ref.thread_id,
+ keep_epoch=new_ref.epoch,
+ )
+ rotated += 1
+ try:
+ await dispatch_context_epoch_started(
+ workflow_id=new_ref.workflow_id,
+ context_node_id=new_ref.context_node_id,
+ thread_id=new_ref.thread_id,
+ epoch=new_ref.epoch,
+ revision=new_ref.revision,
+ provider=thread.provider,
+ reason="workflow_reset",
+ )
+ except Exception:
+ # Lifecycle state is authoritative; event delivery is
+ # metadata-only and may be retried by observers.
+ pass
+ return {
+ "reset": True,
+ "rotated_threads": rotated,
+ }
+
+
+__all__ = [
+ "AgentContextNode",
+ "AgentContextOutput",
+ "AgentContextParams",
+]
+
+
+# Context panel commands are plugin-owned side channels. The node class above
+# stays passive; all durable logic lives in AgentContextStore.
+from services.plugin.edge_walker import ( # noqa: E402
+ register_agent_context_builder,
+)
+from services.ws_handler_registry import register_ws_handlers # noqa: E402
+
+from ._descriptor import build_agent_context_descriptor # noqa: E402
+from ._handlers import WS_HANDLERS # noqa: E402
+
+register_ws_handlers(WS_HANDLERS)
+# The framework walks `input-context` edges but knows nothing about this
+# node's parameters or thread-selection rules; it calls whatever is
+# registered here.
+register_agent_context_builder(build_agent_context_descriptor)
diff --git a/server/nodes/context/_descriptor.py b/server/nodes/context/_descriptor.py
new file mode 100644
index 00000000..d28c0b2f
--- /dev/null
+++ b/server/nodes/context/_descriptor.py
@@ -0,0 +1,87 @@
+"""Context descriptor construction.
+
+The ``context`` node is a declarative UI and policy surface onto an agent's
+context scope — it never owns the journal itself (RFC-0002 section 3). What
+it *does* own is the shape of the descriptor handed to the agent runtime:
+which thread the agent resolves, and the policy the operator configured on
+this node.
+
+That shape lives here rather than in ``services/plugin/edge_walker.py`` so
+the framework carries no knowledge of this plugin's parameters or its
+thread-selection rules. Registered from the package ``__init__`` via
+``services.plugin.edge_walker.register_agent_context_builder``.
+"""
+
+from __future__ import annotations
+
+from typing import Any, Dict, Optional
+
+# Session ids that carry no durable identity — an agent addressed through
+# any of these is not resuming a named conversation.
+_ANONYMOUS_SESSION_IDS = {"", "default", "internal"}
+
+
+def _resolve_thread_session_id(
+ context: Dict[str, Any],
+ delegated_task_id: Optional[str],
+) -> Optional[str]:
+ """Pick the session that identifies this agent's context thread.
+
+ An explicit chat/session id always wins. Otherwise a delegated task is
+ the durable isolation boundary — delegation helpers carry an internal
+ parent session that must NOT be inherited, or every subagent would
+ share its parent's thread.
+ """
+ explicit_session_id = context.get("explicit_session_id")
+ if explicit_session_id:
+ return explicit_session_id
+ if delegated_task_id:
+ return None
+ raw_session_id = context.get("session_id")
+ if str(raw_session_id or "").strip().lower() in _ANONYMOUS_SESSION_IDS:
+ return None
+ return raw_session_id
+
+
+async def build_agent_context_descriptor(
+ source_node_id: str,
+ context: Dict[str, Any],
+ database: Any,
+) -> Optional[Dict[str, Any]]:
+ """Describe the Context scope an agent should run against.
+
+ Returns ``None`` when this connection contributes no context, which the
+ edge walker treats as "skip this edge".
+ """
+ generation = int(
+ context.get("generation") or context.get("workflow_generation") or 0
+ )
+ # Generation zero is an immutable migration/import artifact, not a live
+ # Context namespace. One-off and manual executions stay stateless until
+ # Start admits a real workflow generation.
+ if generation <= 0:
+ return None
+
+ policy = await database.get_node_parameters(source_node_id) or {}
+ delegated_task_id = (
+ context.get("delegated_task_id")
+ or context.get("parent_task_id")
+ or context.get("team_task_id")
+ or context.get("task_id")
+ )
+
+ return {
+ "kind": "context",
+ "node_id": source_node_id,
+ "context_node_id": source_node_id,
+ "workflow_id": str(context.get("workflow_id") or ""),
+ "generation": generation,
+ "user_id": context.get("user_id", "owner"),
+ "execution_id": context.get("execution_id"),
+ "session_id": _resolve_thread_session_id(context, delegated_task_id),
+ "delegated_task_id": delegated_task_id,
+ "policy": policy,
+ }
+
+
+__all__ = ["build_agent_context_descriptor"]
diff --git a/server/nodes/context/_events.py b/server/nodes/context/_events.py
new file mode 100644
index 00000000..a4f72682
--- /dev/null
+++ b/server/nodes/context/_events.py
@@ -0,0 +1,129 @@
+"""Metadata-only Context lifecycle events."""
+
+from __future__ import annotations
+
+from typing import Optional
+
+from services.events.envelope import WorkflowEvent
+
+
+def context_updated(
+ *,
+ workflow_id: str,
+ context_node_id: str,
+ thread_id: str,
+ epoch: int,
+ revision: int,
+ provider: Optional[str],
+ active_token_count: int,
+) -> WorkflowEvent:
+ return WorkflowEvent(
+ source="opencompany://nodes/context",
+ type="com.opencompany.context.updated",
+ subject=context_node_id,
+ workflow_id=workflow_id,
+ data={
+ "workflow_id": workflow_id,
+ "context_node_id": context_node_id,
+ "thread_id": thread_id,
+ "epoch": epoch,
+ "revision": revision,
+ "provider": provider,
+ "active_token_count": max(0, active_token_count),
+ },
+ )
+
+
+def context_compacted(
+ *,
+ workflow_id: str,
+ context_node_id: str,
+ thread_id: str,
+ epoch: int,
+ revision: int,
+ provider: Optional[str],
+ strategy: str,
+ covers_through_sequence: int,
+ active_token_count: int,
+) -> WorkflowEvent:
+ return WorkflowEvent(
+ source="opencompany://nodes/context",
+ type="com.opencompany.context.compacted",
+ subject=context_node_id,
+ workflow_id=workflow_id,
+ data={
+ "workflow_id": workflow_id,
+ "context_node_id": context_node_id,
+ "thread_id": thread_id,
+ "epoch": epoch,
+ "revision": revision,
+ "provider": provider,
+ "strategy": strategy,
+ "covers_through_sequence": covers_through_sequence,
+ "active_token_count": max(0, active_token_count),
+ },
+ )
+
+
+def context_epoch_started(
+ *,
+ workflow_id: str,
+ context_node_id: str,
+ thread_id: str,
+ epoch: int,
+ revision: int,
+ provider: Optional[str],
+ reason: str,
+) -> WorkflowEvent:
+ return WorkflowEvent(
+ source="opencompany://nodes/context",
+ type="com.opencompany.context.epoch.started",
+ subject=context_node_id,
+ workflow_id=workflow_id,
+ data={
+ "workflow_id": workflow_id,
+ "context_node_id": context_node_id,
+ "thread_id": thread_id,
+ "epoch": epoch,
+ "revision": revision,
+ "provider": provider,
+ "reason": reason,
+ },
+ )
+
+
+async def dispatch_context_epoch_started(**metadata) -> None:
+ from services.events.dispatch import emit
+
+ await emit(
+ context_epoch_started(**metadata),
+ wire_routing_key="context.epoch.started",
+ )
+
+
+async def dispatch_context_updated(**metadata) -> None:
+ from services.events.dispatch import emit
+
+ await emit(
+ context_updated(**metadata),
+ wire_routing_key="context.updated",
+ )
+
+
+async def dispatch_context_compacted(**metadata) -> None:
+ from services.events.dispatch import emit
+
+ await emit(
+ context_compacted(**metadata),
+ wire_routing_key="context.compacted",
+ )
+
+
+__all__ = [
+ "context_compacted",
+ "context_epoch_started",
+ "context_updated",
+ "dispatch_context_compacted",
+ "dispatch_context_epoch_started",
+ "dispatch_context_updated",
+]
diff --git a/server/nodes/context/_handlers.py b/server/nodes/context/_handlers.py
new file mode 100644
index 00000000..9d05f6b0
--- /dev/null
+++ b/server/nodes/context/_handlers.py
@@ -0,0 +1,657 @@
+"""Authorized WebSocket handlers for the Context inspector.
+
+Raw journal/replay data is returned only from ``get_agent_context`` after the
+saved workflow is verified to own the requested Context node. Lifecycle
+broadcasts contain metadata only. Exports are deliberately redacted: replay
+payloads, provider bindings, signatures, and message bodies never leave via
+the export path.
+"""
+
+from __future__ import annotations
+
+import base64
+import json
+import re
+import uuid
+from typing import Any, Awaitable, Callable, Dict, Optional
+
+from fastapi import WebSocket
+
+from models.agent_context import (
+ AgentContextEvent,
+ AgentContextThreadSummary,
+)
+from services.agent_context import (
+ AgentContextStore,
+ OpaqueCheckpointError,
+ reconstruct_message_wire_v2,
+)
+from services.agent_context.lifecycle import fence_context_provider_resources
+from services.plugin import NodeUserError
+from services.plugin.ws import ws_response
+
+from ._events import dispatch_context_epoch_started
+
+
+_DEFAULT_PAGE_SIZE = 50
+_MAX_PAGE_SIZE = 200
+
+
+def _database():
+ from core.container import container
+
+ return container.database()
+
+
+def _store() -> AgentContextStore:
+ return AgentContextStore(_database())
+
+
+def _require_external_socket(websocket: WebSocket) -> None:
+ """The internal unauthenticated worker socket may not inspect journals."""
+
+ scope = getattr(websocket, "scope", {}) or {}
+ if scope.get("path") == "/ws/internal":
+ raise NodeUserError("Context inspection requires an authenticated client")
+
+
+def _authenticated_owner(websocket: WebSocket) -> str:
+ state = getattr(websocket, "state", None)
+ value = getattr(state, "user_id", None) if state is not None else None
+ if isinstance(value, (str, int)) and str(value).strip():
+ return str(value)
+ scope = getattr(websocket, "scope", {}) or {}
+ value = scope.get("user_id") if isinstance(scope, dict) else None
+ if isinstance(value, (str, int)) and str(value).strip():
+ return str(value)
+ return "owner"
+
+
+async def _authorize_context_node(
+ *,
+ websocket: WebSocket,
+ workflow_id: str,
+ context_node_id: str,
+) -> Any:
+ _require_external_socket(websocket)
+ if not workflow_id or not context_node_id:
+ raise NodeUserError("workflow_id and context_node_id are required")
+ workflow = await _database().get_workflow(workflow_id)
+ if workflow is None:
+ raise NodeUserError("Workflow not found")
+ graph = workflow.data if isinstance(workflow.data, dict) else {}
+ stored_owner = str(graph.get("owner_id") or "")
+ if stored_owner and stored_owner != _authenticated_owner(websocket):
+ raise NodeUserError("Workflow access denied")
+ owned = any(
+ isinstance(node, dict)
+ and str(node.get("id") or "") == context_node_id
+ and str(node.get("type") or "") == "context"
+ for node in graph.get("nodes", [])
+ )
+ if not owned:
+ raise NodeUserError(
+ "Context node does not belong to the requested workflow"
+ )
+ return workflow
+
+
+def _optional_generation(data: Dict[str, Any]) -> Optional[int]:
+ value = data.get("generation")
+ if value in (None, ""):
+ return None
+ try:
+ generation = int(value)
+ except (TypeError, ValueError) as exc:
+ raise NodeUserError("generation must be an integer") from exc
+ if generation < 0:
+ raise NodeUserError("generation must be non-negative")
+ return generation
+
+
+async def _select_thread(
+ store: AgentContextStore,
+ data: Dict[str, Any],
+) -> Optional[AgentContextThreadSummary]:
+ generation = _optional_generation(data)
+ threads = await store.list_threads(
+ workflow_id=str(data["workflow_id"]),
+ context_node_id=str(data["context_node_id"]),
+ generation=generation,
+ include_archived=False,
+ )
+ requested_thread = str(data.get("thread_id") or "")
+ selected = next(
+ (
+ thread
+ for thread in threads
+ if not requested_thread
+ or thread.ref.thread_id == requested_thread
+ ),
+ None,
+ )
+ if requested_thread and selected is None:
+ raise NodeUserError("Context thread not found")
+ requested_epoch = data.get("epoch")
+ if selected is not None and requested_epoch not in (None, ""):
+ try:
+ epoch = int(requested_epoch)
+ except (TypeError, ValueError) as exc:
+ raise NodeUserError("epoch must be an integer") from exc
+ if epoch != selected.ref.epoch:
+ raise NodeUserError("Requested Context epoch is no longer active")
+ return selected
+
+
+def _encode_cursor(sequence: Optional[int]) -> Optional[str]:
+ if sequence is None:
+ return None
+ return base64.urlsafe_b64encode(str(sequence).encode()).decode().rstrip("=")
+
+
+def _decode_cursor(value: Any) -> int:
+ if value in (None, ""):
+ return 0
+ if not isinstance(value, str) or len(value) > 32:
+ raise NodeUserError("Invalid Context cursor")
+ try:
+ padded = value + "=" * (-len(value) % 4)
+ sequence = int(base64.urlsafe_b64decode(padded).decode())
+ except (ValueError, UnicodeDecodeError) as exc:
+ raise NodeUserError("Invalid Context cursor") from exc
+ if sequence < 0:
+ raise NodeUserError("Invalid Context cursor")
+ return sequence
+
+
+async def _event_view(
+ store: AgentContextStore,
+ event: AgentContextEvent,
+ *,
+ hydrate_payload: bool,
+) -> dict[str, Any]:
+ value = event.model_dump(mode="json", exclude_none=True)
+ if (
+ hydrate_payload
+ and event.payload_ref
+ and event.payload_ref.startswith("sha256:")
+ ):
+ try:
+ value["payload"] = await store.get_blob(event.payload_ref)
+ except Exception:
+ # External/missing payload references stay visible as references;
+ # a journal page must not fail because an attachment was retired.
+ value["payload_unavailable"] = True
+ return value
+
+
+def _fidelity(
+ provider: Optional[str],
+ binding_fidelity: Optional[str],
+) -> tuple[str, bool]:
+ if binding_fidelity:
+ return binding_fidelity, binding_fidelity != "observable_only"
+ normalized = (provider or "").lower()
+ if "codex" in normalized or normalized == "rlm":
+ return "observable_only", False
+ if "claude_code" in normalized or "vertex" in normalized:
+ return "provider_bound", True
+ if normalized:
+ return "provider_replayable", True
+ return "unknown", False
+
+
+async def _snapshot(
+ store: AgentContextStore,
+ thread: AgentContextThreadSummary,
+ *,
+ threads: list[AgentContextThreadSummary],
+ view: str,
+ cursor: Any = None,
+ limit: int = _DEFAULT_PAGE_SIZE,
+) -> dict[str, Any]:
+ ref = thread.ref
+ active = await store.load_active(ref)
+ checkpoints = await store.list_checkpoints(ref)
+ bindings = await store.load_provider_bindings(
+ ref,
+ provider=thread.provider,
+ )
+ fidelity, resumable = _fidelity(
+ thread.provider,
+ bindings[-1].fidelity if bindings else None,
+ )
+ params = await _database().get_node_parameters(ref.context_node_id) or {}
+ context_window = params.get("context_window_override")
+ try:
+ context_window = int(context_window) if context_window else None
+ except (TypeError, ValueError):
+ context_window = None
+ base: dict[str, Any] = {
+ "threads": [
+ {
+ "thread_id": item.ref.thread_id,
+ "generation": item.ref.generation,
+ "epoch": item.ref.epoch,
+ "revision": item.ref.revision,
+ "thread_kind": item.thread_kind,
+ "provider": item.provider,
+ "status": item.status,
+ "active_token_count": item.active_token_count,
+ "updated_at": item.updated_at.isoformat(),
+ }
+ for item in threads
+ ],
+ "thread_id": ref.thread_id,
+ "epoch": ref.epoch,
+ "revision": active.ref.revision,
+ "provider": thread.provider,
+ "fidelity": fidelity,
+ "resumable": resumable,
+ "active_token_count": active.active_token_count,
+ "context_window": context_window,
+ "pressure_ratio": (
+ active.active_token_count / context_window
+ if context_window
+ else None
+ ),
+ "provider_binding_status": "bound" if bindings else "unbound",
+ "checkpoints": [
+ checkpoint.model_dump(mode="json") for checkpoint in checkpoints
+ ],
+ }
+ if view == "active":
+ checkpoint_payload: Any = None
+ if active.checkpoint is not None:
+ try:
+ checkpoint_payload = await store.get_blob(
+ active.checkpoint.replay_payload_ref
+ )
+ except Exception:
+ checkpoint_payload = {"unavailable": True}
+ base["active_replay"] = {
+ "checkpoint": (
+ {
+ "metadata": active.checkpoint.model_dump(mode="json"),
+ "payload": checkpoint_payload,
+ }
+ if active.checkpoint is not None
+ else None
+ ),
+ "tail": [
+ await _event_view(store, event, hydrate_payload=True)
+ for event in active.tail
+ ],
+ }
+ base["events"] = []
+ base["next_cursor"] = None
+ return base
+
+ events, next_after = await store.load_journal_page(
+ active.ref,
+ after_sequence=_decode_cursor(cursor),
+ limit=limit,
+ )
+ base["events"] = [
+ await _event_view(store, event, hydrate_payload=True)
+ for event in events
+ ]
+ base["active_replay"] = None
+ base["next_cursor"] = _encode_cursor(next_after)
+ return base
+
+
+def _empty_snapshot() -> dict[str, Any]:
+ return {
+ "threads": [],
+ "thread_id": None,
+ "epoch": None,
+ "revision": None,
+ "provider": None,
+ "fidelity": "unknown",
+ "resumable": False,
+ "active_token_count": 0,
+ "context_window": None,
+ "pressure_ratio": None,
+ "provider_binding_status": "unbound",
+ "checkpoints": [],
+ "events": [],
+ "active_replay": None,
+ "next_cursor": None,
+ }
+
+
+@ws_response
+async def handle_get_agent_context(
+ data: Dict[str, Any],
+ websocket: WebSocket,
+) -> Dict[str, Any]:
+ await _authorize_context_node(
+ websocket=websocket,
+ workflow_id=str(data.get("workflow_id") or ""),
+ context_node_id=str(data.get("context_node_id") or ""),
+ )
+ store = _store()
+ generation = _optional_generation(data)
+ threads = await store.list_threads(
+ workflow_id=str(data["workflow_id"]),
+ context_node_id=str(data["context_node_id"]),
+ generation=generation,
+ include_archived=False,
+ )
+ requested_thread = str(data.get("thread_id") or "")
+ thread = next(
+ (
+ item
+ for item in threads
+ if not requested_thread or item.ref.thread_id == requested_thread
+ ),
+ None,
+ )
+ if requested_thread and thread is None:
+ raise NodeUserError("Context thread not found")
+ requested_epoch = data.get("epoch")
+ if thread is not None and requested_epoch not in (None, ""):
+ try:
+ epoch = int(requested_epoch)
+ except (TypeError, ValueError) as exc:
+ raise NodeUserError("epoch must be an integer") from exc
+ if epoch != thread.ref.epoch:
+ raise NodeUserError("Requested Context epoch is no longer active")
+ if thread is None:
+ return {"success": True, "context": _empty_snapshot()}
+ view = str(data.get("view") or "journal")
+ if view not in {"journal", "active"}:
+ raise NodeUserError("view must be journal or active")
+ try:
+ limit = int(data.get("limit") or _DEFAULT_PAGE_SIZE)
+ except (TypeError, ValueError) as exc:
+ raise NodeUserError("limit must be an integer") from exc
+ limit = max(1, min(limit, _MAX_PAGE_SIZE))
+ return {
+ "success": True,
+ "context": await _snapshot(
+ store,
+ thread,
+ threads=threads,
+ view=view,
+ cursor=data.get("cursor"),
+ limit=limit,
+ ),
+ }
+
+
+@ws_response
+async def handle_clear_agent_context(
+ data: Dict[str, Any],
+ websocket: WebSocket,
+) -> Dict[str, Any]:
+ await _authorize_context_node(
+ websocket=websocket,
+ workflow_id=str(data.get("workflow_id") or ""),
+ context_node_id=str(data.get("context_node_id") or ""),
+ )
+ store = _store()
+ thread = await _select_thread(store, data)
+ if thread is None:
+ return {"success": True, "context": _empty_snapshot()}
+ operation_id = str(
+ data.get("operation_id")
+ or f"context-ui-clear:{data.get('request_id') or uuid.uuid4()}"
+ )
+ new_ref = await store.start_epoch(
+ thread.ref,
+ operation_id=operation_id,
+ provider=thread.provider,
+ )
+ await fence_context_provider_resources(
+ context_node_id=new_ref.context_node_id,
+ thread_id=new_ref.thread_id,
+ keep_epoch=new_ref.epoch,
+ )
+ await dispatch_context_epoch_started(
+ workflow_id=new_ref.workflow_id,
+ context_node_id=new_ref.context_node_id,
+ thread_id=new_ref.thread_id,
+ epoch=new_ref.epoch,
+ revision=new_ref.revision,
+ provider=thread.provider,
+ reason="clear",
+ )
+ return {
+ "success": True,
+ "context": {
+ "thread_id": new_ref.thread_id,
+ "epoch": new_ref.epoch,
+ "revision": new_ref.revision,
+ },
+ }
+
+
+async def _portable_handoff(
+ store: AgentContextStore,
+ thread: AgentContextThreadSummary,
+) -> str:
+ active = await store.load_active(thread.ref)
+ portable = True
+ try:
+ _, messages = await reconstruct_message_wire_v2(
+ store,
+ active.ref,
+ )
+ except OpaqueCheckpointError:
+ portable = False
+ # Historical opaque checkpoints cannot be translated. Preserve the
+ # exact observable tail and clearly mark the handoff as partial.
+ messages = [
+ event.message_wire_v2
+ for event in active.tail
+ if event.message_wire_v2 is not None
+ ]
+ return await store.put_blob(
+ {
+ "format": "agent-context-portable-handoff-v1",
+ "fidelity": (
+ "portable" if portable else "observable_only"
+ ),
+ "messages": messages,
+ "source": {
+ "workflow_id": thread.ref.workflow_id,
+ "context_node_id": thread.ref.context_node_id,
+ "generation": thread.ref.generation,
+ "thread_id": thread.ref.thread_id,
+ "epoch": thread.ref.epoch,
+ "revision": active.ref.revision,
+ "provider": thread.provider,
+ },
+ # Metadata is safe inside hash-addressed Context storage; it is
+ # not returned by normal graph/status/export paths.
+ "source_checkpoint": (
+ {
+ "provider": active.checkpoint.provider,
+ "strategy": active.checkpoint.strategy,
+ "covers_through_sequence": (
+ active.checkpoint.covers_through_sequence
+ ),
+ "source_hash": active.checkpoint.source_hash,
+ }
+ if active.checkpoint is not None
+ else None
+ ),
+ }
+ )
+
+
+@ws_response
+async def handle_fork_agent_context(
+ data: Dict[str, Any],
+ websocket: WebSocket,
+) -> Dict[str, Any]:
+ await _authorize_context_node(
+ websocket=websocket,
+ workflow_id=str(data.get("workflow_id") or ""),
+ context_node_id=str(data.get("context_node_id") or ""),
+ )
+ store = _store()
+ thread = await _select_thread(store, data)
+ if thread is None:
+ return {"success": True, "context": _empty_snapshot()}
+ # The provider binding is backend-owned (RFC-0002 section 3: the canvas
+ # never owns provider bindings). Deriving it from the thread rather than
+ # the request also stops a stale client snapshot from forking the epoch
+ # onto a provider the thread already moved off.
+ provider = str(thread.provider or "portable")
+ handoff_ref = await _portable_handoff(store, thread)
+ operation_id = str(
+ data.get("operation_id")
+ or f"context-ui-fork:{data.get('request_id') or uuid.uuid4()}"
+ )
+ new_ref = await store.fork_provider(
+ thread.ref,
+ provider=provider,
+ operation_id=operation_id,
+ portable_handoff_ref=handoff_ref,
+ )
+ await dispatch_context_epoch_started(
+ workflow_id=new_ref.workflow_id,
+ context_node_id=new_ref.context_node_id,
+ thread_id=new_ref.thread_id,
+ epoch=new_ref.epoch,
+ revision=new_ref.revision,
+ provider=provider,
+ reason="fork",
+ )
+ return {
+ "success": True,
+ "context": {
+ "thread_id": new_ref.thread_id,
+ "epoch": new_ref.epoch,
+ "revision": new_ref.revision,
+ "provider": provider,
+ },
+ }
+
+
+def _redacted_event(event: AgentContextEvent) -> dict[str, Any]:
+ return {
+ "sequence": event.sequence,
+ "event_type": event.event_type,
+ "provider": event.provider,
+ "previous_hash": event.previous_hash,
+ "payload_hash": event.payload_hash,
+ "redacted": True,
+ }
+
+
+async def _redacted_export(
+ store: AgentContextStore,
+ thread: AgentContextThreadSummary,
+) -> dict[str, Any]:
+ events: list[AgentContextEvent] = []
+ after = 0
+ while True:
+ page, next_after = await store.load_journal_page(
+ thread.ref,
+ after_sequence=after,
+ limit=_MAX_PAGE_SIZE,
+ )
+ events.extend(page)
+ if next_after is None:
+ break
+ after = next_after
+ checkpoints = await store.list_checkpoints(thread.ref, limit=100)
+ return {
+ "schema": "opencompany.agent-context.redacted.v1",
+ "redacted": True,
+ "context": {
+ "workflow_id": thread.ref.workflow_id,
+ "context_node_id": thread.ref.context_node_id,
+ "generation": thread.ref.generation,
+ "thread_id": thread.ref.thread_id,
+ "epoch": thread.ref.epoch,
+ "revision": thread.ref.revision,
+ "provider": thread.provider,
+ "status": thread.status,
+ "active_token_count": thread.active_token_count,
+ },
+ "checkpoints": [
+ {
+ "provider": checkpoint.provider,
+ "strategy": checkpoint.strategy,
+ "covers_through_sequence": (
+ checkpoint.covers_through_sequence
+ ),
+ "active_token_count": checkpoint.active_token_count,
+ "source_revision": checkpoint.source_revision,
+ "source_hash": checkpoint.source_hash,
+ "replay_payload_redacted": True,
+ }
+ for checkpoint in checkpoints
+ ],
+ "events": [_redacted_event(event) for event in events],
+ "provider_bindings_redacted": True,
+ }
+
+
+@ws_response
+async def handle_export_agent_context(
+ data: Dict[str, Any],
+ websocket: WebSocket,
+) -> Dict[str, Any]:
+ await _authorize_context_node(
+ websocket=websocket,
+ workflow_id=str(data.get("workflow_id") or ""),
+ context_node_id=str(data.get("context_node_id") or ""),
+ )
+ store = _store()
+ thread = await _select_thread(store, data)
+ export = (
+ await _redacted_export(store, thread)
+ if thread is not None
+ else {
+ "schema": "opencompany.agent-context.redacted.v1",
+ "redacted": True,
+ "context": None,
+ "checkpoints": [],
+ "events": [],
+ "provider_bindings_redacted": True,
+ }
+ )
+ safe_id = re.sub(
+ r"[^A-Za-z0-9._-]+",
+ "-",
+ str(data["context_node_id"]),
+ ).strip("-")
+ return {
+ "success": True,
+ "filename": f"context-{safe_id or 'export'}-redacted.json",
+ "content": json.dumps(
+ export,
+ ensure_ascii=False,
+ indent=2,
+ sort_keys=True,
+ ),
+ "export": export,
+ }
+
+
+WSHandler = Callable[
+ [Dict[str, Any], WebSocket],
+ Awaitable[Dict[str, Any]],
+]
+
+WS_HANDLERS: Dict[str, WSHandler] = {
+ "get_agent_context": handle_get_agent_context,
+ "clear_agent_context": handle_clear_agent_context,
+ "fork_agent_context": handle_fork_agent_context,
+ "export_agent_context": handle_export_agent_context,
+}
+
+
+__all__ = [
+ "WS_HANDLERS",
+ "handle_clear_agent_context",
+ "handle_export_agent_context",
+ "handle_fork_agent_context",
+ "handle_get_agent_context",
+]
diff --git a/server/nodes/context/icon.svg b/server/nodes/context/icon.svg
new file mode 100644
index 00000000..389c846f
--- /dev/null
+++ b/server/nodes/context/icon.svg
@@ -0,0 +1,5 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-label="Agent Context">
+ <rect x="3" y="3" width="18" height="18" rx="5" fill="#6272a4"/>
+ <path d="M7 8.25h10M7 12h7M7 15.75h4" fill="none" stroke="#fff" stroke-linecap="round" stroke-width="1.75"/>
+ <circle cx="17" cy="15.75" r="2.1" fill="#8be9fd"/>
+</svg>
diff --git a/server/nodes/context/meta.json b/server/nodes/context/meta.json
new file mode 100644
index 00000000..442f6fbb
--- /dev/null
+++ b/server/nodes/context/meta.json
@@ -0,0 +1,3 @@
+{
+ "color": "#6272a4"
+}
diff --git a/server/nodes/scheduler/cron_scheduler/_workflow.py b/server/nodes/scheduler/cron_scheduler/_workflow.py
index 0dd39e01..bad3f446 100644
--- a/server/nodes/scheduler/cron_scheduler/_workflow.py
+++ b/server/nodes/scheduler/cron_scheduler/_workflow.py
@@ -29,7 +29,6 @@ Refs:
from __future__ import annotations
-from datetime import timedelta
from typing import Any, Dict
from temporalio import workflow
@@ -37,17 +36,6 @@ from temporalio.common import WorkflowIDReusePolicy
from temporalio.workflow import ParentClosePolicy
-CHILD_SEARCH_ATTRIBUTES_PATCH = "cron-child-search-attributes-v1"
-COOPERATIVE_PAUSE_SCHEDULING_PATCH = (
- "cron-cooperative-pause-scheduling-v1"
-)
-# Same contract as trigger_listener_workflow.UNBOUNDED_CHILD_RUNS_PATCH:
-# a spawned run may execute or stay paused for months, so new firings
-# start children without lifetime caps; the patch keeps replay
-# compatibility for histories recorded with the old 1h caps.
-UNBOUNDED_CHILD_RUNS_PATCH = "cron-unbounded-child-runs-v1"
-
-
@workflow.defn(name="CronTriggerWorkflow", sandboxed=False)
class CronTriggerWorkflow:
"""One-shot per-firing workflow that spawns a child MachinaWorkflow.
@@ -134,41 +122,26 @@ class CronTriggerWorkflow:
firing_iso = trigger_output["timestamp"]
child_id = f"{workflow_slug}-{trigger_label}-{firing_iso}"
- try:
- use_child_search_attributes = workflow.patched(
- CHILD_SEARCH_ATTRIBUTES_PATCH
- )
- use_cooperative_pause_schedule_gate = workflow.patched(
- COOPERATIVE_PAUSE_SCHEDULING_PATCH
- )
- unbounded_child_runs = workflow.patched(UNBOUNDED_CHILD_RUNS_PATCH)
- except RuntimeError: # direct unit invocation outside Temporal runtime
- use_child_search_attributes = False
- use_cooperative_pause_schedule_gate = False
- unbounded_child_runs = True
from services.temporal.trigger_listener_workflow import (
event_workflow_search_attributes,
)
+ # A spawned run may execute — or stay cooperatively paused — for
+ # months, and Temporal's timeout timer keeps ticking through a
+ # pause, so children start without lifetime caps. Liveness is
+ # enforced at the activity layer via heartbeats.
child_options = {
"id": child_id,
"parent_close_policy": ParentClosePolicy.ABANDON,
"id_reuse_policy": (
WorkflowIDReusePolicy.ALLOW_DUPLICATE_FAILED_ONLY
),
+ "search_attributes": event_workflow_search_attributes(
+ deployment_workflow_id
+ ),
}
- if not unbounded_child_runs:
- # Replay-only: pre-patch histories recorded 1h caps on the
- # child-start command. See UNBOUNDED_CHILD_RUNS_PATCH.
- child_options["execution_timeout"] = timedelta(hours=1)
- child_options["run_timeout"] = timedelta(hours=1)
- if use_child_search_attributes:
- child_options["search_attributes"] = (
- event_workflow_search_attributes(deployment_workflow_id)
- )
-
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+
+ await self._wait_until_resumed()
await workflow.start_child_workflow(
"MachinaWorkflow",
args=[
@@ -219,7 +192,5 @@ def _build_trigger_output(listener_data: Dict[str, Any]) -> Dict[str, Any]:
__all__ = [
- "CHILD_SEARCH_ATTRIBUTES_PATCH",
- "COOPERATIVE_PAUSE_SCHEDULING_PATCH",
"CronTriggerWorkflow",
]
diff --git a/server/nodes/skill/master_skill/__init__.py b/server/nodes/skill/master_skill/__init__.py
index 0ab032cf..2a361712 100644
--- a/server/nodes/skill/master_skill/__init__.py
+++ b/server/nodes/skill/master_skill/__init__.py
@@ -44,7 +44,7 @@ class MasterSkillNode(ActionNode):
group = ("tool",)
description = "Aggregate multiple skills with enable/disable toggles"
component_kind = "tool"
- handles = ({"name": "output-skill", "kind": "output", "position": "top", "label": "Skill", "role": "skill"},)
+ handles = ({"name": "output-tool", "kind": "output", "position": "top", "label": "Skill", "role": "skill"},)
ui_hints = {
"isToolPanel": True,
"isMasterSkillEditor": True,
diff --git a/server/nodes/skill/simple_memory/__init__.py b/server/nodes/skill/simple_memory/__init__.py
index 2a77fcf7..033ba69d 100644
--- a/server/nodes/skill/simple_memory/__init__.py
+++ b/server/nodes/skill/simple_memory/__init__.py
@@ -1,206 +1,24 @@
-"""Simple Memory — Wave 11.E.3 inlined.
+"""Compatibility import for the Simple Memory V2 tool plugin.
-The node exposes two distinct memory surfaces. ``memory_content`` is the
-passive, DB-backed markdown transcript connected agents read and update
-through :mod:`services.memory.runtime`; executing this node does not populate
-that transcript. The ``read`` operation separately queries the process-local
-``services.memory_store`` log and returns timestamped message dictionaries.
-That direct log is currently written by the RLM service and does not survive
-a process restart.
-
-Long-term embedding settings are passive configuration consumed by agent
-paths; they do not make the ``read`` operation query the vector store.
-
-Field shape mirrors the pre-Wave-11 nodeDefinitions (camelCase on main)
-but with snake_case names, since plugin Pydantic params are the source
-of truth post-migration. No buffer/window split, no clear_on_run --
-trimming is always windowed by ``window_size`` and the UI exposes a
-manual "Clear Memory" button instead.
+The canonical plugin now lives under :mod:`nodes.tool.simple_memory`; the old
+module path remains importable for workflow/tests/extensions that referenced
+it directly.
"""
-from __future__ import annotations
-
-from typing import Any, Literal, Optional
-
-from pydantic import BaseModel, ConfigDict, Field
-
-from core.logging import get_logger
-from services.plugin import ActionNode, NodeContext, Operation, TaskQueue
-
-logger = get_logger(__name__)
-
-
-class SimpleMemoryParams(BaseModel):
- session_id: str = Field(
- default="",
- title="Session ID (Override)",
- description=("Leave empty to auto-use connected agent ID. " "Set manually to share memory across agents."),
- json_schema_extra={"placeholder": "Auto (uses agent ID)"},
- )
- window_size: int = Field(
- default=100,
- ge=1,
- le=100,
- title="Window Size",
- description=("Number of message pairs to keep in short-term memory " "(uses global default from Settings)"),
- )
- memory_content: str = Field(
- default="# Conversation History\n\n*No messages yet.*\n",
- title="Conversation History",
- description="Recent conversation history (editable)",
- json_schema_extra={
- "rows": 15,
- "editor": "code",
- "editorLanguage": "markdown",
- },
- )
- long_term_enabled: bool = Field(
- default=False,
- title="Enable Long-Term Memory",
- description="Archive old messages to vector DB for semantic retrieval",
- )
- retrieval_count: int = Field(
- default=3,
- ge=1,
- le=10,
- title="Retrieval Count",
- description="Number of relevant memories to retrieve from long-term storage",
- json_schema_extra={"displayOptions": {"show": {"long_term_enabled": [True]}}},
- )
- embedding_provider: Literal["huggingface", "openai", "ollama"] = Field(
- default="huggingface",
- title="Embedding Provider",
- description=(
- "Provider for long-term semantic memory. HuggingFace uses the "
- "optional local-embeddings extra; OpenAI uses the stored OpenAI "
- "credential."
- ),
- json_schema_extra={
- "displayOptions": {"show": {"long_term_enabled": [True]}}
- },
- )
- embedding_model: str = Field(
- default="",
- title="Embedding Model",
- description=(
- "Provider-specific embedding model. Empty uses BAAI/"
- "bge-small-en-v1.5 for HuggingFace, text-embedding-3-small "
- "for OpenAI, or nomic-embed-text for Ollama."
- ),
- json_schema_extra={
- "displayOptions": {"show": {"long_term_enabled": [True]}}
- },
- )
- embedding_endpoint: str = Field(
- default="",
- title="Embedding Endpoint",
- description=(
- "Optional OpenAI-compatible base URL or Ollama host. "
- "Credentials are never stored here."
- ),
- json_schema_extra={
- "displayOptions": {"show": {"long_term_enabled": [True]}}
- },
- )
- last_session_id: Optional[str] = Field(
- default=None,
- title="Last Claude Session ID",
- description=(
- "Internal: the session UUID claude returned on the most "
- "recent successful run. Display-only — `claude_code_agent` "
- "no longer reads this field. Continuity is driven by "
- "`--continue` so claude tracks its own latest session per "
- "cwd. Kept for back-compat + diagnostic display; clearing "
- "the memory wipes it."
- ),
- json_schema_extra={"hidden": True},
- )
-
- model_config = ConfigDict(extra="ignore")
-
-
-class SimpleMemoryOutput(BaseModel):
- memory_content: Optional[str] = None
- message_count: Optional[int] = None
- session_id: Optional[str] = None
- messages: Optional[list[Any]] = None
- window_size: Optional[int] = None
-
- model_config = ConfigDict(extra="allow")
-
-
-class SimpleMemoryNode(ActionNode):
- type = "simpleMemory"
- display_name = "Simple Memory"
- subtitle = "Conversation History"
- group = ("tool", "memory")
- description = "Markdown-based conversation memory with optional vector DB"
- component_kind = "model"
- handles = ({"name": "output-memory", "kind": "output", "position": "top", "label": "Memory", "role": "memory"},)
- ui_hints = {"isMemoryPanel": True, "hasCodeEditor": True, "hideRunButton": True}
- annotations = {"destructive": False, "readonly": True, "open_world": False}
- task_queue = TaskQueue.DEFAULT
-
- Params = SimpleMemoryParams
- Output = SimpleMemoryOutput
-
- @classmethod
- async def reset_execution_state(
- cls, *, node_id: str, workflow_id: str, execution_id: str,
- graph: dict[str, Any], database: Any,
- ) -> dict[str, Any]:
- """Clear this plugin's mutable state after the execution is archived."""
- from services.memory import clear_agent_session_state
- from services.memory_store import clear_session as clear_direct_session
-
- params = await database.get_node_parameters(node_id) or {}
- configured = str(params.get("session_id") or "").strip()
- if configured and configured != "default":
- sessions = [configured]
- else:
- sessions = [
- str(edge.get("target"))
- for edge in graph.get("edges", [])
- if edge.get("source") == node_id
- and (edge.get("targetHandle") or edge.get("target_handle")) == "input-memory"
- and edge.get("target")
- ]
- sessions.append("default")
- sessions = list(dict.fromkeys(sessions))
- for index, session_id in enumerate(sessions):
- await clear_agent_session_state(
- session_id=session_id,
- workflow_id=workflow_id,
- clear_long_term=True,
- memory_node_id=node_id if index == 0 else None,
- )
- clear_direct_session(session_id)
- await database.clear_conversation(session_id)
- await database.reset_session_token_state(session_id)
- return {
- "reset": True,
- "parameters": await database.get_node_parameters(node_id) or {},
- "sessions": sessions,
- }
-
- @Operation("read")
- async def read(self, ctx: NodeContext, params: SimpleMemoryParams) -> SimpleMemoryOutput:
- """Return the current message log for the session.
-
- Always windowed by ``window_size`` -- no buffer/window split.
- An empty ``session_id`` resolves to ``"default"`` so callers
- that accept the auto-mode default still get a stable bucket.
- """
- from services.memory_store import get_messages
-
- session_id = params.session_id or "default"
- window_size = params.window_size
-
- messages = get_messages(session_id, window_size)
- return SimpleMemoryOutput(
- memory_content=params.memory_content,
- message_count=len(messages),
- session_id=session_id,
- messages=messages,
- window_size=window_size,
- )
+from nodes.tool.simple_memory import (
+ MemoryOperation,
+ MemoryUpdatePatch,
+ SimpleMemoryNode,
+ SimpleMemoryOutput,
+ SimpleMemoryParams,
+ SimpleMemoryToolInput,
+)
+
+__all__ = [
+ "MemoryOperation",
+ "MemoryUpdatePatch",
+ "SimpleMemoryNode",
+ "SimpleMemoryOutput",
+ "SimpleMemoryParams",
+ "SimpleMemoryToolInput",
+]
diff --git a/server/nodes/tool/agent_builder/__init__.py b/server/nodes/tool/agent_builder/__init__.py
index e0ec0762..fbf40854 100644
--- a/server/nodes/tool/agent_builder/__init__.py
+++ b/server/nodes/tool/agent_builder/__init__.py
@@ -62,6 +62,9 @@ _MASTER_SKILL_TYPE = "masterSkill"
_MASTER_SKILL_LABEL = "Master Skill"
_AGENT_BUILDER_TYPE = "agentBuilder"
_TASK_MANAGER_TYPE = "taskManager"
+_CONTEXT_TYPE = "context"
+_CONTEXT_INPUT_HANDLE = "input-context"
+_CONTEXT_OUTPUT_HANDLE = "output-context"
_TEAM_LEAD_TYPES = frozenset({"orchestrator_agent", "ai_employee"})
_DENIED_TOOL_TYPES = frozenset({_AGENT_BUILDER_TYPE, _MASTER_SKILL_TYPE, _TASK_MANAGER_TYPE})
_KEY_PARAM_FIELDS = ("provider", "model", "operation", "url", "query")
@@ -1173,8 +1176,29 @@ class AgentBuilderNode(ToolNode):
position=workflow_ops.anchored(caller_id, offset_x=300, offset_y=200),
)
add_node_op["minted_id"] = minted_id
+ context_ref = f"{client_ref}_context"
+ context_id = _mint_node_id(_CONTEXT_TYPE)
+ context_op = workflow_ops.add_node(
+ context_ref,
+ _CONTEXT_TYPE,
+ {},
+ label="Context",
+ position=workflow_ops.anchored(
+ minted_id,
+ offset_x=-360,
+ offset_y=0,
+ ),
+ )
+ context_op["minted_id"] = context_id
ops = [
add_node_op,
+ context_op,
+ workflow_ops.add_edge(
+ {"client_ref": context_ref},
+ {"client_ref": client_ref},
+ source_handle=_CONTEXT_OUTPUT_HANDLE,
+ target_handle=_CONTEXT_INPUT_HANDLE,
+ ),
workflow_ops.add_edge(
{"client_ref": client_ref},
caller_id,
diff --git a/server/nodes/tool/simple_memory/__init__.py b/server/nodes/tool/simple_memory/__init__.py
new file mode 100644
index 00000000..0ef986f7
--- /dev/null
+++ b/server/nodes/tool/simple_memory/__init__.py
@@ -0,0 +1,342 @@
+"""Simple Memory V2 — an explicit, durable AI tool.
+
+Conversation replay belongs to the companion Context node. This plugin stores
+only memories the model explicitly asks to remember and never injects,
+retrieves, or compacts agent transcripts automatically.
+"""
+
+from __future__ import annotations
+
+from datetime import datetime
+from typing import Any, Literal, Optional
+
+from pydantic import (
+ BaseModel,
+ ConfigDict,
+ Field,
+ field_validator,
+ model_validator,
+)
+
+from services.memory.tool_store import (
+ MemoryScope,
+ MemoryStoreError,
+ MemoryToolStore,
+)
+from services.plugin import (
+ NodeContext,
+ NodeUserError,
+ Operation,
+ TaskQueue,
+ ToolNode,
+)
+from services.plugin.deps import get_database
+
+MemoryOperation = Literal[
+ "remember",
+ "recall",
+ "list",
+ "get",
+ "update",
+ "forget",
+]
+
+
+class SimpleMemoryParams(BaseModel):
+ """Persisted operator configuration; never exposed as model arguments."""
+
+ reset_policy: Literal["preserve", "clear"] = Field(
+ default="preserve",
+ title="Workflow Reset",
+ description=(
+ "Preserve durable memory items across Workflow Reset, or clear "
+ "only this Memory node's isolated namespace."
+ ),
+ )
+
+ # Legacy graph imports may still carry markdown/session/vector fields.
+ # Ignoring them keeps graph loading compatible without reintroducing them
+ # into runtime behavior or the NodeSpec.
+ model_config = ConfigDict(extra="ignore")
+
+
+class MemoryUpdatePatch(BaseModel):
+ content: Optional[str] = Field(default=None, min_length=1, max_length=50_000)
+ title: Optional[str] = Field(default=None, max_length=500)
+ category: Optional[str] = Field(default=None, max_length=100)
+ tags: Optional[list[str]] = Field(default=None, max_length=20)
+ expires_at: Optional[datetime] = None
+
+ model_config = ConfigDict(extra="forbid")
+
+ @field_validator("tags")
+ @classmethod
+ def _validate_tags(cls, tags: Optional[list[str]]) -> Optional[list[str]]:
+ if tags is not None and any(len(str(tag).strip()) > 64 for tag in tags):
+ raise ValueError("tags must be at most 64 characters each")
+ return tags
+
+
+class SimpleMemoryToolInput(BaseModel):
+ """One locked, multi-operation schema visible to the LLM."""
+
+ operation: MemoryOperation = Field(
+ description=(
+ "Memory operation: remember, recall, list, get, update, or forget."
+ )
+ )
+ content: Optional[str] = Field(default=None, min_length=1, max_length=50_000)
+ title: Optional[str] = Field(default=None, max_length=500)
+ category: Optional[str] = Field(default=None, max_length=100)
+ tags: Optional[list[str]] = Field(default=None, max_length=20)
+ expires_at: Optional[datetime] = None
+ query: Optional[str] = Field(default=None, min_length=1, max_length=2_000)
+ categories: Optional[list[str]] = Field(default=None, max_length=20)
+ limit: int = Field(default=20, ge=1, le=100)
+ cursor: Optional[str] = Field(default=None, max_length=4_096)
+ memory_id: Optional[str] = Field(default=None, min_length=1, max_length=64)
+ expected_version: Optional[int] = Field(default=None, ge=1)
+ patch: Optional[MemoryUpdatePatch] = None
+
+ model_config = ConfigDict(extra="forbid")
+
+ @field_validator("tags", "categories")
+ @classmethod
+ def _validate_filters(
+ cls, values: Optional[list[str]]
+ ) -> Optional[list[str]]:
+ if values is not None and any(
+ not str(value).strip() or len(str(value).strip()) > 100
+ for value in values
+ ):
+ raise ValueError(
+ "categories/tags must be non-empty and at most 100 characters"
+ )
+ return values
+
+ @model_validator(mode="after")
+ def _validate_operation_fields(self) -> "SimpleMemoryToolInput":
+ required: dict[str, tuple[str, ...]] = {
+ "remember": ("content",),
+ "recall": ("query",),
+ "get": ("memory_id",),
+ "update": ("memory_id", "expected_version", "patch"),
+ "forget": ("memory_id", "expected_version"),
+ }
+ missing = [
+ field_name
+ for field_name in required.get(self.operation, ())
+ if getattr(self, field_name) is None
+ ]
+ if missing:
+ raise ValueError(
+ f"{self.operation} requires {', '.join(missing)}"
+ )
+ if (
+ self.operation == "update"
+ and self.patch is not None
+ and not self.patch.model_fields_set
+ ):
+ raise ValueError("update patch must contain at least one field")
+ return self
+
+
+class SimpleMemoryOutput(BaseModel):
+ operation: str
+ memory: Optional[dict[str, Any]] = None
+ items: Optional[list[dict[str, Any]]] = None
+ count: Optional[int] = None
+ next_cursor: Optional[str] = None
+ retrieval: Optional[str] = None
+ receipt: Optional[dict[str, Any]] = None
+
+ model_config = ConfigDict(extra="allow")
+
+
+class SimpleMemoryNode(ToolNode):
+ type = "simpleMemory"
+ display_name = "Simple Memory"
+ subtitle = "Explicit Durable Memory"
+ group = ("tool", "memory")
+ description = (
+ "Explicitly remember, recall, list, update, and forget durable facts"
+ )
+ component_kind = "tool"
+ tool_name = "memory"
+ tool_description = (
+ "Store and retrieve durable memories only when useful. Use remember "
+ "for stable facts or decisions; use recall/list/get to retrieve them; "
+ "use update/forget with the item's expected_version."
+ )
+ handles = (
+ {
+ "name": "output-tool",
+ "kind": "output",
+ "position": "top",
+ "label": "Memory",
+ "role": "tools",
+ },
+ )
+ ui_hints = {
+ "isToolPanel": True,
+ "isMemoryPanel": True,
+ "isMemoryToolPanel": True,
+ # Kept as a wire-compatibility hint for older clients. V2 clients
+ # select isMemoryToolPanel first; there is no markdown field to edit.
+ "hasCodeEditor": True,
+ "hideInputSection": True,
+ "hideOutputSection": True,
+ "hideRunButton": True,
+ }
+ annotations = {
+ "destructive": True,
+ "readonly": False,
+ "open_world": False,
+ }
+ task_queue = TaskQueue.DEFAULT
+
+ Params = SimpleMemoryParams
+ ToolInput = SimpleMemoryToolInput
+ Output = SimpleMemoryOutput
+ tool_schema_locked = True
+ server_controlled_fields = frozenset({"reset_policy"})
+
+ @staticmethod
+ def _scope(ctx: NodeContext) -> MemoryScope:
+ return MemoryScope(
+ owner_id=str(ctx.user_id or ctx.raw.get("user_id") or "owner"),
+ workflow_id=str(
+ ctx.workflow_id or ctx.raw.get("workflow_id") or "local"
+ ),
+ memory_node_id=str(ctx.node_id),
+ )
+
+ @staticmethod
+ def _operation_id(ctx: NodeContext) -> Optional[str]:
+ value = (
+ ctx.raw.get("operation_id")
+ or ctx.raw.get("tool_call_id")
+ or ctx.raw.get("mutation_id")
+ )
+ return str(value)[:512] if value else None
+
+ @Operation("memory")
+ async def memory(
+ self,
+ ctx: NodeContext,
+ params: SimpleMemoryToolInput | SimpleMemoryParams,
+ ) -> SimpleMemoryOutput:
+ # The node's Run button is hidden. Treat a framework-side execution as
+ # a harmless list for diagnostics instead of making it a second API.
+ args = (
+ SimpleMemoryToolInput(operation="list")
+ if isinstance(params, SimpleMemoryParams)
+ else params
+ )
+ store = MemoryToolStore(get_database())
+ scope = self._scope(ctx)
+ operation_id = self._operation_id(ctx)
+ try:
+ if args.operation == "remember":
+ result = await store.remember(
+ scope,
+ content=args.content or "",
+ title=args.title,
+ category=args.category,
+ tags=args.tags,
+ expires_at=args.expires_at,
+ operation_id=operation_id,
+ )
+ elif args.operation == "recall":
+ result = await store.recall(
+ scope,
+ query=args.query or "",
+ categories=args.categories,
+ tags=args.tags,
+ limit=args.limit,
+ cursor=args.cursor,
+ )
+ elif args.operation == "list":
+ result = await store.list(
+ scope,
+ categories=args.categories,
+ tags=args.tags,
+ limit=args.limit,
+ cursor=args.cursor,
+ )
+ elif args.operation == "get":
+ result = await store.get(scope, args.memory_id or "")
+ elif args.operation == "update":
+ assert args.patch is not None
+ result = await store.update(
+ scope,
+ memory_id=args.memory_id or "",
+ expected_version=args.expected_version or 0,
+ patch=args.patch.model_dump(exclude_unset=True),
+ operation_id=operation_id,
+ )
+ else:
+ result = await store.forget(
+ scope,
+ memory_id=args.memory_id or "",
+ expected_version=args.expected_version or 0,
+ operation_id=operation_id,
+ )
+ except MemoryStoreError as exc:
+ raise NodeUserError(str(exc)) from exc
+ return SimpleMemoryOutput.model_validate(result)
+
+ @classmethod
+ async def reset_execution_state(
+ cls,
+ *,
+ node_id: str,
+ workflow_id: str,
+ execution_id: str,
+ generation: int,
+ graph: dict[str, Any],
+ database: Any,
+ ) -> dict[str, Any]:
+ del generation
+ persisted = await database.get_node_parameters(node_id) or {}
+ config = SimpleMemoryParams.model_validate(persisted)
+ if config.reset_policy == "preserve":
+ return {
+ "reset": False,
+ "reset_policy": "preserve",
+ "preserved": True,
+ }
+ owner_id = str(graph.get("owner_id") or graph.get("user_id") or "owner")
+ scope = MemoryScope(
+ owner_id=owner_id,
+ workflow_id=str(workflow_id),
+ memory_node_id=str(node_id),
+ )
+ result = await MemoryToolStore(database).clear_namespace(
+ scope,
+ operation_id=f"workflow-reset:{execution_id}:{node_id}",
+ )
+ return {
+ "reset": True,
+ "reset_policy": "clear",
+ **result,
+ }
+
+
+# Plugin-owned side-channel API for the Memory panel.
+from services.ws_handler_registry import register_ws_handlers # noqa: E402
+
+from ._handlers import WS_HANDLERS # noqa: E402
+
+register_ws_handlers(WS_HANDLERS)
+
+
+__all__ = [
+ "WS_HANDLERS",
+ "MemoryOperation",
+ "MemoryUpdatePatch",
+ "SimpleMemoryNode",
+ "SimpleMemoryOutput",
+ "SimpleMemoryParams",
+ "SimpleMemoryToolInput",
+]
diff --git a/server/nodes/tool/simple_memory/_handlers.py b/server/nodes/tool/simple_memory/_handlers.py
new file mode 100644
index 00000000..d3c40d61
--- /dev/null
+++ b/server/nodes/tool/simple_memory/_handlers.py
@@ -0,0 +1,258 @@
+"""Human-facing WebSocket API for the Simple Memory panel.
+
+Every request resolves its namespace from the persisted workflow graph and
+the authenticated WebSocket. Neither the client nor the model can provide a
+namespace identifier. Contents are returned only on these authorized,
+paginated calls; change broadcasts elsewhere remain metadata-only.
+"""
+
+from __future__ import annotations
+
+from typing import Any, Awaitable, Callable, Dict, Optional
+
+from fastapi import WebSocket
+from pydantic import ValidationError
+
+from services.memory.tool_store import (
+ MemoryScope,
+ MemoryStoreError,
+ MemoryToolStore,
+)
+from services.plugin import NodeUserError
+from services.plugin.deps import get_database
+from services.plugin.ws import ws_response
+
+from . import SimpleMemoryToolInput
+
+
+def _authenticated_owner(websocket: WebSocket) -> str:
+ """Read server-authenticated identity without consulting request data."""
+ state = getattr(websocket, "state", None)
+ for attribute in ("user_id", "principal_id", "subject"):
+ value = getattr(state, attribute, None) if state is not None else None
+ if isinstance(value, (str, int)) and str(value).strip():
+ return str(value)
+ scope = getattr(websocket, "scope", None)
+ if isinstance(scope, dict):
+ for key in ("user_id", "principal_id", "subject"):
+ value = scope.get(key)
+ if isinstance(value, (str, int)) and str(value).strip():
+ return str(value)
+ # The current deployment is single-owner when no auth principal is
+ # attached; this mirrors NodeContext.user_id's trusted default.
+ return "owner"
+
+
+async def _resolve_store_and_scope(
+ data: Dict[str, Any], websocket: WebSocket
+) -> tuple[MemoryToolStore, MemoryScope]:
+ workflow_id = str(data.get("workflow_id") or "").strip()
+ node_id = str(
+ data.get("memory_node_id") or data.get("node_id") or ""
+ ).strip()
+ if not workflow_id:
+ raise NodeUserError("workflow_id required")
+ if not node_id:
+ raise NodeUserError("memory_node_id required")
+
+ database = get_database()
+ saved = await database.get_workflow(workflow_id)
+ if saved is None:
+ raise NodeUserError("Workflow not found")
+ graph = saved.data if hasattr(saved, "data") else saved.get("data", saved)
+ owner_id = _authenticated_owner(websocket)
+ stored_owner = (
+ str(graph.get("owner_id") or "")
+ if isinstance(graph, dict)
+ else ""
+ )
+ if stored_owner and stored_owner != owner_id:
+ raise NodeUserError("Workflow access denied")
+ nodes = graph.get("nodes", []) if isinstance(graph, dict) else []
+ matches = [
+ node
+ for node in nodes
+ if str(node.get("id") or "") == node_id
+ and str(node.get("type") or node.get("data", {}).get("type") or "")
+ == "simpleMemory"
+ ]
+ if len(matches) != 1:
+ raise NodeUserError(
+ "Memory node does not belong to the requested workflow"
+ )
+ return MemoryToolStore(database), MemoryScope(
+ owner_id=owner_id,
+ workflow_id=workflow_id,
+ memory_node_id=node_id,
+ )
+
+
+def _validate(operation: str, data: Dict[str, Any]) -> SimpleMemoryToolInput:
+ allowed = {
+ "content",
+ "title",
+ "category",
+ "tags",
+ "expires_at",
+ "query",
+ "categories",
+ "limit",
+ "cursor",
+ "memory_id",
+ "expected_version",
+ "patch",
+ }
+ try:
+ return SimpleMemoryToolInput.model_validate(
+ {
+ "operation": operation,
+ **{key: data[key] for key in allowed if key in data},
+ }
+ )
+ except ValidationError as exc:
+ first = exc.errors()[0] if exc.errors() else {}
+ raise NodeUserError(
+ f"Invalid Memory request: {first.get('msg', str(exc))}"
+ ) from exc
+
+
+def _operation_id(data: Dict[str, Any], operation: str) -> Optional[str]:
+ value = data.get("request_id") or data.get("operation_id")
+ return f"memory-ui:{operation}:{str(value)[:450]}" if value else None
+
+
+@ws_response
+async def handle_list_memory_items(
+ data: Dict[str, Any], websocket: WebSocket
+) -> Dict[str, Any]:
+ store, scope = await _resolve_store_and_scope(data, websocket)
+ operation = "recall" if str(data.get("query") or "").strip() else "list"
+ args = _validate(operation, data)
+ try:
+ result = (
+ await store.recall(
+ scope,
+ query=args.query or "",
+ categories=args.categories,
+ tags=args.tags,
+ limit=args.limit,
+ cursor=args.cursor,
+ )
+ if operation == "recall"
+ else await store.list(
+ scope,
+ categories=args.categories,
+ tags=args.tags,
+ limit=args.limit,
+ cursor=args.cursor,
+ )
+ )
+ except MemoryStoreError as exc:
+ raise NodeUserError(str(exc)) from exc
+ return {"success": True, **result}
+
+
+@ws_response
+async def handle_get_memory_item(
+ data: Dict[str, Any], websocket: WebSocket
+) -> Dict[str, Any]:
+ store, scope = await _resolve_store_and_scope(data, websocket)
+ args = _validate("get", data)
+ try:
+ result = await store.get(scope, args.memory_id or "")
+ except MemoryStoreError as exc:
+ raise NodeUserError(str(exc)) from exc
+ return {"success": True, **result}
+
+
+@ws_response
+async def handle_remember_memory(
+ data: Dict[str, Any], websocket: WebSocket
+) -> Dict[str, Any]:
+ store, scope = await _resolve_store_and_scope(data, websocket)
+ args = _validate("remember", data)
+ result = await store.remember(
+ scope,
+ content=args.content or "",
+ title=args.title,
+ category=args.category,
+ tags=args.tags,
+ expires_at=args.expires_at,
+ operation_id=_operation_id(data, "remember"),
+ )
+ return {"success": True, **result}
+
+
+@ws_response
+async def handle_update_memory_item(
+ data: Dict[str, Any], websocket: WebSocket
+) -> Dict[str, Any]:
+ store, scope = await _resolve_store_and_scope(data, websocket)
+ args = _validate("update", data)
+ assert args.patch is not None
+ try:
+ result = await store.update(
+ scope,
+ memory_id=args.memory_id or "",
+ expected_version=args.expected_version or 0,
+ patch=args.patch.model_dump(exclude_unset=True),
+ operation_id=_operation_id(data, "update"),
+ )
+ except MemoryStoreError as exc:
+ raise NodeUserError(str(exc)) from exc
+ return {"success": True, **result}
+
+
+@ws_response
+async def handle_forget_memory_item(
+ data: Dict[str, Any], websocket: WebSocket
+) -> Dict[str, Any]:
+ store, scope = await _resolve_store_and_scope(data, websocket)
+ args = _validate("forget", data)
+ try:
+ result = await store.forget(
+ scope,
+ memory_id=args.memory_id or "",
+ expected_version=args.expected_version or 0,
+ operation_id=_operation_id(data, "forget"),
+ )
+ except MemoryStoreError as exc:
+ raise NodeUserError(str(exc)) from exc
+ return {"success": True, **result}
+
+
+@ws_response
+async def handle_clear_memory_items(
+ data: Dict[str, Any], websocket: WebSocket
+) -> Dict[str, Any]:
+ """Human-only bulk clear. The LLM ToolInput has no clear operation."""
+ store, scope = await _resolve_store_and_scope(data, websocket)
+ result = await store.clear_namespace(
+ scope,
+ operation_id=_operation_id(data, "clear"),
+ )
+ return {"success": True, **result}
+
+
+WSHandler = Callable[
+ [Dict[str, Any], WebSocket], Awaitable[Dict[str, Any]]
+]
+WS_HANDLERS: Dict[str, WSHandler] = {
+ "list_memory_items": handle_list_memory_items,
+ "get_memory_item": handle_get_memory_item,
+ "remember_memory": handle_remember_memory,
+ "update_memory_item": handle_update_memory_item,
+ "forget_memory_item": handle_forget_memory_item,
+ "clear_memory_items": handle_clear_memory_items,
+}
+
+
+__all__ = [
+ "WS_HANDLERS",
+ "handle_clear_memory_items",
+ "handle_forget_memory_item",
+ "handle_get_memory_item",
+ "handle_list_memory_items",
+ "handle_remember_memory",
+ "handle_update_memory_item",
+]
diff --git a/server/nodes/tool/simple_memory/meta.json b/server/nodes/tool/simple_memory/meta.json
new file mode 100644
index 00000000..9604cb8d
--- /dev/null
+++ b/server/nodes/tool/simple_memory/meta.json
@@ -0,0 +1 @@
+{"color": "#8be9fd"}
diff --git a/server/package-lock.json b/server/package-lock.json
deleted file mode 100644
index 101776d1..00000000
--- a/server/package-lock.json
+++ /dev/null
@@ -1,1666 +0,0 @@
-{
- "name": "opencompany-backend",
- "version": "0.0.0",
- "lockfileVersion": 3,
- "requires": true,
- "packages": {
- "": {
- "name": "opencompany-backend",
- "version": "0.0.0",
- "dependencies": {
- "@react-three/drei": "^10.7.7",
- "@react-three/fiber": "^9.6.1",
- "clsx": "^2.1.1",
- "express": "^5.2.1",
- "morgan": "^1.11.0",
- "tailwind-merge": "^3.5.0",
- "three": "^0.184.0"
- }
- },
- "node_modules/@babel/runtime": {
- "version": "7.29.2",
- "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.2.tgz",
- "integrity": "sha512-JiDShH45zKHWyGe4ZNVRrCjBz8Nh9TMmZG1kh4QTK8hCBTWBi8Da+i7s1fJw7/lYpM4ccepSNfqzZ/QvABBi5g==",
- "license": "MIT",
- "engines": {
- "node": ">=6.9.0"
- }
- },
- "node_modules/@dimforge/rapier3d-compat": {
- "version": "0.12.0",
- "resolved": "https://registry.npmjs.org/@dimforge/rapier3d-compat/-/rapier3d-compat-0.12.0.tgz",
- "integrity": "sha512-uekIGetywIgopfD97oDL5PfeezkFpNhwlzlaEYNOA0N6ghdsOvh/HYjSMek5Q2O1PYvRSDFcqFVJl4r4ZBwOow==",
- "license": "Apache-2.0"
- },
- "node_modules/@mediapipe/tasks-vision": {
- "version": "0.10.17",
- "resolved": "https://registry.npmjs.org/@mediapipe/tasks-vision/-/tasks-vision-0.10.17.tgz",
- "integrity": "sha512-CZWV/q6TTe8ta61cZXjfnnHsfWIdFhms03M9T7Cnd5y2mdpylJM0rF1qRq+wsQVRMLz1OYPVEBU9ph2Bx8cxrg==",
- "license": "Apache-2.0"
- },
- "node_modules/@monogrid/gainmap-js": {
- "version": "3.4.0",
- "resolved": "https://registry.npmjs.org/@monogrid/gainmap-js/-/gainmap-js-3.4.0.tgz",
- "integrity": "sha512-2Z0FATFHaoYJ8b+Y4y4Hgfn3FRFwuU5zRrk+9dFWp4uGAdHGqVEdP7HP+gLA3X469KXHmfupJaUbKo1b/aDKIg==",
- "license": "MIT",
- "dependencies": {
- "promise-worker-transferable": "^1.0.4"
- },
- "peerDependencies": {
- "three": ">= 0.159.0"
- }
- },
- "node_modules/@react-three/drei": {
- "version": "10.7.7",
- "resolved": "https://registry.npmjs.org/@react-three/drei/-/drei-10.7.7.tgz",
- "integrity": "sha512-ff+J5iloR0k4tC++QtD/j9u3w5fzfgFAWDtAGQah9pF2B1YgOq/5JxqY0/aVoQG5r3xSZz0cv5tk2YuBob4xEQ==",
- "license": "MIT",
- "dependencies": {
- "@babel/runtime": "^7.26.0",
- "@mediapipe/tasks-vision": "0.10.17",
- "@monogrid/gainmap-js": "^3.0.6",
- "@use-gesture/react": "^10.3.1",
- "camera-controls": "^3.1.0",
- "cross-env": "^7.0.3",
- "detect-gpu": "^5.0.56",
- "glsl-noise": "^0.0.0",
- "hls.js": "^1.5.17",
- "maath": "^0.10.8",
- "meshline": "^3.3.1",
- "stats-gl": "^2.2.8",
- "stats.js": "^0.17.0",
- "suspend-react": "^0.1.3",
- "three-mesh-bvh": "^0.8.3",
- "three-stdlib": "^2.35.6",
- "troika-three-text": "^0.52.4",
- "tunnel-rat": "^0.1.2",
- "use-sync-external-store": "^1.4.0",
- "utility-types": "^3.11.0",
- "zustand": "^5.0.1"
- },
- "peerDependencies": {
- "@react-three/fiber": "^9.0.0",
- "react": "^19",
- "react-dom": "^19",
- "three": ">=0.159"
- },
- "peerDependenciesMeta": {
- "react-dom": {
- "optional": true
- }
- }
- },
- "node_modules/@react-three/fiber": {
- "version": "9.6.1",
- "resolved": "https://registry.npmjs.org/@react-three/fiber/-/fiber-9.6.1.tgz",
- "integrity": "sha512-zF0rsKcVYpcJwbFEnv2HkHX9cvOEgsfQo/X8lwmR2dn13S4qEQJXir9fxf5js2LQFoXqxOY7MDkOkYx2uZ4gSg==",
- "license": "MIT",
- "dependencies": {
- "@babel/runtime": "^7.17.8",
- "@types/webxr": "*",
- "base64-js": "^1.5.1",
- "buffer": "^6.0.3",
- "its-fine": "^2.0.0",
- "react-use-measure": "^2.1.7",
- "scheduler": "^0.27.0",
- "suspend-react": "^0.1.3",
- "use-sync-external-store": "^1.4.0",
- "zustand": "^5.0.3"
- },
- "peerDependencies": {
- "expo": ">=43.0",
- "expo-asset": ">=8.4",
- "expo-file-system": ">=11.0",
- "expo-gl": ">=11.0",
- "react": ">=19 <19.3",
- "react-dom": ">=19 <19.3",
- "react-native": ">=0.78",
- "three": ">=0.156"
- },
- "peerDependenciesMeta": {
- "expo": {
- "optional": true
- },
- "expo-asset": {
- "optional": true
- },
- "expo-file-system": {
- "optional": true
- },
- "expo-gl": {
- "optional": true
- },
- "react-dom": {
- "optional": true
- },
- "react-native": {
- "optional": true
- }
- }
- },
- "node_modules/@tweenjs/tween.js": {
- "version": "23.1.3",
- "resolved": "https://registry.npmjs.org/@tweenjs/tween.js/-/tween.js-23.1.3.tgz",
- "integrity": "sha512-vJmvvwFxYuGnF2axRtPYocag6Clbb5YS7kLL+SO/TeVFzHqDIWrNKYtcsPMibjDx9O+bu+psAy9NKfWklassUA==",
- "license": "MIT"
- },
- "node_modules/@types/draco3d": {
- "version": "1.4.10",
- "resolved": "https://registry.npmjs.org/@types/draco3d/-/draco3d-1.4.10.tgz",
- "integrity": "sha512-AX22jp8Y7wwaBgAixaSvkoG4M/+PlAcm3Qs4OW8yT9DM4xUpWKeFhLueTAyZF39pviAdcDdeJoACapiAceqNcw==",
- "license": "MIT"
- },
- "node_modules/@types/offscreencanvas": {
- "version": "2019.7.3",
- "resolved": "https://registry.npmjs.org/@types/offscreencanvas/-/offscreencanvas-2019.7.3.tgz",
- "integrity": "sha512-ieXiYmgSRXUDeOntE1InxjWyvEelZGP63M+cGuquuRLuIKKT1osnkXjxev9B7d1nXSug5vpunx+gNlbVxMlC9A==",
- "license": "MIT"
- },
- "node_modules/@types/react": {
- "version": "19.2.14",
- "resolved": "https://registry.npmjs.org/@types/react/-/react-19.2.14.tgz",
- "integrity": "sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w==",
- "license": "MIT",
- "peer": true,
- "dependencies": {
- "csstype": "^3.2.2"
- }
- },
- "node_modules/@types/react-reconciler": {
- "version": "0.28.9",
- "resolved": "https://registry.npmjs.org/@types/react-reconciler/-/react-reconciler-0.28.9.tgz",
- "integrity": "sha512-HHM3nxyUZ3zAylX8ZEyrDNd2XZOnQ0D5XfunJF5FLQnZbHHYq4UWvW1QfelQNXv1ICNkwYhfxjwfnqivYB6bFg==",
- "license": "MIT",
- "peerDependencies": {
- "@types/react": "*"
- }
- },
- "node_modules/@types/stats.js": {
- "version": "0.17.4",
- "resolved": "https://registry.npmjs.org/@types/stats.js/-/stats.js-0.17.4.tgz",
- "integrity": "sha512-jIBvWWShCvlBqBNIZt0KAshWpvSjhkwkEu4ZUcASoAvhmrgAUI2t1dXrjSL4xXVLB4FznPrIsX3nKXFl/Dt4vA==",
- "license": "MIT"
- },
- "node_modules/@types/three": {
- "version": "0.184.0",
- "resolved": "https://registry.npmjs.org/@types/three/-/three-0.184.0.tgz",
- "integrity": "sha512-4mY2tZAu0y0B0567w7013BBXSpsP0+Z48NJvmNo4Y/Pf76yCyz6Jw4P3tUVs10WuYNXXZ+wmHyGWpCek3amJxA==",
- "license": "MIT",
- "dependencies": {
- "@dimforge/rapier3d-compat": "~0.12.0",
- "@tweenjs/tween.js": "~23.1.3",
- "@types/stats.js": "*",
- "@types/webxr": ">=0.5.17",
- "fflate": "~0.8.2",
- "meshoptimizer": "~1.1.1"
- }
- },
- "node_modules/@types/webxr": {
- "version": "0.5.24",
- "resolved": "https://registry.npmjs.org/@types/webxr/-/webxr-0.5.24.tgz",
- "integrity": "sha512-h8fgEd/DpoS9CBrjEQXR+dIDraopAEfu4wYVNY2tEPwk60stPWhvZMf4Foo5FakuQ7HFZoa8WceaWFervK2Ovg==",
- "license": "MIT"
- },
- "node_modules/@use-gesture/core": {
- "version": "10.3.1",
- "resolved": "https://registry.npmjs.org/@use-gesture/core/-/core-10.3.1.tgz",
- "integrity": "sha512-WcINiDt8WjqBdUXye25anHiNxPc0VOrlT8F6LLkU6cycrOGUDyY/yyFmsg3k8i5OLvv25llc0QC45GhR/C8llw==",
- "license": "MIT"
- },
- "node_modules/@use-gesture/react": {
- "version": "10.3.1",
- "resolved": "https://registry.npmjs.org/@use-gesture/react/-/react-10.3.1.tgz",
- "integrity": "sha512-Yy19y6O2GJq8f7CHf7L0nxL8bf4PZCPaVOCgJrusOeFHY1LvHgYXnmnXg6N5iwAnbgbZCDjo60SiM6IPJi9C5g==",
- "license": "MIT",
- "dependencies": {
- "@use-gesture/core": "10.3.1"
- },
- "peerDependencies": {
- "react": ">= 16.8.0"
- }
- },
- "node_modules/accepts": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz",
- "integrity": "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng==",
- "license": "MIT",
- "dependencies": {
- "mime-types": "^3.0.0",
- "negotiator": "^1.0.0"
- },
- "engines": {
- "node": ">= 0.6"
- }
- },
- "node_modules/base64-js": {
- "version": "1.5.1",
- "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
- "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
- "funding": [
- {
- "type": "github",
- "url": "https://github.com/sponsors/feross"
- },
- {
- "type": "patreon",
- "url": "https://www.patreon.com/feross"
- },
- {
- "type": "consulting",
- "url": "https://feross.org/support"
- }
- ],
- "license": "MIT"
- },
- "node_modules/basic-auth": {
- "version": "2.0.1",
- "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
- "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
- "license": "MIT",
- "dependencies": {
- "safe-buffer": "5.1.2"
- },
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/bidi-js": {
- "version": "1.0.3",
- "resolved": "https://registry.npmjs.org/bidi-js/-/bidi-js-1.0.3.tgz",
- "integrity": "sha512-RKshQI1R3YQ+n9YJz2QQ147P66ELpa1FQEg20Dk8oW9t2KgLbpDLLp9aGZ7y8WHSshDknG0bknqGw5/tyCs5tw==",
- "license": "MIT",
- "dependencies": {
- "require-from-string": "^2.0.2"
- }
- },
- "node_modules/body-parser": {
- "version": "2.3.0",
- "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.3.0.tgz",
- "integrity": "sha512-2cGmJupaNgg+QUwVLAucDuWuoMZ6EX9iHDRswZ5lsNYEmwPaRknMPCLZz07yTzVq/83p4o/wzbDZbBrTvGGTIw==",
- "license": "MIT",
- "dependencies": {
- "bytes": "^3.1.2",
- "content-type": "^2.0.0",
- "debug": "^4.4.3",
- "http-errors": "^2.0.1",
- "iconv-lite": "^0.7.2",
- "on-finished": "^2.4.1",
- "qs": "^6.15.2",
- "raw-body": "^3.0.2",
- "type-is": "^2.1.0"
- },
- "engines": {
- "node": ">=18"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/body-parser/node_modules/content-type": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.0.0.tgz",
- "integrity": "sha512-j/O/d7GcZCyNl7/hwZAb606rzqkyvaDctLmckbxLzHvFBzTJHuGEdodATcP3yIRoDrLHkIATJuvzbFlp/ki2cQ==",
- "license": "MIT",
- "engines": {
- "node": ">=18"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/buffer": {
- "version": "6.0.3",
- "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz",
- "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==",
- "funding": [
- {
- "type": "github",
- "url": "https://github.com/sponsors/feross"
- },
- {
- "type": "patreon",
- "url": "https://www.patreon.com/feross"
- },
- {
- "type": "consulting",
- "url": "https://feross.org/support"
- }
- ],
- "license": "MIT",
- "dependencies": {
- "base64-js": "^1.3.1",
- "ieee754": "^1.2.1"
- }
- },
- "node_modules/bytes": {
- "version": "3.1.2",
- "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
- "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/call-bind-apply-helpers": {
- "version": "1.0.2",
- "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz",
- "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==",
- "license": "MIT",
- "dependencies": {
- "es-errors": "^1.3.0",
- "function-bind": "^1.1.2"
- },
- "engines": {
- "node": ">= 0.4"
- }
- },
- "node_modules/call-bound": {
- "version": "1.0.4",
- "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz",
- "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==",
- "license": "MIT",
- "dependencies": {
- "call-bind-apply-helpers": "^1.0.2",
- "get-intrinsic": "^1.3.0"
- },
- "engines": {
- "node": ">= 0.4"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/camera-controls": {
- "version": "3.1.2",
- "resolved": "https://registry.npmjs.org/camera-controls/-/camera-controls-3.1.2.tgz",
- "integrity": "sha512-xkxfpG2ECZ6Ww5/9+kf4mfg1VEYAoe9aDSY+IwF0UEs7qEzwy0aVRfs2grImIECs/PoBtWFrh7RXsQkwG922JA==",
- "license": "MIT",
- "engines": {
- "node": ">=22.0.0",
- "npm": ">=10.5.1"
- },
- "peerDependencies": {
- "three": ">=0.126.1"
- }
- },
- "node_modules/clsx": {
- "version": "2.1.1",
- "resolved": "https://registry.npmjs.org/clsx/-/clsx-2.1.1.tgz",
- "integrity": "sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==",
- "license": "MIT",
- "engines": {
- "node": ">=6"
- }
- },
- "node_modules/content-disposition": {
- "version": "1.1.0",
- "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-1.1.0.tgz",
- "integrity": "sha512-5jRCH9Z/+DRP7rkvY83B+yGIGX96OYdJmzngqnw2SBSxqCFPd0w2km3s5iawpGX8krnwSGmF0FW5Nhr0Hfai3g==",
- "license": "MIT",
- "engines": {
- "node": ">=18"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/content-type": {
- "version": "1.0.5",
- "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz",
- "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.6"
- }
- },
- "node_modules/cookie": {
- "version": "0.7.2",
- "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz",
- "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.6"
- }
- },
- "node_modules/cookie-signature": {
- "version": "1.2.2",
- "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.2.2.tgz",
- "integrity": "sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg==",
- "license": "MIT",
- "engines": {
- "node": ">=6.6.0"
- }
- },
- "node_modules/cross-env": {
- "version": "7.0.3",
- "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz",
- "integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==",
- "license": "MIT",
- "dependencies": {
- "cross-spawn": "^7.0.1"
- },
- "bin": {
- "cross-env": "src/bin/cross-env.js",
- "cross-env-shell": "src/bin/cross-env-shell.js"
- },
- "engines": {
- "node": ">=10.14",
- "npm": ">=6",
- "yarn": ">=1"
- }
- },
- "node_modules/cross-spawn": {
- "version": "7.0.6",
- "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
- "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==",
- "license": "MIT",
- "dependencies": {
- "path-key": "^3.1.0",
- "shebang-command": "^2.0.0",
- "which": "^2.0.1"
- },
- "engines": {
- "node": ">= 8"
- }
- },
- "node_modules/csstype": {
- "version": "3.2.3",
- "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz",
- "integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==",
- "license": "MIT",
- "peer": true
- },
- "node_modules/debug": {
- "version": "4.4.3",
- "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
- "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",
- "license": "MIT",
- "dependencies": {
- "ms": "^2.1.3"
- },
- "engines": {
- "node": ">=6.0"
- },
- "peerDependenciesMeta": {
- "supports-color": {
- "optional": true
- }
- }
- },
- "node_modules/depd": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
- "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/detect-gpu": {
- "version": "5.0.70",
- "resolved": "https://registry.npmjs.org/detect-gpu/-/detect-gpu-5.0.70.tgz",
- "integrity": "sha512-bqerEP1Ese6nt3rFkwPnGbsUF9a4q+gMmpTVVOEzoCyeCc+y7/RvJnQZJx1JwhgQI5Ntg0Kgat8Uu7XpBqnz1w==",
- "license": "MIT",
- "dependencies": {
- "webgl-constants": "^1.1.1"
- }
- },
- "node_modules/draco3d": {
- "version": "1.5.7",
- "resolved": "https://registry.npmjs.org/draco3d/-/draco3d-1.5.7.tgz",
- "integrity": "sha512-m6WCKt/erDXcw+70IJXnG7M3awwQPAsZvJGX5zY7beBqpELw6RDGkYVU0W43AFxye4pDZ5i2Lbyc/NNGqwjUVQ==",
- "license": "Apache-2.0"
- },
- "node_modules/dunder-proto": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
- "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==",
- "license": "MIT",
- "dependencies": {
- "call-bind-apply-helpers": "^1.0.1",
- "es-errors": "^1.3.0",
- "gopd": "^1.2.0"
- },
- "engines": {
- "node": ">= 0.4"
- }
- },
- "node_modules/ee-first": {
- "version": "1.1.1",
- "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
- "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==",
- "license": "MIT"
- },
- "node_modules/encodeurl": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz",
- "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/es-define-property": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
- "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.4"
- }
- },
- "node_modules/es-errors": {
- "version": "1.3.0",
- "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz",
- "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.4"
- }
- },
- "node_modules/es-object-atoms": {
- "version": "1.1.1",
- "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz",
- "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==",
- "license": "MIT",
- "dependencies": {
- "es-errors": "^1.3.0"
- },
- "engines": {
- "node": ">= 0.4"
- }
- },
- "node_modules/escape-html": {
- "version": "1.0.3",
- "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
- "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==",
- "license": "MIT"
- },
- "node_modules/etag": {
- "version": "1.8.1",
- "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
- "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.6"
- }
- },
- "node_modules/express": {
- "version": "5.2.1",
- "resolved": "https://registry.npmjs.org/express/-/express-5.2.1.tgz",
- "integrity": "sha512-hIS4idWWai69NezIdRt2xFVofaF4j+6INOpJlVOLDO8zXGpUVEVzIYk12UUi2JzjEzWL3IOAxcTubgz9Po0yXw==",
- "license": "MIT",
- "dependencies": {
- "accepts": "^2.0.0",
- "body-parser": "^2.2.1",
- "content-disposition": "^1.0.0",
- "content-type": "^1.0.5",
- "cookie": "^0.7.1",
- "cookie-signature": "^1.2.1",
- "debug": "^4.4.0",
- "depd": "^2.0.0",
- "encodeurl": "^2.0.0",
- "escape-html": "^1.0.3",
- "etag": "^1.8.1",
- "finalhandler": "^2.1.0",
- "fresh": "^2.0.0",
- "http-errors": "^2.0.0",
- "merge-descriptors": "^2.0.0",
- "mime-types": "^3.0.0",
- "on-finished": "^2.4.1",
- "once": "^1.4.0",
- "parseurl": "^1.3.3",
- "proxy-addr": "^2.0.7",
- "qs": "^6.14.0",
- "range-parser": "^1.2.1",
- "router": "^2.2.0",
- "send": "^1.1.0",
- "serve-static": "^2.2.0",
- "statuses": "^2.0.1",
- "type-is": "^2.0.1",
- "vary": "^1.1.2"
- },
- "engines": {
- "node": ">= 18"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/fflate": {
- "version": "0.8.2",
- "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz",
- "integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==",
- "license": "MIT"
- },
- "node_modules/finalhandler": {
- "version": "2.1.1",
- "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-2.1.1.tgz",
- "integrity": "sha512-S8KoZgRZN+a5rNwqTxlZZePjT/4cnm0ROV70LedRHZ0p8u9fRID0hJUZQpkKLzro8LfmC8sx23bY6tVNxv8pQA==",
- "license": "MIT",
- "dependencies": {
- "debug": "^4.4.0",
- "encodeurl": "^2.0.0",
- "escape-html": "^1.0.3",
- "on-finished": "^2.4.1",
- "parseurl": "^1.3.3",
- "statuses": "^2.0.1"
- },
- "engines": {
- "node": ">= 18.0.0"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/forwarded": {
- "version": "0.2.0",
- "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
- "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.6"
- }
- },
- "node_modules/fresh": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/fresh/-/fresh-2.0.0.tgz",
- "integrity": "sha512-Rx/WycZ60HOaqLKAi6cHRKKI7zxWbJ31MhntmtwMoaTeF7XFH9hhBp8vITaMidfljRQ6eYWCKkaTK+ykVJHP2A==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/function-bind": {
- "version": "1.1.2",
- "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
- "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
- "license": "MIT",
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/get-intrinsic": {
- "version": "1.3.0",
- "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz",
- "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==",
- "license": "MIT",
- "dependencies": {
- "call-bind-apply-helpers": "^1.0.2",
- "es-define-property": "^1.0.1",
- "es-errors": "^1.3.0",
- "es-object-atoms": "^1.1.1",
- "function-bind": "^1.1.2",
- "get-proto": "^1.0.1",
- "gopd": "^1.2.0",
- "has-symbols": "^1.1.0",
- "hasown": "^2.0.2",
- "math-intrinsics": "^1.1.0"
- },
- "engines": {
- "node": ">= 0.4"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/get-proto": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz",
- "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==",
- "license": "MIT",
- "dependencies": {
- "dunder-proto": "^1.0.1",
- "es-object-atoms": "^1.0.0"
- },
- "engines": {
- "node": ">= 0.4"
- }
- },
- "node_modules/glsl-noise": {
- "version": "0.0.0",
- "resolved": "https://registry.npmjs.org/glsl-noise/-/glsl-noise-0.0.0.tgz",
- "integrity": "sha512-b/ZCF6amfAUb7dJM/MxRs7AetQEahYzJ8PtgfrmEdtw6uyGOr+ZSGtgjFm6mfsBkxJ4d2W7kg+Nlqzqvn3Bc0w==",
- "license": "MIT"
- },
- "node_modules/gopd": {
- "version": "1.2.0",
- "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",
- "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.4"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/has-symbols": {
- "version": "1.1.0",
- "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz",
- "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.4"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/hasown": {
- "version": "2.0.2",
- "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz",
- "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==",
- "license": "MIT",
- "dependencies": {
- "function-bind": "^1.1.2"
- },
- "engines": {
- "node": ">= 0.4"
- }
- },
- "node_modules/hls.js": {
- "version": "1.6.16",
- "resolved": "https://registry.npmjs.org/hls.js/-/hls.js-1.6.16.tgz",
- "integrity": "sha512-VSIRpLfRwlAAdGL4wiTucx2ScRipo0ed1FBatWkyt832jC4CReKstga6yIhYVwGu9LOBjuX9wzmRMeQdBJtzEA==",
- "license": "Apache-2.0"
- },
- "node_modules/http-errors": {
- "version": "2.0.1",
- "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz",
- "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==",
- "license": "MIT",
- "dependencies": {
- "depd": "~2.0.0",
- "inherits": "~2.0.4",
- "setprototypeof": "~1.2.0",
- "statuses": "~2.0.2",
- "toidentifier": "~1.0.1"
- },
- "engines": {
- "node": ">= 0.8"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/iconv-lite": {
- "version": "0.7.2",
- "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.2.tgz",
- "integrity": "sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw==",
- "license": "MIT",
- "dependencies": {
- "safer-buffer": ">= 2.1.2 < 3.0.0"
- },
- "engines": {
- "node": ">=0.10.0"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/ieee754": {
- "version": "1.2.1",
- "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
- "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==",
- "funding": [
- {
- "type": "github",
- "url": "https://github.com/sponsors/feross"
- },
- {
- "type": "patreon",
- "url": "https://www.patreon.com/feross"
- },
- {
- "type": "consulting",
- "url": "https://feross.org/support"
- }
- ],
- "license": "BSD-3-Clause"
- },
- "node_modules/immediate": {
- "version": "3.0.6",
- "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz",
- "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==",
- "license": "MIT"
- },
- "node_modules/inherits": {
- "version": "2.0.4",
- "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
- "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
- "license": "ISC"
- },
- "node_modules/ipaddr.js": {
- "version": "1.9.1",
- "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
- "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.10"
- }
- },
- "node_modules/is-promise": {
- "version": "4.0.0",
- "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-4.0.0.tgz",
- "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==",
- "license": "MIT"
- },
- "node_modules/isexe": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
- "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
- "license": "ISC"
- },
- "node_modules/its-fine": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/its-fine/-/its-fine-2.0.0.tgz",
- "integrity": "sha512-KLViCmWx94zOvpLwSlsx6yOCeMhZYaxrJV87Po5k/FoZzcPSahvK5qJ7fYhS61sZi5ikmh2S3Hz55A2l3U69ng==",
- "license": "MIT",
- "dependencies": {
- "@types/react-reconciler": "^0.28.9"
- },
- "peerDependencies": {
- "react": "^19.0.0"
- }
- },
- "node_modules/lie": {
- "version": "3.3.0",
- "resolved": "https://registry.npmjs.org/lie/-/lie-3.3.0.tgz",
- "integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==",
- "license": "MIT",
- "dependencies": {
- "immediate": "~3.0.5"
- }
- },
- "node_modules/maath": {
- "version": "0.10.8",
- "resolved": "https://registry.npmjs.org/maath/-/maath-0.10.8.tgz",
- "integrity": "sha512-tRvbDF0Pgqz+9XUa4jjfgAQ8/aPKmQdWXilFu2tMy4GWj4NOsx99HlULO4IeREfbO3a0sA145DZYyvXPkybm0g==",
- "license": "MIT",
- "peerDependencies": {
- "@types/three": ">=0.134.0",
- "three": ">=0.134.0"
- }
- },
- "node_modules/math-intrinsics": {
- "version": "1.1.0",
- "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
- "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.4"
- }
- },
- "node_modules/media-typer": {
- "version": "1.1.0",
- "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-1.1.0.tgz",
- "integrity": "sha512-aisnrDP4GNe06UcKFnV5bfMNPBUw4jsLGaWwWfnH3v02GnBuXX2MCVn5RbrWo0j3pczUilYblq7fQ7Nw2t5XKw==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/merge-descriptors": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-2.0.0.tgz",
- "integrity": "sha512-Snk314V5ayFLhp3fkUREub6WtjBfPdCPY1Ln8/8munuLuiYhsABgBVWsozAG+MWMbVEvcdcpbi9R7ww22l9Q3g==",
- "license": "MIT",
- "engines": {
- "node": ">=18"
- },
- "funding": {
- "url": "https://github.com/sponsors/sindresorhus"
- }
- },
- "node_modules/meshline": {
- "version": "3.3.1",
- "resolved": "https://registry.npmjs.org/meshline/-/meshline-3.3.1.tgz",
- "integrity": "sha512-/TQj+JdZkeSUOl5Mk2J7eLcYTLiQm2IDzmlSvYm7ov15anEcDJ92GHqqazxTSreeNgfnYu24kiEvvv0WlbCdFQ==",
- "license": "MIT",
- "peerDependencies": {
- "three": ">=0.137"
- }
- },
- "node_modules/meshoptimizer": {
- "version": "1.1.1",
- "resolved": "https://registry.npmjs.org/meshoptimizer/-/meshoptimizer-1.1.1.tgz",
- "integrity": "sha512-oRFNWJRDA/WTrVj7NWvqa5HqE1t9MYDj2VaWirQCzCCrAd2GHrqR/sQezCxiWATPNlKTcRaPRHPJwIRoPBAp5g==",
- "license": "MIT"
- },
- "node_modules/mime-db": {
- "version": "1.54.0",
- "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz",
- "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.6"
- }
- },
- "node_modules/mime-types": {
- "version": "3.0.2",
- "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-3.0.2.tgz",
- "integrity": "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==",
- "license": "MIT",
- "dependencies": {
- "mime-db": "^1.54.0"
- },
- "engines": {
- "node": ">=18"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/morgan": {
- "version": "1.11.0",
- "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.11.0.tgz",
- "integrity": "sha512-zSkVu3t18r39pw4ixfBKvfZi3y2UOqr7d4WYwcj3m8nXpEQK4rPO6GLzs/CExoRgmX3y9EjmmcXqv6jq0SK46g==",
- "license": "MIT",
- "dependencies": {
- "basic-auth": "~2.0.1",
- "debug": "2.6.9",
- "depd": "~2.0.0",
- "on-finished": "~2.4.1",
- "on-headers": "~1.1.0"
- },
- "engines": {
- "node": ">= 0.8.0"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/morgan/node_modules/debug": {
- "version": "2.6.9",
- "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
- "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
- "license": "MIT",
- "dependencies": {
- "ms": "2.0.0"
- }
- },
- "node_modules/morgan/node_modules/ms": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
- "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
- "license": "MIT"
- },
- "node_modules/ms": {
- "version": "2.1.3",
- "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
- "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
- "license": "MIT"
- },
- "node_modules/negotiator": {
- "version": "1.0.0",
- "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.0.0.tgz",
- "integrity": "sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.6"
- }
- },
- "node_modules/object-inspect": {
- "version": "1.13.4",
- "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
- "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.4"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/on-finished": {
- "version": "2.4.1",
- "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz",
- "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==",
- "license": "MIT",
- "dependencies": {
- "ee-first": "1.1.1"
- },
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/on-headers": {
- "version": "1.1.0",
- "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.1.0.tgz",
- "integrity": "sha512-737ZY3yNnXy37FHkQxPzt4UZ2UWPWiCZWLvFZ4fu5cueciegX0zGPnrlY6bwRg4FdQOe9YU8MkmJwGhoMybl8A==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/once": {
- "version": "1.4.0",
- "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
- "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
- "license": "ISC",
- "dependencies": {
- "wrappy": "1"
- }
- },
- "node_modules/parseurl": {
- "version": "1.3.3",
- "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
- "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/path-key": {
- "version": "3.1.1",
- "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
- "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==",
- "license": "MIT",
- "engines": {
- "node": ">=8"
- }
- },
- "node_modules/path-to-regexp": {
- "version": "8.4.2",
- "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.4.2.tgz",
- "integrity": "sha512-qRcuIdP69NPm4qbACK+aDogI5CBDMi1jKe0ry5rSQJz8JVLsC7jV8XpiJjGRLLol3N+R5ihGYcrPLTno6pAdBA==",
- "license": "MIT",
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/potpack": {
- "version": "1.0.2",
- "resolved": "https://registry.npmjs.org/potpack/-/potpack-1.0.2.tgz",
- "integrity": "sha512-choctRBIV9EMT9WGAZHn3V7t0Z2pMQyl0EZE6pFc/6ml3ssw7Dlf/oAOvFwjm1HVsqfQN8GfeFyJ+d8tRzqueQ==",
- "license": "ISC"
- },
- "node_modules/promise-worker-transferable": {
- "version": "1.0.4",
- "resolved": "https://registry.npmjs.org/promise-worker-transferable/-/promise-worker-transferable-1.0.4.tgz",
- "integrity": "sha512-bN+0ehEnrXfxV2ZQvU2PetO0n4gqBD4ulq3MI1WOPLgr7/Mg9yRQkX5+0v1vagr74ZTsl7XtzlaYDo2EuCeYJw==",
- "license": "Apache-2.0",
- "dependencies": {
- "is-promise": "^2.1.0",
- "lie": "^3.0.2"
- }
- },
- "node_modules/promise-worker-transferable/node_modules/is-promise": {
- "version": "2.2.2",
- "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.2.2.tgz",
- "integrity": "sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==",
- "license": "MIT"
- },
- "node_modules/proxy-addr": {
- "version": "2.0.7",
- "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
- "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==",
- "license": "MIT",
- "dependencies": {
- "forwarded": "0.2.0",
- "ipaddr.js": "1.9.1"
- },
- "engines": {
- "node": ">= 0.10"
- }
- },
- "node_modules/qs": {
- "version": "6.15.2",
- "resolved": "https://registry.npmjs.org/qs/-/qs-6.15.2.tgz",
- "integrity": "sha512-Rzq0KEyX/w/tEybncDgdkZrJgVUsUMk3xjh3t5bv3S1HTAtg+uOYt72+ZfwiQwKdysThkTBdL/rTi6HDmX9Ddw==",
- "license": "BSD-3-Clause",
- "dependencies": {
- "side-channel": "^1.1.0"
- },
- "engines": {
- "node": ">=0.6"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/range-parser": {
- "version": "1.2.1",
- "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
- "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.6"
- }
- },
- "node_modules/raw-body": {
- "version": "3.0.2",
- "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.2.tgz",
- "integrity": "sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA==",
- "license": "MIT",
- "dependencies": {
- "bytes": "~3.1.2",
- "http-errors": "~2.0.1",
- "iconv-lite": "~0.7.0",
- "unpipe": "~1.0.0"
- },
- "engines": {
- "node": ">= 0.10"
- }
- },
- "node_modules/react": {
- "version": "19.2.5",
- "resolved": "https://registry.npmjs.org/react/-/react-19.2.5.tgz",
- "integrity": "sha512-llUJLzz1zTUBrskt2pwZgLq59AemifIftw4aB7JxOqf1HY2FDaGDxgwpAPVzHU1kdWabH7FauP4i1oEeer2WCA==",
- "license": "MIT",
- "peer": true,
- "engines": {
- "node": ">=0.10.0"
- }
- },
- "node_modules/react-use-measure": {
- "version": "2.1.7",
- "resolved": "https://registry.npmjs.org/react-use-measure/-/react-use-measure-2.1.7.tgz",
- "integrity": "sha512-KrvcAo13I/60HpwGO5jpW7E9DfusKyLPLvuHlUyP5zqnmAPhNc6qTRjUQrdTADl0lpPpDVU2/Gg51UlOGHXbdg==",
- "license": "MIT",
- "peerDependencies": {
- "react": ">=16.13",
- "react-dom": ">=16.13"
- },
- "peerDependenciesMeta": {
- "react-dom": {
- "optional": true
- }
- }
- },
- "node_modules/require-from-string": {
- "version": "2.0.2",
- "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
- "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==",
- "license": "MIT",
- "engines": {
- "node": ">=0.10.0"
- }
- },
- "node_modules/router": {
- "version": "2.2.0",
- "resolved": "https://registry.npmjs.org/router/-/router-2.2.0.tgz",
- "integrity": "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==",
- "license": "MIT",
- "dependencies": {
- "debug": "^4.4.0",
- "depd": "^2.0.0",
- "is-promise": "^4.0.0",
- "parseurl": "^1.3.3",
- "path-to-regexp": "^8.0.0"
- },
- "engines": {
- "node": ">= 18"
- }
- },
- "node_modules/safe-buffer": {
- "version": "5.1.2",
- "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
- "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
- "license": "MIT"
- },
- "node_modules/safer-buffer": {
- "version": "2.1.2",
- "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
- "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
- "license": "MIT"
- },
- "node_modules/scheduler": {
- "version": "0.27.0",
- "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.27.0.tgz",
- "integrity": "sha512-eNv+WrVbKu1f3vbYJT/xtiF5syA5HPIMtf9IgY/nKg0sWqzAUEvqY/xm7OcZc/qafLx/iO9FgOmeSAp4v5ti/Q==",
- "license": "MIT"
- },
- "node_modules/send": {
- "version": "1.2.1",
- "resolved": "https://registry.npmjs.org/send/-/send-1.2.1.tgz",
- "integrity": "sha512-1gnZf7DFcoIcajTjTwjwuDjzuz4PPcY2StKPlsGAQ1+YH20IRVrBaXSWmdjowTJ6u8Rc01PoYOGHXfP1mYcZNQ==",
- "license": "MIT",
- "dependencies": {
- "debug": "^4.4.3",
- "encodeurl": "^2.0.0",
- "escape-html": "^1.0.3",
- "etag": "^1.8.1",
- "fresh": "^2.0.0",
- "http-errors": "^2.0.1",
- "mime-types": "^3.0.2",
- "ms": "^2.1.3",
- "on-finished": "^2.4.1",
- "range-parser": "^1.2.1",
- "statuses": "^2.0.2"
- },
- "engines": {
- "node": ">= 18"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/serve-static": {
- "version": "2.2.1",
- "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-2.2.1.tgz",
- "integrity": "sha512-xRXBn0pPqQTVQiC8wyQrKs2MOlX24zQ0POGaj0kultvoOCstBQM5yvOhAVSUwOMjQtTvsPWoNCHfPGwaaQJhTw==",
- "license": "MIT",
- "dependencies": {
- "encodeurl": "^2.0.0",
- "escape-html": "^1.0.3",
- "parseurl": "^1.3.3",
- "send": "^1.2.0"
- },
- "engines": {
- "node": ">= 18"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/setprototypeof": {
- "version": "1.2.0",
- "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
- "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==",
- "license": "ISC"
- },
- "node_modules/shebang-command": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
- "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
- "license": "MIT",
- "dependencies": {
- "shebang-regex": "^3.0.0"
- },
- "engines": {
- "node": ">=8"
- }
- },
- "node_modules/shebang-regex": {
- "version": "3.0.0",
- "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
- "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
- "license": "MIT",
- "engines": {
- "node": ">=8"
- }
- },
- "node_modules/side-channel": {
- "version": "1.1.0",
- "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz",
- "integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==",
- "license": "MIT",
- "dependencies": {
- "es-errors": "^1.3.0",
- "object-inspect": "^1.13.3",
- "side-channel-list": "^1.0.0",
- "side-channel-map": "^1.0.1",
- "side-channel-weakmap": "^1.0.2"
- },
- "engines": {
- "node": ">= 0.4"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/side-channel-list": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.1.tgz",
- "integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==",
- "license": "MIT",
- "dependencies": {
- "es-errors": "^1.3.0",
- "object-inspect": "^1.13.4"
- },
- "engines": {
- "node": ">= 0.4"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/side-channel-map": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz",
- "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==",
- "license": "MIT",
- "dependencies": {
- "call-bound": "^1.0.2",
- "es-errors": "^1.3.0",
- "get-intrinsic": "^1.2.5",
- "object-inspect": "^1.13.3"
- },
- "engines": {
- "node": ">= 0.4"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/side-channel-weakmap": {
- "version": "1.0.2",
- "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz",
- "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==",
- "license": "MIT",
- "dependencies": {
- "call-bound": "^1.0.2",
- "es-errors": "^1.3.0",
- "get-intrinsic": "^1.2.5",
- "object-inspect": "^1.13.3",
- "side-channel-map": "^1.0.1"
- },
- "engines": {
- "node": ">= 0.4"
- },
- "funding": {
- "url": "https://github.com/sponsors/ljharb"
- }
- },
- "node_modules/stats-gl": {
- "version": "2.4.2",
- "resolved": "https://registry.npmjs.org/stats-gl/-/stats-gl-2.4.2.tgz",
- "integrity": "sha512-g5O9B0hm9CvnM36+v7SFl39T7hmAlv541tU81ME8YeSb3i1CIP5/QdDeSB3A0la0bKNHpxpwxOVRo2wFTYEosQ==",
- "license": "MIT",
- "dependencies": {
- "@types/three": "*",
- "three": "^0.170.0"
- },
- "peerDependencies": {
- "@types/three": "*",
- "three": "*"
- }
- },
- "node_modules/stats-gl/node_modules/three": {
- "version": "0.170.0",
- "resolved": "https://registry.npmjs.org/three/-/three-0.170.0.tgz",
- "integrity": "sha512-FQK+LEpYc0fBD+J8g6oSEyyNzjp+Q7Ks1C568WWaoMRLW+TkNNWmenWeGgJjV105Gd+p/2ql1ZcjYvNiPZBhuQ==",
- "license": "MIT"
- },
- "node_modules/stats.js": {
- "version": "0.17.0",
- "resolved": "https://registry.npmjs.org/stats.js/-/stats.js-0.17.0.tgz",
- "integrity": "sha512-hNKz8phvYLPEcRkeG1rsGmV5ChMjKDAWU7/OJJdDErPBNChQXxCo3WZurGpnWc6gZhAzEPFad1aVgyOANH1sMw==",
- "license": "MIT"
- },
- "node_modules/statuses": {
- "version": "2.0.2",
- "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz",
- "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/suspend-react": {
- "version": "0.1.3",
- "resolved": "https://registry.npmjs.org/suspend-react/-/suspend-react-0.1.3.tgz",
- "integrity": "sha512-aqldKgX9aZqpoDp3e8/BZ8Dm7x1pJl+qI3ZKxDN0i/IQTWUwBx/ManmlVJ3wowqbno6c2bmiIfs+Um6LbsjJyQ==",
- "license": "MIT",
- "peerDependencies": {
- "react": ">=17.0"
- }
- },
- "node_modules/tailwind-merge": {
- "version": "3.5.0",
- "resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-3.5.0.tgz",
- "integrity": "sha512-I8K9wewnVDkL1NTGoqWmVEIlUcB9gFriAEkXkfCjX5ib8ezGxtR3xD7iZIxrfArjEsH7F1CHD4RFUtxefdqV/A==",
- "license": "MIT",
- "funding": {
- "type": "github",
- "url": "https://github.com/sponsors/dcastil"
- }
- },
- "node_modules/three": {
- "version": "0.184.0",
- "resolved": "https://registry.npmjs.org/three/-/three-0.184.0.tgz",
- "integrity": "sha512-wtTRjG92pM5eUg/KuUnHsqSAlPM296brTOcLgMRqEeylYTh/CdtvKUvCyyCQTzFuStieWxvZb8mVTMvdPyUpxg==",
- "license": "MIT"
- },
- "node_modules/three-mesh-bvh": {
- "version": "0.8.3",
- "resolved": "https://registry.npmjs.org/three-mesh-bvh/-/three-mesh-bvh-0.8.3.tgz",
- "integrity": "sha512-4G5lBaF+g2auKX3P0yqx+MJC6oVt6sB5k+CchS6Ob0qvH0YIhuUk1eYr7ktsIpY+albCqE80/FVQGV190PmiAg==",
- "license": "MIT",
- "peerDependencies": {
- "three": ">= 0.159.0"
- }
- },
- "node_modules/three-stdlib": {
- "version": "2.36.1",
- "resolved": "https://registry.npmjs.org/three-stdlib/-/three-stdlib-2.36.1.tgz",
- "integrity": "sha512-XyGQrFmNQ5O/IoKm556ftwKsBg11TIb301MB5dWNicziQBEs2g3gtOYIf7pFiLa0zI2gUwhtCjv9fmjnxKZ1Cg==",
- "license": "MIT",
- "dependencies": {
- "@types/draco3d": "^1.4.0",
- "@types/offscreencanvas": "^2019.6.4",
- "@types/webxr": "^0.5.2",
- "draco3d": "^1.4.1",
- "fflate": "^0.6.9",
- "potpack": "^1.0.1"
- },
- "peerDependencies": {
- "three": ">=0.128.0"
- }
- },
- "node_modules/three-stdlib/node_modules/fflate": {
- "version": "0.6.10",
- "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.6.10.tgz",
- "integrity": "sha512-IQrh3lEPM93wVCEczc9SaAOvkmcoQn/G8Bo1e8ZPlY3X3bnAxWaBdvTdvM1hP62iZp0BXWDy4vTAy4fF0+Dlpg==",
- "license": "MIT"
- },
- "node_modules/toidentifier": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz",
- "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==",
- "license": "MIT",
- "engines": {
- "node": ">=0.6"
- }
- },
- "node_modules/troika-three-text": {
- "version": "0.52.4",
- "resolved": "https://registry.npmjs.org/troika-three-text/-/troika-three-text-0.52.4.tgz",
- "integrity": "sha512-V50EwcYGruV5rUZ9F4aNsrytGdKcXKALjEtQXIOBfhVoZU9VAqZNIoGQ3TMiooVqFAbR1w15T+f+8gkzoFzawg==",
- "license": "MIT",
- "dependencies": {
- "bidi-js": "^1.0.2",
- "troika-three-utils": "^0.52.4",
- "troika-worker-utils": "^0.52.0",
- "webgl-sdf-generator": "1.1.1"
- },
- "peerDependencies": {
- "three": ">=0.125.0"
- }
- },
- "node_modules/troika-three-utils": {
- "version": "0.52.4",
- "resolved": "https://registry.npmjs.org/troika-three-utils/-/troika-three-utils-0.52.4.tgz",
- "integrity": "sha512-NORAStSVa/BDiG52Mfudk4j1FG4jC4ILutB3foPnfGbOeIs9+G5vZLa0pnmnaftZUGm4UwSoqEpWdqvC7zms3A==",
- "license": "MIT",
- "peerDependencies": {
- "three": ">=0.125.0"
- }
- },
- "node_modules/troika-worker-utils": {
- "version": "0.52.0",
- "resolved": "https://registry.npmjs.org/troika-worker-utils/-/troika-worker-utils-0.52.0.tgz",
- "integrity": "sha512-W1CpvTHykaPH5brv5VHLfQo9D1OYuo0cSBEUQFFT/nBUzM8iD6Lq2/tgG/f1OelbAS1WtaTPQzE5uM49egnngw==",
- "license": "MIT"
- },
- "node_modules/tunnel-rat": {
- "version": "0.1.2",
- "resolved": "https://registry.npmjs.org/tunnel-rat/-/tunnel-rat-0.1.2.tgz",
- "integrity": "sha512-lR5VHmkPhzdhrM092lI2nACsLO4QubF0/yoOhzX7c+wIpbN1GjHNzCc91QlpxBi+cnx8vVJ+Ur6vL5cEoQPFpQ==",
- "license": "MIT",
- "dependencies": {
- "zustand": "^4.3.2"
- }
- },
- "node_modules/tunnel-rat/node_modules/zustand": {
- "version": "4.5.7",
- "resolved": "https://registry.npmjs.org/zustand/-/zustand-4.5.7.tgz",
- "integrity": "sha512-CHOUy7mu3lbD6o6LJLfllpjkzhHXSBlX8B9+qPddUsIfeF5S/UZ5q0kmCsnRqT1UHFQZchNFDDzMbQsuesHWlw==",
- "license": "MIT",
- "dependencies": {
- "use-sync-external-store": "^1.2.2"
- },
- "engines": {
- "node": ">=12.7.0"
- },
- "peerDependencies": {
- "@types/react": ">=16.8",
- "immer": ">=9.0.6",
- "react": ">=16.8"
- },
- "peerDependenciesMeta": {
- "@types/react": {
- "optional": true
- },
- "immer": {
- "optional": true
- },
- "react": {
- "optional": true
- }
- }
- },
- "node_modules/type-is": {
- "version": "2.1.0",
- "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.1.0.tgz",
- "integrity": "sha512-faYHw0anBbc/kWF3zFTEnxSFOAGUX9GFbOBthvDdLsIlEoWOFOtS0zgCiQYwIskL9iGXZL3kAXD8OoZ4GmMATA==",
- "license": "MIT",
- "dependencies": {
- "content-type": "^2.0.0",
- "media-typer": "^1.1.0",
- "mime-types": "^3.0.0"
- },
- "engines": {
- "node": ">= 18"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/type-is/node_modules/content-type": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.0.0.tgz",
- "integrity": "sha512-j/O/d7GcZCyNl7/hwZAb606rzqkyvaDctLmckbxLzHvFBzTJHuGEdodATcP3yIRoDrLHkIATJuvzbFlp/ki2cQ==",
- "license": "MIT",
- "engines": {
- "node": ">=18"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/express"
- }
- },
- "node_modules/unpipe": {
- "version": "1.0.0",
- "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
- "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/use-sync-external-store": {
- "version": "1.6.0",
- "resolved": "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.6.0.tgz",
- "integrity": "sha512-Pp6GSwGP/NrPIrxVFAIkOQeyw8lFenOHijQWkUTrDvrF4ALqylP2C/KCkeS9dpUM3KvYRQhna5vt7IL95+ZQ9w==",
- "license": "MIT",
- "peerDependencies": {
- "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
- }
- },
- "node_modules/utility-types": {
- "version": "3.11.0",
- "resolved": "https://registry.npmjs.org/utility-types/-/utility-types-3.11.0.tgz",
- "integrity": "sha512-6Z7Ma2aVEWisaL6TvBCy7P8rm2LQoPv6dJ7ecIaIixHcwfbJ0x7mWdbcwlIM5IGQxPZSFYeqRCqlOOeKoJYMkw==",
- "license": "MIT",
- "engines": {
- "node": ">= 4"
- }
- },
- "node_modules/vary": {
- "version": "1.1.2",
- "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
- "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==",
- "license": "MIT",
- "engines": {
- "node": ">= 0.8"
- }
- },
- "node_modules/webgl-constants": {
- "version": "1.1.1",
- "resolved": "https://registry.npmjs.org/webgl-constants/-/webgl-constants-1.1.1.tgz",
- "integrity": "sha512-LkBXKjU5r9vAW7Gcu3T5u+5cvSvh5WwINdr0C+9jpzVB41cjQAP5ePArDtk/WHYdVj0GefCgM73BA7FlIiNtdg=="
- },
- "node_modules/webgl-sdf-generator": {
- "version": "1.1.1",
- "resolved": "https://registry.npmjs.org/webgl-sdf-generator/-/webgl-sdf-generator-1.1.1.tgz",
- "integrity": "sha512-9Z0JcMTFxeE+b2x1LJTdnaT8rT8aEp7MVxkNwoycNmJWwPdzoXzMh0BjJSh/AEFP+KPYZUli814h8bJZFIZ2jA==",
- "license": "MIT"
- },
- "node_modules/which": {
- "version": "2.0.2",
- "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
- "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
- "license": "ISC",
- "dependencies": {
- "isexe": "^2.0.0"
- },
- "bin": {
- "node-which": "bin/node-which"
- },
- "engines": {
- "node": ">= 8"
- }
- },
- "node_modules/wrappy": {
- "version": "1.0.2",
- "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
- "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
- "license": "ISC"
- },
- "node_modules/zustand": {
- "version": "5.0.12",
- "resolved": "https://registry.npmjs.org/zustand/-/zustand-5.0.12.tgz",
- "integrity": "sha512-i77ae3aZq4dhMlRhJVCYgMLKuSiZAaUPAct2AksxQ+gOtimhGMdXljRT21P5BNpeT4kXlLIckvkPM029OljD7g==",
- "license": "MIT",
- "engines": {
- "node": ">=12.20.0"
- },
- "peerDependencies": {
- "@types/react": ">=18.0.0",
- "immer": ">=9.0.6",
- "react": ">=18.0.0",
- "use-sync-external-store": ">=1.2.0"
- },
- "peerDependenciesMeta": {
- "@types/react": {
- "optional": true
- },
- "immer": {
- "optional": true
- },
- "react": {
- "optional": true
- },
- "use-sync-external-store": {
- "optional": true
- }
- }
- }
- }
-}
diff --git a/server/package.json b/server/package.json
deleted file mode 100644
index a7ce8ea6..00000000
--- a/server/package.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "name": "opencompany-backend",
- "private": true,
- "version": "0.0.0",
- "scripts": {
- "start": "python -m uvicorn main:app --host 0.0.0.0 --port ${PORT:?not set - defaults live in .env.template}",
- "dev": "python -m uvicorn main:app --host 127.0.0.1 --port ${PORT:?not set - defaults live in .env.template} --reload --reload-dir . --reload-exclude \"*.pyc\" --reload-exclude \"__pycache__\" --log-level warning"
- },
- "dependencies": {
- "@react-three/drei": "^10.7.7",
- "@react-three/fiber": "^9.6.1",
- "clsx": "^2.1.1",
- "express": "^5.2.1",
- "morgan": "^1.11.0",
- "tailwind-merge": "^3.5.0",
- "three": "^0.184.0"
- },
- "overrides": {
- "body-parser": "^2.3.0",
- "qs": "^6.15.2"
- }
-}
diff --git a/server/pyproject.toml b/server/pyproject.toml
index fa1fd7f2..79dd170e 100644
--- a/server/pyproject.toml
+++ b/server/pyproject.toml
@@ -7,7 +7,6 @@ dependencies = [
# Core
"fastapi>=0.115.0",
"uvicorn[standard]>=0.32.0",
- "pydantic>=2.10.0",
"pydantic-settings>=2.6.0",
# Database
@@ -76,7 +75,6 @@ dependencies = [
# Utils
"python-dotenv>=1.0.0",
"qrcode[pil]>=8.0",
- "pytz>=2024.1",
# IANA timezone database for stdlib zoneinfo on Windows (no system
# tz db there). Was a hidden transitive dep of apscheduler until
# Wave 15.2 retired it; currentTimeTool's ZoneInfo(...) needs it.
@@ -122,7 +120,6 @@ dependencies = [
"google-api-python-client>=2.100.0",
"google-auth>=2.27.0",
"google-auth-oauthlib>=1.2.0",
- "google-auth-httplib2>=0.2.0",
# Temporal (durable workflow execution). Floor 1.25: Wave 18 uses
# PollerBehaviorAutoscaling + max_activities_per_second +
diff --git a/server/requirements.txt b/server/requirements.txt
index ecb33830..37ba3a1d 100644
--- a/server/requirements.txt
+++ b/server/requirements.txt
@@ -121,9 +121,7 @@ google-auth==2.55.2
# google-genai
# opencompany-server
google-auth-httplib2==0.4.0
- # via
- # google-api-python-client
- # opencompany-server
+ # via google-api-python-client
google-auth-oauthlib==1.4.0
# via opencompany-server
google-genai==2.11.0
@@ -342,8 +340,6 @@ python-slugify==8.0.4
# via opencompany-server
python-telegram-bot==22.8
# via opencompany-server
-pytz==2026.2
- # via opencompany-server
pywin32==312 ; sys_platform == 'win32'
# via mcp
pywinpty==3.0.5 ; sys_platform == 'win32'
diff --git a/server/routers/database.py b/server/routers/database.py
index 88393b41..b605b38a 100644
--- a/server/routers/database.py
+++ b/server/routers/database.py
@@ -8,7 +8,11 @@ from core.container import container
from core.database import Database
from core.logging import get_logger
from services.example_loader import import_examples_for_user
-from services.workflow_storage.handlers import handle_save_workflow
+from services.workflow_storage.handlers import (
+ delete_workflow_with_context_archival,
+ handle_get_workflow,
+ handle_save_workflow,
+)
logger = get_logger(__name__)
router = APIRouter(prefix="/api/database", tags=["database"])
@@ -123,22 +127,18 @@ async def get_all_workflows(database: Database = Depends(lambda: container.datab
@router.get("/workflows/{workflow_id}")
async def get_workflow(workflow_id: str, database: Database = Depends(lambda: container.database())):
- """Get workflow by ID."""
+ """Get the server-normalized workflow graph by ID."""
try:
- workflow = await database.get_workflow(workflow_id)
+ result = await handle_get_workflow(
+ {"workflow_id": workflow_id},
+ websocket=None, # type: ignore[arg-type] # unused by handler
+ )
+ workflow = result.get("workflow")
if workflow:
- return {
- "success": True,
- "workflow": {
- "id": workflow.id,
- "name": workflow.name,
- "slug": workflow.slug,
- "data": workflow.data,
- "createdAt": workflow.created_at.isoformat() if workflow.created_at else None,
- "lastModified": workflow.updated_at.isoformat() if workflow.updated_at else None,
- },
- }
- return {"success": False, "error": "Workflow not found"}
+ # Preserve the REST route's established timestamp field names.
+ workflow["createdAt"] = workflow.pop("created_at", None)
+ workflow["lastModified"] = workflow.pop("updated_at", None)
+ return result
except Exception as e:
logger.error("Failed to get workflow", error=str(e), exc_info=True)
return {"success": False, "error": "Failed to get workflow"}
@@ -146,10 +146,12 @@ async def get_workflow(workflow_id: str, database: Database = Depends(lambda: co
@router.delete("/workflows/{workflow_id}")
async def delete_workflow(workflow_id: str, database: Database = Depends(lambda: container.database())):
- """Delete workflow."""
+ """Delete through the same durable Context lifecycle path as WebSocket."""
try:
- success = await database.delete_workflow(workflow_id)
- return {"success": success, "workflow_id": workflow_id}
+ return await delete_workflow_with_context_archival(
+ database,
+ workflow_id,
+ )
except Exception as e:
logger.error("Failed to delete workflow", error=str(e), exc_info=True)
return {"success": False, "error": "Failed to delete workflow"}
diff --git a/server/routers/websocket.py b/server/routers/websocket.py
index 6ed58488..85f6d44d 100644
--- a/server/routers/websocket.py
+++ b/server/routers/websocket.py
@@ -135,7 +135,10 @@ async def handle_get_node_parameters(data: Dict[str, Any], websocket: WebSocket)
node_id = data["node_id"]
parameters = await database.get_node_parameters(node_id)
logger.debug(f"[GET_PARAMS] Node ID: {node_id}")
- logger.debug(f"[GET_PARAMS] Raw from DB: {parameters}")
+ logger.debug(
+ "[GET_PARAMS] Parameter keys: %s",
+ sorted(parameters) if isinstance(parameters, dict) else [],
+ )
logger.debug(f"[GET_PARAMS] Code length: {len(parameters.get('code', '')) if parameters and 'code' in parameters else 'no code field'}")
return {"node_id": node_id, "parameters": parameters or {}, "version": 1, "timestamp": time.time()}
@@ -144,11 +147,48 @@ async def handle_get_node_parameters(data: Dict[str, Any], websocket: WebSocket)
async def handle_get_all_node_parameters(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
"""Get parameters for multiple nodes."""
database = container.database()
+ node_ids = data.get("node_ids", [])
+ if not isinstance(node_ids, list) or len(node_ids) > 1000:
+ raise ValueError("node_ids must be a list of at most 1000 ids")
+
+ export_mode = data.get("purpose") == "export"
+ if export_mode:
+ workflow_id = str(data.get("workflow_id") or "")
+ if not workflow_id:
+ raise ValueError("workflow_id is required for export")
+ workflow = await database.get_workflow(workflow_id)
+ if workflow is None:
+ raise ValueError("Workflow not found")
+ graph = workflow.data if isinstance(workflow.data, dict) else {}
+ authenticated_owner = str(
+ getattr(getattr(websocket, "state", None), "user_id", None)
+ or "owner"
+ )
+ stored_owner = str(graph.get("owner_id") or "")
+ if stored_owner and stored_owner != authenticated_owner:
+ raise ValueError("Workflow access denied")
+ workflow_node_ids = {
+ str(node.get("id"))
+ for node in graph.get("nodes", [])
+ if isinstance(node, dict) and node.get("id")
+ }
+ requested_ids = {str(node_id) for node_id in node_ids}
+ if not requested_ids.issubset(workflow_node_ids):
+ raise ValueError("Export contains nodes outside the workflow")
+ from services.workflow_sanitizer import sanitize_runtime_payload
+
result = {}
- for node_id in data.get("node_ids", []):
+ for node_id in node_ids:
parameters = await database.get_node_parameters(node_id)
if parameters:
- result[node_id] = {"parameters": parameters, "version": 1}
+ result[node_id] = {
+ "parameters": (
+ sanitize_runtime_payload(parameters)
+ if export_mode
+ else parameters
+ ),
+ "version": 1,
+ }
return {"parameters": result, "timestamp": time.time()}
@@ -423,6 +463,15 @@ async def handle_execute_node(data: Dict[str, Any], websocket: WebSocket) -> Dic
)
if key in data
}
+ socket_path = str((getattr(websocket, "scope", {}) or {}).get("path") or "")
+ user_id = str(
+ (
+ data.get("user_id")
+ if socket_path == "/ws/internal"
+ else getattr(getattr(websocket, "state", None), "user_id", None)
+ )
+ or "owner"
+ )
await broadcaster.update_node_status(
node_id,
@@ -446,6 +495,7 @@ async def handle_execute_node(data: Dict[str, Any], websocket: WebSocket) -> Dic
workflow_id=workflow_id,
outputs=data.get("outputs", {}), # Upstream node outputs for data flow
extras=invocation_extras or None,
+ user_id=user_id,
)
if result.get("success"):
@@ -912,6 +962,10 @@ async def handle_execute_workflow(data: Dict[str, Any], websocket: WebSocket) ->
session_id=session_id,
status_callback=status_callback,
workflow_id=workflow_id,
+ user_id=str(
+ getattr(getattr(websocket, "state", None), "user_id", None)
+ or "owner"
+ ),
)
finally:
# Always release the active-run counter so the button never gets stuck
@@ -953,6 +1007,15 @@ async def handle_execute_ai_node(data: Dict[str, Any], websocket: WebSocket) ->
node_id, node_type = data["node_id"], data["node_type"]
workflow_id = data.get("workflow_id") # Per-workflow isolation for tool node glowing
execution_id = str(data.get("execution_id") or uuid.uuid4().hex)
+ socket_path = str((getattr(websocket, "scope", {}) or {}).get("path") or "")
+ user_id = str(
+ (
+ data.get("user_id")
+ if socket_path == "/ws/internal"
+ else getattr(getattr(websocket, "state", None), "user_id", None)
+ )
+ or "owner"
+ )
await broadcaster.update_node_status(node_id, "executing", {"execution_id": execution_id}, workflow_id=workflow_id)
await broadcaster.workflow_run_started(workflow_id)
@@ -967,6 +1030,7 @@ async def handle_execute_ai_node(data: Dict[str, Any], websocket: WebSocket) ->
session_id=data.get("session_id", "default"),
execution_id=execution_id,
workflow_id=workflow_id,
+ user_id=user_id,
)
if result.get("success"):
@@ -1553,6 +1617,7 @@ async def websocket_status_endpoint(websocket: WebSocket):
# Check if auth is disabled (VITE_AUTH_ENABLED=false)
auth_disabled = settings.vite_auth_enabled and settings.vite_auth_enabled.lower() == "false"
+ authenticated_user_id = "owner"
if not auth_disabled:
# Auth enabled - verify token
from core.auth_cookies import get_session_token
@@ -1570,6 +1635,15 @@ async def websocket_status_endpoint(websocket: WebSocket):
await websocket.close(code=4001, reason="Invalid or expired session")
return
+ authenticated_user_id = str(payload.get("sub") or "")
+ if not authenticated_user_id:
+ await websocket.close(code=4001, reason="Invalid session subject")
+ return
+
+ # Plugin-owned handlers resolve namespace ownership from trusted
+ # connection state. Client payloads cannot choose a Memory/Context owner.
+ websocket.state.user_id = authenticated_user_id
+
broadcaster = get_status_broadcaster()
await broadcaster.connect(websocket)
diff --git a/server/services/agent_context/__init__.py b/server/services/agent_context/__init__.py
new file mode 100644
index 00000000..52abc31c
--- /dev/null
+++ b/server/services/agent_context/__init__.py
@@ -0,0 +1,56 @@
+"""Durable Context V2 storage.
+
+The store is intentionally independent of agent/provider implementations.
+Runtimes append exact transitions and receive bounded references; provider
+adapters own rendering and replay validation.
+"""
+
+from services.agent_context.store import (
+ AgentContextError,
+ AgentContextStore,
+ CompactionConflictError,
+ ContextArchivedError,
+ ContextNotFoundError,
+ RevisionConflictError,
+ StaleEpochError,
+)
+from services.agent_context.runtime import (
+ AgentContextTransitionWriter,
+ OpaqueCheckpointError,
+ reconstruct_message_wire_v2,
+ reconstruct_messages,
+)
+from services.agent_context.compaction import (
+ AgentContextCompactionService,
+ ContextCompactionCandidate,
+ ContextCompactionPolicy,
+ ContextCompactionResult,
+ ProviderContextAdapter,
+ ProviderContextCapabilities,
+ get_provider_context_adapter,
+ provider_context_request_options,
+)
+from services.agent_context.legacy import import_generation_zero_handoff
+
+__all__ = [
+ "AgentContextError",
+ "AgentContextCompactionService",
+ "AgentContextStore",
+ "AgentContextTransitionWriter",
+ "CompactionConflictError",
+ "ContextCompactionCandidate",
+ "ContextCompactionPolicy",
+ "ContextCompactionResult",
+ "ContextArchivedError",
+ "ContextNotFoundError",
+ "OpaqueCheckpointError",
+ "RevisionConflictError",
+ "ProviderContextAdapter",
+ "ProviderContextCapabilities",
+ "StaleEpochError",
+ "get_provider_context_adapter",
+ "import_generation_zero_handoff",
+ "provider_context_request_options",
+ "reconstruct_message_wire_v2",
+ "reconstruct_messages",
+]
diff --git a/server/services/agent_context/compaction.py b/server/services/agent_context/compaction.py
new file mode 100644
index 00000000..8d6709e4
--- /dev/null
+++ b/server/services/agent_context/compaction.py
@@ -0,0 +1,771 @@
+"""Capability-driven compaction for Agent Context V2.
+
+The raw journal is immutable. This module only selects a committed
+transaction prefix, asks a provider adapter for a replay candidate, validates
+that candidate, and compare-and-swap activates a checkpoint in
+``AgentContextStore``.
+
+Provider-specific behavior is registered behind :class:`ProviderContextAdapter`;
+the orchestration path never switches on provider names.
+"""
+
+from __future__ import annotations
+
+import json
+import re
+from copy import deepcopy
+from dataclasses import dataclass, field
+from typing import Any, Awaitable, Callable, Optional, Protocol, Sequence
+
+from models.agent_context import (
+ AgentContextCheckpoint,
+ AgentContextCompactionPlan,
+ AgentContextEvent,
+ AgentContextRef,
+)
+from services.agent_context.store import AgentContextStore
+from services.llm.protocol import message_from_wire, message_to_wire
+from services.model_registry import get_model_registry
+
+
+PortableCompactor = Callable[
+ [list[dict[str, Any]], "ContextCompactionPolicy"],
+ Awaitable["ContextCompactionCandidate"],
+]
+
+
+@dataclass(frozen=True)
+class ContextCompactionPolicy:
+ mode: str = "auto"
+ trigger_ratio: float = 0.8
+ context_window_override: Optional[int] = None
+ exact_tail_retention_count: int = 8
+
+ @classmethod
+ def from_mapping(
+ cls,
+ value: Optional[dict[str, Any]],
+ ) -> "ContextCompactionPolicy":
+ raw = value or {}
+ mode = str(raw.get("compaction_mode") or "auto")
+ if mode not in {"auto", "native", "portable", "disabled"}:
+ mode = "auto"
+ try:
+ trigger_ratio = float(raw.get("trigger_ratio", 0.8))
+ except (TypeError, ValueError):
+ trigger_ratio = 0.8
+ trigger_ratio = min(0.95, max(0.1, trigger_ratio))
+ try:
+ override = int(raw.get("context_window_override") or 0) or None
+ except (TypeError, ValueError):
+ override = None
+ if override is not None:
+ override = max(1024, override)
+ try:
+ tail = int(raw.get("exact_tail_retention_count", 8))
+ except (TypeError, ValueError):
+ tail = 8
+ return cls(
+ mode=mode,
+ trigger_ratio=trigger_ratio,
+ context_window_override=override,
+ exact_tail_retention_count=max(1, min(1000, tail)),
+ )
+
+
+@dataclass(frozen=True)
+class ProviderContextCapabilities:
+ context_window: int
+ supports_native_compaction: bool = False
+ replay_fidelity: str = "provider_replayable"
+ native_strategy: Optional[str] = None
+
+
+@dataclass(frozen=True)
+class ContextCompactionCandidate:
+ strategy: str
+ replay_payload: dict[str, Any]
+ active_token_count: int
+ lifetime_usage: dict[str, int] = field(default_factory=dict)
+
+
+@dataclass(frozen=True)
+class ContextCompactionResult:
+ ref: AgentContextRef
+ pressure_tokens: int
+ context_window: int
+ compacted: bool = False
+ checkpoint: Optional[AgentContextCheckpoint] = None
+ reason: str = ""
+ lifetime_usage: dict[str, int] = field(default_factory=dict)
+
+
+class ProviderContextAdapter(Protocol):
+ """Provider replay/pressure boundary required by Context V2."""
+
+ def capabilities(self, model: str) -> ProviderContextCapabilities: ...
+
+ def measure_active_context(self, rendered_request: Any) -> int: ...
+
+ async def compact(
+ self,
+ committed_prefix: Sequence[AgentContextEvent],
+ policy: ContextCompactionPolicy,
+ *,
+ base_replay: Optional[dict[str, Any]] = None,
+ resolved_messages: Optional[list[dict[str, Any]]] = None,
+ portable_compactor: Optional[PortableCompactor] = None,
+ ) -> Optional[ContextCompactionCandidate]: ...
+
+ def validate_replay(
+ self,
+ candidate: ContextCompactionCandidate,
+ ) -> bool: ...
+
+ def request_options(
+ self,
+ *,
+ model: str,
+ policy: ContextCompactionPolicy,
+ context_window: int,
+ ) -> Optional[dict[str, Any]]: ...
+
+
+class PortableContextAdapter:
+ """Structured portable fallback used when native support is unverified."""
+
+ def __init__(self, provider: str = "portable") -> None:
+ self.provider = provider
+
+ def capabilities(self, model: str) -> ProviderContextCapabilities:
+ registry = get_model_registry()
+ return ProviderContextCapabilities(
+ context_window=registry.get_context_length(model, self.provider),
+ )
+
+ def measure_active_context(self, rendered_request: Any) -> int:
+ # Used only when a provider did not return exact input usage. The
+ # estimate is deliberately conservative and deterministic.
+ encoded = json.dumps(
+ rendered_request,
+ ensure_ascii=False,
+ separators=(",", ":"),
+ default=str,
+ ).encode("utf-8")
+ return max(1, (len(encoded) + 2) // 3)
+
+ async def compact(
+ self,
+ committed_prefix: Sequence[AgentContextEvent],
+ policy: ContextCompactionPolicy,
+ *,
+ base_replay: Optional[dict[str, Any]] = None,
+ resolved_messages: Optional[list[dict[str, Any]]] = None,
+ portable_compactor: Optional[PortableCompactor] = None,
+ ) -> Optional[ContextCompactionCandidate]:
+ if portable_compactor is None:
+ return None
+ wires = (
+ deepcopy(resolved_messages)
+ if resolved_messages is not None
+ else _base_messages(base_replay)
+ )
+ if resolved_messages is None:
+ wires.extend(_message_wires(committed_prefix))
+ if not wires:
+ return None
+ return await portable_compactor(wires, policy)
+
+ def validate_replay(
+ self,
+ candidate: ContextCompactionCandidate,
+ ) -> bool:
+ messages = candidate.replay_payload.get("messages")
+ if not isinstance(messages, list) or not messages:
+ return False
+ try:
+ for wire in messages:
+ if not isinstance(wire, dict):
+ return False
+ message_from_wire(wire)
+ except (TypeError, ValueError):
+ return False
+ return candidate.active_token_count >= 0
+
+ def request_options(
+ self,
+ *,
+ model: str,
+ policy: ContextCompactionPolicy,
+ context_window: int,
+ ) -> Optional[dict[str, Any]]:
+ del model, policy, context_window
+ return None
+
+
+class NativeOutputContextAdapter(PortableContextAdapter):
+ """Replay adapter for providers that emit a compaction output block."""
+
+ def __init__(
+ self,
+ provider: str,
+ *,
+ model_pattern: str,
+ payload_collection: str,
+ compaction_type: str = "compaction",
+ native_strategy: str,
+ minimum_trigger_tokens: int = 1,
+ durability_pause: bool = False,
+ ) -> None:
+ super().__init__(provider)
+ self._model_pattern = re.compile(model_pattern, re.IGNORECASE)
+ self._payload_collection = payload_collection
+ self._compaction_type = compaction_type
+ self._native_strategy = native_strategy
+ self._minimum_trigger_tokens = minimum_trigger_tokens
+ self._durability_pause = durability_pause
+
+ def capabilities(self, model: str) -> ProviderContextCapabilities:
+ base = super().capabilities(model)
+ supported = bool(self._model_pattern.search(model or ""))
+ return ProviderContextCapabilities(
+ context_window=base.context_window,
+ supports_native_compaction=supported,
+ replay_fidelity=base.replay_fidelity,
+ native_strategy=self._native_strategy if supported else None,
+ )
+
+ async def compact(
+ self,
+ committed_prefix: Sequence[AgentContextEvent],
+ policy: ContextCompactionPolicy,
+ *,
+ base_replay: Optional[dict[str, Any]] = None,
+ resolved_messages: Optional[list[dict[str, Any]]] = None,
+ portable_compactor: Optional[PortableCompactor] = None,
+ ) -> Optional[ContextCompactionCandidate]:
+ native = self._native_candidate(committed_prefix)
+ if native is not None:
+ return native
+ if policy.mode == "native":
+ return None
+ return await super().compact(
+ committed_prefix,
+ policy,
+ base_replay=base_replay,
+ resolved_messages=resolved_messages,
+ portable_compactor=portable_compactor,
+ )
+
+ def request_options(
+ self,
+ *,
+ model: str,
+ policy: ContextCompactionPolicy,
+ context_window: int,
+ ) -> Optional[dict[str, Any]]:
+ capabilities = self.capabilities(model)
+ if (
+ policy.mode not in {"auto", "native"}
+ or not capabilities.supports_native_compaction
+ ):
+ return None
+ threshold = max(
+ self._minimum_trigger_tokens,
+ int(context_window * policy.trigger_ratio),
+ )
+ if threshold >= context_window:
+ return None
+ return {
+ "type": "compaction",
+ "compact_threshold": threshold,
+ "pause_after_compaction": self._durability_pause,
+ "strategy": self._native_strategy,
+ }
+
+ def _native_candidate(
+ self,
+ committed_prefix: Sequence[AgentContextEvent],
+ ) -> Optional[ContextCompactionCandidate]:
+ for event in reversed(committed_prefix):
+ wire = event.message_wire_v2
+ if not isinstance(wire, dict):
+ continue
+ state = wire.get("provider_state")
+ if not isinstance(state, dict) or state.get("provider") != self.provider:
+ continue
+ payload = state.get("payload")
+ if not isinstance(payload, dict):
+ continue
+ blocks = payload.get(self._payload_collection)
+ if not isinstance(blocks, list):
+ continue
+ marker_index = -1
+ for index, block in enumerate(blocks):
+ if (
+ isinstance(block, dict)
+ and block.get("type") == self._compaction_type
+ ):
+ marker_index = index
+ if marker_index < 0:
+ continue
+ compacted_wire = deepcopy(wire)
+ compacted_state = compacted_wire["provider_state"]
+ compacted_payload = compacted_state["payload"]
+ # Provider documentation requires passing the canonical compacted
+ # output unchanged. We only discard blocks preceding the latest
+ # provider-issued compaction marker.
+ compacted_payload[self._payload_collection] = deepcopy(
+ blocks[marker_index:]
+ )
+ measured = self.measure_active_context([compacted_wire])
+ return ContextCompactionCandidate(
+ strategy=self._native_strategy,
+ replay_payload={"messages": [compacted_wire]},
+ active_token_count=measured,
+ )
+ return None
+
+
+_ADAPTERS: dict[str, ProviderContextAdapter] = {}
+
+
+def register_provider_context_adapter(
+ provider: str,
+ adapter: ProviderContextAdapter,
+) -> None:
+ normalized = provider.strip().lower()
+ if not normalized:
+ raise ValueError("provider is required")
+ current = _ADAPTERS.get(normalized)
+ if current is not None and current is not adapter:
+ raise ValueError(f"Context adapter already registered for {normalized}")
+ _ADAPTERS[normalized] = adapter
+
+
+def get_provider_context_adapter(provider: str) -> ProviderContextAdapter:
+ normalized = str(provider or "").strip().lower()
+ return _ADAPTERS.get(normalized) or PortableContextAdapter(
+ normalized or "portable"
+ )
+
+
+def provider_context_request_options(
+ *,
+ provider: str,
+ model: str,
+ policy: ContextCompactionPolicy,
+) -> Optional[dict[str, Any]]:
+ adapter = get_provider_context_adapter(provider)
+ capabilities = adapter.capabilities(model)
+ context_window = (
+ policy.context_window_override or capabilities.context_window
+ )
+ return adapter.request_options(
+ model=model,
+ policy=policy,
+ context_window=context_window,
+ )
+
+
+class AgentContextCompactionService:
+ """CAS checkpoint orchestration over one durable Context store."""
+
+ def __init__(self, store: AgentContextStore) -> None:
+ self.store = store
+
+ async def update_pressure_and_compact(
+ self,
+ ref: AgentContextRef,
+ *,
+ operation_id: str,
+ provider: str,
+ model: str,
+ policy: ContextCompactionPolicy,
+ active_input_tokens: int,
+ output_headroom: int,
+ rendered_request: Any = None,
+ portable_compactor: Optional[PortableCompactor] = None,
+ ) -> ContextCompactionResult:
+ adapter = get_provider_context_adapter(provider)
+ capabilities = adapter.capabilities(model)
+ context_window = (
+ policy.context_window_override or capabilities.context_window
+ )
+ measured_input = (
+ max(0, int(adapter.measure_active_context(rendered_request)))
+ if rendered_request is not None
+ else 0
+ )
+ pressure_tokens = max(
+ 0,
+ active_input_tokens,
+ measured_input,
+ ) + max(
+ 0, output_headroom
+ )
+ current_ref = await self.store.record_active_pressure(
+ ref,
+ operation_id=f"{operation_id}:pressure",
+ active_token_count=pressure_tokens,
+ )
+ if policy.mode == "disabled":
+ return ContextCompactionResult(
+ ref=current_ref,
+ pressure_tokens=pressure_tokens,
+ context_window=context_window,
+ reason="disabled",
+ )
+
+ state = await self.store.load_active(current_ref)
+ native_marker_sequence = _native_compaction_marker_sequence(
+ adapter,
+ state.tail,
+ )
+ if native_marker_sequence is not None:
+ # A native marker is itself the provider-issued replay boundary.
+ # Never claim later tool/final events as covered by a candidate
+ # whose payload contains only state through this marker.
+ coverage = native_marker_sequence
+ else:
+ coverage = select_compaction_boundary(
+ state.tail,
+ exact_tail_retention_count=(
+ policy.exact_tail_retention_count
+ ),
+ )
+ if coverage is None:
+ return ContextCompactionResult(
+ ref=state.ref,
+ pressure_tokens=pressure_tokens,
+ context_window=context_window,
+ reason="no_committed_prefix",
+ )
+
+ native_present = _has_native_compaction_marker(
+ adapter,
+ state.tail,
+ coverage,
+ )
+ triggered = (
+ context_window > 0
+ and pressure_tokens >= int(context_window * policy.trigger_ratio)
+ )
+ if not native_present and not triggered:
+ return ContextCompactionResult(
+ ref=state.ref,
+ pressure_tokens=pressure_tokens,
+ context_window=context_window,
+ reason="below_threshold",
+ )
+ if (
+ policy.mode == "native"
+ and not capabilities.supports_native_compaction
+ ):
+ return ContextCompactionResult(
+ ref=state.ref,
+ pressure_tokens=pressure_tokens,
+ context_window=context_window,
+ reason="native_unsupported",
+ )
+
+ strategy = (
+ capabilities.native_strategy
+ if native_present and capabilities.native_strategy
+ else "portable"
+ )
+ plan = await self.store.prepare_compaction(
+ state.ref,
+ operation_id=f"{operation_id}:prepare",
+ provider=provider,
+ strategy=strategy,
+ covers_through_sequence=coverage,
+ )
+ base_replay = await self._base_replay(plan)
+ source_wires = await self._resolve_plan_messages(
+ plan,
+ base_replay=base_replay,
+ )
+ source_pressure = max(
+ pressure_tokens,
+ adapter.measure_active_context(source_wires)
+ + max(0, output_headroom),
+ )
+ try:
+ candidate = await adapter.compact(
+ plan.committed_prefix,
+ policy,
+ base_replay=base_replay,
+ resolved_messages=source_wires,
+ portable_compactor=portable_compactor,
+ )
+ if candidate is None:
+ await self.store.fail_compaction(
+ attempt_id=plan.attempt_id,
+ error_code="candidate_unavailable",
+ )
+ return ContextCompactionResult(
+ ref=state.ref,
+ pressure_tokens=pressure_tokens,
+ context_window=context_window,
+ reason="candidate_unavailable",
+ )
+ candidate_pressure = (
+ candidate.active_token_count + max(0, output_headroom)
+ )
+ if (
+ not adapter.validate_replay(candidate)
+ or candidate_pressure >= source_pressure
+ ):
+ await self.store.fail_compaction(
+ attempt_id=plan.attempt_id,
+ error_code="candidate_validation_failed",
+ )
+ return ContextCompactionResult(
+ ref=state.ref,
+ pressure_tokens=pressure_tokens,
+ context_window=context_window,
+ reason="candidate_validation_failed",
+ lifetime_usage=candidate.lifetime_usage,
+ )
+ replay_ref = await self.store.put_blob(
+ candidate.replay_payload
+ )
+ checkpoint = await self.store.commit_checkpoint(
+ state.ref,
+ attempt_id=plan.attempt_id,
+ operation_id=f"{operation_id}:commit",
+ replay_payload_ref=replay_ref,
+ active_token_count=candidate_pressure,
+ )
+ latest = (await self.store.load_active(state.ref)).ref
+ return ContextCompactionResult(
+ ref=latest,
+ pressure_tokens=candidate_pressure,
+ context_window=context_window,
+ compacted=True,
+ checkpoint=checkpoint,
+ reason=candidate.strategy,
+ lifetime_usage=candidate.lifetime_usage,
+ )
+ except BaseException:
+ await self.store.fail_compaction(
+ attempt_id=plan.attempt_id,
+ error_code="candidate_exception",
+ )
+ raise
+
+ async def _base_replay(
+ self,
+ plan: AgentContextCompactionPlan,
+ ) -> Optional[dict[str, Any]]:
+ if plan.base_checkpoint is None:
+ return None
+ value = await self.store.get_blob(
+ plan.base_checkpoint.replay_payload_ref
+ )
+ return value if isinstance(value, dict) else None
+
+ async def _resolve_plan_messages(
+ self,
+ plan: AgentContextCompactionPlan,
+ *,
+ base_replay: Optional[dict[str, Any]],
+ ) -> list[dict[str, Any]]:
+ snapshot_messages: Optional[list[dict[str, Any]]] = None
+ snapshot_sequence = 0
+ for event in reversed(plan.committed_prefix):
+ if (
+ event.event_type != "request.snapshot"
+ or not event.payload_ref
+ ):
+ continue
+ payload = await self.store.get_blob(event.payload_ref)
+ candidates = (
+ payload.get("messages")
+ if isinstance(payload, dict)
+ else None
+ )
+ if not isinstance(candidates, list):
+ continue
+ snapshot_messages = []
+ for candidate in candidates:
+ if not isinstance(candidate, dict):
+ raise ValueError(
+ "request snapshot contains a non-message item"
+ )
+ snapshot_messages.append(
+ dict(message_to_wire(message_from_wire(candidate)))
+ )
+ snapshot_sequence = event.sequence
+ break
+ wires = (
+ snapshot_messages
+ if snapshot_messages is not None
+ else _base_messages(base_replay)
+ )
+ wires.extend(
+ deepcopy(event.message_wire_v2)
+ for event in plan.committed_prefix
+ if event.sequence > snapshot_sequence
+ and isinstance(event.message_wire_v2, dict)
+ )
+ return wires
+
+
+def select_compaction_boundary(
+ events: Sequence[AgentContextEvent],
+ *,
+ exact_tail_retention_count: int,
+) -> Optional[int]:
+ """Return the newest complete transaction before the retained exact tail."""
+
+ if len(events) <= exact_tail_retention_count:
+ return None
+ last_candidate_index = len(events) - exact_tail_retention_count - 1
+ for index in range(last_candidate_index, -1, -1):
+ if _is_complete_transaction(events, index):
+ return events[index].sequence
+ return None
+
+
+def _is_complete_transaction(
+ events: Sequence[AgentContextEvent],
+ index: int,
+) -> bool:
+ event = events[index]
+ if event.event_type in {"response.final", "response.truncated"}:
+ return True
+ wire = event.message_wire_v2
+ if event.event_type == "message.assistant" and isinstance(wire, dict):
+ calls = wire.get("tool_calls")
+ return not isinstance(calls, list) or not calls
+ if event.event_type != "message.tool_result":
+ return False
+
+ assistant_index: Optional[int] = None
+ for candidate in range(index - 1, -1, -1):
+ if events[candidate].event_type == "message.assistant":
+ assistant_index = candidate
+ break
+ if assistant_index is None:
+ return False
+ assistant_wire = events[assistant_index].message_wire_v2 or {}
+ calls = assistant_wire.get("tool_calls")
+ if not isinstance(calls, list) or not calls:
+ return False
+ requested = {
+ str(call.get("id") or "")
+ for call in calls
+ if isinstance(call, dict)
+ }
+ completed: set[str] = set()
+ for result in events[assistant_index + 1 : index + 1]:
+ if result.event_type == "message.assistant":
+ return False
+ if result.event_type != "message.tool_result":
+ continue
+ result_wire = result.message_wire_v2 or {}
+ completed.add(str(result_wire.get("tool_call_id") or ""))
+ return bool(requested) and requested.issubset(completed)
+
+
+def _message_wires(
+ events: Sequence[AgentContextEvent],
+) -> list[dict[str, Any]]:
+ return [
+ deepcopy(event.message_wire_v2)
+ for event in events
+ if isinstance(event.message_wire_v2, dict)
+ ]
+
+
+def _base_messages(
+ replay: Optional[dict[str, Any]],
+) -> list[dict[str, Any]]:
+ if not isinstance(replay, dict):
+ return []
+ messages = replay.get("messages")
+ if not isinstance(messages, list):
+ return []
+ return [
+ deepcopy(wire)
+ for wire in messages
+ if isinstance(wire, dict)
+ ]
+
+
+def _has_native_compaction_marker(
+ adapter: ProviderContextAdapter,
+ tail: Sequence[AgentContextEvent],
+ coverage: int,
+) -> bool:
+ if not isinstance(adapter, NativeOutputContextAdapter):
+ return False
+ prefix = [event for event in tail if event.sequence <= coverage]
+ return adapter._native_candidate(prefix) is not None
+
+
+def _native_compaction_marker_sequence(
+ adapter: ProviderContextAdapter,
+ tail: Sequence[AgentContextEvent],
+) -> Optional[int]:
+ if not isinstance(adapter, NativeOutputContextAdapter):
+ return None
+ for event in reversed(tail):
+ if adapter._native_candidate([event]) is not None:
+ return event.sequence
+ return None
+
+
+def _register_builtin_adapters() -> None:
+ # OpenAI Responses emits an opaque encrypted ``compaction`` output item.
+ register_provider_context_adapter(
+ "openai",
+ NativeOutputContextAdapter(
+ "openai",
+ model_pattern=r"^(gpt-5(?:[.-]|$)|o[134](?:[.-]|$))",
+ payload_collection="output",
+ native_strategy="openai_responses_compaction",
+ ),
+ )
+ # Anthropic's beta Messages compaction returns a replayable compaction
+ # block and supports a pause so the block can be committed durably first.
+ register_provider_context_adapter(
+ "anthropic",
+ NativeOutputContextAdapter(
+ "anthropic",
+ model_pattern=(
+ r"^claude-(?:sonnet-4-6|opus-4-[678]|"
+ r"fable-5|mythos(?:-5|-preview))$"
+ ),
+ payload_collection="content",
+ native_strategy="anthropic_compact_20260112",
+ minimum_trigger_tokens=50_000,
+ durability_pause=True,
+ ),
+ )
+ register_provider_context_adapter(
+ "gemini",
+ PortableContextAdapter("gemini"),
+ )
+
+
+_register_builtin_adapters()
+
+
+__all__ = [
+ "AgentContextCompactionService",
+ "ContextCompactionCandidate",
+ "ContextCompactionPolicy",
+ "ContextCompactionResult",
+ "NativeOutputContextAdapter",
+ "PortableCompactor",
+ "PortableContextAdapter",
+ "ProviderContextAdapter",
+ "ProviderContextCapabilities",
+ "get_provider_context_adapter",
+ "provider_context_request_options",
+ "register_provider_context_adapter",
+ "select_compaction_boundary",
+]
diff --git a/server/services/agent_context/legacy.py b/server/services/agent_context/legacy.py
new file mode 100644
index 00000000..eeeb268a
--- /dev/null
+++ b/server/services/agent_context/legacy.py
@@ -0,0 +1,153 @@
+"""One-time handoff from immutable generation-zero migration artifacts."""
+
+from __future__ import annotations
+
+import hashlib
+from typing import Any
+
+from models.agent_context import AgentContextRef, AgentContextThreadSummary
+from services.agent_context.store import AgentContextStore
+from services.llm.protocol import Message, message_to_wire
+
+
+async def import_generation_zero_handoff(
+ store: AgentContextStore,
+ target_ref: AgentContextRef,
+) -> AgentContextRef:
+ """Copy legacy partial context into a live generation idempotently.
+
+ Graph migration writes recognized legacy state to generation zero so the
+ original artifact is immutable. A newly admitted generation receives a
+ clearly labelled MessageWire boundary that can participate in normal
+ replay. Provider bindings are copied only when their source thread is
+ unambiguous; raw binding values remain in the blob store.
+ """
+
+ if target_ref.generation <= 0:
+ return target_ref
+ sources = await store.list_threads(
+ workflow_id=target_ref.workflow_id,
+ context_node_id=target_ref.context_node_id,
+ generation=0,
+ include_archived=True,
+ limit=1000,
+ )
+ if not sources:
+ return target_ref
+
+ exact = [
+ source
+ for source in sources
+ if source.ref.thread_id == target_ref.thread_id
+ ]
+ selected = exact or sorted(sources, key=lambda item: item.ref.thread_id)
+ current = target_ref
+ for source in selected:
+ current = await _import_legacy_events(store, source, current)
+
+ # A continuation UUID/interaction ID must not be guessed when multiple
+ # legacy Memory sessions could have supplied it.
+ if len(selected) == 1:
+ await _copy_provider_bindings(store, selected[0], current)
+ return current
+
+
+async def _import_legacy_events(
+ store: AgentContextStore,
+ source: AgentContextThreadSummary,
+ target_ref: AgentContextRef,
+) -> AgentContextRef:
+ after = 0
+ current = target_ref
+ while True:
+ events, next_after = await store.load_journal_page(
+ source.ref,
+ after_sequence=after,
+ limit=200,
+ )
+ for event in events:
+ if event.event_type != "legacy_partial" or not event.payload_ref:
+ continue
+ payload = await store.get_blob(event.payload_ref)
+ content = (
+ payload.get("content")
+ if isinstance(payload, dict)
+ else None
+ )
+ if not content:
+ continue
+ message = Message(
+ role="user",
+ content=(
+ "Legacy conversation context imported with "
+ "legacy_partial fidelity. It may be incomplete; treat "
+ "the current request and current system instruction as "
+ "authoritative.\n\n"
+ f"{content}"
+ ),
+ )
+ result = await store.append_transition(
+ current,
+ event_type="legacy_partial.handoff",
+ operation_id=_operation_id(
+ "legacy-context-handoff",
+ target_ref=current,
+ source_identity=(
+ f"{source.ref.thread_id}:{event.payload_hash}"
+ ),
+ ),
+ message_wire_v2=dict(message_to_wire(message)),
+ payload_ref=event.payload_ref,
+ provider="legacy",
+ )
+ current = result.ref
+ if next_after is None:
+ break
+ after = next_after
+ return current
+
+
+async def _copy_provider_bindings(
+ store: AgentContextStore,
+ source: AgentContextThreadSummary,
+ target_ref: AgentContextRef,
+) -> None:
+ bindings = await store.load_provider_bindings(source.ref)
+ for binding in bindings:
+ payload: Any = await store.get_blob(binding.binding_ref)
+ await store.bind_provider(
+ target_ref,
+ provider=binding.provider,
+ binding_type=binding.binding_type,
+ binding=payload,
+ operation_id=_operation_id(
+ "legacy-context-binding",
+ target_ref=target_ref,
+ source_identity=(
+ f"{source.ref.thread_id}:{binding.provider}:"
+ f"{binding.binding_type}:"
+ f"{binding.binding_ref.removeprefix('sha256:')}"
+ ),
+ ),
+ fidelity=binding.fidelity,
+ )
+
+
+def _operation_id(
+ prefix: str,
+ *,
+ target_ref: AgentContextRef,
+ source_identity: str,
+) -> str:
+ # RuntimeMutation IDs are globally unique, not merely unique within a
+ # Context thread. Include the complete target fence while keeping the
+ # persisted identifier bounded.
+ material = (
+ f"{target_ref.workflow_id}:{target_ref.context_node_id}:"
+ f"{target_ref.generation}:{target_ref.thread_id}:"
+ f"{source_identity}"
+ )
+ return f"{prefix}:{hashlib.sha256(material.encode('utf-8')).hexdigest()}"
+
+
+__all__ = ["import_generation_zero_handoff"]
diff --git a/server/services/agent_context/lifecycle.py b/server/services/agent_context/lifecycle.py
new file mode 100644
index 00000000..ffb607f1
--- /dev/null
+++ b/server/services/agent_context/lifecycle.py
@@ -0,0 +1,44 @@
+"""Best-effort cleanup for provider resources owned by a Context epoch."""
+
+from __future__ import annotations
+
+from core.logging import get_logger
+
+
+logger = get_logger(__name__)
+
+
+async def fence_context_provider_resources(
+ *,
+ context_node_id: str,
+ thread_id: str,
+ keep_epoch: int,
+) -> None:
+ """Terminate warm provider state that belongs to an older epoch.
+
+ The durable epoch in :class:`AgentContextStore` is authoritative. Pool
+ cleanup is intentionally best-effort because the provider bridge also
+ rejects stale epoch keys when they are acquired.
+ """
+
+ try:
+ from services.cli_agent.factory import get_session_pool
+
+ pool = get_session_pool("claude")
+ terminate_context = getattr(pool, "terminate_context", None)
+ if terminate_context is not None:
+ await terminate_context(
+ context_node_id,
+ thread_id=thread_id,
+ keep_epoch=keep_epoch,
+ )
+ except Exception as exc:
+ logger.warning(
+ "[Context] provider resource cleanup failed for %s/%s: %s",
+ context_node_id,
+ thread_id,
+ exc,
+ )
+
+
+__all__ = ["fence_context_provider_resources"]
diff --git a/server/services/agent_context/runtime.py b/server/services/agent_context/runtime.py
new file mode 100644
index 00000000..55244d5d
--- /dev/null
+++ b/server/services/agent_context/runtime.py
@@ -0,0 +1,192 @@
+"""Small runtime adapters bound to one Context thread."""
+
+from __future__ import annotations
+
+import asyncio
+from typing import Any, Optional
+
+from models.agent_context import (
+ AgentContextAppendResult,
+ AgentContextRef,
+)
+from services.agent_context.store import AgentContextStore
+from services.llm.protocol import (
+ Message,
+ message_from_wire,
+ message_to_wire,
+)
+
+
+class OpaqueCheckpointError(RuntimeError):
+ """Raised when a native replay payload cannot be rendered portably."""
+
+
+class AgentContextTransitionWriter:
+ """Epoch-bound implementation of ``AgentContextTransitionSink``.
+
+ The writer owns only the latest bounded ref. Payload bodies are moved to
+ hash-addressed blob storage before the journal entry is committed.
+ """
+
+ def __init__(
+ self,
+ store: AgentContextStore,
+ ref: AgentContextRef,
+ ) -> None:
+ self.store = store
+ self.ref = ref
+ self._append_lock = asyncio.Lock()
+
+ async def append_transition(
+ self,
+ *,
+ event_type: str,
+ operation_id: str,
+ provider: str,
+ message_wire_v2: Optional[dict[str, Any]] = None,
+ payload: Optional[dict[str, Any]] = None,
+ ) -> AgentContextAppendResult:
+ payload_ref = (
+ await self.store.put_blob(payload)
+ if payload is not None
+ else None
+ )
+ async with self._append_lock:
+ result = await self.store.append_transition(
+ self.ref,
+ event_type=event_type,
+ operation_id=operation_id,
+ provider=provider,
+ message_wire_v2=message_wire_v2,
+ payload_ref=payload_ref,
+ )
+ self.ref = result.ref
+ return result
+
+
+async def reconstruct_message_wire_v2(
+ store: AgentContextStore,
+ ref: AgentContextRef,
+) -> tuple[AgentContextRef, list[dict[str, Any]]]:
+ """Reconstruct portable checkpoint messages plus the exact live tail.
+
+ Provider-native checkpoints are intentionally opaque and must be replayed
+ by that provider's context adapter. Portable checkpoints use a JSON
+ object containing ``messages`` (or ``message_wire_v2`` /
+ ``replay_messages``) and can be reconstructed here.
+ """
+
+ state = await store.load_active(ref)
+ checkpoint_wires: list[dict[str, Any]] = []
+ if state.checkpoint is not None:
+ payload = await store.get_blob(state.checkpoint.replay_payload_ref)
+ candidates: Any
+ if isinstance(payload, list):
+ candidates = payload
+ elif isinstance(payload, dict):
+ candidates = (
+ payload.get("messages")
+ or payload.get("message_wire_v2")
+ or payload.get("replay_messages")
+ )
+ else:
+ candidates = None
+ if not isinstance(candidates, list):
+ raise OpaqueCheckpointError(
+ "checkpoint replay payload is provider-native"
+ )
+ for candidate in candidates:
+ if not isinstance(candidate, dict):
+ raise OpaqueCheckpointError(
+ "portable checkpoint contains a non-message item"
+ )
+ checkpoint_wires.append(
+ dict(message_to_wire(message_from_wire(candidate)))
+ )
+ # A provider fork starts a fresh epoch from an explicit portable handoff.
+ # It is a replay boundary even before the first request snapshot exists.
+ handoff_wires: Optional[list[dict[str, Any]]] = None
+ handoff_sequence = 0
+ for event in reversed(state.tail):
+ if event.event_type != "provider_handoff" or not event.payload_ref:
+ continue
+ payload = await store.get_blob(event.payload_ref)
+ candidates = (
+ payload.get("messages") if isinstance(payload, dict) else None
+ )
+ if not isinstance(candidates, list):
+ continue
+ handoff_wires = []
+ for candidate in candidates:
+ if not isinstance(candidate, dict):
+ raise OpaqueCheckpointError(
+ "provider handoff contains a non-message item"
+ )
+ handoff_wires.append(
+ dict(message_to_wire(message_from_wire(candidate)))
+ )
+ handoff_sequence = event.sequence
+ break
+
+ # Request snapshots are exact render boundaries. They include system and
+ # user messages, which do not otherwise have a standalone transition in
+ # the journal. Start from the latest committed snapshot and apply only
+ # the assistant/tool transitions that followed it.
+ snapshot_wires: Optional[list[dict[str, Any]]] = None
+ snapshot_sequence = handoff_sequence
+ for event in reversed(state.tail):
+ if event.event_type != "request.snapshot" or not event.payload_ref:
+ continue
+ payload = await store.get_blob(event.payload_ref)
+ candidates = (
+ payload.get("messages") if isinstance(payload, dict) else None
+ )
+ if not isinstance(candidates, list):
+ continue
+ validated: list[dict[str, Any]] = []
+ for candidate in candidates:
+ if not isinstance(candidate, dict):
+ raise OpaqueCheckpointError(
+ "request snapshot contains a non-message item"
+ )
+ validated.append(
+ dict(message_to_wire(message_from_wire(candidate)))
+ )
+ snapshot_wires = validated
+ snapshot_sequence = event.sequence
+ break
+
+ wires = (
+ snapshot_wires
+ if snapshot_wires is not None
+ else (
+ handoff_wires
+ if handoff_wires is not None
+ else checkpoint_wires
+ )
+ )
+ wires.extend(
+ dict(event.message_wire_v2)
+ for event in state.tail
+ if event.sequence > snapshot_sequence
+ and event.message_wire_v2 is not None
+ )
+ return state.ref, wires
+
+
+async def reconstruct_messages(
+ store: AgentContextStore,
+ ref: AgentContextRef,
+) -> tuple[AgentContextRef, list[Message]]:
+ """Typed convenience wrapper over :func:`reconstruct_message_wire_v2`."""
+
+ current_ref, wires = await reconstruct_message_wire_v2(store, ref)
+ return current_ref, [message_from_wire(wire) for wire in wires]
+
+
+__all__ = [
+ "AgentContextTransitionWriter",
+ "OpaqueCheckpointError",
+ "reconstruct_message_wire_v2",
+ "reconstruct_messages",
+]
diff --git a/server/services/agent_context/store.py b/server/services/agent_context/store.py
new file mode 100644
index 00000000..b37446ff
--- /dev/null
+++ b/server/services/agent_context/store.py
@@ -0,0 +1,1899 @@
+"""Transactional, append-only storage for Agent Context V2."""
+
+from __future__ import annotations
+
+import hashlib
+import json
+from contextlib import asynccontextmanager
+from copy import deepcopy
+from datetime import datetime, timezone
+from typing import Any, AsyncIterator, Optional
+
+from sqlalchemy import delete, update
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.ext.asyncio import AsyncSession
+from sqlmodel import select
+
+from models.agent_context import (
+ AgentContextActiveState,
+ AgentContextAppendResult,
+ AgentContextBlobRecord,
+ AgentContextCheckpoint,
+ AgentContextCheckpointRecord,
+ AgentContextCompactionAttemptRecord,
+ AgentContextCompactionPlan,
+ AgentContextEvent,
+ AgentContextEventRecord,
+ AgentContextProviderBinding,
+ AgentContextProviderBindingRecord,
+ AgentContextRef,
+ AgentContextThreadRecord,
+ AgentContextThreadSummary,
+ ContextFidelity,
+ ContextThreadKind,
+)
+from models.database import RuntimeMutation
+from services.llm.protocol import (
+ MESSAGE_WIRE_VERSION,
+ message_from_wire,
+ message_to_wire,
+)
+
+
+ZERO_HASH = "0" * 64
+_MAX_CAS_ATTEMPTS = 12
+
+
+class AgentContextError(RuntimeError):
+ """Base class for stable Context-store failures."""
+
+
+class ContextNotFoundError(AgentContextError):
+ pass
+
+
+class ContextArchivedError(AgentContextError):
+ pass
+
+
+class StaleEpochError(AgentContextError):
+ pass
+
+
+class RevisionConflictError(AgentContextError):
+ pass
+
+
+class CompactionConflictError(AgentContextError):
+ pass
+
+
+class AgentContextStore:
+ """Durable journal/checkpoint repository.
+
+ ``database`` can be the application's :class:`core.database.Database` or
+ an ``async_sessionmaker``. Keeping this boundary tiny makes the same store
+ usable from in-process execution and Temporal activities.
+ """
+
+ def __init__(self, database: Any):
+ self._database = database
+
+ @asynccontextmanager
+ async def _session(self) -> AsyncIterator[AsyncSession]:
+ get_session = getattr(self._database, "get_session", None)
+ if callable(get_session):
+ async with get_session() as session:
+ yield session
+ return
+ async with self._database() as session:
+ yield session
+
+ async def resolve_thread(
+ self,
+ *,
+ workflow_id: str,
+ context_node_id: str,
+ generation: int,
+ session_id: Optional[str] = None,
+ delegated_task_id: Optional[str] = None,
+ execution_id: Optional[str] = None,
+ ) -> AgentContextRef:
+ """Resolve the RFC priority order into one isolated durable thread."""
+
+ kind, source_id = _resolve_thread_identity(
+ session_id=session_id,
+ delegated_task_id=delegated_task_id,
+ execution_id=execution_id,
+ )
+ thread_id = _thread_id(kind, source_id)
+ _validate_identity("workflow_id", workflow_id, 255)
+ _validate_identity("context_node_id", context_node_id, 255)
+ if generation < 0:
+ raise ValueError("generation must be non-negative")
+
+ for _ in range(_MAX_CAS_ATTEMPTS):
+ async with self._session() as session:
+ row = await _select_thread(
+ session,
+ workflow_id=workflow_id,
+ context_node_id=context_node_id,
+ generation=generation,
+ thread_id=thread_id,
+ )
+ if row is not None:
+ return _to_ref(row)
+ row = AgentContextThreadRecord(
+ workflow_id=workflow_id,
+ context_node_id=context_node_id,
+ generation=generation,
+ thread_id=thread_id,
+ thread_kind=kind,
+ last_event_hash=ZERO_HASH,
+ )
+ session.add(row)
+ try:
+ await session.commit()
+ await session.refresh(row)
+ return _to_ref(row)
+ except IntegrityError:
+ await session.rollback()
+ # Another worker resolved the same deterministic thread.
+ continue
+ raise RevisionConflictError("context_thread_resolution_conflict")
+
+ async def load_active(
+ self,
+ ref: AgentContextRef,
+ ) -> AgentContextActiveState:
+ """Load the active checkpoint and exact tail for the current epoch."""
+
+ async with self._session() as session:
+ thread = await self._require_thread(session, ref)
+ checkpoint_row: Optional[AgentContextCheckpointRecord] = None
+ source_row: Optional[AgentContextEventRecord] = None
+ if thread.active_checkpoint_id is not None:
+ checkpoint_row = await session.get(
+ AgentContextCheckpointRecord,
+ thread.active_checkpoint_id,
+ )
+ if (
+ checkpoint_row is None
+ or checkpoint_row.context_thread_id != thread.id
+ or checkpoint_row.epoch != thread.epoch
+ or checkpoint_row.status != "active"
+ or checkpoint_row.covers_through_sequence
+ != thread.active_checkpoint_sequence
+ or checkpoint_row.source_revision > thread.revision
+ ):
+ raise AgentContextError("active_checkpoint_invariant_broken")
+ source_result = await session.execute(
+ select(AgentContextEventRecord).where(
+ AgentContextEventRecord.context_thread_id
+ == thread.id,
+ AgentContextEventRecord.epoch == thread.epoch,
+ AgentContextEventRecord.sequence
+ == checkpoint_row.covers_through_sequence,
+ )
+ )
+ source_row = source_result.scalar_one_or_none()
+ if (
+ source_row is None
+ or source_row.payload_hash != checkpoint_row.source_hash
+ ):
+ raise AgentContextError(
+ "checkpoint_source_integrity_broken"
+ )
+ await _verified_blob_record(
+ session,
+ checkpoint_row.replay_payload_ref,
+ )
+ elif thread.active_checkpoint_sequence != 0:
+ raise AgentContextError("active_checkpoint_invariant_broken")
+ result = await session.execute(
+ select(AgentContextEventRecord)
+ .where(
+ AgentContextEventRecord.context_thread_id == thread.id,
+ AgentContextEventRecord.epoch == thread.epoch,
+ AgentContextEventRecord.sequence
+ > thread.active_checkpoint_sequence,
+ )
+ .order_by(AgentContextEventRecord.sequence)
+ )
+ tail_rows = list(result.scalars().all())
+ await _verify_active_event_chain(
+ session,
+ thread,
+ checkpoint_source=source_row,
+ tail=tail_rows,
+ )
+ tail = [_to_event(row) for row in tail_rows]
+ return AgentContextActiveState(
+ ref=_to_ref(thread),
+ checkpoint=(
+ _to_checkpoint(checkpoint_row)
+ if checkpoint_row is not None
+ else None
+ ),
+ tail=tail,
+ active_token_count=max(0, thread.active_token_count),
+ )
+
+ async def load_thread_summary(
+ self,
+ ref: AgentContextRef,
+ ) -> AgentContextThreadSummary:
+ """Load one exact thread's metadata without a bounded list scan."""
+
+ async with self._session() as session:
+ thread = await self._require_thread(session, ref)
+ return _to_thread_summary(thread)
+
+ async def list_threads(
+ self,
+ *,
+ workflow_id: str,
+ context_node_id: str,
+ generation: Optional[int] = None,
+ include_archived: bool = False,
+ limit: int = 100,
+ ) -> list[AgentContextThreadSummary]:
+ """List bounded thread metadata without loading journal payloads."""
+
+ _validate_identity("workflow_id", workflow_id, 255)
+ _validate_identity("context_node_id", context_node_id, 255)
+ limit = max(1, min(int(limit), 1000))
+ async with self._session() as session:
+ statement = select(AgentContextThreadRecord).where(
+ AgentContextThreadRecord.workflow_id == workflow_id,
+ AgentContextThreadRecord.context_node_id == context_node_id,
+ )
+ if generation is not None:
+ statement = statement.where(
+ AgentContextThreadRecord.generation == generation
+ )
+ if not include_archived:
+ statement = statement.where(
+ AgentContextThreadRecord.status == "active"
+ )
+ result = await session.execute(
+ statement.order_by(
+ AgentContextThreadRecord.generation.desc(),
+ AgentContextThreadRecord.updated_at.desc(),
+ AgentContextThreadRecord.id.desc(),
+ ).limit(limit)
+ )
+ return [_to_thread_summary(row) for row in result.scalars().all()]
+
+ async def iter_threads(
+ self,
+ *,
+ workflow_id: str,
+ context_node_id: str,
+ generation: Optional[int] = None,
+ include_archived: bool = False,
+ page_size: int = 250,
+ ) -> AsyncIterator[AgentContextThreadSummary]:
+ """Keyset-scan thread metadata without a fixed total-result cap.
+
+ Lifecycle operations mutate ``updated_at`` while consuming this
+ iterator, so pagination is deliberately anchored to the immutable
+ database row id instead of the presentation ordering used by
+ :meth:`list_threads`.
+ """
+
+ _validate_identity("workflow_id", workflow_id, 255)
+ _validate_identity("context_node_id", context_node_id, 255)
+ page_size = max(1, min(int(page_size), 1000))
+ after_id = 0
+ while True:
+ async with self._session() as session:
+ statement = select(AgentContextThreadRecord).where(
+ AgentContextThreadRecord.workflow_id == workflow_id,
+ AgentContextThreadRecord.context_node_id
+ == context_node_id,
+ AgentContextThreadRecord.id > after_id,
+ )
+ if generation is not None:
+ statement = statement.where(
+ AgentContextThreadRecord.generation == generation
+ )
+ if not include_archived:
+ statement = statement.where(
+ AgentContextThreadRecord.status == "active"
+ )
+ result = await session.execute(
+ statement.order_by(
+ AgentContextThreadRecord.id.asc()
+ ).limit(page_size)
+ )
+ rows = list(result.scalars().all())
+ if not rows:
+ return
+ for row in rows:
+ yield _to_thread_summary(row)
+ last_id = rows[-1].id
+ if last_id is None:
+ raise AgentContextError("context_thread_identity_missing")
+ after_id = int(last_id)
+ if len(rows) < page_size:
+ return
+
+ async def load_journal_page(
+ self,
+ ref: AgentContextRef,
+ *,
+ after_sequence: int = 0,
+ limit: int = 50,
+ epoch: Optional[int] = None,
+ ) -> tuple[list[AgentContextEvent], Optional[int]]:
+ """Load raw journal without mutating it; optionally narrow an epoch."""
+
+ after_sequence = max(0, int(after_sequence))
+ limit = max(1, min(int(limit), 200))
+ async with self._session() as session:
+ thread = await self._require_thread(session, ref)
+ conditions = [
+ AgentContextEventRecord.context_thread_id == thread.id,
+ AgentContextEventRecord.sequence > after_sequence,
+ ]
+ if epoch is not None:
+ conditions.append(AgentContextEventRecord.epoch == epoch)
+ result = await session.execute(
+ select(AgentContextEventRecord)
+ .where(*conditions)
+ .order_by(AgentContextEventRecord.sequence)
+ .limit(limit + 1)
+ )
+ rows = list(result.scalars().all())
+ has_more = len(rows) > limit
+ page = rows[:limit]
+ next_after = page[-1].sequence if has_more and page else None
+ return [_to_event(row) for row in page], next_after
+
+ async def list_checkpoints(
+ self,
+ ref: AgentContextRef,
+ *,
+ limit: int = 20,
+ ) -> list[AgentContextCheckpoint]:
+ """List current-epoch checkpoint metadata, newest first."""
+
+ limit = max(1, min(int(limit), 100))
+ async with self._session() as session:
+ thread = await self._require_thread(session, ref)
+ result = await session.execute(
+ select(AgentContextCheckpointRecord)
+ .where(
+ AgentContextCheckpointRecord.context_thread_id
+ == thread.id,
+ AgentContextCheckpointRecord.epoch == thread.epoch,
+ )
+ .order_by(
+ AgentContextCheckpointRecord.covers_through_sequence.desc(),
+ AgentContextCheckpointRecord.id.desc(),
+ )
+ .limit(limit)
+ )
+ return [
+ _to_checkpoint(row) for row in result.scalars().all()
+ ]
+
+ async def append_transition(
+ self,
+ ref: AgentContextRef,
+ *,
+ event_type: str,
+ operation_id: str,
+ message_wire_v2: Optional[dict[str, Any]] = None,
+ payload_ref: Optional[str] = None,
+ provider: Optional[str] = None,
+ expected_revision: Optional[int] = None,
+ ) -> AgentContextAppendResult:
+ """Append one transition with ordering, hash chaining, and fencing."""
+
+ _validate_identity("event_type", event_type, 100)
+ _validate_identity("operation_id", operation_id, 512)
+ if payload_ref is not None:
+ _validate_identity("payload_ref", payload_ref, 512)
+ if provider is not None:
+ _validate_identity("provider", provider, 100)
+ wire = _validate_message_wire(message_wire_v2)
+
+ for _ in range(_MAX_CAS_ATTEMPTS):
+ async with self._session() as session:
+ thread = await self._require_thread(
+ session,
+ ref,
+ permit_archived=True,
+ permit_stale_epoch=True,
+ )
+ existing = await _event_by_operation(
+ session,
+ thread_id=int(thread.id),
+ operation_id=operation_id,
+ )
+ if existing is not None:
+ _assert_idempotent_event_reuse(
+ existing,
+ event_type=event_type,
+ message_wire_v2=wire,
+ payload_ref=payload_ref,
+ provider=provider,
+ )
+ return AgentContextAppendResult(
+ ref=_to_ref(thread),
+ event=_to_event(existing),
+ applied=False,
+ )
+ self._assert_writable(thread, ref)
+ if (
+ expected_revision is not None
+ and thread.revision != expected_revision
+ ):
+ raise RevisionConflictError(
+ f"context_revision_conflict:"
+ f"{expected_revision}:{thread.revision}"
+ )
+
+ sequence = thread.next_sequence
+ previous_hash = thread.last_event_hash or ZERO_HASH
+ payload_hash = _event_hash(
+ sequence=sequence,
+ epoch=thread.epoch,
+ event_type=event_type,
+ message_wire_v2=wire,
+ payload_ref=payload_ref,
+ operation_id=operation_id,
+ provider=provider,
+ previous_hash=previous_hash,
+ )
+ now = datetime.now(timezone.utc)
+ event = AgentContextEventRecord(
+ context_thread_id=int(thread.id),
+ epoch=thread.epoch,
+ sequence=sequence,
+ event_type=event_type,
+ message_wire_v2=deepcopy(wire),
+ payload_ref=payload_ref,
+ operation_id=operation_id,
+ provider=provider,
+ previous_hash=previous_hash,
+ payload_hash=payload_hash,
+ created_at=now,
+ )
+ result = await session.execute(
+ update(AgentContextThreadRecord)
+ .where(
+ AgentContextThreadRecord.id == thread.id,
+ AgentContextThreadRecord.epoch == thread.epoch,
+ AgentContextThreadRecord.revision == thread.revision,
+ AgentContextThreadRecord.status == "active",
+ )
+ .values(
+ next_sequence=sequence + 1,
+ last_event_hash=payload_hash,
+ revision=thread.revision + 1,
+ provider=provider or thread.provider,
+ updated_at=now,
+ )
+ .execution_options(synchronize_session=False)
+ )
+ if result.rowcount != 1:
+ await session.rollback()
+ if expected_revision is not None:
+ raise RevisionConflictError(
+ "context_revision_conflict"
+ )
+ continue
+ try:
+ session.add(event)
+ await session.flush()
+ session.add(
+ RuntimeMutation(
+ mutation_id=operation_id,
+ resource_type="agent_context",
+ resource_id=str(thread.id),
+ operation="append_transition",
+ result={
+ "event_id": event.id,
+ "sequence": sequence,
+ "payload_hash": payload_hash,
+ },
+ )
+ )
+ await session.commit()
+ except IntegrityError:
+ await session.rollback()
+ continue
+ updated = await self._load_ref(ref)
+ return AgentContextAppendResult(
+ ref=updated,
+ event=_to_event(event),
+ applied=True,
+ )
+ raise RevisionConflictError("context_append_conflict")
+
+ async def record_active_pressure(
+ self,
+ ref: AgentContextRef,
+ *,
+ operation_id: str,
+ active_token_count: int,
+ ) -> AgentContextRef:
+ """Persist next-request context pressure without changing the journal.
+
+ Pressure is operational metadata, not lifetime billing. The update is
+ fenced by the thread revision and idempotent through
+ :class:`RuntimeMutation`; transcript or provider payloads never enter
+ that ledger.
+ """
+
+ _validate_identity("operation_id", operation_id, 512)
+ if active_token_count < 0:
+ raise ValueError("active_token_count must be non-negative")
+
+ for _ in range(_MAX_CAS_ATTEMPTS):
+ async with self._session() as session:
+ thread = await self._require_thread(
+ session,
+ ref,
+ permit_stale_epoch=True,
+ )
+ prior = await _runtime_mutation(
+ session,
+ thread_id=int(thread.id),
+ operation_id=operation_id,
+ operation="record_active_pressure",
+ )
+ if prior is not None:
+ return _to_ref(thread)
+ self._assert_writable(thread, ref)
+ now = datetime.now(timezone.utc)
+ result = await session.execute(
+ update(AgentContextThreadRecord)
+ .where(
+ AgentContextThreadRecord.id == thread.id,
+ AgentContextThreadRecord.epoch == thread.epoch,
+ AgentContextThreadRecord.revision == thread.revision,
+ AgentContextThreadRecord.status == "active",
+ )
+ .values(
+ active_token_count=active_token_count,
+ revision=thread.revision + 1,
+ updated_at=now,
+ )
+ .execution_options(synchronize_session=False)
+ )
+ if result.rowcount != 1:
+ await session.rollback()
+ continue
+ session.add(
+ RuntimeMutation(
+ mutation_id=operation_id,
+ resource_type="agent_context",
+ resource_id=str(thread.id),
+ operation="record_active_pressure",
+ result={"active_token_count": active_token_count},
+ )
+ )
+ try:
+ await session.commit()
+ except IntegrityError:
+ await session.rollback()
+ continue
+ return await self._load_ref(ref)
+ raise RevisionConflictError("context_pressure_update_conflict")
+
+ async def prepare_compaction(
+ self,
+ ref: AgentContextRef,
+ *,
+ operation_id: str,
+ provider: str,
+ strategy: str,
+ covers_through_sequence: int,
+ ) -> AgentContextCompactionPlan:
+ """Lease a stable committed prefix without changing active replay."""
+
+ _validate_identity("operation_id", operation_id, 512)
+ _validate_identity("provider", provider, 100)
+ _validate_identity("strategy", strategy, 100)
+ async with self._session() as session:
+ thread = await self._require_thread(session, ref)
+ self._assert_writable(thread, ref)
+ existing_result = await session.execute(
+ select(AgentContextCompactionAttemptRecord).where(
+ AgentContextCompactionAttemptRecord.context_thread_id
+ == thread.id,
+ AgentContextCompactionAttemptRecord.operation_id
+ == operation_id,
+ )
+ )
+ existing = existing_result.scalar_one_or_none()
+ if existing is not None:
+ return await _to_plan(session, thread, existing)
+
+ if (
+ covers_through_sequence
+ <= thread.active_checkpoint_sequence
+ or covers_through_sequence >= thread.next_sequence
+ ):
+ raise ValueError("invalid_compaction_coverage")
+ source_result = await session.execute(
+ select(AgentContextEventRecord).where(
+ AgentContextEventRecord.context_thread_id == thread.id,
+ AgentContextEventRecord.epoch == thread.epoch,
+ AgentContextEventRecord.sequence
+ == covers_through_sequence,
+ )
+ )
+ source = source_result.scalar_one_or_none()
+ if source is None:
+ raise ValueError("compaction_source_event_not_found")
+ attempt = AgentContextCompactionAttemptRecord(
+ context_thread_id=int(thread.id),
+ epoch=thread.epoch,
+ provider=provider,
+ strategy=strategy,
+ covers_through_sequence=covers_through_sequence,
+ base_checkpoint_id=thread.active_checkpoint_id,
+ base_checkpoint_sequence=thread.active_checkpoint_sequence,
+ source_revision=thread.revision,
+ source_hash=source.payload_hash,
+ operation_id=operation_id,
+ )
+ session.add(attempt)
+ try:
+ await session.flush()
+ session.add(
+ RuntimeMutation(
+ mutation_id=operation_id,
+ resource_type="agent_context",
+ resource_id=str(thread.id),
+ operation="prepare_compaction",
+ result={
+ "attempt_id": attempt.id,
+ "source_hash": source.payload_hash,
+ },
+ )
+ )
+ await session.commit()
+ except IntegrityError:
+ await session.rollback()
+ # The operation can only race with an identical preparation.
+ retry = await session.execute(
+ select(AgentContextCompactionAttemptRecord).where(
+ AgentContextCompactionAttemptRecord.context_thread_id
+ == thread.id,
+ AgentContextCompactionAttemptRecord.operation_id
+ == operation_id,
+ )
+ )
+ attempt = retry.scalar_one_or_none()
+ if attempt is None:
+ raise RevisionConflictError(
+ "compaction_prepare_conflict"
+ )
+ return await _to_plan(session, thread, attempt)
+
+ async def commit_checkpoint(
+ self,
+ ref: AgentContextRef,
+ *,
+ attempt_id: int,
+ operation_id: str,
+ replay_payload_ref: str,
+ active_token_count: int,
+ ) -> AgentContextCheckpoint:
+ """CAS-activate a validated candidate, retaining concurrent tail."""
+
+ _validate_identity("operation_id", operation_id, 512)
+ _validate_identity("replay_payload_ref", replay_payload_ref, 512)
+ if active_token_count < 0:
+ raise ValueError("active_token_count must be non-negative")
+
+ for _ in range(_MAX_CAS_ATTEMPTS):
+ async with self._session() as session:
+ thread = await self._require_thread(
+ session,
+ ref,
+ permit_stale_epoch=True,
+ )
+ existing_result = await session.execute(
+ select(AgentContextCheckpointRecord).where(
+ AgentContextCheckpointRecord.context_thread_id
+ == thread.id,
+ AgentContextCheckpointRecord.operation_id
+ == operation_id,
+ )
+ )
+ existing = existing_result.scalar_one_or_none()
+ if existing is not None:
+ return _to_checkpoint(existing)
+ self._assert_writable(thread, ref)
+ attempt = await session.get(
+ AgentContextCompactionAttemptRecord,
+ attempt_id,
+ )
+ if (
+ attempt is None
+ or attempt.context_thread_id != thread.id
+ ):
+ raise ContextNotFoundError(
+ "compaction_attempt_not_found"
+ )
+ if attempt.status == "committed" and attempt.checkpoint_id:
+ checkpoint = await session.get(
+ AgentContextCheckpointRecord,
+ attempt.checkpoint_id,
+ )
+ if checkpoint is None:
+ raise AgentContextError(
+ "committed_checkpoint_missing"
+ )
+ return _to_checkpoint(checkpoint)
+ if attempt.status != "prepared":
+ raise CompactionConflictError(
+ f"compaction_attempt_{attempt.status}"
+ )
+ if attempt.epoch != thread.epoch:
+ raise StaleEpochError("compaction_epoch_fenced")
+ if (
+ attempt.base_checkpoint_id != thread.active_checkpoint_id
+ or attempt.base_checkpoint_sequence
+ != thread.active_checkpoint_sequence
+ ):
+ raise CompactionConflictError(
+ "active_checkpoint_changed"
+ )
+ source_result = await session.execute(
+ select(AgentContextEventRecord).where(
+ AgentContextEventRecord.context_thread_id == thread.id,
+ AgentContextEventRecord.epoch == thread.epoch,
+ AgentContextEventRecord.sequence
+ == attempt.covers_through_sequence,
+ )
+ )
+ source = source_result.scalar_one_or_none()
+ if (
+ source is None
+ or source.payload_hash != attempt.source_hash
+ ):
+ raise CompactionConflictError(
+ "compaction_source_changed"
+ )
+
+ checkpoint = AgentContextCheckpointRecord(
+ context_thread_id=int(thread.id),
+ epoch=thread.epoch,
+ provider=attempt.provider,
+ strategy=attempt.strategy,
+ covers_through_sequence=attempt.covers_through_sequence,
+ replay_payload_ref=replay_payload_ref,
+ active_token_count=active_token_count,
+ source_revision=attempt.source_revision,
+ source_hash=attempt.source_hash,
+ operation_id=operation_id,
+ status="active",
+ )
+ session.add(checkpoint)
+ await session.flush()
+ now = datetime.now(timezone.utc)
+ result = await session.execute(
+ update(AgentContextThreadRecord)
+ .where(
+ AgentContextThreadRecord.id == thread.id,
+ AgentContextThreadRecord.epoch == thread.epoch,
+ AgentContextThreadRecord.revision == thread.revision,
+ AgentContextThreadRecord.active_checkpoint_id
+ == attempt.base_checkpoint_id,
+ AgentContextThreadRecord.active_checkpoint_sequence
+ == attempt.base_checkpoint_sequence,
+ )
+ .values(
+ active_checkpoint_id=checkpoint.id,
+ active_checkpoint_sequence=(
+ attempt.covers_through_sequence
+ ),
+ active_token_count=active_token_count,
+ revision=thread.revision + 1,
+ updated_at=now,
+ )
+ .execution_options(synchronize_session=False)
+ )
+ if result.rowcount != 1:
+ await session.rollback()
+ continue
+ if attempt.base_checkpoint_id is not None:
+ await session.execute(
+ update(AgentContextCheckpointRecord)
+ .where(
+ AgentContextCheckpointRecord.id
+ == attempt.base_checkpoint_id
+ )
+ .values(status="superseded")
+ )
+ attempt.status = "committed"
+ attempt.checkpoint_id = checkpoint.id
+ attempt.completed_at = now
+ session.add(
+ RuntimeMutation(
+ mutation_id=operation_id,
+ resource_type="agent_context",
+ resource_id=str(thread.id),
+ operation="commit_checkpoint",
+ result={
+ "checkpoint_id": checkpoint.id,
+ "source_hash": attempt.source_hash,
+ },
+ )
+ )
+ try:
+ await session.commit()
+ except IntegrityError:
+ await session.rollback()
+ continue
+ return _to_checkpoint(checkpoint)
+ raise CompactionConflictError("checkpoint_activation_conflict")
+
+ async def fail_compaction(
+ self,
+ *,
+ attempt_id: int,
+ error_code: str,
+ ) -> None:
+ """Record a failed candidate without touching active replay state."""
+
+ _validate_identity("error_code", error_code, 255)
+ async with self._session() as session:
+ attempt = await session.get(
+ AgentContextCompactionAttemptRecord,
+ attempt_id,
+ )
+ if attempt is None:
+ raise ContextNotFoundError("compaction_attempt_not_found")
+ if attempt.status == "prepared":
+ attempt.status = "failed"
+ attempt.error_code = error_code
+ attempt.completed_at = datetime.now(timezone.utc)
+ await session.commit()
+
+ async def start_epoch(
+ self,
+ ref: AgentContextRef,
+ *,
+ operation_id: str,
+ provider: Optional[str] = None,
+ handoff_payload_ref: Optional[str] = None,
+ ) -> AgentContextRef:
+ """Rotate an epoch and fence every writer holding the old ref."""
+
+ _validate_identity("operation_id", operation_id, 512)
+ if provider is not None:
+ _validate_identity("provider", provider, 100)
+ if handoff_payload_ref is not None:
+ _validate_identity(
+ "handoff_payload_ref",
+ handoff_payload_ref,
+ 512,
+ )
+ request_hash = _mutation_request_hash(
+ "start_epoch",
+ {
+ "provider": provider,
+ "handoff_payload_ref": handoff_payload_ref,
+ },
+ )
+ async with self._session() as session:
+ thread = await self._require_thread(
+ session,
+ ref,
+ permit_archived=True,
+ permit_stale_epoch=True,
+ )
+ prior = await _runtime_mutation(
+ session,
+ thread_id=int(thread.id),
+ operation_id=operation_id,
+ operation="start_epoch",
+ )
+ if prior is not None:
+ _assert_idempotent_mutation_reuse(
+ prior,
+ request_hash=request_hash,
+ )
+ return _to_ref(thread)
+ self._assert_writable(thread, ref)
+ now = datetime.now(timezone.utc)
+ next_epoch = thread.epoch + 1
+ next_revision = thread.revision + 1
+ next_sequence = thread.next_sequence
+ last_hash = thread.last_event_hash
+ handoff_event: Optional[AgentContextEventRecord] = None
+ await session.execute(
+ update(AgentContextProviderBindingRecord)
+ .where(
+ AgentContextProviderBindingRecord.context_thread_id
+ == thread.id,
+ AgentContextProviderBindingRecord.epoch == thread.epoch,
+ AgentContextProviderBindingRecord.status == "active",
+ )
+ .values(status="archived")
+ )
+ await session.execute(
+ update(AgentContextCheckpointRecord)
+ .where(
+ AgentContextCheckpointRecord.context_thread_id
+ == thread.id,
+ AgentContextCheckpointRecord.epoch == thread.epoch,
+ AgentContextCheckpointRecord.status == "active",
+ )
+ .values(status="archived")
+ )
+ await session.execute(
+ update(AgentContextCompactionAttemptRecord)
+ .where(
+ AgentContextCompactionAttemptRecord.context_thread_id
+ == thread.id,
+ AgentContextCompactionAttemptRecord.epoch == thread.epoch,
+ AgentContextCompactionAttemptRecord.status == "prepared",
+ )
+ .values(
+ status="stale",
+ error_code="epoch_rotated",
+ completed_at=now,
+ )
+ )
+ if handoff_payload_ref is not None:
+ handoff_operation = f"{operation_id}:handoff"
+ handoff_hash = _event_hash(
+ sequence=next_sequence,
+ epoch=next_epoch,
+ event_type="provider_handoff",
+ message_wire_v2=None,
+ payload_ref=handoff_payload_ref,
+ operation_id=handoff_operation,
+ provider=provider,
+ previous_hash=last_hash,
+ )
+ handoff_event = AgentContextEventRecord(
+ context_thread_id=int(thread.id),
+ epoch=next_epoch,
+ sequence=next_sequence,
+ event_type="provider_handoff",
+ payload_ref=handoff_payload_ref,
+ operation_id=handoff_operation,
+ provider=provider,
+ previous_hash=last_hash,
+ payload_hash=handoff_hash,
+ )
+ next_sequence += 1
+ next_revision += 1
+ last_hash = handoff_hash
+ result = await session.execute(
+ update(AgentContextThreadRecord)
+ .where(
+ AgentContextThreadRecord.id == thread.id,
+ AgentContextThreadRecord.epoch == thread.epoch,
+ AgentContextThreadRecord.revision == thread.revision,
+ AgentContextThreadRecord.status == "active",
+ )
+ .values(
+ epoch=next_epoch,
+ revision=next_revision,
+ next_sequence=next_sequence,
+ last_event_hash=last_hash,
+ active_checkpoint_id=None,
+ active_checkpoint_sequence=0,
+ active_token_count=0,
+ provider=provider,
+ updated_at=now,
+ )
+ .execution_options(synchronize_session=False)
+ )
+ if result.rowcount != 1:
+ await session.rollback()
+ raise RevisionConflictError("epoch_rotation_conflict")
+ if handoff_event is not None:
+ session.add(handoff_event)
+ session.add(
+ RuntimeMutation(
+ mutation_id=operation_id,
+ resource_type="agent_context",
+ resource_id=str(thread.id),
+ operation="start_epoch",
+ result={
+ "epoch": next_epoch,
+ "last_event_hash": last_hash,
+ "request_hash": request_hash,
+ },
+ )
+ )
+ await session.commit()
+ return await self._load_ref(ref)
+
+ async def fork_provider(
+ self,
+ ref: AgentContextRef,
+ *,
+ provider: str,
+ operation_id: str,
+ portable_handoff_ref: str,
+ ) -> AgentContextRef:
+ """Start a provider epoch with a portable handoff boundary."""
+
+ return await self.start_epoch(
+ ref,
+ operation_id=operation_id,
+ provider=provider,
+ handoff_payload_ref=portable_handoff_ref,
+ )
+
+ async def bind_provider(
+ self,
+ ref: AgentContextRef,
+ *,
+ provider: str,
+ binding_type: str,
+ binding: Any,
+ operation_id: str,
+ fidelity: ContextFidelity = "provider_bound",
+ ) -> AgentContextProviderBinding:
+ """Persist an opaque binding in blob storage and expose only its ref."""
+
+ _validate_identity("provider", provider, 100)
+ _validate_identity("binding_type", binding_type, 100)
+ _validate_identity("operation_id", operation_id, 512)
+ binding_ref = await self.put_blob(binding)
+ binding_hash = binding_ref.removeprefix("sha256:")
+ request_hash = _mutation_request_hash(
+ "bind_provider",
+ {
+ "provider": provider,
+ "binding_type": binding_type,
+ "binding_hash": binding_hash,
+ "fidelity": fidelity,
+ },
+ )
+ async with self._session() as session:
+ thread = await self._require_thread(
+ session,
+ ref,
+ permit_stale_epoch=True,
+ )
+ result = await session.execute(
+ select(AgentContextProviderBindingRecord).where(
+ AgentContextProviderBindingRecord.context_thread_id
+ == thread.id,
+ AgentContextProviderBindingRecord.operation_id
+ == operation_id,
+ )
+ )
+ record = result.scalar_one_or_none()
+ if record is not None:
+ _assert_idempotent_binding_reuse(
+ record,
+ provider=provider,
+ binding_type=binding_type,
+ binding_hash=binding_hash,
+ fidelity=fidelity,
+ )
+ prior = await _runtime_mutation(
+ session,
+ thread_id=int(thread.id),
+ operation_id=operation_id,
+ operation="bind_provider",
+ )
+ if prior is None:
+ raise AgentContextError(
+ "context_binding_idempotency_invariant_broken"
+ )
+ _assert_idempotent_mutation_reuse(
+ prior,
+ request_hash=request_hash,
+ )
+ return _to_binding(record)
+ self._assert_writable(thread, ref)
+ if record is None:
+ record = AgentContextProviderBindingRecord(
+ context_thread_id=int(thread.id),
+ epoch=thread.epoch,
+ provider=provider,
+ binding_type=binding_type,
+ binding_ref=binding_ref,
+ binding_hash=binding_hash,
+ fidelity=fidelity,
+ operation_id=operation_id,
+ )
+ session.add(record)
+ await session.flush()
+ session.add(
+ RuntimeMutation(
+ mutation_id=operation_id,
+ resource_type="agent_context",
+ resource_id=str(thread.id),
+ operation="bind_provider",
+ result={
+ "binding_id": record.id,
+ "binding_hash": binding_hash,
+ "request_hash": request_hash,
+ },
+ )
+ )
+ await session.commit()
+ return _to_binding(record)
+
+ async def load_provider_bindings(
+ self,
+ ref: AgentContextRef,
+ *,
+ provider: Optional[str] = None,
+ ) -> list[AgentContextProviderBinding]:
+ async with self._session() as session:
+ thread = await self._require_thread(session, ref)
+ statement = select(AgentContextProviderBindingRecord).where(
+ AgentContextProviderBindingRecord.context_thread_id
+ == thread.id,
+ AgentContextProviderBindingRecord.epoch == thread.epoch,
+ AgentContextProviderBindingRecord.status == "active",
+ )
+ if provider is not None:
+ statement = statement.where(
+ AgentContextProviderBindingRecord.provider == provider
+ )
+ result = await session.execute(
+ statement.order_by(AgentContextProviderBindingRecord.id)
+ )
+ return [_to_binding(row) for row in result.scalars().all()]
+
+ async def put_blob(
+ self,
+ payload: Any,
+ *,
+ media_type: str = "application/json",
+ ) -> str:
+ """Store exact JSON or bytes by SHA-256 and return an opaque ref."""
+
+ _validate_identity("media_type", media_type, 100)
+ if isinstance(payload, (bytes, bytearray, memoryview)):
+ binary = bytes(payload)
+ encoded = binary
+ json_payload = None
+ else:
+ encoded = _canonical_json(payload)
+ binary = None
+ json_payload = deepcopy(payload)
+ digest = hashlib.sha256(encoded).hexdigest()
+ async with self._session() as session:
+ existing = await session.get(AgentContextBlobRecord, digest)
+ if existing is None:
+ session.add(
+ AgentContextBlobRecord(
+ payload_hash=digest,
+ media_type=media_type,
+ json_payload=json_payload,
+ binary_payload=binary,
+ byte_size=len(encoded),
+ )
+ )
+ try:
+ await session.commit()
+ except IntegrityError:
+ await session.rollback()
+ return f"sha256:{digest}"
+
+ async def get_blob(self, payload_ref: str) -> Any:
+ async with self._session() as session:
+ row = await _verified_blob_record(session, payload_ref)
+ if row.binary_payload is not None:
+ return bytes(row.binary_payload)
+ return deepcopy(row.json_payload)
+
+ async def archive(
+ self,
+ ref: AgentContextRef,
+ *,
+ operation_id: str,
+ ) -> AgentContextRef:
+ """Archive one thread, rotating its epoch to fence late writes."""
+
+ _validate_identity("operation_id", operation_id, 512)
+ async with self._session() as session:
+ thread = await self._require_thread(
+ session,
+ ref,
+ permit_archived=True,
+ permit_stale_epoch=True,
+ )
+ prior = await _runtime_mutation(
+ session,
+ thread_id=int(thread.id),
+ operation_id=operation_id,
+ operation="archive",
+ )
+ if prior is not None:
+ return _to_ref(thread)
+ if thread.status == "archived":
+ return _to_ref(thread)
+ self._assert_writable(thread, ref)
+ now = datetime.now(timezone.utc)
+ result = await session.execute(
+ update(AgentContextThreadRecord)
+ .where(
+ AgentContextThreadRecord.id == thread.id,
+ AgentContextThreadRecord.epoch == thread.epoch,
+ AgentContextThreadRecord.revision == thread.revision,
+ )
+ .values(
+ status="archived",
+ epoch=thread.epoch + 1,
+ revision=thread.revision + 1,
+ active_checkpoint_id=None,
+ active_checkpoint_sequence=0,
+ active_token_count=0,
+ provider=None,
+ archived_at=now,
+ updated_at=now,
+ )
+ .execution_options(synchronize_session=False)
+ )
+ if result.rowcount != 1:
+ await session.rollback()
+ raise RevisionConflictError("context_archive_conflict")
+ await session.execute(
+ update(AgentContextProviderBindingRecord)
+ .where(
+ AgentContextProviderBindingRecord.context_thread_id
+ == thread.id,
+ AgentContextProviderBindingRecord.status == "active",
+ )
+ .values(status="archived")
+ )
+ await session.execute(
+ update(AgentContextCheckpointRecord)
+ .where(
+ AgentContextCheckpointRecord.context_thread_id
+ == thread.id,
+ AgentContextCheckpointRecord.status == "active",
+ )
+ .values(status="archived")
+ )
+ await session.execute(
+ update(AgentContextCompactionAttemptRecord)
+ .where(
+ AgentContextCompactionAttemptRecord.context_thread_id
+ == thread.id,
+ AgentContextCompactionAttemptRecord.status == "prepared",
+ )
+ .values(
+ status="stale",
+ error_code="context_archived",
+ completed_at=now,
+ )
+ )
+ session.add(
+ RuntimeMutation(
+ mutation_id=operation_id,
+ resource_type="agent_context",
+ resource_id=str(thread.id),
+ operation="archive",
+ result={"archived_epoch": thread.epoch},
+ )
+ )
+ await session.commit()
+ return await self._load_ref(ref)
+
+ async def archive_context(
+ self,
+ *,
+ workflow_id: str,
+ context_node_id: str,
+ generation: Optional[int] = None,
+ operation_id: str,
+ ) -> list[AgentContextRef]:
+ """Archive Context threads, across every generation by default."""
+
+ _validate_identity("operation_id", operation_id, 500)
+ async with self._session() as session:
+ statement = select(AgentContextThreadRecord).where(
+ AgentContextThreadRecord.workflow_id == workflow_id,
+ AgentContextThreadRecord.context_node_id == context_node_id,
+ )
+ if generation is not None:
+ statement = statement.where(
+ AgentContextThreadRecord.generation == generation
+ )
+ result = await session.execute(
+ statement.order_by(
+ AgentContextThreadRecord.generation,
+ AgentContextThreadRecord.thread_id,
+ AgentContextThreadRecord.id,
+ )
+ )
+ refs = [_to_ref(row) for row in result.scalars().all()]
+ archived: list[AgentContextRef] = []
+ for index, ref in enumerate(refs):
+ archived.append(
+ await self.archive(
+ ref,
+ operation_id=f"{operation_id}:{index}",
+ )
+ )
+ return archived
+
+ async def purge(
+ self,
+ *,
+ workflow_id: str,
+ context_node_id: str,
+ generation: Optional[int] = None,
+ ) -> int:
+ """Permanently delete a Context journal.
+
+ Hash-addressed blobs are intentionally retained here. A global
+ mark-and-sweep performed immediately after this transaction can race
+ with another transaction that has stored a blob but has not yet
+ committed its reference. Blob collection belongs in a separately
+ coordinated maintenance job with an age/grace-period fence.
+ """
+
+ async with self._session() as session:
+ statement = select(AgentContextThreadRecord).where(
+ AgentContextThreadRecord.workflow_id == workflow_id,
+ AgentContextThreadRecord.context_node_id == context_node_id,
+ )
+ if generation is not None:
+ statement = statement.where(
+ AgentContextThreadRecord.generation == generation
+ )
+ result = await session.execute(statement)
+ thread_ids = [
+ int(row.id)
+ for row in result.scalars().all()
+ if row.id is not None
+ ]
+ if not thread_ids:
+ return 0
+ for model in (
+ AgentContextEventRecord,
+ AgentContextCheckpointRecord,
+ AgentContextProviderBindingRecord,
+ AgentContextCompactionAttemptRecord,
+ ):
+ await session.execute(
+ delete(model).where(
+ model.context_thread_id.in_(thread_ids)
+ )
+ )
+ await session.execute(
+ delete(RuntimeMutation).where(
+ RuntimeMutation.resource_type == "agent_context",
+ RuntimeMutation.resource_id.in_(
+ [str(value) for value in thread_ids]
+ ),
+ )
+ )
+ await session.execute(
+ delete(AgentContextThreadRecord).where(
+ AgentContextThreadRecord.id.in_(thread_ids)
+ )
+ )
+ await session.commit()
+ return len(thread_ids)
+
+ async def _require_thread(
+ self,
+ session: AsyncSession,
+ ref: AgentContextRef,
+ *,
+ permit_archived: bool = False,
+ permit_stale_epoch: bool = False,
+ ) -> AgentContextThreadRecord:
+ thread = await _select_thread(
+ session,
+ workflow_id=ref.workflow_id,
+ context_node_id=ref.context_node_id,
+ generation=ref.generation,
+ thread_id=ref.thread_id,
+ )
+ if thread is None:
+ raise ContextNotFoundError("agent_context_thread_not_found")
+ if thread.epoch != ref.epoch and not permit_stale_epoch:
+ raise StaleEpochError(
+ f"context_epoch_fenced:{ref.epoch}:{thread.epoch}"
+ )
+ if thread.status == "archived" and not permit_archived:
+ raise ContextArchivedError("agent_context_thread_archived")
+ return thread
+
+ def _assert_writable(
+ self,
+ thread: AgentContextThreadRecord,
+ ref: AgentContextRef,
+ ) -> None:
+ if thread.epoch != ref.epoch:
+ raise StaleEpochError("context_epoch_fenced")
+ if thread.status != "active":
+ raise ContextArchivedError("agent_context_thread_archived")
+
+ async def _load_ref(self, ref: AgentContextRef) -> AgentContextRef:
+ async with self._session() as session:
+ thread = await _select_thread(
+ session,
+ workflow_id=ref.workflow_id,
+ context_node_id=ref.context_node_id,
+ generation=ref.generation,
+ thread_id=ref.thread_id,
+ )
+ if thread is None:
+ raise ContextNotFoundError(
+ "agent_context_thread_not_found"
+ )
+ return _to_ref(thread)
+
+
+def _resolve_thread_identity(
+ *,
+ session_id: Optional[str],
+ delegated_task_id: Optional[str],
+ execution_id: Optional[str],
+) -> tuple[ContextThreadKind, str]:
+ if session_id is not None and session_id.strip():
+ return "session", session_id.strip()
+ if delegated_task_id is not None and delegated_task_id.strip():
+ return "task", delegated_task_id.strip()
+ if execution_id is not None and execution_id.strip():
+ return "execution", execution_id.strip()
+ raise ValueError(
+ "execution_id is required when no explicit session or delegated "
+ "task is present"
+ )
+
+
+def _thread_id(kind: ContextThreadKind, source_id: str) -> str:
+ _validate_identity(f"{kind}_id", source_id, 4096)
+ raw = f"{kind}:{source_id}"
+ if len(raw) <= 512:
+ return raw
+ return f"{kind}:sha256:{hashlib.sha256(source_id.encode()).hexdigest()}"
+
+
+def _validate_identity(name: str, value: str, max_length: int) -> None:
+ if not isinstance(value, str) or not value.strip():
+ raise ValueError(f"{name} must be a non-empty string")
+ if len(value) > max_length:
+ raise ValueError(f"{name} exceeds {max_length} characters")
+
+
+async def _select_thread(
+ session: AsyncSession,
+ *,
+ workflow_id: str,
+ context_node_id: str,
+ generation: int,
+ thread_id: str,
+) -> Optional[AgentContextThreadRecord]:
+ result = await session.execute(
+ select(AgentContextThreadRecord).where(
+ AgentContextThreadRecord.workflow_id == workflow_id,
+ AgentContextThreadRecord.context_node_id == context_node_id,
+ AgentContextThreadRecord.generation == generation,
+ AgentContextThreadRecord.thread_id == thread_id,
+ )
+ )
+ return result.scalar_one_or_none()
+
+
+async def _event_by_operation(
+ session: AsyncSession,
+ *,
+ thread_id: int,
+ operation_id: str,
+) -> Optional[AgentContextEventRecord]:
+ result = await session.execute(
+ select(AgentContextEventRecord).where(
+ AgentContextEventRecord.context_thread_id == thread_id,
+ AgentContextEventRecord.operation_id == operation_id,
+ )
+ )
+ return result.scalar_one_or_none()
+
+
+def _assert_idempotent_event_reuse(
+ existing: AgentContextEventRecord,
+ *,
+ event_type: str,
+ message_wire_v2: Optional[dict[str, Any]],
+ payload_ref: Optional[str],
+ provider: Optional[str],
+) -> None:
+ """Accept an operation retry only when it is the exact same transition."""
+
+ stored_hash = _event_record_hash(existing)
+ retry_hash = _event_hash(
+ sequence=existing.sequence,
+ epoch=existing.epoch,
+ event_type=event_type,
+ message_wire_v2=message_wire_v2,
+ payload_ref=payload_ref,
+ operation_id=existing.operation_id,
+ provider=provider,
+ previous_hash=existing.previous_hash,
+ )
+ if (
+ existing.event_type != event_type
+ or existing.message_wire_v2 != message_wire_v2
+ or existing.payload_ref != payload_ref
+ or existing.provider != provider
+ or stored_hash != existing.payload_hash
+ or retry_hash != existing.payload_hash
+ ):
+ raise AgentContextError("context_operation_id_reuse_mismatch")
+
+
+async def _runtime_mutation(
+ session: AsyncSession,
+ *,
+ thread_id: int,
+ operation_id: str,
+ operation: str,
+) -> Optional[RuntimeMutation]:
+ result = await session.execute(
+ select(RuntimeMutation).where(
+ RuntimeMutation.resource_type == "agent_context",
+ RuntimeMutation.resource_id == str(thread_id),
+ RuntimeMutation.mutation_id == operation_id,
+ RuntimeMutation.operation == operation,
+ )
+ )
+ return result.scalar_one_or_none()
+
+
+def _mutation_request_hash(
+ operation: str,
+ inputs: dict[str, Any],
+) -> str:
+ """Hash the exact server-controlled inputs for one idempotent mutation."""
+
+ return hashlib.sha256(
+ _canonical_json(
+ {
+ "operation": operation,
+ "inputs": inputs,
+ }
+ )
+ ).hexdigest()
+
+
+def _assert_idempotent_mutation_reuse(
+ mutation: RuntimeMutation,
+ *,
+ request_hash: str,
+) -> None:
+ result = mutation.result if isinstance(mutation.result, dict) else {}
+ if result.get("request_hash") != request_hash:
+ raise AgentContextError("context_operation_id_reuse_mismatch")
+
+
+def _assert_idempotent_binding_reuse(
+ record: AgentContextProviderBindingRecord,
+ *,
+ provider: str,
+ binding_type: str,
+ binding_hash: str,
+ fidelity: ContextFidelity,
+) -> None:
+ if (
+ record.provider != provider
+ or record.binding_type != binding_type
+ or record.binding_hash != binding_hash
+ or record.binding_ref != f"sha256:{binding_hash}"
+ or record.fidelity != fidelity
+ ):
+ raise AgentContextError("context_operation_id_reuse_mismatch")
+
+
+def _validate_message_wire(
+ value: Optional[dict[str, Any]],
+) -> Optional[dict[str, Any]]:
+ if value is None:
+ return None
+ if not isinstance(value, dict):
+ raise TypeError("message_wire_v2 must be a JSON object")
+ if value.get("version") != MESSAGE_WIRE_VERSION:
+ raise ValueError(
+ f"Context requires MessageWireV2 version {MESSAGE_WIRE_VERSION}"
+ )
+ # Round-tripping through the existing native codec enforces JSON-safe
+ # provider state and preserves ordered blocks/tool calls.
+ return dict(message_to_wire(message_from_wire(value)))
+
+
+def _event_hash(
+ *,
+ sequence: int,
+ epoch: int,
+ event_type: str,
+ message_wire_v2: Optional[dict[str, Any]],
+ payload_ref: Optional[str],
+ operation_id: str,
+ provider: Optional[str],
+ previous_hash: str,
+) -> str:
+ body = {
+ "sequence": sequence,
+ "epoch": epoch,
+ "event_type": event_type,
+ "message_wire_v2": message_wire_v2,
+ "payload_ref": payload_ref,
+ "operation_id": operation_id,
+ "provider": provider,
+ "previous_hash": previous_hash,
+ }
+ return hashlib.sha256(_canonical_json(body)).hexdigest()
+
+
+def _event_record_hash(row: AgentContextEventRecord) -> str:
+ return _event_hash(
+ sequence=row.sequence,
+ epoch=row.epoch,
+ event_type=row.event_type,
+ message_wire_v2=row.message_wire_v2,
+ payload_ref=row.payload_ref,
+ operation_id=row.operation_id,
+ provider=row.provider,
+ previous_hash=row.previous_hash,
+ )
+
+
+async def _verify_active_event_chain(
+ session: AsyncSession,
+ thread: AgentContextThreadRecord,
+ *,
+ checkpoint_source: Optional[AgentContextEventRecord],
+ tail: list[AgentContextEventRecord],
+) -> None:
+ """Validate the bounded replay chain and the thread's journal head."""
+
+ anchor = checkpoint_source or (tail[0] if tail else None)
+ predecessor: Optional[AgentContextEventRecord] = None
+ if anchor is not None and anchor.sequence > 1:
+ predecessor_result = await session.execute(
+ select(AgentContextEventRecord).where(
+ AgentContextEventRecord.context_thread_id == thread.id,
+ AgentContextEventRecord.sequence == anchor.sequence - 1,
+ )
+ )
+ predecessor = predecessor_result.scalar_one_or_none()
+ if predecessor is None:
+ raise AgentContextError("context_event_sequence_integrity_broken")
+
+ if checkpoint_source is not None:
+ _verify_event_record(
+ checkpoint_source,
+ expected_sequence=checkpoint_source.sequence,
+ expected_previous_hash=(
+ predecessor.payload_hash if predecessor is not None else ZERO_HASH
+ ),
+ )
+ expected_sequence = checkpoint_source.sequence + 1
+ previous_hash = checkpoint_source.payload_hash
+ elif tail:
+ expected_sequence = tail[0].sequence
+ previous_hash = (
+ predecessor.payload_hash if predecessor is not None else ZERO_HASH
+ )
+ if predecessor is None and expected_sequence != 1:
+ raise AgentContextError("context_event_sequence_integrity_broken")
+ else:
+ expected_sequence = thread.next_sequence
+ previous_hash = thread.last_event_hash
+
+ for row in tail:
+ _verify_event_record(
+ row,
+ expected_sequence=expected_sequence,
+ expected_previous_hash=previous_hash,
+ )
+ expected_sequence += 1
+ previous_hash = row.payload_hash
+
+ latest_result = await session.execute(
+ select(AgentContextEventRecord)
+ .where(AgentContextEventRecord.context_thread_id == thread.id)
+ .order_by(AgentContextEventRecord.sequence.desc())
+ .limit(1)
+ )
+ latest = latest_result.scalar_one_or_none()
+ if latest is None:
+ if thread.next_sequence != 1 or thread.last_event_hash != ZERO_HASH:
+ raise AgentContextError("context_journal_head_integrity_broken")
+ return
+ if (
+ thread.next_sequence != latest.sequence + 1
+ or thread.last_event_hash != latest.payload_hash
+ ):
+ raise AgentContextError("context_journal_head_integrity_broken")
+
+
+def _verify_event_record(
+ row: AgentContextEventRecord,
+ *,
+ expected_sequence: int,
+ expected_previous_hash: str,
+) -> None:
+ if row.sequence != expected_sequence:
+ raise AgentContextError("context_event_sequence_integrity_broken")
+ if row.previous_hash != expected_previous_hash:
+ raise AgentContextError("context_event_chain_integrity_broken")
+ if _event_record_hash(row) != row.payload_hash:
+ raise AgentContextError("context_event_hash_integrity_broken")
+
+
+def _canonical_json(value: Any) -> bytes:
+ try:
+ return json.dumps(
+ value,
+ ensure_ascii=False,
+ allow_nan=False,
+ sort_keys=True,
+ separators=(",", ":"),
+ ).encode("utf-8")
+ except (TypeError, ValueError) as exc:
+ raise TypeError("Context payload must be exact JSON data") from exc
+
+
+def _parse_blob_ref(value: str) -> str:
+ if not isinstance(value, str) or not value.startswith("sha256:"):
+ raise ValueError("invalid_context_blob_ref")
+ digest = value.removeprefix("sha256:")
+ if len(digest) != 64 or any(
+ character not in "0123456789abcdef" for character in digest
+ ):
+ raise ValueError("invalid_context_blob_ref")
+ return digest
+
+
+async def _verified_blob_record(
+ session: AsyncSession,
+ payload_ref: str,
+) -> AgentContextBlobRecord:
+ digest = _parse_blob_ref(payload_ref)
+ row = await session.get(AgentContextBlobRecord, digest)
+ if row is None:
+ raise ContextNotFoundError("context_blob_not_found")
+ if row.binary_payload is not None:
+ if row.json_payload is not None:
+ raise AgentContextError("context_blob_integrity_broken")
+ encoded = bytes(row.binary_payload)
+ else:
+ encoded = _canonical_json(row.json_payload)
+ actual_digest = hashlib.sha256(encoded).hexdigest()
+ if (
+ row.payload_hash != digest
+ or actual_digest != digest
+ or row.byte_size != len(encoded)
+ ):
+ raise AgentContextError("context_blob_integrity_broken")
+ return row
+
+
+def _to_ref(row: AgentContextThreadRecord) -> AgentContextRef:
+ return AgentContextRef(
+ workflow_id=row.workflow_id,
+ context_node_id=row.context_node_id,
+ generation=row.generation,
+ thread_id=row.thread_id,
+ epoch=row.epoch,
+ revision=row.revision,
+ )
+
+
+def _to_thread_summary(
+ row: AgentContextThreadRecord,
+) -> AgentContextThreadSummary:
+ return AgentContextThreadSummary(
+ ref=_to_ref(row),
+ thread_kind=row.thread_kind, # type: ignore[arg-type]
+ provider=row.provider,
+ status=row.status,
+ active_checkpoint_sequence=max(
+ 0,
+ row.active_checkpoint_sequence,
+ ),
+ active_token_count=max(0, row.active_token_count),
+ updated_at=row.updated_at,
+ )
+
+
+def _to_event(row: AgentContextEventRecord) -> AgentContextEvent:
+ return AgentContextEvent(
+ sequence=row.sequence,
+ event_type=row.event_type,
+ message_wire_v2=deepcopy(row.message_wire_v2),
+ payload_ref=row.payload_ref,
+ operation_id=row.operation_id,
+ provider=row.provider,
+ previous_hash=row.previous_hash,
+ payload_hash=row.payload_hash,
+ )
+
+
+def _to_checkpoint(
+ row: AgentContextCheckpointRecord,
+) -> AgentContextCheckpoint:
+ return AgentContextCheckpoint(
+ provider=row.provider,
+ strategy=row.strategy,
+ covers_through_sequence=row.covers_through_sequence,
+ replay_payload_ref=row.replay_payload_ref,
+ active_token_count=row.active_token_count,
+ source_revision=row.source_revision,
+ source_hash=row.source_hash,
+ )
+
+
+def _to_binding(
+ row: AgentContextProviderBindingRecord,
+) -> AgentContextProviderBinding:
+ return AgentContextProviderBinding(
+ provider=row.provider,
+ binding_type=row.binding_type,
+ binding_ref=row.binding_ref,
+ epoch=row.epoch,
+ fidelity=row.fidelity, # type: ignore[arg-type]
+ )
+
+
+async def _to_plan(
+ session: AsyncSession,
+ thread: AgentContextThreadRecord,
+ attempt: AgentContextCompactionAttemptRecord,
+) -> AgentContextCompactionPlan:
+ checkpoint: Optional[AgentContextCheckpoint] = None
+ if attempt.base_checkpoint_id is not None:
+ checkpoint_row = await session.get(
+ AgentContextCheckpointRecord,
+ attempt.base_checkpoint_id,
+ )
+ if checkpoint_row is None:
+ raise AgentContextError("base_checkpoint_missing")
+ checkpoint = _to_checkpoint(checkpoint_row)
+ result = await session.execute(
+ select(AgentContextEventRecord)
+ .where(
+ AgentContextEventRecord.context_thread_id == thread.id,
+ AgentContextEventRecord.epoch == attempt.epoch,
+ AgentContextEventRecord.sequence
+ > attempt.base_checkpoint_sequence,
+ AgentContextEventRecord.sequence
+ <= attempt.covers_through_sequence,
+ )
+ .order_by(AgentContextEventRecord.sequence)
+ )
+ prefix = [_to_event(row) for row in result.scalars().all()]
+ if not prefix or prefix[-1].payload_hash != attempt.source_hash:
+ raise CompactionConflictError("compaction_prefix_changed")
+ return AgentContextCompactionPlan(
+ attempt_id=int(attempt.id),
+ ref=_to_ref(thread),
+ provider=attempt.provider,
+ strategy=attempt.strategy,
+ base_checkpoint=checkpoint,
+ committed_prefix=prefix,
+ covers_through_sequence=attempt.covers_through_sequence,
+ source_revision=attempt.source_revision,
+ source_hash=attempt.source_hash,
+ )
+
+
+__all__ = [
+ "AgentContextError",
+ "AgentContextStore",
+ "CompactionConflictError",
+ "ContextArchivedError",
+ "ContextNotFoundError",
+ "RevisionConflictError",
+ "StaleEpochError",
+]
diff --git a/server/services/agent_runtime.py b/server/services/agent_runtime.py
index e8cce5ec..56c2e216 100644
--- a/server/services/agent_runtime.py
+++ b/server/services/agent_runtime.py
@@ -8,8 +8,18 @@ from __future__ import annotations
import asyncio
import json
-from dataclasses import dataclass, field
-from typing import Any, Awaitable, Callable, Dict, List, Optional, Sequence, Type
+from dataclasses import asdict, dataclass, field
+from typing import (
+ Any,
+ Awaitable,
+ Callable,
+ Dict,
+ List,
+ Optional,
+ Protocol,
+ Sequence,
+ Type,
+)
from pydantic import BaseModel, ValidationError
@@ -23,12 +33,33 @@ from services.llm.protocol import (
ToolCall,
ToolDef,
Usage,
+ message_to_wire,
+ messages_to_wire,
)
from services.tool_identity import DuplicateToolNameError
logger = get_logger(__name__)
+class AgentContextTransitionSink(Protocol):
+ """Bound, epoch-fenced sink for one Context thread.
+
+ Store implementations decide whether payloads are inlined or replaced by
+ hash-addressed blob references. The runtime only emits committed
+ transitions in provider-observable order.
+ """
+
+ async def append_transition(
+ self,
+ *,
+ event_type: str,
+ operation_id: str,
+ provider: str,
+ message_wire_v2: Optional[Dict[str, Any]] = None,
+ payload: Optional[Dict[str, Any]] = None,
+ ) -> Any: ...
+
+
@dataclass
class AgentToolSpec:
"""LLM-visible definition plus local execution/validation metadata."""
@@ -92,6 +123,7 @@ async def run_native_llm_step(
max_tokens: int,
thinking: Optional[ThinkingConfig] = None,
tools: Optional[Sequence[ToolDef | AgentToolSpec]] = None,
+ context_management: Optional[Dict[str, Any]] = None,
sdk_max_retries: int = 0,
explicit_max_retries: int = 2,
translate_errors: bool = True,
@@ -119,6 +151,7 @@ async def run_native_llm_step(
max_tokens=max_tokens,
thinking=thinking,
tools=definitions or None,
+ context_management=context_management,
sdk_max_retries=max(0, int(sdk_max_retries)),
# Preserve structured metadata until this agent-step boundary.
translate_errors=False,
@@ -153,7 +186,11 @@ def _validated_tool_args(
try:
value = spec.args_schema.model_validate(call.args or {})
- return value.model_dump(mode="json"), None
+ # Preserve the security boundary used by ToolNode.execute_as_tool:
+ # only fields the model actually supplied may participate in the
+ # invocation/config merge. Pydantic defaults are schema metadata, not
+ # model-authored arguments.
+ return value.model_dump(mode="json", exclude_unset=True), None
except ValidationError as exc:
return None, str(exc)
@@ -175,6 +212,15 @@ async def run_native_agent_loop(
rebind_from_operations: Optional[
Callable[[List[Dict[str, Any]]], Awaitable[List[AgentToolSpec]]]
] = None,
+ context_management: Optional[Dict[str, Any]] = None,
+ compaction_pause_callback: Optional[
+ Callable[
+ [int, LLMResponse, List[Message]],
+ Awaitable[Optional[List[Message]]],
+ ]
+ ] = None,
+ context_transition_sink: Optional[AgentContextTransitionSink] = None,
+ context_operation_id: Optional[str] = None,
) -> Dict[str, Any]:
"""Run the shared buffered native tool-agent loop.
@@ -189,6 +235,27 @@ async def run_native_agent_loop(
thinking_parts: List[str] = []
last_response: Optional[LLMResponse] = None
iteration = 0
+ if context_transition_sink is not None and not context_operation_id:
+ raise ValueError(
+ "context_operation_id is required when a Context transition sink is used"
+ )
+
+ async def persist_transition(
+ event_type: str,
+ *,
+ operation_id: str,
+ message: Optional[Message] = None,
+ payload: Optional[Dict[str, Any]] = None,
+ ) -> None:
+ if context_transition_sink is None:
+ return
+ await context_transition_sink.append_transition(
+ event_type=event_type,
+ operation_id=f"{context_operation_id}:{operation_id}",
+ provider=provider,
+ message_wire_v2=message_to_wire(message) if message else None,
+ payload=payload,
+ )
for iteration in range(1, max_iterations + 1):
if progress_callback is not None:
@@ -197,19 +264,59 @@ async def run_native_agent_loop(
except Exception as exc: # progress is observational
logger.debug("[Agent loop] progress callback failed: %s", exc)
- response = await run_native_llm_step(
- chat_unifier,
- provider=provider,
- api_key=api_key,
- messages=messages,
- model=model,
- temperature=temperature,
- max_tokens=max_tokens,
- thinking=thinking,
- tools=current_tools,
+ definitions = [_tool_definition(tool) for tool in current_tools]
+ await persist_transition(
+ "request.snapshot",
+ operation_id=f"iteration:{iteration}:request",
+ payload={
+ "iteration": iteration,
+ "provider": provider,
+ "model": model,
+ "settings": {
+ "temperature": temperature,
+ "max_tokens": max_tokens,
+ "thinking": asdict(thinking) if thinking is not None else None,
+ },
+ "messages": messages_to_wire(messages),
+ "tools": [
+ {
+ "name": definition.name,
+ "description": definition.description,
+ "parameters": definition.parameters,
+ }
+ for definition in definitions
+ ],
+ "dynamic_tool_count": len(current_tools),
+ },
)
+ try:
+ response = await run_native_llm_step(
+ chat_unifier,
+ provider=provider,
+ api_key=api_key,
+ messages=messages,
+ model=model,
+ temperature=temperature,
+ max_tokens=max_tokens,
+ thinking=thinking,
+ tools=current_tools,
+ context_management=context_management,
+ )
+ except BaseException as exc:
+ if isinstance(exc, asyncio.CancelledError):
+ raise
+ await persist_transition(
+ "provider.outcome_ambiguous",
+ operation_id=f"iteration:{iteration}:provider-error",
+ payload={
+ "iteration": iteration,
+ "error_type": type(exc).__name__,
+ "ambiguous": True,
+ },
+ )
+ raise
last_response = response
- usage = add_usage(usage, response.usage)
+ usage = add_usage(usage, response.billing_usage or response.usage)
assistant = response.assistant_message
if assistant is None:
@@ -219,6 +326,21 @@ async def run_native_agent_loop(
tool_calls=list(response.tool_calls),
)
messages.append(assistant)
+ # This commit is intentionally before any requested tool executes.
+ await persist_transition(
+ "message.assistant",
+ operation_id=f"iteration:{iteration}:assistant",
+ message=assistant,
+ payload={
+ "iteration": iteration,
+ "finish_reason": response.finish_reason,
+ "model": response.model or model,
+ "usage": asdict(response.usage),
+ "billing_usage": asdict(
+ response.billing_usage or response.usage
+ ),
+ },
+ )
if response.thinking:
thinking_parts.append(
@@ -233,7 +355,42 @@ async def run_native_agent_loop(
logger.warning("[Agent loop] provider response blocked by safety filters")
calls = list(response.tool_calls or assistant.tool_calls or ())
+ if (
+ str(response.finish_reason).strip().lower() == "compaction"
+ and not calls
+ ):
+ # Anthropic's durability pause returns only a compaction block.
+ # It has already been journaled above; continue from that exact
+ # block instead of exposing it as the user's final response.
+ await persist_transition(
+ "provider.compaction_paused",
+ operation_id=f"iteration:{iteration}:compaction-pause",
+ payload={
+ "iteration": iteration,
+ "finish_reason": response.finish_reason,
+ },
+ )
+ if compaction_pause_callback is not None:
+ replacement = await compaction_pause_callback(
+ iteration,
+ response,
+ list(messages),
+ )
+ if replacement is not None:
+ messages = list(replacement)
+ continue
if not calls:
+ await persist_transition(
+ "response.final",
+ operation_id=f"iteration:{iteration}:final",
+ payload={
+ "iteration": iteration,
+ "usage": asdict(response.usage),
+ "lifetime_usage": asdict(usage),
+ "active_input_tokens": response.usage.input_tokens,
+ "finish_reason": response.finish_reason,
+ },
+ )
return {
"messages": messages,
"iteration": iteration,
@@ -259,7 +416,7 @@ async def run_native_agent_loop(
specs = _tool_specs_by_name(current_tools)
iteration_new_tools: List[AgentToolSpec] = []
- for call in calls:
+ for call_index, call in enumerate(calls, start=1):
if call.name not in specs:
result: Any = {
"error": "Unknown tool",
@@ -278,6 +435,18 @@ async def run_native_agent_loop(
result["raw_arguments"] = call.raw_arguments
else:
try:
+ # AgentToolSpec.execution is the trusted server-side
+ # config consumed by execute_tool. Attach the provider
+ # call identity there, never to model-controlled args,
+ # so stateful tools can make mutations idempotent.
+ spec = specs[call.name]
+ spec.execution["tool_call_id"] = call.id
+ spec.execution["operation_id"] = (
+ f"{context_operation_id}:iteration:{iteration}:"
+ f"tool:{call_index}:{call.id or call.name}"
+ if context_operation_id
+ else f"{provider}:tool:{call.id or call.name}"
+ )
result = await tool_executor(call.name, args or {})
except Exception as exc:
# Tool failures are fed back to the model.
@@ -308,13 +477,29 @@ async def run_native_agent_loop(
"[Agent loop] tool rebind failed: %s", exc, exc_info=True
)
- messages.append(
- Message(
- role="tool",
- content=json.dumps(result, default=str),
- tool_call_id=call.id,
- name=call.name,
- )
+ tool_message = Message(
+ role="tool",
+ content=json.dumps(result, default=str),
+ tool_call_id=call.id,
+ name=call.name,
+ )
+ messages.append(tool_message)
+ await persist_transition(
+ "message.tool_result",
+ operation_id=(
+ f"iteration:{iteration}:tool:{call_index}:{call.id or call.name}"
+ ),
+ message=tool_message,
+ payload={
+ "iteration": iteration,
+ "tool_call_id": call.id,
+ "tool_name": call.name,
+ "outcome": (
+ "error"
+ if isinstance(result, dict) and "error" in result
+ else "success"
+ ),
+ },
)
if iteration_new_tools:
@@ -333,6 +518,16 @@ async def run_native_agent_loop(
),
)
messages.append(terminal)
+ await persist_transition(
+ "response.truncated",
+ operation_id=f"iteration:{iteration}:truncated",
+ message=terminal,
+ payload={
+ "iteration": iteration,
+ "lifetime_usage": asdict(usage),
+ "reason": "recursion_limit",
+ },
+ )
return {
"messages": messages,
"iteration": iteration,
@@ -344,6 +539,7 @@ async def run_native_agent_loop(
__all__ = [
+ "AgentContextTransitionSink",
"AgentToolSpec",
"add_usage",
"run_native_llm_step",
diff --git a/server/services/ai.py b/server/services/ai.py
index a29f1986..c9c9ae12 100644
--- a/server/services/ai.py
+++ b/server/services/ai.py
@@ -6,11 +6,11 @@ Every in-process and durable agent execution goes through
from __future__ import annotations
-import functools
+import hashlib
import time
-from dataclasses import dataclass
+from dataclasses import asdict, dataclass
from datetime import datetime
-from typing import Awaitable, Dict, Any, List, Optional, Callable, Type, TYPE_CHECKING
+from typing import Dict, Any, List, Optional, Type, TYPE_CHECKING
import json
@@ -61,8 +61,6 @@ from services.llm.config import (
resolve_max_tokens as native_resolve_max_tokens,
resolve_temperature as native_resolve_temperature,
)
-from services.llm.vertex import is_vertex_express_key
-from services.llm.messages import filter_empty_messages as _filter_native_messages
from services.agent_runtime import AgentToolSpec, run_native_agent_loop
@@ -295,6 +293,20 @@ def _build_skill_system_prompt(skill_data: List[Dict[str, Any]], log_prefix: str
return build_skill_system_prompt(skill_data, log_prefix)
+@dataclass
+class _AgentContextRuntime:
+ """Backend-only execution binding for one visible Context node."""
+
+ store: Any
+ writer: Any
+ policy: Any
+ request_options: Optional[Dict[str, Any]]
+ operation_id: str
+ history: List[NativeMessage]
+ provider: str
+ model: str
+
+
class AIService:
"""AI model service backed by the native provider SDK layer."""
@@ -326,6 +338,436 @@ class AIService:
"""Detect AI provider from model name."""
return detect_provider_from_model(model)
+ async def _prepare_context(
+ self,
+ *,
+ node_id: str,
+ context_data: Optional[Dict[str, Any]],
+ execution_context: Optional[Dict[str, Any]],
+ workflow_id: Optional[str],
+ provider: str,
+ model: str,
+ database: Any,
+ ) -> Optional["_AgentContextRuntime"]:
+ """Resolve one backend-owned Context thread and reconstruct replay."""
+
+ if not context_data or context_data.get("kind") != "context":
+ return None
+
+ from services.agent_context import (
+ AgentContextStore,
+ AgentContextTransitionWriter,
+ ContextCompactionPolicy,
+ import_generation_zero_handoff,
+ provider_context_request_options,
+ reconstruct_message_wire_v2,
+ )
+ from services.llm.protocol import (
+ Message,
+ message_from_wire,
+ message_to_wire,
+ )
+
+ raw_context = execution_context or {}
+ admitted_workflow_id = str(
+ workflow_id or raw_context.get("workflow_id") or ""
+ )
+ descriptor_workflow_id = str(context_data.get("workflow_id") or "")
+ if (
+ admitted_workflow_id
+ and descriptor_workflow_id
+ and admitted_workflow_id != descriptor_workflow_id
+ ):
+ raise ValueError("Context V2 workflow scope mismatch")
+ resolved_workflow_id = (
+ admitted_workflow_id or descriptor_workflow_id
+ )
+ context_node_id = str(
+ context_data.get("context_node_id")
+ or context_data.get("node_id")
+ or ""
+ )
+ if not resolved_workflow_id or not context_node_id:
+ raise ValueError(
+ "Context V2 requires workflow_id and context_node_id"
+ )
+ admitted_generation = int(
+ raw_context.get("generation")
+ or raw_context.get("workflow_generation")
+ or 0
+ )
+ descriptor_generation = int(context_data.get("generation") or 0)
+ if (
+ admitted_generation
+ and descriptor_generation
+ and admitted_generation != descriptor_generation
+ ):
+ raise ValueError("Context V2 generation scope mismatch")
+ generation = admitted_generation or descriptor_generation
+ if generation <= 0:
+ raise ValueError(
+ "Context V2 requires an admitted workflow generation"
+ )
+ execution_id = str(
+ raw_context.get("execution_id")
+ or context_data.get("execution_id")
+ or ""
+ )
+ delegated_task_id = str(
+ raw_context.get("delegated_task_id")
+ or raw_context.get("parent_task_id")
+ or raw_context.get("team_task_id")
+ or context_data.get("delegated_task_id")
+ or ""
+ )
+ session_id = str(context_data.get("session_id") or "")
+ if not (execution_id or delegated_task_id or session_id):
+ raise ValueError(
+ "Context V2 requires an execution, delegated task, or "
+ "explicit session identity"
+ )
+
+ store = AgentContextStore(database)
+ ref = await store.resolve_thread(
+ workflow_id=resolved_workflow_id,
+ context_node_id=context_node_id,
+ generation=generation,
+ session_id=session_id or None,
+ delegated_task_id=delegated_task_id or None,
+ execution_id=execution_id or None,
+ )
+ ref = await import_generation_zero_handoff(store, ref)
+
+ # A provider transition is an epoch boundary. Opaque state remains
+ # archived while the observable MessageWireV2 replay is handed off.
+ summary = await store.load_thread_summary(ref)
+ if (
+ summary.provider
+ and summary.provider != provider
+ ):
+ _, handoff_wires = await reconstruct_message_wire_v2(store, ref)
+ portable_handoff_wires: list[dict[str, Any]] = []
+ for handoff_wire in handoff_wires:
+ source_message = message_from_wire(handoff_wire)
+ if (
+ source_message.provider_state
+ and source_message.role == "assistant"
+ and not source_message.content
+ and not source_message.tool_calls
+ ):
+ # A native compaction marker is replayable only by its
+ # issuing provider. Forking it as "portable" would silently
+ # erase the compacted prefix for the destination provider.
+ raise ValueError(
+ "Provider change requires a portable Context "
+ "checkpoint; the active checkpoint is provider-native"
+ )
+ portable_message = Message(
+ role=source_message.role,
+ content=source_message.content,
+ tool_calls=list(source_message.tool_calls),
+ tool_call_id=source_message.tool_call_id,
+ name=source_message.name,
+ )
+ portable_handoff_wires.append(
+ dict(message_to_wire(portable_message))
+ )
+ handoff_ref = await store.put_blob(
+ {
+ "format": "agent-context-portable-handoff-v1",
+ "source_provider": summary.provider,
+ "target_provider": provider,
+ "messages": portable_handoff_wires,
+ }
+ )
+ ref = await store.fork_provider(
+ ref,
+ provider=provider,
+ operation_id=(
+ f"context-provider-fork:{generation}:"
+ f"{ref.thread_id}:epoch-{ref.epoch}:"
+ f"{summary.provider}-to-{provider}"
+ ),
+ portable_handoff_ref=handoff_ref,
+ )
+ try:
+ from nodes.context._events import (
+ dispatch_context_epoch_started,
+ )
+
+ await dispatch_context_epoch_started(
+ workflow_id=resolved_workflow_id,
+ context_node_id=context_node_id,
+ thread_id=ref.thread_id,
+ epoch=ref.epoch,
+ revision=ref.revision,
+ provider=provider,
+ reason="provider_change",
+ )
+ except Exception as exc:
+ logger.debug(
+ "[Context] provider epoch event failed: %s",
+ exc,
+ )
+
+ ref, history_wires = await reconstruct_message_wire_v2(store, ref)
+ history = [message_from_wire(wire) for wire in history_wires]
+ policy = ContextCompactionPolicy.from_mapping(
+ context_data.get("policy")
+ if isinstance(context_data.get("policy"), dict)
+ else None
+ )
+ request_options = provider_context_request_options(
+ provider=provider,
+ model=model,
+ policy=policy,
+ )
+ identity = (
+ execution_id
+ or delegated_task_id
+ or session_id
+ or ref.thread_id
+ )
+ material = (
+ f"{resolved_workflow_id}:{context_node_id}:{generation}:"
+ f"{ref.thread_id}:{ref.epoch}:{ref.revision}:{identity}:{node_id}"
+ )
+ operation_id = (
+ "agent-context:"
+ + hashlib.sha256(material.encode("utf-8")).hexdigest()
+ )
+ return _AgentContextRuntime(
+ store=store,
+ writer=AgentContextTransitionWriter(store, ref),
+ policy=policy,
+ request_options=request_options,
+ operation_id=operation_id,
+ history=history,
+ provider=provider,
+ model=model,
+ )
+
+ async def _portable_context_candidate(
+ self,
+ wires: list[dict[str, Any]],
+ _policy: Any,
+ *,
+ provider: str,
+ api_key: str,
+ model: str,
+ ) -> Any:
+ """Create a structured portable checkpoint from observable replay."""
+
+ from services.agent_context import ContextCompactionCandidate
+ from services.agent_runtime import run_native_llm_step
+ from services.llm.protocol import Message, message_from_wire, message_to_wire
+ from services.model_registry import get_model_registry
+
+ transcript: list[str] = []
+ for wire in wires:
+ message = message_from_wire(wire)
+ line = f"{message.role.upper()}: {message.content}"
+ if message.tool_calls:
+ line += "\nTOOL_CALLS: " + json.dumps(
+ [
+ {
+ "id": call.id,
+ "name": call.name,
+ "args": call.args,
+ "raw_arguments": call.raw_arguments,
+ "parse_error": call.parse_error,
+ }
+ for call in message.tool_calls
+ ],
+ ensure_ascii=False,
+ default=str,
+ )
+ if message.tool_call_id:
+ line += (
+ f"\nTOOL_RESULT_FOR: {message.tool_call_id}"
+ f" ({message.name or 'tool'})"
+ )
+ transcript.append(line)
+ prompt = (
+ "Create a compact, provider-portable continuation checkpoint for "
+ "the agent transcript below. Preserve resolved instructions, "
+ "durable facts, exact identifiers, completed and pending tool "
+ "work, decisions, errors, constraints, open questions, and the "
+ "next action. Never claim a tool ran unless its result appears. "
+ "Do not include commentary about summarizing.\n\n"
+ + "\n\n".join(transcript)
+ )
+ max_summary_tokens = min(
+ 4096,
+ get_model_registry().get_max_output_tokens(model, provider),
+ )
+ response = await run_native_llm_step(
+ self.chat_unifier,
+ provider=provider,
+ api_key=api_key,
+ messages=[Message(role="user", content=prompt)],
+ model=model,
+ temperature=0.2,
+ max_tokens=max_summary_tokens,
+ sdk_max_retries=0,
+ explicit_max_retries=0,
+ )
+ summary_message = Message(
+ role="user",
+ content=(
+ "<agent_context_checkpoint>\n"
+ f"{response.content}\n"
+ "</agent_context_checkpoint>"
+ ),
+ )
+ summary_wire = dict(message_to_wire(summary_message))
+ return ContextCompactionCandidate(
+ strategy="portable_structured",
+ replay_payload={
+ "format": "agent-context-portable-v1",
+ "messages": [summary_wire],
+ },
+ active_token_count=max(
+ 1,
+ (len(summary_message.content.encode("utf-8")) + 2) // 3,
+ ),
+ lifetime_usage=asdict(
+ response.billing_usage or response.usage
+ ),
+ )
+
+ async def _finalize_context(
+ self,
+ runtime: Optional["_AgentContextRuntime"],
+ *,
+ final_state: Dict[str, Any],
+ api_key: str,
+ max_tokens: int,
+ operation_suffix: str = "finalize",
+ ) -> Optional[Any]:
+ if runtime is None:
+ return None
+ from services.agent_context import AgentContextCompactionService
+ from services.llm.protocol import message_to_wire
+
+ response = final_state.get("response")
+ active_usage = getattr(response, "usage", None)
+ active_input = int(
+ getattr(active_usage, "input_tokens", 0) or 0
+ ) + int(getattr(active_usage, "output_tokens", 0) or 0)
+ rendered_messages = [
+ dict(message_to_wire(message))
+ for message in list(final_state.get("messages") or [])
+ ]
+ try:
+ result = await AgentContextCompactionService(
+ runtime.store
+ ).update_pressure_and_compact(
+ runtime.writer.ref,
+ operation_id=(
+ f"{runtime.operation_id}:{operation_suffix}"
+ ),
+ provider=runtime.provider,
+ model=runtime.model,
+ policy=runtime.policy,
+ active_input_tokens=active_input,
+ output_headroom=max_tokens,
+ rendered_request=rendered_messages,
+ portable_compactor=lambda wires, policy: (
+ self._portable_context_candidate(
+ wires,
+ policy,
+ provider=runtime.provider,
+ api_key=api_key,
+ model=runtime.model,
+ )
+ ),
+ )
+ runtime.writer.ref = result.ref
+ if result.lifetime_usage:
+ final_state["usage"] = _coerce_native_usage(
+ final_state.get("usage")
+ ) + _coerce_native_usage(result.lifetime_usage)
+ try:
+ from nodes.context._events import (
+ dispatch_context_compacted,
+ dispatch_context_updated,
+ )
+
+ await dispatch_context_updated(
+ workflow_id=result.ref.workflow_id,
+ context_node_id=result.ref.context_node_id,
+ thread_id=result.ref.thread_id,
+ epoch=result.ref.epoch,
+ revision=result.ref.revision,
+ provider=runtime.provider,
+ active_token_count=result.pressure_tokens,
+ )
+ if result.compacted and result.checkpoint is not None:
+ await dispatch_context_compacted(
+ workflow_id=result.ref.workflow_id,
+ context_node_id=result.ref.context_node_id,
+ thread_id=result.ref.thread_id,
+ epoch=result.ref.epoch,
+ revision=result.ref.revision,
+ provider=runtime.provider,
+ strategy=result.checkpoint.strategy,
+ covers_through_sequence=(
+ result.checkpoint.covers_through_sequence
+ ),
+ active_token_count=result.pressure_tokens,
+ )
+ except Exception as exc:
+ logger.debug("[Context] metadata event failed: %s", exc)
+ return result
+ except Exception as exc:
+ # A failed candidate never replaces the prior checkpoint. The
+ # completed model response remains successful and replayable.
+ logger.warning("[Context] compaction skipped: %s", exc)
+ return None
+
+ async def _commit_context_compaction_pause(
+ self,
+ runtime: Optional["_AgentContextRuntime"],
+ *,
+ iteration: int,
+ response: LLMResponse,
+ messages: List[NativeMessage],
+ api_key: str,
+ max_tokens: int,
+ ) -> Optional[List[NativeMessage]]:
+ """Durably activate a provider pause before the next model request."""
+
+ if runtime is None:
+ return None
+ result = await self._finalize_context(
+ runtime,
+ final_state={
+ "response": response,
+ "usage": response.billing_usage or response.usage,
+ },
+ api_key=api_key,
+ max_tokens=max_tokens,
+ operation_suffix=f"compaction-pause:{iteration}",
+ )
+ if result is None or not result.compacted:
+ return messages
+ from services.agent_context import reconstruct_messages
+
+ current_ref, replay = await reconstruct_messages(
+ runtime.store,
+ runtime.writer.ref,
+ )
+ runtime.writer.ref = current_ref
+ # Provider compaction checkpoints replace conversation replay, not the
+ # effective request instruction. Anthropic's durability pause occurs
+ # mid-loop, so dropping the system message here would silently change
+ # the agent's contract on the very next request.
+ system_messages = [
+ message for message in messages if message.role == "system"
+ ]
+ return [*system_messages, *replay]
+
def _extract_text_content(self, content, ai_response=None) -> str:
"""Extract text content from various response formats.
@@ -860,6 +1302,20 @@ class AIService:
# Broadcast: Initializing model
await broadcast_status("initializing", {"message": f"Initializing {provider} model...", "provider": provider, "model": model, "active_skills": [], "last_skills": [], "last_tool_name": None, "last_capability": None})
+ context_runtime = await self._prepare_context(
+ node_id=node_id,
+ context_data=memory_data,
+ execution_context=context,
+ workflow_id=workflow_id,
+ provider=provider,
+ model=model,
+ database=database or self.database,
+ )
+ if context_runtime is not None:
+ # Simple Memory is an explicit tool in V2. Legacy automatic
+ # recall/persistence remains only for immutable V1 snapshots.
+ memory_data = None
+
# Build initial messages for state. The SystemMessage is
# PREPENDED after tool building (below), because the
# delegation-guidance block at the end of the tool-building
@@ -870,7 +1326,15 @@ class AIService:
# and the LLM would never see the delegation contract,
# which is exactly what previously broke ``aiAgent``-driven
# delegation through the ``input-tools`` handle.
- initial_messages: List[NativeMessage] = []
+ initial_messages: List[NativeMessage] = (
+ [
+ message
+ for message in context_runtime.history
+ if message.role != "system"
+ ]
+ if context_runtime is not None
+ else []
+ )
# Add memory history from connected simpleMemory node (markdown-based)
session_id = None
@@ -1057,6 +1521,7 @@ class AIService:
config["nodes"] = context.get("nodes", [])
config["edges"] = context.get("edges", [])
config["workspace_dir"] = context.get("workspace_dir", "")
+ config["user_id"] = context.get("user_id", "owner")
# Stable per-run id so session-keyed tools (browser)
# reuse one instance across the agent loop.
config["execution_id"] = context.get("execution_id")
@@ -1273,6 +1738,44 @@ class AIService:
max_iterations=recursion_limit,
progress_callback=_emit_progress if broadcaster else None,
rebind_from_operations=_rebind_from_operations if auto_rebind_enabled else None,
+ context_management=(
+ context_runtime.request_options
+ if context_runtime is not None
+ else None
+ ),
+ compaction_pause_callback=(
+ (
+ lambda iteration, response, messages: (
+ self._commit_context_compaction_pause(
+ context_runtime,
+ iteration=iteration,
+ response=response,
+ messages=messages,
+ api_key=api_key,
+ max_tokens=max_tokens,
+ )
+ )
+ )
+ if context_runtime is not None
+ else None
+ ),
+ context_transition_sink=(
+ context_runtime.writer
+ if context_runtime is not None
+ else None
+ ),
+ context_operation_id=(
+ context_runtime.operation_id
+ if context_runtime is not None
+ else None
+ ),
+ )
+
+ context_result = await self._finalize_context(
+ context_runtime,
+ final_state=final_state,
+ api_key=api_key,
+ max_tokens=max_tokens,
)
# Extract the AI response (last message in the accumulated messages)
@@ -1394,6 +1897,16 @@ class AIService:
# Add memory info if used
if session_id:
result["memory"] = {"session_id": session_id, "history_loaded": history_count}
+ if context_runtime is not None:
+ result["context"] = {
+ "context_node_id": context_runtime.writer.ref.context_node_id,
+ "thread_id": context_runtime.writer.ref.thread_id,
+ "epoch": context_runtime.writer.ref.epoch,
+ "revision": context_runtime.writer.ref.revision,
+ "compacted": bool(
+ context_result and context_result.compacted
+ ),
+ }
log_execution_time(logger, "ai_agent_loop", start_time, time.time())
log_api_call(logger, provider, model, "agent", True)
@@ -1636,10 +2149,33 @@ class AIService:
# Broadcast: Initializing
await broadcast_status("initializing", {"message": f"Initializing {provider} model...", "provider": provider, "model": model, "active_skills": [], "last_skills": [], "last_tool_name": None, "last_capability": None})
+ context_runtime = await self._prepare_context(
+ node_id=node_id,
+ context_data=memory_data,
+ execution_context=context,
+ workflow_id=workflow_id,
+ provider=provider,
+ model=model,
+ database=database or self.database,
+ )
+ if context_runtime is not None:
+ memory_data = None
+
# Build messages
- messages: List[NativeMessage] = []
+ messages: List[NativeMessage] = (
+ [
+ message
+ for message in context_runtime.history
+ if message.role != "system"
+ ]
+ if context_runtime is not None
+ else []
+ )
if system_message:
- messages.append(NativeMessage(role="system", content=system_message))
+ messages.insert(
+ 0,
+ NativeMessage(role="system", content=system_message),
+ )
# Load memory history if connected (markdown-based like AI Agent)
session_id = None
@@ -1762,6 +2298,7 @@ class AIService:
config["nodes"] = context.get("nodes", [])
config["edges"] = context.get("edges", [])
config["workspace_dir"] = context.get("workspace_dir", "")
+ config["user_id"] = context.get("user_id", "owner")
# Stable per-run id so session-keyed tools (browser)
# reuse one instance across the agent loop.
config["execution_id"] = context.get("execution_id")
@@ -1921,6 +2458,37 @@ class AIService:
max_iterations=recursion_limit,
progress_callback=_emit_progress if broadcaster else None,
rebind_from_operations=_rebind_from_operations if auto_rebind_enabled else None,
+ context_management=(
+ context_runtime.request_options
+ if context_runtime is not None
+ else None
+ ),
+ compaction_pause_callback=(
+ (
+ lambda iteration, response, current_messages: (
+ self._commit_context_compaction_pause(
+ context_runtime,
+ iteration=iteration,
+ response=response,
+ messages=current_messages,
+ api_key=api_key,
+ max_tokens=max_tokens,
+ )
+ )
+ )
+ if context_runtime is not None
+ else None
+ ),
+ context_transition_sink=(
+ context_runtime.writer
+ if context_runtime is not None
+ else None
+ ),
+ context_operation_id=(
+ context_runtime.operation_id
+ if context_runtime is not None
+ else None
+ ),
)
# Extract response
@@ -1944,6 +2512,37 @@ class AIService:
thinking=thinking_config,
initial_messages=messages,
max_iterations=1,
+ context_management=(
+ context_runtime.request_options
+ if context_runtime is not None
+ else None
+ ),
+ compaction_pause_callback=(
+ (
+ lambda iteration, response, current_messages: (
+ self._commit_context_compaction_pause(
+ context_runtime,
+ iteration=iteration,
+ response=response,
+ messages=current_messages,
+ api_key=api_key,
+ max_tokens=max_tokens,
+ )
+ )
+ )
+ if context_runtime is not None
+ else None
+ ),
+ context_transition_sink=(
+ context_runtime.writer
+ if context_runtime is not None
+ else None
+ ),
+ context_operation_id=(
+ context_runtime.operation_id
+ if context_runtime is not None
+ else None
+ ),
)
all_messages = final_state["messages"]
ai_response = all_messages[-1] if all_messages else None
@@ -1955,6 +2554,13 @@ class AIService:
)
thinking_content = final_state.get("thinking_content")
+ context_result = await self._finalize_context(
+ context_runtime,
+ final_state=final_state,
+ api_key=api_key,
+ max_tokens=max_tokens,
+ )
+
logger.info(f"[ChatAgent] Response generated, thinking={'yes' if thinking_content else 'no'}, iterations={iterations}")
# Track token usage if memory connected (for compaction service)
@@ -2057,6 +2663,16 @@ class AIService:
if session_id:
result["memory"] = {"session_id": session_id, "history_loaded": history_count}
+ if context_runtime is not None:
+ result["context"] = {
+ "context_node_id": context_runtime.writer.ref.context_node_id,
+ "thread_id": context_runtime.writer.ref.thread_id,
+ "epoch": context_runtime.writer.ref.epoch,
+ "revision": context_runtime.writer.ref.revision,
+ "compacted": bool(
+ context_result and context_result.compacted
+ ),
+ }
if skill_data:
result["skills"] = {
@@ -2185,6 +2801,13 @@ class AIService:
connected_services = tool_info.get("connected_services", [])
default_tool_name, default_tool_description = _resolve_default_tool_name_description(node_type)
+ from services.node_registry import get_node_class
+
+ plugin_cls = get_node_class(node_type)
+ schema_locked = bool(
+ plugin_cls is not None
+ and getattr(plugin_cls, "tool_schema_locked", False)
+ )
# Team-handle expansion assigns a per-node identity to custom
# aiAgent delegates. It intentionally outranks schemas and class
# defaults because those are type-wide and would collide.
@@ -2193,7 +2816,16 @@ class AIService:
# Check database for stored schema (source of truth)
db_schema = await self.database.get_tool_schema(node_id) if node_id else None
- if db_schema:
+ if schema_locked:
+ # Security-sensitive first-party tools own both their
+ # canonical name and invocation schema. A stale/custom row
+ # from the generic Tool editor must not replace that
+ # contract.
+ tool_name = default_tool_name or node_type
+ tool_description = (
+ default_tool_description or f"Execute {node_label} node"
+ )
+ elif db_schema:
# Use database schema as source of truth
logger.debug(f"[Agent] Using DB schema for tool node {node_id}")
tool_name = delegated_name or db_schema.get("tool_name", default_tool_name or f"tool_{node_label}")
@@ -2244,7 +2876,11 @@ class AIService:
schema_params = dict(node_params)
if connected_services:
schema_params["connected_services"] = connected_services
- if db_schema and db_schema.get("schema_config"):
+ if (
+ not schema_locked
+ and db_schema
+ and db_schema.get("schema_config")
+ ):
schema_params["db_schema_config"] = db_schema["schema_config"]
schema = self._get_tool_schema(node_type, schema_params)
@@ -2292,6 +2928,15 @@ class AIService:
"""
from pydantic import BaseModel, Field
+ # Plugin-locked ToolInput is authoritative even when an older client
+ # left a custom ToolSchema row in the database.
+ from services.node_registry import get_node_class
+
+ plugin_cls = get_node_class(node_type)
+ if plugin_cls is not None and getattr(plugin_cls, "tool_schema_locked", False):
+ tool_input_model = getattr(plugin_cls, "tool_input_model", None)
+ return tool_input_model() if callable(tool_input_model) else plugin_cls.Params
+
# Check if we have a database-stored schema config (source of truth)
db_schema_config = params.get("db_schema_config")
if db_schema_config:
@@ -2380,10 +3025,10 @@ class AIService:
# AI-tool-usable plugin is covered by this lookup (contract invariant
# test_fast_path_covers_every_plugin_tool). Wave 11.D.13 stripped the
# per-type ad-hoc schemas that used to live below this gate.
- from services.node_registry import get_node_class
-
- plugin_cls = get_node_class(node_type)
if plugin_cls is not None and hasattr(plugin_cls, "Params"):
+ tool_input_model = getattr(plugin_cls, "tool_input_model", None)
+ if callable(tool_input_model):
+ return tool_input_model()
return plugin_cls.Params
# Generic schema for other nodes
diff --git a/server/services/cli_agent/context_bridge.py b/server/services/cli_agent/context_bridge.py
new file mode 100644
index 00000000..f73d2dac
--- /dev/null
+++ b/server/services/cli_agent/context_bridge.py
@@ -0,0 +1,456 @@
+"""Backend-only Context V2 bridge for specialized agent runtimes.
+
+Canvas Context nodes carry policy and topology only. This module resolves the
+durable thread, writes exact observable provider events to the append-only
+journal, and owns opaque provider continuation bindings. Specialized node
+plugins pass the descriptor returned by ``collect_agent_connections``; they do
+not read or mutate Context node parameters themselves.
+
+The bridge is intentionally absent for legacy ``input-memory`` descriptors so
+already-recorded V1 generations retain their original Simple Memory behavior.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import hashlib
+import json
+import uuid
+from dataclasses import asdict, dataclass, field, is_dataclass
+from typing import Any, Literal, Optional
+
+from models.agent_context import (
+ AgentContextActiveState,
+ AgentContextRef,
+ ContextFidelity,
+)
+from services.agent_context import (
+ AgentContextStore,
+ AgentContextTransitionWriter,
+ OpaqueCheckpointError,
+ import_generation_zero_handoff,
+ reconstruct_message_wire_v2,
+)
+from services.llm.protocol import Message, message_from_wire, message_to_wire
+
+
+ProviderFidelity = Literal[
+ "provider_replayable",
+ "provider_bound",
+ "observable_only",
+]
+
+
+def is_context(value: Any) -> bool:
+ """Return whether an edge-walker descriptor is a Context V2 reference."""
+
+ return isinstance(value, dict) and value.get("kind") == "context"
+
+
+def _jsonable(value: Any) -> Any:
+ """Convert provider SDK/dataclass values into exact JSON-safe payloads."""
+
+ if value is None or isinstance(value, (str, int, float, bool)):
+ return value
+ if isinstance(value, dict):
+ return {str(key): _jsonable(item) for key, item in value.items()}
+ if isinstance(value, (list, tuple, set, frozenset)):
+ return [_jsonable(item) for item in value]
+ if is_dataclass(value):
+ return _jsonable(asdict(value))
+ model_dump = getattr(value, "model_dump", None)
+ if callable(model_dump):
+ try:
+ return _jsonable(model_dump(mode="json", exclude_none=False))
+ except TypeError:
+ return _jsonable(model_dump())
+ if hasattr(value, "__dict__"):
+ return {
+ str(key): _jsonable(item)
+ for key, item in vars(value).items()
+ if not str(key).startswith("_")
+ }
+ return str(value)
+
+
+def _portable_wire(value: dict[str, Any]) -> dict[str, Any]:
+ """Validate a portable wire and remove prior-provider opaque state."""
+
+ wire = dict(message_to_wire(message_from_wire(value)))
+ wire["provider_state"] = {}
+ return wire
+
+
+async def _observable_handoff_messages(
+ store: AgentContextStore,
+ state: AgentContextActiveState,
+ *,
+ include_message_wires: bool,
+) -> list[dict[str, Any]]:
+ """Render exact observable boundaries when native replay is unavailable."""
+
+ messages: list[dict[str, Any]] = []
+ for event in state.tail:
+ if event.event_type in {
+ "provider_handoff",
+ "request.snapshot",
+ }:
+ continue
+ if event.message_wire_v2 is not None:
+ if include_message_wires:
+ messages.append(_portable_wire(event.message_wire_v2))
+ continue
+
+ payload = (
+ await store.get_blob(event.payload_ref)
+ if event.payload_ref
+ else None
+ )
+ envelope = {
+ "sequence": event.sequence,
+ "event_type": event.event_type,
+ "provider": event.provider,
+ "operation_id": event.operation_id,
+ "payload": _jsonable(payload),
+ }
+ role = (
+ "user"
+ if event.event_type in {"provider.request", "request.snapshot"}
+ else "assistant"
+ )
+ messages.append(
+ dict(
+ message_to_wire(
+ Message(
+ role=role,
+ content=json.dumps(
+ envelope,
+ ensure_ascii=False,
+ sort_keys=True,
+ separators=(",", ":"),
+ ),
+ )
+ )
+ )
+ )
+ return messages
+
+
+async def _build_portable_handoff(
+ store: AgentContextStore,
+ state: AgentContextActiveState,
+ *,
+ from_provider: str,
+ to_provider: str,
+) -> tuple[str, tuple[dict[str, Any], ...]]:
+ """Persist a provider-neutral replay payload for an epoch fork."""
+
+ fidelity = "portable"
+ reconstructed: list[dict[str, Any]] = []
+ try:
+ _, reconstructed = await reconstruct_message_wire_v2(
+ store,
+ state.ref,
+ )
+ messages = [_portable_wire(item) for item in reconstructed]
+ except OpaqueCheckpointError:
+ messages = []
+ observable_messages = await _observable_handoff_messages(
+ store,
+ state,
+ include_message_wires=not reconstructed,
+ )
+ if observable_messages:
+ messages.extend(observable_messages)
+ if not reconstructed:
+ fidelity = "observable_only"
+
+ payload_ref = await store.put_blob(
+ {
+ "format": "portable_provider_handoff_v2",
+ "fidelity": fidelity,
+ "from_provider": from_provider,
+ "to_provider": to_provider,
+ "source_epoch": state.ref.epoch,
+ "messages": messages,
+ "source_checkpoint": (
+ {
+ "provider": state.checkpoint.provider,
+ "strategy": state.checkpoint.strategy,
+ "covers_through_sequence": (
+ state.checkpoint.covers_through_sequence
+ ),
+ "source_hash": state.checkpoint.source_hash,
+ }
+ if state.checkpoint is not None
+ else None
+ ),
+ }
+ )
+ return payload_ref, tuple(messages)
+
+
+async def _pending_handoff_messages(
+ store: AgentContextStore,
+ ref: AgentContextRef,
+) -> tuple[AgentContextRef, tuple[dict[str, Any], ...]]:
+ """Recover an unconsumed handoff after a crash between fork and request."""
+
+ state = await store.load_active(ref)
+ handoff = next(
+ (
+ event
+ for event in reversed(state.tail)
+ if event.event_type == "provider_handoff"
+ and event.payload_ref
+ ),
+ None,
+ )
+ if handoff is None:
+ return state.ref, ()
+ if any(
+ event.sequence > handoff.sequence
+ and event.event_type
+ in {
+ "provider.result",
+ "provider.response",
+ "provider.final",
+ }
+ for event in state.tail
+ ):
+ return state.ref, ()
+ payload = await store.get_blob(handoff.payload_ref)
+ candidates = (
+ payload.get("messages") if isinstance(payload, dict) else None
+ )
+ if not isinstance(candidates, list):
+ raise ValueError("provider handoff is missing portable messages")
+ messages: list[dict[str, Any]] = []
+ for candidate in candidates:
+ if not isinstance(candidate, dict):
+ raise ValueError(
+ "provider handoff contains a non-message item"
+ )
+ messages.append(_portable_wire(candidate))
+ return state.ref, tuple(messages)
+
+
+@dataclass
+class SpecializedAgentContextBridge:
+ """One epoch-bound Context writer for a specialized provider.
+
+ ``fidelity`` and ``resumable`` are attached to every opaque event. That
+ makes the fidelity boundary explicit in the durable record rather than an
+ inference made later by the UI.
+ """
+
+ store: AgentContextStore
+ writer: AgentContextTransitionWriter
+ provider: str
+ operation_prefix: str
+ fidelity: ProviderFidelity
+ resumable: bool
+ portable_handoff_messages: tuple[dict[str, Any], ...] = field(
+ default_factory=tuple
+ )
+
+ def __post_init__(self) -> None:
+ self._sequence = 0
+ self._sequence_lock = asyncio.Lock()
+
+ @property
+ def ref(self) -> AgentContextRef:
+ return self.writer.ref
+
+ @property
+ def pool_key(self) -> tuple[str, str, int]:
+ """Claude pool identity; generation is already isolated by node id."""
+
+ return (
+ self.ref.context_node_id,
+ self.ref.thread_id,
+ self.ref.epoch,
+ )
+
+ @classmethod
+ async def resolve(
+ cls,
+ database: Any,
+ descriptor: dict[str, Any],
+ *,
+ provider: str,
+ fidelity: ProviderFidelity,
+ resumable: bool,
+ operation_prefix: Optional[str] = None,
+ ) -> "SpecializedAgentContextBridge":
+ if not is_context(descriptor):
+ raise ValueError("specialized Context bridge requires context_v2")
+ workflow_id = str(descriptor.get("workflow_id") or "")
+ context_node_id = str(
+ descriptor.get("context_node_id")
+ or descriptor.get("node_id")
+ or ""
+ )
+ execution_id = descriptor.get("execution_id")
+ delegated_task_id = descriptor.get("delegated_task_id")
+ session_id = descriptor.get("session_id")
+ generation = int(descriptor.get("generation") or 0)
+ if generation <= 0:
+ raise ValueError(
+ "specialized Context bridge requires an admitted generation"
+ )
+ store = AgentContextStore(database)
+ ref = await store.resolve_thread(
+ workflow_id=workflow_id,
+ context_node_id=context_node_id,
+ generation=generation,
+ session_id=str(session_id) if session_id else None,
+ delegated_task_id=(
+ str(delegated_task_id) if delegated_task_id else None
+ ),
+ execution_id=str(execution_id) if execution_id else None,
+ )
+ ref = await import_generation_zero_handoff(store, ref)
+ # A provider change is an epoch boundary. Preserve a portable index
+ # of the active checkpoint/tail in blob storage, archive opaque
+ # bindings, and fence every writer holding the previous ref.
+ summary = await store.load_thread_summary(ref)
+ prior_provider = summary.provider
+ portable_messages: tuple[dict[str, Any], ...] = ()
+ if (
+ prior_provider
+ and prior_provider != "legacy"
+ and prior_provider != provider
+ ):
+ state = await store.load_active(ref)
+ handoff_ref, portable_messages = (
+ await _build_portable_handoff(
+ store,
+ state,
+ from_provider=prior_provider,
+ to_provider=provider,
+ )
+ )
+ identity = (
+ f"{workflow_id}:{context_node_id}:"
+ f"{ref.generation}:{ref.thread_id}:{ref.epoch}:{provider}"
+ )
+ digest = hashlib.sha256(identity.encode("utf-8")).hexdigest()
+ ref = await store.fork_provider(
+ ref,
+ provider=provider,
+ operation_id=f"specialized-provider-fork:{digest}",
+ portable_handoff_ref=handoff_ref,
+ )
+ else:
+ ref, portable_messages = await _pending_handoff_messages(
+ store,
+ ref,
+ )
+ prefix = operation_prefix or (
+ f"specialized-context:{provider}:"
+ f"{execution_id or delegated_task_id or session_id or uuid.uuid4().hex}"
+ )
+ return cls(
+ store=store,
+ writer=AgentContextTransitionWriter(store, ref),
+ provider=provider,
+ operation_prefix=prefix,
+ fidelity=fidelity,
+ resumable=resumable,
+ portable_handoff_messages=portable_messages,
+ )
+
+ def augment_prompt(self, prompt: str) -> str:
+ """Render a pending portable handoff into a prompt-only adapter."""
+
+ if not self.portable_handoff_messages:
+ return prompt
+ transcript = json.dumps(
+ list(self.portable_handoff_messages),
+ ensure_ascii=False,
+ sort_keys=True,
+ separators=(",", ":"),
+ )
+ return (
+ "## Prior agent context (portable MessageWireV2 replay)\n"
+ "The backend reconstructed the following committed context "
+ "from the previous provider. Treat it as prior conversation "
+ "state and continue from it.\n"
+ f"{transcript}\n\n"
+ "## Current request\n"
+ f"{prompt}"
+ )
+
+ async def append_observable(
+ self,
+ event_type: str,
+ payload: Any,
+ *,
+ operation_suffix: Optional[str] = None,
+ ) -> None:
+ """Append one exact provider boundary/event before presentation."""
+
+ if operation_suffix is None:
+ async with self._sequence_lock:
+ self._sequence += 1
+ operation_suffix = f"event:{self._sequence}"
+ await self.writer.append_transition(
+ event_type=event_type,
+ operation_id=f"{self.operation_prefix}:{operation_suffix}",
+ provider=self.provider,
+ payload={
+ "fidelity": self.fidelity,
+ "resumable": self.resumable,
+ "observable_only": self.fidelity == "observable_only",
+ "non_resumable": not self.resumable,
+ "event": _jsonable(payload),
+ },
+ )
+
+ async def capture_provider_event(self, event: dict[str, Any]) -> None:
+ """Raw stream sink called before buffers or 4,000-char UI truncation."""
+
+ await self.append_observable("provider.stream", event)
+
+ async def load_binding(
+ self,
+ binding_type: str,
+ ) -> Optional[dict[str, Any]]:
+ """Load the most recently committed binding of ``binding_type``."""
+
+ bindings = await self.store.load_provider_bindings(
+ self.writer.ref,
+ provider=self.provider,
+ )
+ for binding in reversed(bindings):
+ if binding.binding_type != binding_type:
+ continue
+ payload = await self.store.get_blob(binding.binding_ref)
+ return dict(payload) if isinstance(payload, dict) else None
+ return None
+
+ async def bind_provider(
+ self,
+ binding_type: str,
+ binding: dict[str, Any],
+ *,
+ operation_suffix: str,
+ fidelity: ContextFidelity = "provider_bound",
+ ) -> None:
+ """Persist an opaque continuation identity outside graph/runtime data."""
+
+ await self.store.bind_provider(
+ self.writer.ref,
+ provider=self.provider,
+ binding_type=binding_type,
+ binding=_jsonable(binding),
+ operation_id=f"{self.operation_prefix}:{operation_suffix}",
+ fidelity=fidelity,
+ )
+
+
+__all__ = [
+ "SpecializedAgentContextBridge",
+ "is_context",
+]
diff --git a/server/services/cli_agent/mcp_server.py b/server/services/cli_agent/mcp_server.py
index 02c96982..d177b667 100644
--- a/server/services/cli_agent/mcp_server.py
+++ b/server/services/cli_agent/mcp_server.py
@@ -59,6 +59,7 @@ class BatchContext:
node_id: str
workspace_dir: Path
execution_id: Optional[str] = None
+ user_id: str = "owner"
connected_skill_names: Set[str] = field(default_factory=set)
connected_skill_descriptors: List[Dict[str, Any]] = field(default_factory=list)
allowed_credentials: Set[str] = field(default_factory=set)
@@ -85,10 +86,12 @@ def issue_token() -> str:
def _validate_connected_tool_names(connected_tools: List[Dict[str, Any]]) -> None:
- """Reject ambiguous node-type dispatch within one CLI agent surface."""
+ """Reject ambiguous canonical names within one CLI agent surface."""
+ from services.cli_agent.workflow_tools import _connected_tool_name
+
ensure_unique_tool_names(
{
- "name": tool["node_type"],
+ "name": _connected_tool_name(tool),
"node_id": tool.get("node_id") or "",
"label": tool.get("label") or tool["node_type"],
}
@@ -110,8 +113,9 @@ def register_batch(token: str, ctx: BatchContext) -> None:
# physical nodes of the same effective name would otherwise select the
# first one silently.
_validate_connected_tool_names(ctx.connected_tools)
- _active_tokens[token] = ctx
- tool_names = [t.get("node_type") for t in ctx.connected_tools]
+ from services.cli_agent.workflow_tools import _connected_tool_name
+
+ tool_names = [_connected_tool_name(t) for t in ctx.connected_tools]
logger.info(
"[CC-Agent MCP register_batch] node=%s wf=%s token=%s... " "skills=%d tools=%d creds=%d %s",
ctx.node_id,
@@ -125,7 +129,11 @@ def register_batch(token: str, ctx: BatchContext) -> None:
# Per-batch workflow-tool exposure lives in workflow_tools.py.
from services.cli_agent.workflow_tools import expose_workflow_tools
+ # Schema compatibility is checked before the bearer token becomes
+ # callable. A conflicting global FastMCP name must leave no half-
+ # registered batch behind.
expose_workflow_tools(ctx.connected_tools)
+ _active_tokens[token] = ctx
def unregister_batch(token: str) -> None:
@@ -196,14 +204,23 @@ def rebind_batch(
ctx.broadcaster = broadcaster
if connected_tools is not None:
from services.cli_agent.workflow_tools import (
+ _connected_tool_name,
expose_workflow_tools,
unexpose_workflow_tools,
)
- old_by_type = {t.get("node_type"): t for t in ctx.connected_tools if t.get("node_type")}
- new_by_type = {t.get("node_type"): t for t in connected_tools if t.get("node_type")}
- added = [v for k, v in new_by_type.items() if k not in old_by_type]
- removed = [v for k, v in old_by_type.items() if k not in new_by_type]
+ old_by_name = {
+ _connected_tool_name(t): t
+ for t in ctx.connected_tools
+ if _connected_tool_name(t)
+ }
+ new_by_name = {
+ _connected_tool_name(t): t
+ for t in connected_tools
+ if _connected_tool_name(t)
+ }
+ added = [v for k, v in new_by_name.items() if k not in old_by_name]
+ removed = [v for k, v in old_by_name.items() if k not in new_by_name]
if removed:
unexpose_workflow_tools(removed)
if added:
@@ -216,7 +233,7 @@ def rebind_batch(
token[:8],
len(added),
len(removed),
- len(old_by_type.keys() & new_by_type.keys()),
+ len(old_by_name.keys() & new_by_name.keys()),
len(ctx.connected_tools),
)
if connected_skill_names is not None:
diff --git a/server/services/cli_agent/service.py b/server/services/cli_agent/service.py
index 78ec9805..45bb4f06 100644
--- a/server/services/cli_agent/service.py
+++ b/server/services/cli_agent/service.py
@@ -34,6 +34,10 @@ import anyio
from core.logging import get_logger
from services.cli_agent.config import get_provider_config
+from services.cli_agent.context_bridge import (
+ SpecializedAgentContextBridge,
+ is_context,
+)
from services.cli_agent.factory import create_cli_provider
from services.cli_agent.mcp_server import (
BatchContext,
@@ -79,10 +83,12 @@ class AICliService:
connected_skill_descriptors: Optional[List[Dict[str, Any]]] = None,
connected_tools: Optional[List[Dict[str, Any]]] = None,
connected_memory: Optional[Dict[str, Any]] = None,
+ connected_context: Optional[Dict[str, Any]] = None,
execution_id: Optional[str] = None,
allowed_credentials: Optional[List[str]] = None,
max_parallel: int = DEFAULT_MAX_PARALLEL,
mcp_port: Optional[int] = None,
+ ai_service: Any = None,
) -> BatchResult:
"""Run a list of CLI tasks under one batch.
@@ -94,6 +100,75 @@ class AICliService:
"""
provider = create_cli_provider(provider_name)
task_list: List[BaseAICliTaskSpec] = list(tasks)
+ connected_tools = list(connected_tools or [])
+ context_bridge: Optional[SpecializedAgentContextBridge] = None
+ if is_context(connected_context):
+ from services.plugin.deps import get_database
+
+ provider_id = (
+ "claude_code" if provider_name == "claude" else provider_name
+ )
+ context_bridge = await SpecializedAgentContextBridge.resolve(
+ get_database(),
+ connected_context,
+ provider=provider_id,
+ fidelity=(
+ "provider_bound"
+ if provider_name == "claude"
+ else "observable_only"
+ ),
+ resumable=provider_name == "claude",
+ operation_prefix=(
+ f"cli-context:{provider_id}:"
+ f"{execution_id or connected_context.get('execution_id') or 'run'}:"
+ f"{node_id}"
+ ),
+ )
+ task_list = [
+ task.model_copy(
+ update={
+ "prompt": context_bridge.augment_prompt(
+ task.prompt
+ )
+ }
+ )
+ for task in task_list
+ ]
+
+ # Claude's opaque UUID is the only supported CLI continuation
+ # identity. Resume it explicitly; never use --continue, whose
+ # cwd-wide "latest" lookup can cross Context threads.
+ if provider_name == "claude":
+ if len(task_list) != 1:
+ raise ValueError(
+ "Context-bound Claude batches require exactly one "
+ "task so one thread maps to one provider session."
+ )
+ binding = await context_bridge.load_binding("session_uuid")
+ resume_uuid = str((binding or {}).get("session_uuid") or "")
+ if resume_uuid:
+ task = task_list[0]
+ task_list[0] = task.model_copy(
+ update={
+ "resume_session_id": resume_uuid,
+ "continue_session": False,
+ }
+ )
+
+ await context_bridge.append_observable(
+ "provider.request",
+ {
+ "node_id": node_id,
+ "workflow_id": workflow_id,
+ "provider": provider_name,
+ "tasks": task_list,
+ "tool_node_ids": [
+ tool.get("node_id") for tool in (connected_tools or [])
+ ],
+ "skill_names": list(connected_skill_names or []),
+ },
+ operation_suffix="request",
+ )
# Pass the per-workflow workspace dir
# (``data/workspaces/<workflow_id>/`` — injected into ctx by
@@ -152,20 +227,50 @@ class AICliService:
"an existing repo).",
workspace_dir,
)
- return self._abort_not_git_repo(
+ aborted = self._abort_not_git_repo(
provider_name=provider_name,
tasks=task_list,
)
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "runtime.error",
+ {
+ "error": "working_directory_not_git_repo",
+ "workspace_dir": str(workspace_dir),
+ "tasks": aborted.tasks,
+ },
+ operation_suffix="result",
+ )
+ return aborted
logger.info(
"[CC-Agent run_batch] resolved repo_root=%s for workspace=%s",
resolved_repo_root,
workspace_dir,
)
+ # Only a runnable batch needs an MCP surface. Build it through the
+ # same backend pipeline as native agents after inexpensive validation
+ # has succeeded, so abort paths do not require AIService/DB access.
+ connected_tools = await self._canonical_tool_surface(
+ connected_tools,
+ ai_service=ai_service,
+ )
+ from services.cli_agent.workflow_tools import _connected_tool_name
+
+ connected_tool_names = [
+ name
+ for tool in connected_tools
+ if (name := _connected_tool_name(tool))
+ ]
+
# Per-batch bearer token + MCP context
from core.env_defaults import env_value
token = issue_token()
+ # Single source for the skill-turn scope: registration (via
+ # BatchContext.execution_id) and the teardown clear must key on
+ # the SAME value or badges never clear.
+ turn_execution_id = execution_id or token
port = mcp_port or int(
os.environ.get("OPENCOMPANY_BACKEND_PORT")
or os.environ.get("MACHINA_BACKEND_PORT")
@@ -178,7 +283,12 @@ class AICliService:
# A batch token is a safe ephemeral conversation scope when the
# caller has no durable execution id; never share loaded-skill
# state across later CLI runs of the same workflow/node.
- execution_id=execution_id or token,
+ # NOTE: skill-turn state is registered under this exact value,
+ # so the teardown clear MUST use it too — keying the clear on
+ # the bare ``execution_id`` leaves badges lit forever on manual
+ # canvas runs, where ``execution_id`` is None.
+ execution_id=turn_execution_id,
+ user_id=str((connected_context or {}).get("user_id") or "owner"),
connected_skill_names=set(connected_skill_names or []),
connected_skill_descriptors=list(connected_skill_descriptors or []),
allowed_credentials=set(allowed_credentials or []),
@@ -234,9 +344,16 @@ class AICliService:
defaults=defaults,
mcp_port=port,
batch_token=token,
- connected_tool_names=[t.get("node_type") for t in (connected_tools or []) if t.get("node_type")],
+ connected_tool_names=connected_tool_names,
connected_skill_names=list(connected_skill_names or []),
- memory_bound=bool(connected_memory),
+ memory_bound=bool(connected_memory) or (
+ context_bridge is not None and provider_name == "claude"
+ ),
+ context_event_sink=(
+ context_bridge.capture_provider_event
+ if context_bridge is not None
+ else None
+ ),
)
async with self._lock:
self._active_sessions[key].append(session)
@@ -271,14 +388,22 @@ class AICliService:
# owns the PTY lifetime; the bearer token stays embedded in the
# spawned claude's argv across batches (CLI handles its own MCP
# auth — we don't issue/unregister per turn).
- use_pool = bool(connected_memory) and provider_name == "claude" and len(task_list) == 1
+ use_pool = (
+ provider_name == "claude"
+ and len(task_list) == 1
+ and (bool(connected_memory) or context_bridge is not None)
+ )
results: List[SessionResult]
try:
if use_pool:
results = [
await self._run_pooled_turn(
task=task_list[0],
- memory_node_id=connected_memory["node_id"],
+ session_key=(
+ context_bridge.pool_key
+ if context_bridge is not None
+ else connected_memory["node_id"]
+ ),
cwd=resolved_repo_root,
workspace_dir=Path(workspace_dir).resolve(),
defaults=defaults,
@@ -287,6 +412,11 @@ class AICliService:
connected_tools=connected_tools or [],
connected_skill_names=list(connected_skill_names or []),
workflow_id=workflow_id,
+ context_event_sink=(
+ context_bridge.capture_provider_event
+ if context_bridge is not None
+ else None
+ ),
)
]
else:
@@ -294,12 +424,23 @@ class AICliService:
*(run_one(t) for t in task_list),
return_exceptions=False,
)
+ except BaseException as exc:
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "provider.ambiguous_outcome",
+ {
+ "error_type": type(exc).__name__,
+ "error": str(exc),
+ },
+ operation_suffix="result",
+ )
+ raise
finally:
async with self._lock:
self._active_sessions.pop(key, None)
from services.skill_runtime import clear_skill_turn
- await clear_skill_turn(workflow_id, str(ctx.execution_id or ""), node_id)
+ await clear_skill_turn(workflow_id, str(turn_execution_id), node_id)
# When ``use_pool`` is True, the bearer token for THIS batch
# is consumed by ``ClaudeSessionPool.acquire`` directly:
# - Cold spawn: pool stores ``token`` on
@@ -342,6 +483,60 @@ class AICliService:
exc,
)
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "provider.result",
+ {
+ "provider": provider_name,
+ "tasks": results,
+ },
+ operation_suffix="result",
+ )
+ if provider_name == "claude":
+ stale_binding = any(
+ item.error
+ and "No conversation found with session ID"
+ in item.error
+ for item in results
+ )
+ if stale_binding:
+ await context_bridge.bind_provider(
+ "session_uuid",
+ {
+ "session_uuid": None,
+ "stale": True,
+ "context_node_id": context_bridge.ref.context_node_id,
+ "thread_id": context_bridge.ref.thread_id,
+ "epoch": context_bridge.ref.epoch,
+ },
+ operation_suffix="session-binding-stale",
+ )
+ from services.cli_agent.factory import get_session_pool
+
+ pool = get_session_pool("claude")
+ if pool is not None:
+ await pool.terminate(context_bridge.pool_key)
+ else:
+ resumed = next(
+ (
+ item
+ for item in reversed(results)
+ if item.success and item.session_id
+ ),
+ None,
+ )
+ if resumed is not None:
+ await context_bridge.bind_provider(
+ "session_uuid",
+ {
+ "session_uuid": resumed.session_id,
+ "context_node_id": context_bridge.ref.context_node_id,
+ "thread_id": context_bridge.ref.thread_id,
+ "epoch": context_bridge.ref.epoch,
+ },
+ operation_suffix="session-binding",
+ )
+
elapsed_ms = int((time.monotonic() - start) * 1000)
n_succeeded = sum(1 for r in results if r.success)
n_failed = len(results) - n_succeeded
@@ -387,6 +582,52 @@ class AICliService:
)
return result
+ @staticmethod
+ async def _canonical_tool_surface(
+ connected_tools: List[Dict[str, Any]],
+ *,
+ ai_service: Any = None,
+ ) -> List[Dict[str, Any]]:
+ """Build the CLI/MCP surface through the normal agent tool pipeline."""
+
+ if not connected_tools:
+ return []
+ from services.plugin.deps import get_ai_service
+ from services.tool_identity import ensure_unique_tool_names
+
+ ai_service = ai_service or get_ai_service()
+ surface: List[Dict[str, Any]] = []
+ identities: List[Dict[str, str]] = []
+ for tool_info in connected_tools:
+ tool, execution = await ai_service._build_tool_from_node(tool_info)
+ if tool is None or execution is None:
+ raise ValueError(
+ "Could not build connected tool "
+ f"{tool_info.get('node_type')!r}"
+ )
+ entry = {
+ **tool_info,
+ "_agent_tool_name": tool.name,
+ "_agent_tool_description": tool.description,
+ "_agent_tool_schema": tool.parameters,
+ "_agent_tool_input_model": tool.args_schema,
+ "_agent_tool_execution": execution,
+ }
+ surface.append(entry)
+ identities.append(
+ {
+ "name": tool.name,
+ "node_id": str(tool_info.get("node_id") or ""),
+ "label": str(
+ tool_info.get("label")
+ or tool_info.get("node_type")
+ or "tool"
+ ),
+ }
+ )
+ ensure_unique_tool_names(identities)
+ return surface
+
async def cancel_workflow(self, workflow_id: str) -> int:
"""Cancel every active session for a workflow. Returns count cancelled."""
cancelled = 0
@@ -427,7 +668,7 @@ class AICliService:
self,
*,
task: BaseAICliTaskSpec,
- memory_node_id: str,
+ session_key: Any,
cwd: Path,
workspace_dir: Path,
defaults: Dict[str, Any],
@@ -436,6 +677,7 @@ class AICliService:
connected_tools: List[Dict[str, Any]],
connected_skill_names: List[str],
workflow_id: str,
+ context_event_sink: Any = None,
) -> SessionResult:
"""Route one memory-bound claude turn through ``ClaudeSessionPool``.
@@ -466,7 +708,13 @@ class AICliService:
await pool.start_reaper()
mcp_endpoint_url = f"http://127.0.0.1:{mcp_port}/mcp/ide/mcp"
- tool_names = [t.get("node_type") for t in (connected_tools or []) if t.get("node_type")]
+ from services.cli_agent.workflow_tools import _connected_tool_name
+
+ tool_names = [
+ name
+ for tool in (connected_tools or [])
+ if (name := _connected_tool_name(tool))
+ ]
env = {**os.environ, "PYTHONUNBUFFERED": "1"}
# Project-local claude auth dir — same as ``AICliSession.env``.
@@ -474,13 +722,13 @@ class AICliService:
env["CLAUDE_CONFIG_DIR"] = str(OPENCOMPANY_CLAUDE_DIR)
# Composio-style parent-run-ID for MCP correlation.
- parent_run_id = f"{workflow_id}:{memory_node_id}:{mcp_bearer_token[:8]}"
+ parent_run_id = f"{workflow_id}:{session_key}:{mcp_bearer_token[:8]}"
env["OPENCOMPANY_PARENT_RUN_ID"] = parent_run_id
env["MACHINA_PARENT_RUN_ID"] = parent_run_id # legacy child-process contract
try:
pooled = await pool.acquire(
- memory_node_id,
+ session_key,
spec=task,
cwd=cwd,
env=env,
@@ -491,6 +739,7 @@ class AICliService:
connected_skill_names=connected_skill_names,
workspace_dir=workspace_dir,
workflow_id=workflow_id,
+ context_event_sink=context_event_sink,
)
except BaseException:
# A cold spawn can fail before the pool adopts the token. The
diff --git a/server/services/cli_agent/session.py b/server/services/cli_agent/session.py
index 51ed4eff..d5ab7095 100644
--- a/server/services/cli_agent/session.py
+++ b/server/services/cli_agent/session.py
@@ -41,7 +41,7 @@ import sys
import time
import uuid
from pathlib import Path
-from typing import Any, Dict, List, Optional
+from typing import Any, Awaitable, Callable, Dict, List, Optional
import anyio
@@ -94,6 +94,9 @@ class AICliSession(BaseProcessSupervisor):
connected_tool_names: Optional[List[str]] = None,
connected_skill_names: Optional[List[str]] = None,
memory_bound: bool = False,
+ context_event_sink: Optional[
+ Callable[[Dict[str, Any]], Awaitable[None]]
+ ] = None,
) -> None:
super().__init__()
self._provider = provider
@@ -129,6 +132,11 @@ class AICliSession(BaseProcessSupervisor):
# incompatible with this — every spawn would land under a
# brand-new project_key with no prior JSONL.
self._memory_bound: bool = bool(memory_bound)
+ # Backend Context V2 raw-event sink. It receives each decoded
+ # provider event before UI logging and before result presentation
+ # truncates the assistant text to 4,000 characters.
+ self._context_event_sink = context_event_sink
+ self._context_capture_error: Optional[str] = None
# Streaming state
self._events: List[Dict[str, Any]] = []
@@ -474,11 +482,30 @@ class AICliSession(BaseProcessSupervisor):
:class:`CompactionService` so the local-threshold path doesn't
also fire.
"""
+ if self._context_event_sink is not None:
+ try:
+ await self._context_event_sink(dict(event))
+ except Exception as exc: # noqa: BLE001 - journal failure visible
+ self._context_capture_error = (
+ f"context_event_persistence_failed: {exc}"
+ )
+ self._logger.error(
+ "[%s] Context raw-event append failed: %s",
+ self.label,
+ exc,
+ exc_info=True,
+ )
+ self._result_event.set()
+ return
self._events.append(event)
await self._on_event(event)
if self._provider.is_final_event(event):
self._result_event.set()
- if event.get("type") == "system" and event.get("subtype") == "compact_boundary":
+ if (
+ self._context_event_sink is None
+ and event.get("type") == "system"
+ and event.get("subtype") == "compact_boundary"
+ ):
await self._record_native_compaction(event)
async def _record_native_compaction(self, event: Dict[str, Any]) -> None:
@@ -545,14 +572,24 @@ class AICliSession(BaseProcessSupervisor):
"provider": self._provider.name,
"status": "finalising",
}
- for k in ("total_cost_usd", "duration_ms", "num_turns", "session_id"):
+ status_keys = ["total_cost_usd", "duration_ms", "num_turns"]
+ if self._context_event_sink is None:
+ status_keys.append("session_id")
+ for k in status_keys:
v = event.get(k)
if v is not None:
payload[k] = v
await self._safe_node_status("executing", payload)
else:
- msg = event.get("message") or event.get("text") or event.get("delta") or json.dumps(event)
- text = msg if isinstance(msg, str) else json.dumps(msg)
+ if self._context_event_sink is not None:
+ text = (
+ "provider event "
+ f"type={event.get('type', 'unknown')} "
+ f"subtype={event.get('subtype', '')}"
+ )
+ else:
+ msg = event.get("message") or event.get("text") or event.get("delta") or json.dumps(event)
+ text = msg if isinstance(msg, str) else json.dumps(msg)
await self._safe_terminal_log(text[:500], level="info")
def _log_event_summary(self, event: Dict[str, Any]) -> None:
@@ -581,7 +618,7 @@ class AICliSession(BaseProcessSupervisor):
tu.get("name"),
list((tu.get("input") or {}).keys()),
)
- elif texts:
+ elif texts and self._context_event_sink is None:
sample = " ".join(t for t in texts if isinstance(t, str))[:300]
self._logger.info(
"[CC-Agent stream] assistant.text: %r",
@@ -594,13 +631,19 @@ class AICliSession(BaseProcessSupervisor):
list((event.get("input") or {}).keys()),
)
elif etype == "tool_result":
- content = event.get("content") or ""
- preview = content if isinstance(content, str) else json.dumps(content)
- self._logger.info(
- "[CC-Agent stream] tool_result is_error=%s content=%r",
- event.get("is_error", False),
- preview[:300],
- )
+ if self._context_event_sink is not None:
+ self._logger.info(
+ "[CC-Agent stream] tool_result is_error=%s",
+ event.get("is_error", False),
+ )
+ else:
+ content = event.get("content") or ""
+ preview = content if isinstance(content, str) else json.dumps(content)
+ self._logger.info(
+ "[CC-Agent stream] tool_result is_error=%s content=%r",
+ event.get("is_error", False),
+ preview[:300],
+ )
elif etype == "hook":
self._logger.info(
"[CC-Agent stream] hook %s",
@@ -611,7 +654,11 @@ class AICliSession(BaseProcessSupervisor):
"[CC-Agent stream] result is_error=%s subtype=%s " "session_id=%s duration_ms=%s num_turns=%s cost=%s",
event.get("is_error", False),
event.get("subtype"),
- event.get("session_id"),
+ (
+ None
+ if self._context_event_sink is not None
+ else event.get("session_id")
+ ),
event.get("duration_ms"),
event.get("num_turns"),
event.get("total_cost_usd"),
@@ -709,6 +756,12 @@ class AICliSession(BaseProcessSupervisor):
self._result_event.wait(),
timeout=timeout_seconds,
)
+ if self._context_capture_error:
+ self._exit_code = -1
+ return self._build_result(
+ success=False,
+ error=self._context_capture_error,
+ )
# The result event fired; claude wrote `result` on the JSONL.
# The subprocess is still alive (interactive TUI stays
# running) — we treat the turn as exit-code 0 since claude
diff --git a/server/services/cli_agent/workflow_tools.py b/server/services/cli_agent/workflow_tools.py
index bb8ed729..b46a21be 100644
--- a/server/services/cli_agent/workflow_tools.py
+++ b/server/services/cli_agent/workflow_tools.py
@@ -7,9 +7,9 @@ those tools on the very first ``tools/list`` and can invoke them
directly — no two-step generic-wrapper indirection.
Schema generation is delegated to FastMCP: we build a function whose
-``inspect.signature`` mirrors the plugin's Pydantic ``Params`` field-
-for-field, so FastMCP advertises a flat ``inputSchema`` matching the
-plugin Params. Per-batch scoping is enforced inside the handler via
+``inspect.signature`` mirrors the canonical ``AgentToolSpec.args_schema``
+field-for-field, so FastMCP advertises the same flat ``ToolInput`` contract
+as native agents. Per-batch scoping is enforced inside the handler via
``_require_batch()`` so concurrent batches sharing the same tool name
are isolated; refcounts (``add_tool`` on first wire, ``remove_tool`` on
last unwire) keep the FastMCP registry tidy.
@@ -24,7 +24,9 @@ Public API:
from __future__ import annotations
import asyncio
+import hashlib
import inspect
+import json
from typing import Any, Dict, List, Optional
from core.logging import get_logger
@@ -32,10 +34,61 @@ from core.logging import get_logger
logger = get_logger(__name__)
-# node_type -> count of active batches that wired it. Tools are added
+# canonical tool name -> count of active batches that wired it. Tools are added
# to FastMCP on first wire, removed on last unwire — per-handler scope
# checks ``_require_batch`` against the calling batch's connected_tools.
_active_tool_refcounts: Dict[str, int] = {}
+_active_tool_schema_fingerprints: Dict[str, str] = {}
+
+
+def _connected_tool_name(entry: Dict[str, Any]) -> str:
+ """Return the canonical builder name, or the legacy raw node type.
+
+ Production agent nodes enrich every entry through
+ ``AIService._build_tool_from_node`` before registering a batch. Keeping
+ raw entries keyed by ``node_type`` preserves the low-level MCP extension
+ contract for callers that deliberately register a ``BatchContext``
+ directly; it also avoids running a second, incomplete name resolver here.
+ """
+ name = str(entry.get("_agent_tool_name") or "")
+ if name:
+ return name
+ return str(entry.get("node_type") or "")
+
+
+def _entry_input_model(entry: Dict[str, Any]) -> Optional[type]:
+ """Resolve the current batch entry's canonical invocation model."""
+
+ input_model = entry.get("_agent_tool_input_model")
+ if input_model is not None:
+ return input_model
+ from services.node_registry import get_node_class
+
+ cls = get_node_class(str(entry.get("node_type") or ""))
+ if cls is None:
+ return None
+ model_factory = getattr(cls, "tool_input_model", None)
+ return (
+ model_factory()
+ if callable(model_factory)
+ else getattr(cls, "Params", None)
+ )
+
+
+def _entry_schema_fingerprint(
+ entry: Dict[str, Any],
+ input_model: type,
+) -> str:
+ schema = entry.get("_agent_tool_schema")
+ if not isinstance(schema, dict):
+ schema = input_model.model_json_schema()
+ encoded = json.dumps(
+ schema,
+ sort_keys=True,
+ separators=(",", ":"),
+ default=str,
+ ).encode("utf-8")
+ return hashlib.sha256(encoded).hexdigest()
def expose_workflow_tools(connected_tools: List[Dict[str, Any]]) -> None:
@@ -45,31 +98,68 @@ def expose_workflow_tools(connected_tools: List[Dict[str, Any]]) -> None:
return
from services.node_registry import get_node_class
+ # FastMCP owns one global handler per visible name. Preflight the entire
+ # batch before mutating that registry so concurrent workflows can share a
+ # name only when they expose the exact same schema.
+ prepared: List[tuple[Dict[str, Any], str, type, str]] = []
+ pending_fingerprints: Dict[str, str] = {}
for entry in connected_tools:
node_type = entry.get("node_type")
if not node_type:
continue
- prev = _active_tool_refcounts.get(node_type, 0)
- _active_tool_refcounts[node_type] = prev + 1
- if prev > 0:
- continue # already exposed by another concurrent batch
+ tool_name = _connected_tool_name(entry)
cls = get_node_class(node_type)
- if cls is None or getattr(cls, "Params", None) is None:
+ input_model = _entry_input_model(entry)
+ if cls is None or input_model is None:
logger.warning(
- "[CC-Agent MCP] cannot expose %s: class or Params missing",
- node_type,
+ "[CC-Agent MCP] cannot expose %s: class or ToolInput missing",
+ tool_name,
)
continue
+ fingerprint = _entry_schema_fingerprint(entry, input_model)
+ active_fingerprint = _active_tool_schema_fingerprints.get(tool_name)
+ batch_fingerprint = pending_fingerprints.get(tool_name)
+ if (
+ active_fingerprint is not None
+ and active_fingerprint != fingerprint
+ ) or (
+ batch_fingerprint is not None
+ and batch_fingerprint != fingerprint
+ ):
+ raise ValueError(
+ "Concurrent MCP tool schema conflict for canonical name "
+ f"{tool_name!r}"
+ )
+ pending_fingerprints[tool_name] = fingerprint
+ prepared.append((entry, tool_name, input_model, fingerprint))
+
+ for entry, tool_name, input_model, fingerprint in prepared:
+ prev = _active_tool_refcounts.get(tool_name, 0)
+ if prev > 0:
+ _active_tool_refcounts[tool_name] = prev + 1
+ continue # identical schema already exposed by another batch
+ node_type = str(entry["node_type"])
try:
- handler = _build_handler(node_type, cls.Params)
+ handler = _build_handler(tool_name, node_type, input_model)
mcp.add_tool(
handler,
- name=node_type,
- description=(getattr(cls, "description", None) or f"OpenCompany workflow tool: {node_type}"),
+ name=tool_name,
+ description=(
+ entry.get("_agent_tool_description")
+ or getattr(cls, "description", None)
+ or f"OpenCompany workflow tool: {tool_name}"
+ ),
)
- logger.info("[CC-Agent MCP] exposed mcp__opencompany__%s", node_type)
+ _active_tool_refcounts[tool_name] = 1
+ _active_tool_schema_fingerprints[tool_name] = fingerprint
+ logger.info("[CC-Agent MCP] exposed mcp__opencompany__%s", tool_name)
except Exception as exc: # pragma: no cover
- logger.warning("[CC-Agent MCP] add_tool(%s) failed: %s", node_type, exc)
+ logger.warning(
+ "[CC-Agent MCP] add_tool(%s) failed: %s",
+ tool_name,
+ exc,
+ )
+ raise
_schedule_list_changed_notify()
@@ -79,19 +169,20 @@ def unexpose_workflow_tools(connected_tools: List[Dict[str, Any]]) -> None:
if mcp is None:
return
for entry in connected_tools:
- node_type = entry.get("node_type")
- if not node_type:
+ tool_name = _connected_tool_name(entry)
+ if not tool_name:
continue
- remaining = _active_tool_refcounts.get(node_type, 0) - 1
+ remaining = _active_tool_refcounts.get(tool_name, 0) - 1
if remaining > 0:
- _active_tool_refcounts[node_type] = remaining
+ _active_tool_refcounts[tool_name] = remaining
continue
- _active_tool_refcounts.pop(node_type, None)
+ _active_tool_refcounts.pop(tool_name, None)
+ _active_tool_schema_fingerprints.pop(tool_name, None)
try:
- mcp.remove_tool(node_type)
- logger.info("[CC-Agent MCP] removed mcp__opencompany__%s", node_type)
+ mcp.remove_tool(tool_name)
+ logger.info("[CC-Agent MCP] removed mcp__opencompany__%s", tool_name)
except Exception as exc: # pragma: no cover
- logger.debug("[CC-Agent MCP] remove_tool(%s): %s", node_type, exc)
+ logger.debug("[CC-Agent MCP] remove_tool(%s): %s", tool_name, exc)
_schedule_list_changed_notify()
@@ -144,14 +235,25 @@ def _schedule_list_changed_notify() -> None:
loop.create_task(_do_notify())
-def _build_handler(node_type: str, params_cls: type):
+def _build_handler(
+ tool_name: str,
+ node_type: str | type,
+ input_model: Optional[type] = None,
+):
"""Build an async handler whose ``inspect.signature`` mirrors the
- plugin Params field-for-field. FastMCP iterates that signature to
+ canonical ToolInput field-for-field. FastMCP iterates that signature to
derive the ``inputSchema``; flat fields → flat MCP arguments.
"""
from pydantic_core import PydanticUndefined
- fields = getattr(params_cls, "model_fields", {}) or {}
+ # Private two-argument compatibility for existing extension/tests:
+ # _build_handler(node_type, Params). Production passes the canonical
+ # three-argument form.
+ if input_model is None:
+ input_model = node_type # type: ignore[assignment]
+ node_type = tool_name
+
+ fields = getattr(input_model, "model_fields", {}) or {}
parameters: List[inspect.Parameter] = []
annotations: Dict[str, Any] = {"return": Dict[str, Any]}
for fname, finfo in fields.items():
@@ -181,25 +283,37 @@ def _build_handler(node_type: str, params_cls: type):
ctx = _require_batch()
entry = next(
- (t for t in ctx.connected_tools if t.get("node_type") == node_type),
+ (
+ t
+ for t in ctx.connected_tools
+ if _connected_tool_name(t) == tool_name
+ ),
None,
)
if entry is None:
return {
- "error": f"tool {node_type!r} not connected to this batch",
+ "error": f"tool {tool_name!r} not connected to this batch",
"status": 403,
}
- # Validate via the plugin's own Params model, then dump back to
- # a plain dict for execute_tool. `exclude_unset=True` drops
- # defaults the agent didn't supply.
+ # Resolve from the authenticated current batch, not the first workflow
+ # that happened to install this global FastMCP handler.
+ current_input_model = _entry_input_model(entry)
+ if current_input_model is None:
+ return {
+ "error": "Canonical ToolInput is unavailable",
+ "status": 500,
+ }
try:
- validated = params_cls(**kwargs)
- args = validated.model_dump(exclude_unset=True)
- except Exception:
- args = dict(kwargs)
+ validated = current_input_model.model_validate(kwargs)
+ args = validated.model_dump(mode="json", exclude_unset=True)
+ except Exception as exc:
+ return {
+ "error": "Invalid tool arguments",
+ "details": str(exc),
+ }
logger.info(
"[CC-Agent MCP %s] node=%s wf=%s args_keys=%s",
- node_type,
+ tool_name,
ctx.node_id,
ctx.workflow_id,
list(args.keys()),
@@ -207,13 +321,15 @@ def _build_handler(node_type: str, params_cls: type):
from services.handlers.tools import execute_tool
config: Dict[str, Any] = {
- "node_type": node_type,
+ **dict(entry.get("_agent_tool_execution") or {}),
+ "node_type": entry.get("node_type") or node_type,
"node_id": entry.get("node_id"),
"workflow_id": ctx.workflow_id,
"execution_id": ctx.execution_id,
+ "user_id": ctx.user_id,
"workspace_dir": str(ctx.workspace_dir),
"parent_node_id": ctx.node_id,
- "label": entry.get("label") or node_type,
+ "label": entry.get("label") or tool_name,
"parameters": dict(entry.get("parameters") or {}),
}
broadcaster = ctx.broadcaster
@@ -227,14 +343,14 @@ def _build_handler(node_type: str, params_cls: type):
{
"phase": "executing_tool",
"agent_type": "native_cli",
- "tool_name": node_type,
+ "tool_name": tool_name,
},
workflow_id=ctx.workflow_id,
)
await broadcaster.broadcast_agent_capability(
ctx.node_id,
capability_kind="tool",
- capability_name=node_type,
+ capability_name=tool_name,
state="started",
workflow_id=ctx.workflow_id,
execution_id=ctx.execution_id,
@@ -242,7 +358,7 @@ def _build_handler(node_type: str, params_cls: type):
invocation_source="native_mcp",
)
try:
- result = await execute_tool(node_type, args, config)
+ result = await execute_tool(tool_name, args, config)
except Exception as exc:
await broadcaster.update_node_status(
ctx.node_id,
@@ -250,7 +366,7 @@ def _build_handler(node_type: str, params_cls: type):
{
"phase": "tool_completed",
"agent_type": "native_cli",
- "tool_name": node_type,
+ "tool_name": tool_name,
"tool_failed": True,
},
workflow_id=ctx.workflow_id,
@@ -258,7 +374,7 @@ def _build_handler(node_type: str, params_cls: type):
await broadcaster.broadcast_agent_capability(
ctx.node_id,
capability_kind="tool",
- capability_name=node_type,
+ capability_name=tool_name,
state="failed",
workflow_id=ctx.workflow_id,
execution_id=ctx.execution_id,
@@ -276,7 +392,7 @@ def _build_handler(node_type: str, params_cls: type):
{
"phase": "tool_completed",
"agent_type": "native_cli",
- "tool_name": node_type,
+ "tool_name": tool_name,
"tool_failed": failed,
},
workflow_id=ctx.workflow_id,
@@ -284,7 +400,7 @@ def _build_handler(node_type: str, params_cls: type):
await broadcaster.broadcast_agent_capability(
ctx.node_id,
capability_kind="tool",
- capability_name=node_type,
+ capability_name=tool_name,
state="failed" if failed else "completed",
workflow_id=ctx.workflow_id,
execution_id=ctx.execution_id,
@@ -295,20 +411,20 @@ def _build_handler(node_type: str, params_cls: type):
if failed:
logger.warning(
"[CC-Agent MCP %s] node=%s ERROR: %s",
- node_type,
+ tool_name,
entry.get("node_id"),
result.get("error"),
)
else:
logger.info(
"[CC-Agent MCP %s] node=%s OK (result_keys=%s)",
- node_type,
+ tool_name,
entry.get("node_id"),
list(result.keys())[:8],
)
return result
- _handler.__name__ = node_type
+ _handler.__name__ = tool_name
_handler.__annotations__ = annotations
_handler.__signature__ = inspect.Signature( # type: ignore[attr-defined]
parameters,
@@ -320,3 +436,4 @@ def _build_handler(node_type: str, params_cls: type):
def _reset_for_tests() -> None: # pragma: no cover
"""Wipe the refcount registry. ONLY use in tests."""
_active_tool_refcounts.clear()
+ _active_tool_schema_fingerprints.clear()
diff --git a/server/services/deployment/control.py b/server/services/deployment/control.py
index dd18c758..3a4bc07c 100644
--- a/server/services/deployment/control.py
+++ b/server/services/deployment/control.py
@@ -69,8 +69,21 @@ class WorkflowControlService:
async def begin_generation(
self, *, workflow_id: str, nodes: list[dict], edges: list[dict], session_id: str,
- idempotency_key: str, reset: bool = False,
+ idempotency_key: str, reset: bool = False, graph_version: int = 0,
+ owner_id: str = "owner",
) -> tuple[WorkflowControlExecution, bool]:
+ from services.workflow_sanitizer import sanitize_runtime_payload
+
+ safe_graph = sanitize_runtime_payload(
+ {
+ "graphVersion": max(0, int(graph_version or 0)),
+ "owner_id": str(owner_id or "owner"),
+ "nodes": nodes,
+ "edges": edges,
+ }
+ )
+ nodes = list(safe_graph.get("nodes") or [])
+ edges = list(safe_graph.get("edges") or [])
existing = await self.database.get_workflow_control_by_idempotency_key(workflow_id, idempotency_key)
if existing:
return existing, False
@@ -85,7 +98,13 @@ class WorkflowControlService:
data_scope_id=execution_id,
controller_workflow_id=f"workflow-control-{workflow_id}-g{generation}",
session_id=session_id, graph_hash=_graph_hash(nodes, edges),
- graph_snapshot={"nodes": nodes, "edges": edges}, idempotency_key=idempotency_key,
+ graph_snapshot={
+ "graphVersion": safe_graph["graphVersion"],
+ "owner_id": safe_graph["owner_id"],
+ "nodes": nodes,
+ "edges": edges,
+ },
+ idempotency_key=idempotency_key,
# Revisions are monotonic across generations so a delayed request
# from an archived generation cannot pass CAS against a newer one
# that happens to be at the same lifecycle step.
diff --git a/server/services/deployment/handlers.py b/server/services/deployment/handlers.py
index 37788595..b5ceaeef 100644
--- a/server/services/deployment/handlers.py
+++ b/server/services/deployment/handlers.py
@@ -95,11 +95,70 @@ async def handle_deploy_workflow(data: Dict[str, Any], websocket: WebSocket) ->
workflow_id = data.get("workflow_id")
nodes = data.get("nodes", [])
edges = data.get("edges", [])
- from services.workflow_migrations import normalize_legacy_android_toolkit
-
- nodes, edges, normalized_parameters, migration_warnings = normalize_legacy_android_toolkit(
- nodes, edges, data.get("parameters_by_id")
+ from services.workflow_context_migration import (
+ import_legacy_context_receipts,
+ load_node_parameters,
+ persist_parameter_aliases,
)
+ from services.workflow_migrations import normalize_workflow_graph
+ from services.workflow_sanitizer import sanitize_workflow_graph
+
+ generation = int(data.get("generation") or 0)
+ if "graphVersion" in data:
+ requested_graph_version = int(data.get("graphVersion") or 0)
+ elif "graph_version" in data:
+ requested_graph_version = int(data.get("graph_version") or 0)
+ else:
+ requested_graph_version = 0
+ context_v2 = generation > 0 and requested_graph_version >= 2
+
+ parameters_by_id = data.get("parameters_by_id")
+ if not isinstance(parameters_by_id, dict):
+ parameters_by_id = await load_node_parameters(
+ container.database(),
+ nodes,
+ )
+ if context_v2:
+ normalization = normalize_workflow_graph(
+ str(workflow_id or ""),
+ nodes,
+ edges,
+ parameters_by_id,
+ )
+ safe_graph = sanitize_workflow_graph(normalization.graph_data())
+ nodes = list(safe_graph["nodes"])
+ edges = list(safe_graph["edges"])
+ normalized_parameters = normalization.node_parameters
+ migration_warnings = normalization.warnings
+ imported = await import_legacy_context_receipts(
+ container.database(),
+ normalization.state_imports,
+ )
+ await persist_parameter_aliases(
+ container.database(),
+ aliases=normalization.aliases,
+ parameters=normalized_parameters,
+ context_import_completed=(
+ imported == len(normalization.state_imports)
+ ),
+ )
+ graph_version = normalization.graph_version
+ graph_aliases = normalization.aliases
+ else:
+ # Generation zero is reserved for migration artifacts, and existing
+ # controlled V1 generations must replay their immutable topology.
+ safe_graph = sanitize_workflow_graph(
+ {"graphVersion": 0, "nodes": nodes, "edges": edges}
+ )
+ nodes = list(safe_graph["nodes"])
+ edges = list(safe_graph["edges"])
+ normalized_parameters = {
+ str(node_id): dict(value or {})
+ for node_id, value in parameters_by_id.items()
+ }
+ migration_warnings = []
+ graph_version = 0
+ graph_aliases = {}
if migration_warnings:
logger.warning("[Deploy] %s", "; ".join(migration_warnings))
session_id = data.get("session_id", "default")
@@ -133,6 +192,7 @@ async def handle_deploy_workflow(data: Dict[str, Any], websocket: WebSocket) ->
nodes=nodes,
edges=edges,
parameters_by_id=normalized_parameters,
+ enforce_context=context_v2,
)
if deploy_report["errors"]:
return {
@@ -205,6 +265,20 @@ async def handle_deploy_workflow(data: Dict[str, Any], websocket: WebSocket) ->
session_id=session_id,
status_callback=status_callback,
workflow_id=workflow_id,
+ graph_version=graph_version,
+ generation=int(data.get("generation") or 0),
+ user_id=str(
+ (
+ getattr(
+ getattr(websocket, "state", None),
+ "user_id",
+ None,
+ )
+ if websocket is not None
+ else data.get("user_id")
+ )
+ or "owner"
+ ),
)
if not result.get("success"):
@@ -284,6 +358,13 @@ async def handle_deploy_workflow(data: Dict[str, Any], websocket: WebSocket) ->
"is_running": True,
"locked": True,
"timestamp": time.time(),
+ "graph": {
+ "graphVersion": graph_version,
+ "nodes": nodes,
+ "edges": edges,
+ },
+ "aliases": graph_aliases,
+ "migration_warnings": migration_warnings,
}
@@ -1324,11 +1405,20 @@ async def _rearm_generation(control) -> None:
"workflow_id": control.workflow_id,
"nodes": nodes,
"edges": edges,
+ "generation": control.generation,
+ # Snapshots created before Context V2 deliberately retain version 0,
+ # so a process restart cannot mutate their Temporal command sequence.
+ "graphVersion": int(
+ snapshot.get("graphVersion")
+ or snapshot.get("graph_version")
+ or 0
+ ),
# Runtime persistence is generation-scoped (same contract as
# handle_start_workflow's deploy call).
"session_id": control.data_scope_id or control.execution_id,
"execution_id": control.execution_id,
"root_execution_id": control.root_execution_id,
+ "user_id": str(snapshot.get("owner_id") or "owner"),
}
deployed = await handle_deploy_workflow(deploy_data, None)
if not deployed.get("success"):
@@ -1455,7 +1545,6 @@ async def _rebuild_missing_controller(service: WorkflowControlService, control):
await _rearm_generation(control)
await _broadcast_control(control, extra={"recovery": "controller_rebuilt"})
return control
- return restored
async def _duplicate_start_response(
@@ -1512,6 +1601,10 @@ async def handle_get_workflow_control_status(data: Dict[str, Any], websocket: We
async def handle_start_workflow(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
"""Create generation one and retain deploy_workflow wire compatibility."""
workflow_id = data["workflow_id"]
+ owner_id = str(
+ getattr(getattr(websocket, "state", None), "user_id", None)
+ or "owner"
+ )
key = data.get("idempotency_key") or f"start:{workflow_id}:{uuid.uuid4().hex}"
service = _control_service()
existing = await service.database.get_workflow_control_by_idempotency_key(
@@ -1531,10 +1624,71 @@ async def handle_start_workflow(data: Dict[str, Any], websocket: WebSocket) -> D
expected_revision = int(data["expected_revision"])
if expected_revision != (latest.revision if latest else 0):
raise ValueError("control_revision_conflict")
+ if latest is not None and latest.status != "reset":
+ raise ValueError("workflow_already_started")
+
+ # Admit exactly the normalized, sanitized V2 graph. Doing this after the
+ # control row was created would leave restart recovery with a legacy
+ # snapshot/hash even though the live deployment had already migrated.
+ from services.workflow_context_migration import (
+ import_legacy_context_receipts,
+ load_node_parameters,
+ persist_parameter_aliases,
+ )
+ from services.workflow_migrations import normalize_workflow_graph
+ from services.workflow_sanitizer import sanitize_workflow_graph
+
+ raw_nodes = list(data.get("nodes") or [])
+ raw_edges = list(data.get("edges") or [])
+ parameters_by_id = data.get("parameters_by_id")
+ if not isinstance(parameters_by_id, dict):
+ parameters_by_id = await load_node_parameters(
+ service.database,
+ raw_nodes,
+ )
+ normalization = normalize_workflow_graph(
+ workflow_id,
+ raw_nodes,
+ raw_edges,
+ parameters_by_id,
+ )
+ safe_graph = sanitize_workflow_graph(normalization.graph_data())
+ admitted_nodes = list(safe_graph["nodes"])
+ admitted_edges = list(safe_graph["edges"])
+ from services.workflow_validator import validate_workflow
+ validation = await validate_workflow(
+ nodes=admitted_nodes,
+ edges=admitted_edges,
+ parameters_by_id=normalization.node_parameters,
+ )
+ if validation["errors"]:
+ return {
+ "success": False,
+ "error": "validation_failed",
+ "report": validation,
+ "graph": safe_graph,
+ "aliases": normalization.aliases,
+ "migration_warnings": normalization.warnings,
+ }
+ imported = await import_legacy_context_receipts(
+ service.database,
+ normalization.state_imports,
+ )
+ await persist_parameter_aliases(
+ service.database,
+ aliases=normalization.aliases,
+ parameters=normalization.node_parameters,
+ context_import_completed=(imported == len(normalization.state_imports)),
+ )
control, created = await service.begin_generation(
- workflow_id=workflow_id, nodes=data.get("nodes", []), edges=data.get("edges", []),
- session_id=data.get("session_id", "default"), idempotency_key=key,
+ workflow_id=workflow_id,
+ nodes=admitted_nodes,
+ edges=admitted_edges,
+ session_id=data.get("session_id", "default"),
+ idempotency_key=key,
+ graph_version=normalization.graph_version,
+ owner_id=owner_id,
)
if not created:
control, controller_status = await _reconcile_control(service, control)
@@ -1559,9 +1713,15 @@ async def handle_start_workflow(data: Dict[str, Any], websocket: WebSocket) -> D
# outputs for a controlled run.
deploy_data = {
**data,
+ "nodes": admitted_nodes,
+ "edges": admitted_edges,
+ "parameters_by_id": normalization.node_parameters,
+ "graphVersion": normalization.graph_version,
+ "generation": control.generation,
"session_id": control.data_scope_id or control.execution_id,
"execution_id": control.execution_id,
"root_execution_id": control.root_execution_id,
+ "user_id": owner_id,
}
deployed = await handle_deploy_workflow(deploy_data, websocket)
if not deployed.get("success"):
@@ -1615,7 +1775,17 @@ async def handle_start_workflow(data: Dict[str, Any], websocket: WebSocket) -> D
control,
controller_status=controller_status,
)
- return {"success": True, **payload}
+ return {
+ "success": True,
+ **payload,
+ "graph": {
+ "graphVersion": normalization.graph_version,
+ "nodes": admitted_nodes,
+ "edges": admitted_edges,
+ },
+ "aliases": normalization.aliases,
+ "migration_warnings": normalization.warnings,
+ }
@ws_handler("workflow_id")
diff --git a/server/services/deployment/manager.py b/server/services/deployment/manager.py
index 1b901b34..07f3c72c 100644
--- a/server/services/deployment/manager.py
+++ b/server/services/deployment/manager.py
@@ -167,6 +167,9 @@ class DeploymentManager:
session_id: str = "default",
status_callback: Optional[Callable] = None,
workflow_id: Optional[str] = None,
+ graph_version: int = 0,
+ generation: int = 0,
+ user_id: str = "owner",
) -> Dict[str, Any]:
"""Deploy workflow in event-driven mode.
@@ -176,6 +179,9 @@ class DeploymentManager:
session_id: Session identifier
status_callback: Status update callback
workflow_id: Workflow ID for per-workflow deployment tracking
+ graph_version: Normalized graph contract version
+ generation: Durable workflow-control generation
+ user_id: Authenticated server-owned user identity
"""
# Generate workflow_id if not provided
if not workflow_id:
@@ -225,6 +231,9 @@ class DeploymentManager:
nodes=nodes,
edges=edges,
session_id=session_id,
+ user_id=str(user_id or "owner"),
+ graph_version=max(0, int(graph_version or 0)),
+ generation=max(0, int(generation or 0)),
settings=self._settings.copy(),
)
@@ -776,6 +785,9 @@ class DeploymentManager:
# workflow_id for tests / direct-call paths that bypass deploy.
slug = state.workflow_slug or workflow_id
listener_id = self._listener_workflow_id(slug, trigger_label)
+ from services.temporal.executor import (
+ capture_temporal_routing_input,
+ )
# ``trigger_label`` flows into the per-firing run id
# (``<slug>-<label>-<event_id>``) inside both child workflows so
@@ -793,7 +805,16 @@ class DeploymentManager:
"nodes": state.nodes,
"edges": state.edges,
"session_id": state.session_id,
+ "user_id": state.user_id,
+ **capture_temporal_routing_input(),
}
+ if state.graph_version > 0 and state.generation > 0:
+ listener_args.update(
+ {
+ "graphVersion": state.graph_version,
+ "generation": state.generation,
+ }
+ )
if is_polling:
# ``version`` feeds the polling workflow's activity-name
# construction (``poll.{type}.v{version}``). Pulled from the
@@ -829,6 +850,7 @@ class DeploymentManager:
"execution_id": control.execution_id,
"root_execution_id": control.root_execution_id,
"data_scope_id": control.data_scope_id or control.execution_id,
+ "generation": control.generation,
})
# The controller owns listeners as child workflows. This keeps
# trigger activity in the deployment's Temporal execution tree
@@ -1177,14 +1199,25 @@ class DeploymentManager:
# Execute filtered graph with deployment's workflow_id for scoped status
# Use Temporal for proper parallel branch execution
status_callback = self._status_callbacks.get(workflow_id)
+ execution_kwargs: Dict[str, Any] = {
+ "nodes": filtered_nodes,
+ "edges": filtered_edges,
+ "session_id": run_session_id,
+ "status_callback": status_callback,
+ "skip_clear_outputs": True,
+ "workflow_id": workflow_id,
+ "use_temporal": True,
+ "user_id": state.user_id,
+ }
+ if state.graph_version >= 2 and state.generation > 0:
+ execution_kwargs.update(
+ {
+ "graph_version": state.graph_version,
+ "generation": state.generation,
+ }
+ )
result = await self._execute_workflow(
- nodes=filtered_nodes,
- edges=filtered_edges,
- session_id=run_session_id,
- status_callback=status_callback,
- skip_clear_outputs=True,
- workflow_id=workflow_id, # Pass deployment's workflow_id for status scoping
- use_temporal=True, # Force Temporal for parallel node execution
+ **execution_kwargs,
)
result["run_id"] = run_id
diff --git a/server/services/deployment/runtime_state.py b/server/services/deployment/runtime_state.py
index f4e7fe3d..fd138da6 100644
--- a/server/services/deployment/runtime_state.py
+++ b/server/services/deployment/runtime_state.py
@@ -12,6 +12,7 @@ async def archive_and_reset_node_state(control: Any, database: Any, broadcaster:
class owns any external mutable state through ``reset_execution_state``.
"""
from services.node_registry import get_node_class
+ from services.workflow_sanitizer import sanitize_runtime_payload
graph = control.graph_snapshot or {}
archived_nodes: Dict[str, Any] = {}
@@ -23,8 +24,10 @@ async def archive_and_reset_node_state(control: Any, database: Any, broadcaster:
params = await database.get_node_parameters(str(node_id)) or {}
archived_nodes[str(node_id)] = {
"type": node.get("type"),
- "canvas_data": dict(node.get("data") or {}),
- "parameters": dict(params),
+ "canvas_data": sanitize_runtime_payload(
+ dict(node.get("data") or {})
+ ),
+ "parameters": sanitize_runtime_payload(dict(params)),
}
scope_id = control.data_scope_id or control.execution_id
@@ -40,7 +43,10 @@ async def archive_and_reset_node_state(control: Any, database: Any, broadcaster:
continue
result = await node_class.reset_execution_state(
node_id=str(node_id), workflow_id=control.workflow_id,
- execution_id=control.execution_id, graph=graph, database=database,
+ execution_id=control.execution_id,
+ generation=int(getattr(control, "generation", 0) or 0),
+ graph=graph,
+ database=database,
)
if not result.get("reset"):
continue
diff --git a/server/services/deployment/state.py b/server/services/deployment/state.py
index fe57c431..fe3d4560 100644
--- a/server/services/deployment/state.py
+++ b/server/services/deployment/state.py
@@ -15,6 +15,12 @@ class DeploymentState:
nodes: List[Dict]
edges: List[Dict]
session_id: str
+ user_id: str = "owner"
+ # Context V2 cutover metadata. Both must be present for
+ # MachinaWorkflow to select AgentWorkflowV2; legacy/uncontrolled
+ # deployments leave them at zero and retain AgentWorkflow V1.
+ graph_version: int = 0
+ generation: int = 0
# Human-readable slug resolved from DB at deploy time. Used to
# prefix Temporal workflow IDs (listener + per-firing run) so
# operators browsing the Temporal Web UI can identify runs by
@@ -31,6 +37,9 @@ class DeploymentState:
"workflow_slug": self.workflow_slug,
"is_running": self.is_running,
"session_id": self.session_id,
+ "user_id": self.user_id,
+ "graph_version": self.graph_version,
+ "generation": self.generation,
"settings": self.settings,
"deployed_at": self.deployed_at,
"node_count": len(self.nodes),
diff --git a/server/services/example_loader.py b/server/services/example_loader.py
index 503a2b2a..93823161 100644
--- a/server/services/example_loader.py
+++ b/server/services/example_loader.py
@@ -54,7 +54,13 @@ async def import_examples_for_user(database) -> int:
Returns the count of workflows imported.
"""
from services.workflow_import import remap_node_ids
- from services.workflow_naming import canonicalize_node_ids, next_available_slug
+ from services.workflow_naming import next_available_slug
+ from services.workflow_migrations import normalize_workflow_graph
+ from services.workflow_context_migration import (
+ import_legacy_context_receipts,
+ persist_parameter_aliases,
+ )
+ from services.workflow_sanitizer import sanitize_workflow_graph
from services.workflow_validator import validate_workflow
examples = get_example_workflows()
@@ -74,11 +80,15 @@ async def import_examples_for_user(database) -> int:
example.get("edges", []),
example.get("nodeParameters", {}),
)
- nodes, edges, node_aliases = canonicalize_node_ids(workflow_id, nodes, edges)
- node_parameters = {
- node_aliases.get(node_id, node_id): params
- for node_id, params in node_parameters.items()
- }
+ normalization = normalize_workflow_graph(
+ workflow_id,
+ nodes,
+ edges,
+ node_parameters,
+ )
+ nodes = normalization.nodes
+ edges = normalization.edges
+ node_parameters = normalization.node_parameters
# Pre-save validation. parameters_by_id is the freshly-remapped
# map so INVALID_PARAM check sees the configured defaults instead
# of empty dicts.
@@ -118,20 +128,28 @@ async def import_examples_for_user(database) -> int:
name=example_name,
slug=slug,
description=example.get("description"),
- data={"nodes": nodes, "edges": edges},
+ data=sanitize_workflow_graph(
+ {
+ "graphVersion": normalization.graph_version,
+ "nodes": nodes,
+ "edges": edges,
+ }
+ ),
)
if success:
imported += 1
logger.info(f"Imported example: {example.get('name')}")
- # Save embedded nodeParameters under the remapped node ids.
- for node_id, params in node_parameters.items():
- if params:
- try:
- await database.save_node_parameters(node_id, params)
- logger.debug(f"Saved parameters for node {node_id}")
- except Exception as e:
- logger.error(f"Failed to save parameters for node {node_id}: {e}")
+ await import_legacy_context_receipts(
+ database,
+ normalization.state_imports,
+ )
+ await persist_parameter_aliases(
+ database,
+ aliases={},
+ parameters=node_parameters,
+ context_import_completed=True,
+ )
return imported
diff --git a/server/services/execution/executor.py b/server/services/execution/executor.py
index f135f515..b7caa737 100644
--- a/server/services/execution/executor.py
+++ b/server/services/execution/executor.py
@@ -759,9 +759,15 @@ class WorkflowExecutor:
target_handle = edge.get("targetHandle")
# Nodes connected to AI Agent config handles are sub-nodes
- # These handles: input-memory, input-tools, input-skill, input-teammates
+ # These handles: input-context, input-tools, input-skill, input-teammates
if target in ai_agent_node_ids and source and target_handle:
- if target_handle in ("input-memory", "input-tools", "input-skill", "input-teammates"):
+ if target_handle in (
+ "input-context",
+ "input-memory", # immutable V1 snapshot compatibility
+ "input-tools",
+ "input-skill",
+ "input-teammates",
+ ):
subnode_ids.add(source)
# Filter out config nodes and sub-nodes from execution
diff --git a/server/services/execution/models.py b/server/services/execution/models.py
index 82f3eb1b..f3d0f8e7 100644
--- a/server/services/execution/models.py
+++ b/server/services/execution/models.py
@@ -349,9 +349,15 @@ class ExecutionContext:
target_handle = edge.get("targetHandle")
# Nodes connected to AI Agent config handles are sub-nodes
- # These handles: input-memory, input-tools, input-skill, input-teammates
+ # These handles: input-context, input-tools, input-skill, input-teammates
if target in ai_agent_node_ids and source and target_handle:
- if target_handle in ("input-memory", "input-tools", "input-skill", "input-teammates"):
+ if target_handle in (
+ "input-context",
+ "input-memory", # immutable V1 snapshot compatibility
+ "input-tools",
+ "input-skill",
+ "input-teammates",
+ ):
subnode_ids.add(source)
# Initialize node executions for all nodes (excluding config nodes and sub-nodes)
diff --git a/server/services/handlers/tools.py b/server/services/handlers/tools.py
index 056a1d9a..e416697d 100644
--- a/server/services/handlers/tools.py
+++ b/server/services/handlers/tools.py
@@ -164,6 +164,12 @@ async def _dispatch_tool(tool_name: str, tool_args: Dict[str, Any], config: Dict
"edges": config.get("edges", []),
"workflow_id": config.get("workflow_id"),
"parent_node_id": config.get("parent_node_id"),
+ # Provider/runtime call identity is a trusted idempotency key for
+ # stateful tools. It is carried on NodeContext, never accepted from
+ # the model's JSON arguments.
+ "tool_call_id": config.get("tool_call_id"),
+ "operation_id": config.get("operation_id"),
+ "user_id": config.get("user_id", "owner"),
# Stable per-run id so session-keyed tools (browser) reuse one
# instance across the agent loop instead of falling back to a
# shared default session.
@@ -503,16 +509,31 @@ async def _execute_delegated_agent(
# Create execution context for child agent. Forward the parent's
# execution_id so session-keyed tools (browser) used by the child
# share the parent run's instance.
+ # Scope keys the child must inherit, or it resolves no context of its own
+ # and its turns are silently never journalled. Forwarded verbatim; the
+ # explicit keys below always win.
+ _inherited_scope = {
+ key: config[key]
+ for key in (
+ "graphVersion",
+ "generation",
+ "data_scope_id",
+ "context_execution_id",
+ "context_session_id",
+ "user_id",
+ "session_id",
+ "workspace_dir",
+ )
+ if key in config
+ }
child_context = {
+ **_inherited_scope,
"nodes": nodes,
"edges": edges,
"workflow_id": workflow_id,
"outputs": {},
"parent_task_id": task_id,
"execution_id": config.get("execution_id"),
- "root_execution_id": config.get("root_execution_id"),
- "delegation_depth": config.get("delegation_depth", 0),
- "team_id": config.get("team_id"),
"max_concurrent_subagents": config.get("max_concurrent_subagents", 3),
"max_delegation_depth": config.get("max_delegation_depth", 2),
"ai_service": config.get("ai_service"),
diff --git a/server/services/llm/messages.py b/server/services/llm/messages.py
index fd3703b5..8c0deb9d 100644
--- a/server/services/llm/messages.py
+++ b/server/services/llm/messages.py
@@ -32,10 +32,14 @@ def filter_empty_messages(messages: Sequence) -> List:
filtered.append(m)
continue
- # AI/assistant with tool_calls -- keep even if content is empty
+ # AI/assistant replay state must survive even when it has no rendered
+ # text. Provider compaction, reasoning signatures, and ordered output
+ # blocks are inputs to the next request, not presentation content.
if role in ("ai", "assistant"):
tool_calls = getattr(m, "tool_calls", None)
- if tool_calls:
+ provider_state = getattr(m, "provider_state", None)
+ blocks = getattr(m, "blocks", None)
+ if tool_calls or provider_state or blocks:
filtered.append(m)
continue
diff --git a/server/services/llm/protocol.py b/server/services/llm/protocol.py
index dfac60ec..68604f55 100644
--- a/server/services/llm/protocol.py
+++ b/server/services/llm/protocol.py
@@ -263,6 +263,9 @@ class LLMResponse:
thinking: Optional[str] = None
tool_calls: List[ToolCall] = field(default_factory=list)
usage: Usage = field(default_factory=Usage)
+ # Some context-management APIs expose active (post-compaction) usage at
+ # the top level and a separate iteration list for total billed work.
+ billing_usage: Optional[Usage] = None
model: str = ""
finish_reason: str = "stop"
raw: Any = None
@@ -271,6 +274,8 @@ class LLMResponse:
def __post_init__(self) -> None:
"""Keep the flat response API while exposing a replayable message."""
+ if self.billing_usage is None:
+ self.billing_usage = self.usage
if self.assistant_message is None:
blocks: List[ContentBlock] = []
if self.thinking:
@@ -718,6 +723,7 @@ class LLMProvider(Protocol):
max_tokens: int = 4096,
thinking: Optional[ThinkingConfig] = None,
tools: Optional[List[ToolDef]] = None,
+ context_management: Optional[Dict[str, Any]] = None,
) -> LLMResponse: ...
async def fetch_models(self, api_key: str) -> List[str]: ...
diff --git a/server/services/llm/providers/anthropic.py b/server/services/llm/providers/anthropic.py
index bb33f696..44780d7a 100644
--- a/server/services/llm/providers/anthropic.py
+++ b/server/services/llm/providers/anthropic.py
@@ -54,8 +54,10 @@ class AnthropicProvider:
max_tokens: int = 4096,
thinking: Optional[ThinkingConfig] = None,
tools: Optional[List[ToolDef]] = None,
+ context_management: Optional[Dict[str, Any]] = None,
) -> LLMResponse:
system, api_msgs = self._split_system(messages)
+ policy = self._model_policy(model)
params: Dict[str, Any] = {
"model": model,
@@ -65,21 +67,71 @@ class AnthropicProvider:
if system:
params["system"] = system
- # Thinking / extended thinking
- if thinking and thinking.enabled:
+ thinking_on = bool(thinking and thinking.enabled)
+
+ if policy["adaptive_thinking"]:
+ # ``budget_tokens`` was removed on these generations and is a 400,
+ # not a warning — the model chooses its own depth. ``disabled`` is
+ # itself rejected on Fable/Mythos, so "thinking off" is expressed by
+ # omitting the field and letting the model's own default stand.
+ if thinking_on:
+ params["thinking"] = {
+ "type": "adaptive",
+ # ``display`` defaults to "omitted" here, which would empty
+ # the reasoning text the node publishes downstream.
+ "display": "summarized",
+ }
+ elif thinking_on:
budget = max(1024, int(thinking.budget or 2048))
if max_tokens <= budget:
params["max_tokens"] = budget + 1024
params["thinking"] = {"type": "enabled", "budget_tokens": budget}
- params["temperature"] = 1 # required by Anthropic when thinking
- else:
- params["temperature"] = temperature
+
+ if policy["sampling_params"]:
+ # Anthropic requires temperature=1 alongside budget thinking.
+ params["temperature"] = 1 if thinking_on else temperature
# Tools
if tools:
params["tools"] = [self._to_api_tool(t) for t in tools]
- resp = await self._client.messages.create(**params)
+ # Always stream. The SDK refuses a non-streaming request whose
+ # ``max_tokens`` implies more than ten minutes of generation
+ # (``_calculate_nonstreaming_timeout``), and ``resolve_max_tokens``
+ # hands us the model's full output cap — 128K on every current
+ # Claude model — so the plain ``create()`` path raises
+ # ``ValueError: Streaming is required ...`` before the request is
+ # ever sent. ``get_final_message()`` returns the same accumulated
+ # ``Message`` the non-streaming call would have, so ``_normalize``
+ # is unaffected.
+ if (
+ isinstance(context_management, dict)
+ and context_management.get("type") == "compaction"
+ ):
+ threshold = int(
+ context_management.get("compact_threshold") or 0
+ )
+ edit: Dict[str, Any] = {
+ "type": "compact_20260112",
+ "pause_after_compaction": bool(
+ context_management.get("pause_after_compaction")
+ ),
+ }
+ if threshold >= 50_000:
+ edit["trigger"] = {
+ "type": "input_tokens",
+ "value": threshold,
+ }
+ stream_ctx = self._client.beta.messages.stream(
+ betas=["compact-2026-01-12"],
+ context_management={"edits": [edit]},
+ **params,
+ )
+ else:
+ stream_ctx = self._client.messages.stream(**params)
+
+ async with stream_ctx as stream:
+ resp = await stream.get_final_message()
return self._normalize(resp, model)
# ------------------------------------------------------------------
@@ -103,6 +155,30 @@ class AnthropicProvider:
# internals
# ------------------------------------------------------------------
+ def _model_policy(self, model: str) -> Dict[str, bool]:
+ """Resolve JSON-driven per-model request-shape quirks.
+
+ Mirrors ``OpenAIProvider._model_policy``: the model families that
+ dropped sampling parameters and the ``budget_tokens`` thinking form
+ are listed in ``llm_defaults.json`` rather than hardcoded here, so a
+ new Claude generation is a config edit.
+ """
+
+ from services.llm.config import LLM_DEFAULTS
+
+ config = LLM_DEFAULTS.get("providers", {}).get(self.provider_name, {})
+
+ def matches(key: str) -> bool:
+ return any(
+ model.startswith(prefix) or prefix in model
+ for prefix in config.get(key, ())
+ )
+
+ return {
+ "adaptive_thinking": matches("adaptive_thinking_models"),
+ "sampling_params": not matches("sampling_params_removed"),
+ }
+
def _split_system(self, messages: List[Message]):
"""Extract system message (Anthropic takes it as a top-level param)."""
system_parts = []
@@ -272,6 +348,16 @@ class AnthropicProvider:
),
}
)
+ elif block.type == "compaction":
+ summary = getattr(block, "content", "")
+ if not isinstance(summary, str):
+ summary = str(summary or "")
+ blocks.append(
+ ContentBlock(type="compaction", text=summary)
+ )
+ provider_blocks.append(
+ {"type": "compaction", "content": summary}
+ )
input_tokens = getattr(resp.usage, "input_tokens", 0) or 0
output_tokens = getattr(resp.usage, "output_tokens", 0) or 0
@@ -293,6 +379,25 @@ class AnthropicProvider:
cache_creation_tokens=cache_creation_tokens,
cache_read_tokens=cache_read_tokens,
)
+ iterations = getattr(resp.usage, "iterations", None) or ()
+ billed_input = 0
+ billed_output = 0
+ for iteration in iterations:
+ billed_input += int(
+ getattr(iteration, "input_tokens", 0) or 0
+ )
+ billed_output += int(
+ getattr(iteration, "output_tokens", 0) or 0
+ )
+ billing_usage = (
+ Usage(
+ input_tokens=billed_input,
+ output_tokens=billed_output,
+ total_tokens=billed_input + billed_output,
+ )
+ if billed_input or billed_output
+ else usage
+ )
content = "\n".join(text_parts)
thinking = "\n\n".join(thinking_parts) if thinking_parts else None
@@ -311,6 +416,7 @@ class AnthropicProvider:
thinking=thinking,
tool_calls=tool_calls,
usage=usage,
+ billing_usage=billing_usage,
model=model,
finish_reason=resp.stop_reason or "stop",
raw=resp,
diff --git a/server/services/llm/providers/gemini.py b/server/services/llm/providers/gemini.py
index c5b53844..e9130184 100644
--- a/server/services/llm/providers/gemini.py
+++ b/server/services/llm/providers/gemini.py
@@ -78,7 +78,9 @@ class GeminiProvider:
max_tokens: int = 4096,
thinking: Optional[ThinkingConfig] = None,
tools: Optional[List[ToolDef]] = None,
+ context_management: Optional[Dict[str, Any]] = None,
) -> LLMResponse:
+ del context_management # Gemini stays on portable Context checkpoints.
from google.genai import types
system_instruction, contents = self._split_system_and_contents(messages)
diff --git a/server/services/llm/providers/openai.py b/server/services/llm/providers/openai.py
index 378474d4..306ad8aa 100644
--- a/server/services/llm/providers/openai.py
+++ b/server/services/llm/providers/openai.py
@@ -62,11 +62,14 @@ class OpenAIProvider:
max_tokens: int = 4096,
thinking: Optional[ThinkingConfig] = None,
tools: Optional[List[ToolDef]] = None,
+ context_management: Optional[Dict[str, Any]] = None,
) -> LLMResponse:
model = self._clean_model(model)
policy = self._model_policy(model, thinking)
if policy["use_responses"] and (
- tools or self._has_responses_state(messages)
+ tools
+ or self._has_responses_state(messages)
+ or context_management
):
return await self._chat_responses(
messages=messages,
@@ -74,6 +77,7 @@ class OpenAIProvider:
max_tokens=max_tokens,
thinking=thinking,
tools=tools,
+ context_management=context_management,
)
params: Dict[str, Any] = {
@@ -128,6 +132,7 @@ class OpenAIProvider:
max_tokens: int,
thinking: Optional[ThinkingConfig],
tools: Optional[List[ToolDef]],
+ context_management: Optional[Dict[str, Any]],
) -> LLMResponse:
"""Run a self-contained Responses API turn for reasoning tool use."""
@@ -148,6 +153,20 @@ class OpenAIProvider:
params["tools"] = [
self._to_responses_tool(tool) for tool in tools
]
+ if (
+ isinstance(context_management, dict)
+ and context_management.get("type") == "compaction"
+ ):
+ threshold = int(
+ context_management.get("compact_threshold") or 0
+ )
+ if threshold > 0:
+ params["context_management"] = [
+ {
+ "type": "compaction",
+ "compact_threshold": threshold,
+ }
+ ]
response = await self._client.responses.create(**params)
return self._normalize_responses(response, model)
diff --git a/server/services/llm/providers/openrouter.py b/server/services/llm/providers/openrouter.py
index c8dcfc0c..b25e3bbb 100644
--- a/server/services/llm/providers/openrouter.py
+++ b/server/services/llm/providers/openrouter.py
@@ -49,7 +49,11 @@ class OpenRouterProvider(OpenAIProvider):
max_tokens: int = 4096,
thinking: Optional[ThinkingConfig] = None,
tools: Optional[List[ToolDef]] = None,
+ context_management: Optional[Dict[str, Any]] = None,
) -> LLMResponse:
+ # OpenAI-compatible endpoints must prove support independently; the
+ # OpenRouter adapter currently uses portable Context checkpoints.
+ del context_management
# Strip [FREE] prefix from model ID
clean_model = model.replace("[FREE] ", "")
return await super().chat(
@@ -59,6 +63,7 @@ class OpenRouterProvider(OpenAIProvider):
max_tokens=max_tokens,
thinking=thinking,
tools=tools,
+ context_management=None,
)
async def fetch_models(self, api_key: str) -> List[str]:
diff --git a/server/services/llm/unifier.py b/server/services/llm/unifier.py
index e3009c83..870959fb 100644
--- a/server/services/llm/unifier.py
+++ b/server/services/llm/unifier.py
@@ -88,6 +88,7 @@ class ChatUnifier:
max_tokens: int = 4096,
thinking: Optional[ThinkingConfig] = None,
tools: Optional[List[ToolDef]] = None,
+ context_management: Optional[Dict[str, Any]] = None,
sdk_max_retries: int = 2,
translate_errors: bool = True,
) -> LLMResponse:
@@ -112,6 +113,7 @@ class ChatUnifier:
max_tokens=max_tokens,
thinking=thinking,
tools=tools,
+ context_management=context_management,
)
except spec.sdk_exception_types as e:
error = LLMError.from_exception(provider, e)
diff --git a/server/services/memory/__init__.py b/server/services/memory/__init__.py
index c0724938..8df2197a 100644
--- a/server/services/memory/__init__.py
+++ b/server/services/memory/__init__.py
@@ -40,6 +40,10 @@ from services.memory.runtime import (
append_memory_turns_atomic,
update_memory_parameters_atomic,
)
+from services.memory.tool_store import (
+ MemoryScope,
+ MemoryToolStore,
+)
__all__ = [
# Markdown helpers — used by every agent bridge (aiAgent /
@@ -64,4 +68,7 @@ __all__ = [
# Transactional runtime writes.
"append_memory_turns_atomic",
"update_memory_parameters_atomic",
+ # Explicit durable Memory tool.
+ "MemoryScope",
+ "MemoryToolStore",
]
diff --git a/server/services/memory/tool_store.py b/server/services/memory/tool_store.py
new file mode 100644
index 00000000..2c7f2c76
--- /dev/null
+++ b/server/services/memory/tool_store.py
@@ -0,0 +1,877 @@
+"""Durable, namespaced storage for the explicit Simple Memory tool.
+
+This module deliberately owns its tables instead of adding memory-specific
+methods to :mod:`core.database`. Importing the Simple Memory plugin registers
+the SQLModel tables before normal ``Database.startup()`` calls
+``SQLModel.metadata.create_all``; :meth:`MemoryToolStore.ensure_schema` is a
+defensive path for standalone workers/tests that import the plugin later.
+
+Memory retrieval is lexical and durable. SQLite uses an FTS5 projection when
+available, with parameterized SQL/LIKE fallback on every platform. Embedding
+projections are rebuildable accelerators: generation failure is recorded but
+never makes the authoritative item unavailable.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import base64
+import hashlib
+import inspect
+import json
+import re
+import uuid
+from dataclasses import dataclass
+from datetime import datetime, timezone
+from typing import Any, Awaitable, Callable, Iterable, Literal, Optional, Sequence
+
+from pydantic import BaseModel, ConfigDict
+from sqlalchemy import Column, DateTime, JSON, Text, delete, func, or_, text
+from sqlmodel import Field, SQLModel, select
+
+from core.logging import get_logger
+
+logger = get_logger(__name__)
+
+EmbeddingFunction = Callable[[str], Awaitable[Sequence[float]] | Sequence[float]]
+
+
+def _utcnow() -> datetime:
+ return datetime.now(timezone.utc)
+
+
+class MemoryToolNamespace(SQLModel, table=True):
+ """Server-controlled isolation boundary for one Simple Memory node."""
+
+ __tablename__ = "agent_memory_namespaces"
+
+ id: str = Field(primary_key=True, max_length=80)
+ owner_id: str = Field(default="owner", index=True, max_length=255)
+ workflow_id: str = Field(index=True, max_length=255)
+ memory_node_id: str = Field(index=True, max_length=255)
+ created_at: datetime = Field(
+ default_factory=_utcnow,
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+ updated_at: datetime = Field(
+ default_factory=_utcnow,
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+
+
+class MemoryToolItem(SQLModel, table=True):
+ """Authoritative durable memory item."""
+
+ __tablename__ = "agent_memory_items"
+
+ id: str = Field(primary_key=True, max_length=64)
+ namespace_id: str = Field(index=True, max_length=80)
+ content: str = Field(sa_column=Column(Text, nullable=False))
+ title: Optional[str] = Field(default=None, max_length=500)
+ category: Optional[str] = Field(default=None, index=True, max_length=100)
+ tags: list[str] = Field(default_factory=list, sa_column=Column(JSON, nullable=False))
+ version: int = Field(default=1, ge=1)
+ expires_at: Optional[datetime] = Field(
+ default=None,
+ sa_column=Column(DateTime(timezone=True), nullable=True),
+ )
+ created_at: datetime = Field(
+ default_factory=_utcnow,
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+ updated_at: datetime = Field(
+ default_factory=_utcnow,
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+
+
+class MemoryToolEmbeddingProjection(SQLModel, table=True):
+ """Rebuildable semantic projection; never the source of truth."""
+
+ __tablename__ = "agent_memory_embedding_projections"
+
+ item_id: str = Field(primary_key=True, max_length=64)
+ namespace_id: str = Field(index=True, max_length=80)
+ # SQLModel 0.0.x cannot map typing.Literal directly on table models.
+ # Service code constrains this field to the two values below.
+ status: str = Field(max_length=20)
+ vector: Optional[list[float]] = Field(default=None, sa_column=Column(JSON))
+ error: Optional[str] = Field(default=None, max_length=2000)
+ updated_at: datetime = Field(
+ default_factory=_utcnow,
+ sa_column=Column(DateTime(timezone=True), nullable=False),
+ )
+
+
+@dataclass(frozen=True)
+class MemoryScope:
+ """Trusted scope derived from NodeContext, never from LLM arguments."""
+
+ owner_id: str
+ workflow_id: str
+ memory_node_id: str
+
+ @property
+ def namespace_id(self) -> str:
+ material = "\0".join(
+ (self.owner_id, self.workflow_id, self.memory_node_id)
+ ).encode("utf-8")
+ return "mem_" + hashlib.sha256(material).hexdigest()[:48]
+
+
+class MemoryStoreError(ValueError):
+ """Base class for user-correctable memory mutations."""
+
+
+class MemoryNotFoundError(MemoryStoreError):
+ pass
+
+
+class MemoryVersionConflictError(MemoryStoreError):
+ pass
+
+
+class MemoryItemView(BaseModel):
+ id: str
+ content: str
+ title: Optional[str] = None
+ category: Optional[str] = None
+ tags: list[str]
+ version: int
+ expires_at: Optional[datetime] = None
+ created_at: datetime
+ updated_at: datetime
+ indexing_state: Literal["lexical", "embedding_ready", "embedding_failed"] = (
+ "lexical"
+ )
+
+ model_config = ConfigDict(from_attributes=True)
+
+
+def _normalize_tags(tags: Optional[Iterable[str]]) -> list[str]:
+ normalized: list[str] = []
+ for tag in tags or ():
+ value = str(tag).strip().lower()
+ if value and value not in normalized:
+ normalized.append(value)
+ return normalized
+
+
+def _normalize_categories(categories: Optional[Iterable[str]]) -> list[str]:
+ return [
+ value
+ for value in dict.fromkeys(
+ str(category).strip().lower() for category in categories or ()
+ )
+ if value
+ ]
+
+
+def _encode_cursor(offset: int) -> str:
+ raw = json.dumps({"offset": offset}, separators=(",", ":")).encode("utf-8")
+ return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
+
+
+def _decode_cursor(cursor: Optional[str]) -> int:
+ if not cursor:
+ return 0
+ try:
+ padded = cursor + "=" * (-len(cursor) % 4)
+ decoded = json.loads(base64.urlsafe_b64decode(padded).decode("utf-8"))
+ offset = int(decoded["offset"])
+ except (KeyError, TypeError, ValueError, json.JSONDecodeError) as exc:
+ raise MemoryStoreError("Invalid memory cursor") from exc
+ if offset < 0 or offset > 1_000_000:
+ raise MemoryStoreError("Invalid memory cursor")
+ return offset
+
+
+class MemoryToolStore:
+ """Transactional CRUD/search service for one database instance."""
+
+ _schema_lock = asyncio.Lock()
+ _initialized_engines: set[Any] = set()
+ _engine_fts: dict[Any, bool] = {}
+
+ def __init__(
+ self,
+ database: Any,
+ *,
+ embedder: Optional[EmbeddingFunction] = None,
+ ) -> None:
+ self.database = database
+ self.embedder = embedder
+ self._fts_available = False
+
+ async def ensure_schema(self) -> None:
+ engine = getattr(self.database, "engine", None)
+ if engine is None:
+ raise RuntimeError("Database is not initialized")
+ if engine in self._initialized_engines:
+ self._fts_available = self._engine_fts.get(engine, False)
+ return
+ async with self._schema_lock:
+ if engine in self._initialized_engines:
+ self._fts_available = self._engine_fts.get(engine, False)
+ return
+ tables = (
+ MemoryToolNamespace.__table__,
+ MemoryToolItem.__table__,
+ MemoryToolEmbeddingProjection.__table__,
+ )
+ async with engine.begin() as connection:
+ for table_obj in tables:
+ await connection.run_sync(
+ lambda sync_connection, table_obj=table_obj: table_obj.create(
+ sync_connection, checkfirst=True
+ )
+ )
+ if engine.dialect.name == "sqlite":
+ try:
+ await connection.execute(
+ text(
+ "CREATE VIRTUAL TABLE IF NOT EXISTS "
+ "agent_memory_items_fts USING fts5("
+ "memory_id UNINDEXED, namespace_id UNINDEXED, "
+ "title, content, category, tags)"
+ )
+ )
+ self._fts_available = True
+ except Exception:
+ logger.warning(
+ "SQLite FTS5 unavailable; memory recall will use SQL",
+ exc_info=True,
+ )
+ self._fts_available = False
+ self._initialized_engines.add(engine)
+ self._engine_fts[engine] = self._fts_available
+
+ async def _ensure_namespace(self, session: Any, scope: MemoryScope) -> None:
+ namespace = await session.get(MemoryToolNamespace, scope.namespace_id)
+ if namespace is None:
+ session.add(
+ MemoryToolNamespace(
+ id=scope.namespace_id,
+ owner_id=scope.owner_id,
+ workflow_id=scope.workflow_id,
+ memory_node_id=scope.memory_node_id,
+ )
+ )
+ await session.flush()
+ return
+ # The identifier is content-addressed, but verify its material anyway
+ # so a database corruption cannot collapse tenant/node boundaries.
+ if (
+ namespace.owner_id != scope.owner_id
+ or namespace.workflow_id != scope.workflow_id
+ or namespace.memory_node_id != scope.memory_node_id
+ ):
+ raise RuntimeError("Memory namespace integrity check failed")
+ namespace.updated_at = _utcnow()
+ session.add(namespace)
+
+ @staticmethod
+ def _active_clause() -> Any:
+ now = _utcnow()
+ return or_(
+ MemoryToolItem.expires_at.is_(None),
+ MemoryToolItem.expires_at > now,
+ )
+
+ async def _projection_states(
+ self, session: Any, item_ids: Sequence[str]
+ ) -> dict[str, str]:
+ if not item_ids:
+ return {}
+ result = await session.execute(
+ select(MemoryToolEmbeddingProjection).where(
+ MemoryToolEmbeddingProjection.item_id.in_(item_ids)
+ )
+ )
+ return {row.item_id: row.status for row in result.scalars().all()}
+
+ @staticmethod
+ def _serialize_item(
+ item: MemoryToolItem, projection_status: Optional[str] = None
+ ) -> dict[str, Any]:
+ if projection_status == "ready":
+ indexing_state = "embedding_ready"
+ elif projection_status == "failed":
+ indexing_state = "embedding_failed"
+ else:
+ indexing_state = "lexical"
+ return MemoryItemView(
+ id=item.id,
+ content=item.content,
+ title=item.title,
+ category=item.category,
+ tags=list(item.tags or []),
+ version=item.version,
+ expires_at=item.expires_at,
+ created_at=item.created_at,
+ updated_at=item.updated_at,
+ indexing_state=indexing_state,
+ ).model_dump(mode="json")
+
+ async def _fts_upsert(self, session: Any, item: MemoryToolItem) -> None:
+ if not self._fts_available:
+ return
+ try:
+ await session.execute(
+ text(
+ "DELETE FROM agent_memory_items_fts "
+ "WHERE memory_id = :memory_id"
+ ),
+ {"memory_id": item.id},
+ )
+ await session.execute(
+ text(
+ "INSERT INTO agent_memory_items_fts "
+ "(memory_id, namespace_id, title, content, category, tags) "
+ "VALUES (:memory_id, :namespace_id, :title, :content, "
+ ":category, :tags)"
+ ),
+ {
+ "memory_id": item.id,
+ "namespace_id": item.namespace_id,
+ "title": item.title or "",
+ "content": item.content,
+ "category": item.category or "",
+ "tags": " ".join(item.tags or []),
+ },
+ )
+ except Exception:
+ # FTS is an acceleration projection. Losing it must not roll back
+ # an otherwise valid authoritative item mutation.
+ logger.warning("Failed to update memory FTS projection", exc_info=True)
+ self._fts_available = False
+ engine = getattr(self.database, "engine", None)
+ if engine is not None:
+ self._engine_fts[engine] = False
+
+ async def _fts_delete(self, session: Any, item_id: str) -> None:
+ if not self._fts_available:
+ return
+ try:
+ await session.execute(
+ text(
+ "DELETE FROM agent_memory_items_fts "
+ "WHERE memory_id = :memory_id"
+ ),
+ {"memory_id": item_id},
+ )
+ except Exception:
+ logger.warning("Failed to delete memory FTS projection", exc_info=True)
+ self._fts_available = False
+ engine = getattr(self.database, "engine", None)
+ if engine is not None:
+ self._engine_fts[engine] = False
+
+ async def _refresh_embedding(
+ self, namespace_id: str, item_id: str, content: str
+ ) -> str:
+ if self.embedder is None:
+ return "lexical"
+ status: Literal["ready", "failed"]
+ vector: Optional[list[float]]
+ error: Optional[str]
+ try:
+ generated = self.embedder(content)
+ if inspect.isawaitable(generated):
+ generated = await generated
+ vector = [float(value) for value in generated]
+ if not vector:
+ raise ValueError("embedding is empty")
+ status, error = "ready", None
+ except Exception as exc:
+ status, vector = "failed", None
+ error = str(exc)[:2000]
+ logger.warning(
+ "Memory embedding generation failed; lexical index remains active",
+ item_id=item_id,
+ exc_info=True,
+ )
+ async with self.database.get_session() as session:
+ row = await session.get(MemoryToolEmbeddingProjection, item_id)
+ if row is None:
+ row = MemoryToolEmbeddingProjection(
+ item_id=item_id,
+ namespace_id=namespace_id,
+ status=status,
+ )
+ row.status = status
+ row.vector = vector
+ row.error = error
+ row.updated_at = _utcnow()
+ session.add(row)
+ await session.commit()
+ return "embedding_ready" if status == "ready" else "embedding_failed"
+
+ async def remember(
+ self,
+ scope: MemoryScope,
+ *,
+ content: str,
+ title: Optional[str] = None,
+ category: Optional[str] = None,
+ tags: Optional[Iterable[str]] = None,
+ expires_at: Optional[datetime] = None,
+ operation_id: Optional[str] = None,
+ ) -> dict[str, Any]:
+ await self.ensure_schema()
+ item_id = uuid.uuid4().hex
+ normalized_category = str(category).strip().lower() if category else None
+ normalized_tags = _normalize_tags(tags)
+
+ async def _mutate(session: Any) -> dict[str, Any]:
+ await self._ensure_namespace(session, scope)
+ item = MemoryToolItem(
+ id=item_id,
+ namespace_id=scope.namespace_id,
+ content=content,
+ title=title,
+ category=normalized_category,
+ tags=normalized_tags,
+ expires_at=expires_at,
+ )
+ session.add(item)
+ await session.flush()
+ await self._fts_upsert(session, item)
+ return {
+ "operation": "remember",
+ "memory": self._serialize_item(item),
+ }
+
+ result, applied = await self.database.run_runtime_mutation(
+ resource_type="agent_memory",
+ resource_id=scope.namespace_id,
+ operation="remember",
+ mutate=_mutate,
+ mutation_id=operation_id,
+ )
+ if applied:
+ state = await self._refresh_embedding(
+ scope.namespace_id, item_id, content
+ )
+ if isinstance(result.get("memory"), dict):
+ result["memory"]["indexing_state"] = state
+ result["receipt"] = {
+ "operation_id": operation_id,
+ "applied": applied,
+ }
+ return result
+
+ async def get(
+ self, scope: MemoryScope, memory_id: str
+ ) -> dict[str, Any]:
+ await self.ensure_schema()
+ async with self.database.get_session() as session:
+ result = await session.execute(
+ select(MemoryToolItem).where(
+ MemoryToolItem.id == memory_id,
+ MemoryToolItem.namespace_id == scope.namespace_id,
+ self._active_clause(),
+ )
+ )
+ item = result.scalar_one_or_none()
+ if item is None:
+ raise MemoryNotFoundError("Memory item not found")
+ states = await self._projection_states(session, [item.id])
+ return {
+ "operation": "get",
+ "memory": self._serialize_item(item, states.get(item.id)),
+ }
+
+ async def list(
+ self,
+ scope: MemoryScope,
+ *,
+ categories: Optional[Iterable[str]] = None,
+ tags: Optional[Iterable[str]] = None,
+ limit: int = 20,
+ cursor: Optional[str] = None,
+ ) -> dict[str, Any]:
+ await self.ensure_schema()
+ category_filter = _normalize_categories(categories)
+ tag_filter = set(_normalize_tags(tags))
+ offset = _decode_cursor(cursor)
+ clauses = [
+ MemoryToolItem.namespace_id == scope.namespace_id,
+ self._active_clause(),
+ ]
+ if category_filter:
+ clauses.append(MemoryToolItem.category.in_(category_filter))
+ async with self.database.get_session() as session:
+ # Tags live in portable JSON. Scan bounded chunks so exact tag
+ # filtering behaves the same on SQLite and Postgres.
+ collected: list[tuple[MemoryToolItem, int]] = []
+ scanned = offset
+ while len(collected) < limit + 1 and scanned < offset + 1000:
+ batch_start = scanned
+ batch_size = max(50, limit * 2)
+ batch_result = await session.execute(
+ select(MemoryToolItem)
+ .where(*clauses)
+ .order_by(
+ MemoryToolItem.created_at.desc(),
+ MemoryToolItem.id.desc(),
+ )
+ .offset(scanned)
+ .limit(batch_size)
+ )
+ batch = list(batch_result.scalars().all())
+ if not batch:
+ break
+ for index, item in enumerate(batch):
+ scanned = batch_start + index + 1
+ if tag_filter and not tag_filter.issubset(set(item.tags or [])):
+ continue
+ collected.append((item, scanned))
+ if len(collected) >= limit + 1:
+ break
+ if len(batch) < batch_size:
+ break
+ page_entries = collected[:limit]
+ page = [item for item, _position in page_entries]
+ states = await self._projection_states(
+ session, [item.id for item in page]
+ )
+ has_more = len(collected) > limit
+ next_offset = (
+ page_entries[-1][1] if page_entries else scanned
+ )
+ return {
+ "operation": "list",
+ "items": [
+ self._serialize_item(item, states.get(item.id))
+ for item in page
+ ],
+ "count": len(page),
+ "next_cursor": (
+ _encode_cursor(next_offset) if has_more else None
+ ),
+ "retrieval": "sql",
+ }
+
+ async def _fts_ids(
+ self, namespace_id: str, query: str
+ ) -> Optional[list[str]]:
+ if not self._fts_available:
+ return None
+ tokens = [
+ token.lower()
+ for token in re.findall(r"[^\W_]+", query, flags=re.UNICODE)
+ if token
+ ][:20]
+ if not tokens:
+ return []
+ # Tokens are reduced to Unicode word characters before interpolation;
+ # all scope/data remains parameterized.
+ match_query = " AND ".join(f'"{token}"*' for token in tokens)
+ try:
+ async with self.database.get_session() as session:
+ result = await session.execute(
+ text(
+ "SELECT memory_id FROM agent_memory_items_fts "
+ "WHERE namespace_id = :namespace_id "
+ "AND agent_memory_items_fts MATCH :query "
+ "ORDER BY bm25(agent_memory_items_fts) LIMIT 1000"
+ ),
+ {"namespace_id": namespace_id, "query": match_query},
+ )
+ return [str(row[0]) for row in result.fetchall()]
+ except Exception:
+ logger.warning(
+ "Memory FTS query failed; using SQL lexical fallback",
+ exc_info=True,
+ )
+ self._fts_available = False
+ engine = getattr(self.database, "engine", None)
+ if engine is not None:
+ self._engine_fts[engine] = False
+ return None
+
+ async def recall(
+ self,
+ scope: MemoryScope,
+ *,
+ query: str,
+ categories: Optional[Iterable[str]] = None,
+ tags: Optional[Iterable[str]] = None,
+ limit: int = 20,
+ cursor: Optional[str] = None,
+ ) -> dict[str, Any]:
+ await self.ensure_schema()
+ category_filter = _normalize_categories(categories)
+ tag_filter = set(_normalize_tags(tags))
+ offset = _decode_cursor(cursor)
+ ranked_ids = await self._fts_ids(scope.namespace_id, query)
+ retrieval = "fts" if ranked_ids is not None else "sql"
+ async with self.database.get_session() as session:
+ clauses = [
+ MemoryToolItem.namespace_id == scope.namespace_id,
+ self._active_clause(),
+ ]
+ if category_filter:
+ clauses.append(MemoryToolItem.category.in_(category_filter))
+ if ranked_ids is not None:
+ if not ranked_ids:
+ candidates: list[MemoryToolItem] = []
+ else:
+ selected = await session.execute(
+ select(MemoryToolItem).where(
+ *clauses, MemoryToolItem.id.in_(ranked_ids)
+ )
+ )
+ by_id = {item.id: item for item in selected.scalars().all()}
+ candidates = [
+ by_id[item_id]
+ for item_id in ranked_ids
+ if item_id in by_id
+ ]
+ else:
+ terms = [
+ token.lower()
+ for token in re.findall(
+ r"[^\W_]+", query, flags=re.UNICODE
+ )
+ if token
+ ][:20]
+ for term in terms:
+ pattern = f"%{term}%"
+ clauses.append(
+ or_(
+ func.lower(MemoryToolItem.content).like(pattern),
+ func.lower(
+ func.coalesce(MemoryToolItem.title, "")
+ ).like(pattern),
+ func.lower(
+ func.coalesce(MemoryToolItem.category, "")
+ ).like(pattern),
+ )
+ )
+ selected = await session.execute(
+ select(MemoryToolItem)
+ .where(*clauses)
+ .order_by(
+ MemoryToolItem.updated_at.desc(),
+ MemoryToolItem.id.desc(),
+ )
+ .limit(1000)
+ )
+ candidates = list(selected.scalars().all())
+ filtered = [
+ item
+ for item in candidates
+ if not tag_filter
+ or tag_filter.issubset(set(item.tags or []))
+ ]
+ page = filtered[offset : offset + limit]
+ states = await self._projection_states(
+ session, [item.id for item in page]
+ )
+ next_offset = offset + len(page)
+ return {
+ "operation": "recall",
+ "items": [
+ self._serialize_item(item, states.get(item.id))
+ for item in page
+ ],
+ "count": len(page),
+ "next_cursor": (
+ _encode_cursor(next_offset)
+ if next_offset < len(filtered)
+ else None
+ ),
+ "retrieval": retrieval,
+ }
+
+ async def update(
+ self,
+ scope: MemoryScope,
+ *,
+ memory_id: str,
+ expected_version: int,
+ patch: dict[str, Any],
+ operation_id: Optional[str] = None,
+ ) -> dict[str, Any]:
+ await self.ensure_schema()
+
+ async def _mutate(session: Any) -> dict[str, Any]:
+ result = await session.execute(
+ select(MemoryToolItem)
+ .where(
+ MemoryToolItem.id == memory_id,
+ MemoryToolItem.namespace_id == scope.namespace_id,
+ self._active_clause(),
+ )
+ .with_for_update()
+ )
+ item = result.scalar_one_or_none()
+ if item is None:
+ raise MemoryNotFoundError("Memory item not found")
+ if item.version != expected_version:
+ raise MemoryVersionConflictError(
+ f"Memory version conflict: expected {expected_version}, "
+ f"current {item.version}"
+ )
+ for key, value in patch.items():
+ if key == "tags":
+ value = _normalize_tags(value)
+ elif key == "category" and value is not None:
+ value = str(value).strip().lower() or None
+ setattr(item, key, value)
+ item.version += 1
+ item.updated_at = _utcnow()
+ session.add(item)
+ await session.flush()
+ await self._fts_upsert(session, item)
+ return {
+ "operation": "update",
+ "memory": self._serialize_item(item),
+ }
+
+ result, applied = await self.database.run_runtime_mutation(
+ resource_type="agent_memory",
+ resource_id=scope.namespace_id,
+ operation="update",
+ mutate=_mutate,
+ mutation_id=operation_id,
+ )
+ memory = result.get("memory") or {}
+ if applied and memory:
+ state = await self._refresh_embedding(
+ scope.namespace_id,
+ memory_id,
+ str(memory.get("content") or ""),
+ )
+ memory["indexing_state"] = state
+ result["receipt"] = {
+ "operation_id": operation_id,
+ "applied": applied,
+ }
+ return result
+
+ async def forget(
+ self,
+ scope: MemoryScope,
+ *,
+ memory_id: str,
+ expected_version: int,
+ operation_id: Optional[str] = None,
+ ) -> dict[str, Any]:
+ await self.ensure_schema()
+
+ async def _mutate(session: Any) -> dict[str, Any]:
+ result = await session.execute(
+ select(MemoryToolItem)
+ .where(
+ MemoryToolItem.id == memory_id,
+ MemoryToolItem.namespace_id == scope.namespace_id,
+ )
+ .with_for_update()
+ )
+ item = result.scalar_one_or_none()
+ if item is None:
+ raise MemoryNotFoundError("Memory item not found")
+ if item.version != expected_version:
+ raise MemoryVersionConflictError(
+ f"Memory version conflict: expected {expected_version}, "
+ f"current {item.version}"
+ )
+ forgotten = self._serialize_item(item)
+ await self._fts_delete(session, item.id)
+ await session.execute(
+ delete(MemoryToolEmbeddingProjection).where(
+ MemoryToolEmbeddingProjection.item_id == item.id
+ )
+ )
+ await session.delete(item)
+ return {"operation": "forget", "memory": forgotten}
+
+ result, applied = await self.database.run_runtime_mutation(
+ resource_type="agent_memory",
+ resource_id=scope.namespace_id,
+ operation="forget",
+ mutate=_mutate,
+ mutation_id=operation_id,
+ )
+ result["receipt"] = {
+ "operation_id": operation_id,
+ "applied": applied,
+ }
+ return result
+
+ async def clear_namespace(
+ self,
+ scope: MemoryScope,
+ *,
+ operation_id: Optional[str] = None,
+ ) -> dict[str, Any]:
+ """Clear durable items only; namespace and receipts remain durable."""
+ await self.ensure_schema()
+
+ async def _mutate(session: Any) -> dict[str, Any]:
+ await self._ensure_namespace(session, scope)
+ result = await session.execute(
+ select(MemoryToolItem.id).where(
+ MemoryToolItem.namespace_id == scope.namespace_id
+ )
+ )
+ item_ids = list(result.scalars().all())
+ if self._fts_available:
+ try:
+ await session.execute(
+ text(
+ "DELETE FROM agent_memory_items_fts "
+ "WHERE namespace_id = :namespace_id"
+ ),
+ {"namespace_id": scope.namespace_id},
+ )
+ except Exception:
+ logger.warning(
+ "Failed to clear memory FTS projection", exc_info=True
+ )
+ self._fts_available = False
+ engine = getattr(self.database, "engine", None)
+ if engine is not None:
+ self._engine_fts[engine] = False
+ await session.execute(
+ delete(MemoryToolEmbeddingProjection).where(
+ MemoryToolEmbeddingProjection.namespace_id
+ == scope.namespace_id
+ )
+ )
+ await session.execute(
+ delete(MemoryToolItem).where(
+ MemoryToolItem.namespace_id == scope.namespace_id
+ )
+ )
+ return {"operation": "clear", "cleared": len(item_ids)}
+
+ result, applied = await self.database.run_runtime_mutation(
+ resource_type="agent_memory",
+ resource_id=scope.namespace_id,
+ operation="clear",
+ mutate=_mutate,
+ mutation_id=operation_id,
+ )
+ result["receipt"] = {
+ "operation_id": operation_id,
+ "applied": applied,
+ }
+ return result
+
+
+__all__ = [
+ "EmbeddingFunction",
+ "MemoryItemView",
+ "MemoryNotFoundError",
+ "MemoryScope",
+ "MemoryStoreError",
+ "MemoryToolEmbeddingProjection",
+ "MemoryToolItem",
+ "MemoryToolNamespace",
+ "MemoryToolStore",
+ "MemoryVersionConflictError",
+]
diff --git a/server/services/plugin/base.py b/server/services/plugin/base.py
index 9c591722..15812a86 100644
--- a/server/services/plugin/base.py
+++ b/server/services/plugin/base.py
@@ -132,6 +132,7 @@ class BaseNode:
node_id: str,
workflow_id: str,
execution_id: str,
+ generation: int,
graph: Dict[str, Any],
database: Any,
) -> Dict[str, Any]:
@@ -163,6 +164,11 @@ class BaseNode:
component_kind: ClassVar[str] = "generic"
usable_as_tool: ClassVar[bool] = False
+ # Explicit agent capability used by graph validation/migration. Rendering
+ # kind and palette group are intentionally not proxies: several
+ # non-agents render with componentKind="agent", while Codex-style agents
+ # have historically been missing from central type lists.
+ requires_context: ClassVar[bool] = False
# Whether this plugin requires the parent workflow's full canvas
# (``nodes`` + ``edges`` arrays) in its NodeContext. Default ``False``
@@ -300,8 +306,31 @@ class BaseNode:
meta["subtitle"] = cls.subtitle
if color:
meta["color"] = color
- if cls.handles:
- meta["handles"] = list(cls.handles)
+ handles = [dict(handle) for handle in cls.handles]
+ is_invokable_tool = cls.usable_as_tool or (
+ cls.component_kind == "tool"
+ and cls.ui_hints.get("isMasterSkillEditor") is not True
+ )
+ if is_invokable_tool and not any(
+ handle.get("name") == "output-tool"
+ and handle.get("kind") == "output"
+ for handle in handles
+ ):
+ # The backend NodeSpec is the topology source of truth. Before
+ # generic handle rendering, SquareNode synthesized this endpoint
+ # from frontend group membership, which left dual-purpose plugin
+ # specs incomplete and made their tool connection disappear.
+ handles.append(
+ {
+ "name": "output-tool",
+ "kind": "output",
+ "position": "top",
+ "label": "Tool",
+ "role": "tools",
+ }
+ )
+ if handles:
+ meta["handles"] = handles
if cls.credentials:
meta["credentials"] = [c.id for c in cls.credentials]
if cls.hide_output_handle:
@@ -312,6 +341,8 @@ class BaseNode:
meta["visibility"] = cls.visibility
ui_hints = _derive_auto_ui_hints(cls.group)
ui_hints.update(cls.ui_hints)
+ if cls.requires_context:
+ ui_hints["requiresContext"] = True
# How long this node may legitimately run, so the client can size its
# request budget instead of keeping its own list of "slow" node types.
#
@@ -433,14 +464,26 @@ class BaseNode:
if isinstance(context.raw, dict):
context.raw["_raw_parameters"] = parameters
- try:
- params_obj = self._validate_params(parameters)
- except ValidationError as e:
- return self._wrap_error(
- start_time=start_time,
- error=f"Invalid parameters: {e.errors()[0].get('msg', str(e))}",
- error_type="ValidationError",
- )
+ # Split-schema ToolNodes arrive here only through execute_as_tool,
+ # which has already validated model arguments against ToolInput and
+ # persisted configuration against Params. Reuse that exact object so
+ # the operation cannot accidentally observe a merged payload.
+ validated_tool_input = (
+ context.raw.get("_validated_tool_input")
+ if context.raw.get("_split_tool_schema") is True
+ else None
+ )
+ if isinstance(validated_tool_input, BaseModel):
+ params_obj = validated_tool_input
+ else:
+ try:
+ params_obj = self._validate_params(parameters)
+ except ValidationError as e:
+ return self._wrap_error(
+ start_time=start_time,
+ error=f"Invalid parameters: {e.errors()[0].get('msg', str(e))}",
+ error_type="ValidationError",
+ )
op_name = self._pick_operation(parameters)
op_spec = self._operations.get(op_name)
@@ -532,10 +575,22 @@ class BaseNode:
node_params: Dict[str, Any],
context: NodeContext,
) -> Dict[str, Any]:
- """LLM-invoked tool call. The AI model supplies ``tool_args`` for
- fields it decides to fill; ``node_params`` carries static config
- the user set on the node (e.g. an API endpoint base URL). Merge
- wins for the LLM so it can override.
+ """LLM-invoked tool call with separate input/config validation.
+
+ The AI model supplies ``tool_args`` while ``node_params`` carries
+ persisted configuration. ToolNode subclasses may declare a distinct
+ ``ToolInput`` model. The two payloads are validated independently and
+ made available to the operation as follows:
+
+ * Default/legacy tools (``ToolInput is Params``) receive the combined
+ Params object. Model arguments retain their historical precedence,
+ except fields explicitly listed in ``server_controlled_fields``.
+ * Split-schema tools receive the validated ToolInput object; validated
+ configuration is available only through
+ ``ctx.raw["_tool_config"]``. It is never copied into model input.
+
+ Trusted workflow/node scope always remains on NodeContext and is never
+ sourced from model arguments.
Unwraps the :meth:`_wrap_success` envelope to a flat dict —
tool-call responses fed back into an LLM shouldn't include
@@ -546,8 +601,84 @@ class BaseNode:
method is idempotent there. ActionNode+``usable_as_tool`` classes
get their ``{success, result}`` envelope unwrapped.
"""
- merged = {**node_params, **tool_args}
- envelope = await self.execute(context.node_id, merged, context)
+ from services.plugin.tool import ToolNode
+
+ is_tool_node = isinstance(self, ToolNode)
+ if not is_tool_node:
+ # Dual-purpose ActionNodes retain the established tool contract.
+ # ToolInput is a ToolNode extension and must not change them.
+ envelope = await self.execute(
+ context.node_id,
+ {**node_params, **tool_args},
+ context,
+ )
+ if "success" not in envelope:
+ return envelope
+ if envelope.get("success") is False:
+ return {
+ "error": envelope.get("error", "tool execution failed")
+ }
+ result = envelope.get("result")
+ return result if isinstance(result, dict) else {"result": result}
+
+ input_model = type(self).tool_input_model()
+
+ try:
+ validated_input = input_model.model_validate(tool_args)
+ # Only arguments the model actually supplied participate in the
+ # compatibility merge. Otherwise ToolInput defaults would
+ # overwrite an operator's persisted value even though the model
+ # never attempted to set that field.
+ input_payload = validated_input.model_dump(exclude_unset=True)
+
+ if input_model is self.Params:
+ # Validate the persisted subset on its own before combining
+ # it with invocation arguments. Required Params fields are
+ # often model-supplied on legacy tools, hence the derived
+ # partial model rather than treating an omitted argument as a
+ # malformed node configuration.
+ type(self).partial_config_model().model_validate(node_params)
+ # Preserve the legacy contract for existing plugins, while
+ # allowing a plugin to lock specific persisted settings.
+ effective = {**node_params, **input_payload}
+ for field_name in getattr(type(self), "server_controlled_fields", ()):
+ if field_name in node_params:
+ effective[field_name] = node_params[field_name]
+ validated_config = self.Params.model_validate(effective)
+ invocation_payload = validated_config.model_dump()
+ split_schema = False
+ else:
+ validated_config = self.Params.model_validate(node_params)
+ invocation_payload = input_payload
+ split_schema = True
+ except ValidationError as e:
+ first = e.errors()[0] if e.errors() else {}
+ return {
+ "error": f"Invalid tool input/configuration: {first.get('msg', str(e))}",
+ "error_type": "ValidationError",
+ }
+
+ # A NodeContext belongs to one invocation, but preserve pre-existing
+ # escape-hatch values for tests/adapters that deliberately reuse one.
+ marker = object()
+ previous_config = context.raw.get("_tool_config", marker)
+ previous_input = context.raw.get("_validated_tool_input", marker)
+ previous_split = context.raw.get("_split_tool_schema", marker)
+ context.raw["_tool_config"] = validated_config
+ context.raw["_validated_tool_input"] = validated_input
+ context.raw["_split_tool_schema"] = split_schema
+ try:
+ envelope = await self.execute(context.node_id, invocation_payload, context)
+ finally:
+ for key, previous in (
+ ("_tool_config", previous_config),
+ ("_validated_tool_input", previous_input),
+ ("_split_tool_schema", previous_split),
+ ):
+ if previous is marker:
+ context.raw.pop(key, None)
+ else:
+ context.raw[key] = previous
# ToolNode skips the envelope wrap entirely — its _wrap_success
# returns the flat Output dict directly. Detect by the absence
# of the {success, ...} envelope keys.
@@ -774,6 +905,43 @@ class BaseNode:
from core.container import container
from services.status_broadcaster import get_status_broadcaster
+ if context.get("protocol") == "agent-context-tool-v2":
+ from services.temporal.agent_activities import (
+ execute_tool_activity,
+ )
+
+ pending = dict(context.get("pending_tool") or {})
+ tool_node_id = str(pending.get("node_id") or "")
+ activity.heartbeat(
+ f"Executing Context V2 tool {cls.type}: {tool_node_id}"
+ )
+ beat_task: Optional[asyncio.Task] = None
+ if cls.start_to_close_timeout > cls.heartbeat_timeout:
+
+ async def _tool_beat_loop() -> None:
+ while True:
+ await asyncio.sleep(30)
+ activity.heartbeat(
+ f"Still executing Context V2 tool "
+ f"{cls.type}: {tool_node_id}"
+ )
+
+ beat_task = asyncio.create_task(_tool_beat_loop())
+ try:
+ result = await execute_tool_activity(
+ context,
+ expected_node_type=cls.type,
+ expected_version=cls.version,
+ )
+ activity.heartbeat(
+ f"Context V2 tool {cls.type} completed: "
+ f"{tool_node_id}"
+ )
+ return result
+ finally:
+ if beat_task is not None:
+ beat_task.cancel()
+
node_id = context["node_id"]
workflow_id = context.get("workflow_id")
execution_id = context.get("execution_id")
@@ -883,6 +1051,7 @@ class BaseNode:
workflow_id=workflow_id,
outputs=context.get("inputs", {}),
extras=extras or None,
+ user_id=str(context.get("user_id") or "owner"),
)
result["node_id"] = node_id
diff --git a/server/services/plugin/edge_walker.py b/server/services/plugin/edge_walker.py
index ddc98aa2..3d19615a 100644
--- a/server/services/plugin/edge_walker.py
+++ b/server/services/plugin/edge_walker.py
@@ -8,7 +8,7 @@ module. Used by :class:`AIAgentNode`, :class:`ChatAgentNode`,
Three helpers:
-- :func:`collect_agent_connections` — walks ``input-memory``,
+- :func:`collect_agent_connections` — walks ``input-context``,
``input-skill``, ``input-tools``, ``input-main`` / ``input-chat``,
``input-task`` edges into a single tuple. Knows about the
``masterSkill`` expansion + direct Android service tools +
@@ -130,6 +130,47 @@ def register_master_skill_expander(fn: MasterSkillExpander) -> None:
_MASTER_SKILL_EXPANDER = fn
+# ---- Agent-Context descriptor builder registry ---------------------------
+
+AgentContextBuilder = Callable[
+ [str, Dict[str, Any], Any],
+ Awaitable[Optional[Dict[str, Any]]],
+]
+"""Async callable that turns a Context node connected on ``input-context``
+into the descriptor the agent runtime consumes. Signature:
+``(source_node_id, context, database) -> descriptor | None``.
+
+Returning ``None`` means "this connection contributes no context" and the
+edge is skipped. The framework does not know the descriptor's shape — the
+plugin that owns the node owns its keys."""
+
+_AGENT_CONTEXT_BUILDER: Optional[AgentContextBuilder] = None
+
+
+def register_agent_context_builder(fn: AgentContextBuilder) -> None:
+ """Publish the Context descriptor builder.
+
+ Idempotent on equality. ``nodes.context`` registers its builder from
+ ``__init__.py`` on package import. Edge-walking code calls
+ :func:`get_agent_context_builder` and runs whatever is registered, so
+ the framework carries no knowledge of the Context node's parameters,
+ thread-selection rules, or descriptor keys.
+ """
+ global _AGENT_CONTEXT_BUILDER
+ if _AGENT_CONTEXT_BUILDER is not None and _AGENT_CONTEXT_BUILDER != fn:
+ raise ValueError(
+ "register_agent_context_builder: callback already registered "
+ "by a different callable; refusing to overwrite"
+ )
+ _AGENT_CONTEXT_BUILDER = fn
+
+
+def get_agent_context_builder() -> Optional[AgentContextBuilder]:
+ """Return the registered Context builder, or None when no plugin has
+ wired one (in which case ``input-context`` edges contribute nothing)."""
+ return _AGENT_CONTEXT_BUILDER
+
+
def get_master_skill_expander() -> Optional[MasterSkillExpander]:
"""Return the registered expander callback, or None when no skill
plugin has wired it (in which case Master-Skill nodes silently
@@ -143,7 +184,7 @@ async def collect_agent_connections(
database: "Database",
log_prefix: str = "[Agent]",
) -> Tuple[
- Optional[Dict[str, Any]], # memory_data
+ Optional[Dict[str, Any]], # context_data (legacy memory_data on V1)
List[Dict[str, Any]], # skill_data
List[Dict[str, Any]], # tool_data
Optional[Dict[str, Any]], # input_data
@@ -152,14 +193,16 @@ async def collect_agent_connections(
"""Walk edges targeting ``node_id`` and collect everything an agent
needs from its connected nodes.
- Returns ``(memory, skills, tools, input, task)``. See module
+ Returns ``(context, skills, tools, input, task)``. The first item is an
+ Agent Context reference/policy descriptor for V2 graphs; immutable V1
+ snapshots may still yield their legacy Memory descriptor.
docstring for behaviour notes.
"""
nodes = context.get("nodes")
edges = context.get("edges")
workflow_id = context.get("workflow_id")
- memory_data: Optional[Dict[str, Any]] = None
+ context_data: Optional[Dict[str, Any]] = None
skill_data: List[Dict[str, Any]] = []
tool_data: List[Dict[str, Any]] = []
input_data: Optional[Dict[str, Any]] = None
@@ -173,7 +216,7 @@ async def collect_agent_connections(
)
if not edges or not nodes:
- return memory_data, skill_data, tool_data, input_data, task_data
+ return context_data, skill_data, tool_data, input_data, task_data
incoming_edges = [e for e in edges if e.get("target") == node_id]
logger.info(f"{log_prefix} Incoming edges to {node_id}: {len(incoming_edges)}")
@@ -196,9 +239,23 @@ async def collect_agent_connections(
if not source_node:
continue
- if target_handle == "input-memory":
+ if target_handle == "input-context":
+ # The descriptor's shape belongs to the plugin that owns the
+ # node, not to the framework. Whatever is registered decides
+ # whether this edge contributes context at all.
+ builder = get_agent_context_builder()
+ if builder is not None:
+ built = await builder(
+ str(source_node_id or ""),
+ {**context, "workflow_id": str(workflow_id or "")},
+ database,
+ )
+ if built:
+ context_data = built
+ elif target_handle == "input-memory":
+ # Immutable V1 workflow snapshots retain their recorded behavior.
if source_node.get("type") == "simpleMemory":
- memory_data = await _build_memory_entry(
+ context_data = await _build_memory_entry(
node_id,
source_node_id,
database,
@@ -266,7 +323,7 @@ async def collect_agent_connections(
logger.info(
f"{log_prefix} Collected: {len(skill_data)} skills, {len(tool_data)} tools, "
- f"memory={'yes' if memory_data else 'no'}, "
+ f"context={'yes' if context_data else 'no'}, "
f"input={'yes' if input_data else 'no'}, "
f"task={'yes' if task_data else 'no'}"
)
@@ -275,7 +332,7 @@ async def collect_agent_connections(
for td in tool_data:
logger.info(f"{log_prefix} Tool: type={td.get('node_type')}, node_id={td.get('node_id')}")
- return memory_data, skill_data, tool_data, input_data, task_data
+ return context_data, skill_data, tool_data, input_data, task_data
async def _build_memory_entry(
diff --git a/server/services/plugin/tool.py b/server/services/plugin/tool.py
index fde78a30..e70a7681 100644
--- a/server/services/plugin/tool.py
+++ b/server/services/plugin/tool.py
@@ -7,8 +7,11 @@ JSON Schema the LLM sees — derived from :class:`Params` automatically.
from __future__ import annotations
-from copy import deepcopy
-from typing import Any, ClassVar, Dict, Optional
+from copy import copy, deepcopy
+from functools import lru_cache
+from typing import Any, ClassVar, Dict, FrozenSet, Optional, Type
+
+from pydantic import BaseModel, ConfigDict, create_model
from core.logging import get_logger
from services.plugin.base import BaseNode
@@ -116,15 +119,71 @@ class ToolNode(BaseNode, abstract=True):
"open_world": False,
}
+ # ``Params`` describes persisted, operator-controlled node
+ # configuration. ``ToolInput`` describes arguments an LLM is allowed to
+ # supply for a single invocation. Existing tools keep their historical
+ # contract automatically: when a subclass does not declare ToolInput,
+ # ``__init_subclass__`` aliases it to that subclass's Params model.
+ #
+ # Keeping these as distinct models is a security boundary. In particular,
+ # a model must never be able to smuggle workflow/node scope or overwrite a
+ # server-controlled setting merely because both dictionaries happened to
+ # be merged before validation.
+ ToolInput: ClassVar[Optional[Type[BaseModel]]] = None
+ server_controlled_fields: ClassVar[FrozenSet[str]] = frozenset()
+
+ # Some first-party tools have a security-sensitive public schema. Their
+ # ToolInput must win over a stale/custom ToolSchema row saved by an older
+ # client. Simple Memory opts into this contract.
+ tool_schema_locked: ClassVar[bool] = False
+
+ def __init_subclass__(cls, abstract: bool = False, **kwargs):
+ if not abstract and "ToolInput" not in cls.__dict__:
+ cls.ToolInput = cls.Params
+ super().__init_subclass__(abstract=abstract, **kwargs)
+
+ @classmethod
+ def tool_input_model(cls) -> Type[BaseModel]:
+ """Return the invocation model, defaulting to persisted ``Params``."""
+ return cls.ToolInput or cls.Params
+
+ @classmethod
+ @lru_cache(maxsize=None)
+ def partial_config_model(cls) -> Type[BaseModel]:
+ """Model used to validate the persisted subset for legacy tools.
+
+ Historically a ToolNode's Params mixed operator configuration with
+ required invocation arguments, so a saved node may legitimately omit
+ fields the LLM must provide. This derived model keeps every field's
+ type/constraints and extra-field policy while making absence valid.
+ """
+ partial_fields: Dict[str, tuple[Any, Any]] = {}
+ for field_name, field_info in cls.Params.model_fields.items():
+ optional_info = copy(field_info)
+ optional_info.default = None
+ optional_info.default_factory = None
+ partial_fields[field_name] = (field_info.annotation, optional_info)
+ return create_model(
+ f"{cls.Params.__name__}PersistedConfig",
+ __config__=ConfigDict(
+ extra=cls.Params.model_config.get("extra", "ignore")
+ ),
+ **partial_fields,
+ )
+
@classmethod
def as_tool_schema(cls) -> Dict[str, Any]:
"""LLM-visible schema: ``{name, description, parameters}`` where
- ``parameters`` is the Pydantic JSON schema of :class:`Params`."""
+ ``parameters`` is the Pydantic JSON schema of :class:`ToolInput`."""
# Inline $defs / $ref — LLM function-calling doesn't cope with
# indirection, and a bare strip would leave dangling $ref.
- schema = inline_schema_refs(cls.Params.model_json_schema())
+ schema = inline_schema_refs(cls.tool_input_model().model_json_schema())
return {
- "name": cls.type,
+ "name": (
+ cls.tool_name
+ if cls.tool_schema_locked and cls.tool_name
+ else cls.type
+ ),
"description": cls.description or cls.display_name or cls.type,
"parameters": schema,
}
diff --git a/server/services/rlm/adapters.py b/server/services/rlm/adapters.py
index 3ef27e8b..9ae97c54 100644
--- a/server/services/rlm/adapters.py
+++ b/server/services/rlm/adapters.py
@@ -6,8 +6,19 @@
"""
import asyncio
+import inspect
import re
-from typing import Dict, Any, List, Tuple, Optional
+import threading
+from concurrent.futures import TimeoutError as FutureTimeoutError
+from typing import (
+ Any,
+ Awaitable,
+ Callable,
+ Dict,
+ List,
+ Optional,
+ Tuple,
+)
from core.logging import get_logger
from .constants import PROVIDER_TO_BACKEND, PROVIDER_BASE_URLS
@@ -76,6 +87,10 @@ class ToolBridgeAdapter:
RLM's synchronous exec() REPL thread.
"""
+ TOOL_TIMEOUT_SECONDS = 60.0
+ CANCELLATION_GRACE_SECONDS = 1.0
+ CONTEXT_EVENT_TIMEOUT_SECONDS = 10.0
+
# Brief descriptions for common tool types (used in RLM REPL context)
TOOL_DESCRIPTIONS = {
"calculatorTool": "Math operations: add, subtract, multiply, divide, power, sqrt, mod, abs. Args: operation, a, b",
@@ -103,6 +118,9 @@ class ToolBridgeAdapter:
tool_data: Optional[List[Dict[str, Any]]], context: Optional[Dict] = None, loop: Optional[asyncio.AbstractEventLoop] = None,
broadcaster=None, parent_node_id: Optional[str] = None, workflow_id: Optional[str] = None,
provider: Optional[str] = None,
+ ambiguous_outcome_sink: Optional[
+ Callable[[Dict[str, Any]], Awaitable[None]]
+ ] = None,
) -> Dict[str, Dict]:
from constants import AI_AGENT_TYPES, AI_CHAT_MODEL_TYPES
from services.handlers.tools import execute_tool
@@ -123,13 +141,48 @@ class ToolBridgeAdapter:
node_id = tool_info.get("node_id", "")
label = tool_info.get("label", node_type)
params = tool_info.get("parameters", {})
- # Provider-visible name is the capability identity shown to the
- # model and UI. Human labels remain descriptions only.
- tool_name = re.sub(r"[^a-zA-Z0-9_]", "_", label.lower().replace(" ", "_"))
-
- def _make_sync_wrapper(t_type, t_id, t_params, t_label, t_name):
+ tool_name = tool_info.get("_agent_tool_name")
+ input_model = tool_info.get("_agent_tool_input_model")
+ if not tool_name or input_model is None:
+ from services.node_registry import get_node_class
+
+ node_cls = get_node_class(node_type)
+ if node_cls is not None:
+ tool_name = tool_name or (
+ getattr(node_cls, "tool_name", "") or node_type
+ )
+ model_factory = getattr(
+ node_cls, "tool_input_model", None
+ )
+ input_model = (
+ model_factory()
+ if callable(model_factory)
+ else getattr(node_cls, "Params", None)
+ )
+ if not tool_name:
+ tool_name = re.sub(
+ r"[^a-zA-Z0-9_]",
+ "_",
+ label.lower().replace(" ", "_"),
+ )
+ if tool_name in tools:
+ raise ValueError(
+ f"Duplicate canonical tool name {tool_name!r}"
+ )
+
+ def _make_sync_wrapper(
+ t_type,
+ t_id,
+ t_params,
+ t_label,
+ t_name,
+ t_input_model,
+ t_execution,
+ t_ambiguous_outcome_sink,
+ ):
def wrapper(**kwargs):
config = {
+ **dict(t_execution or {}),
"node_type": t_type,
"node_id": t_id,
"parameters": t_params,
@@ -143,6 +196,22 @@ class ToolBridgeAdapter:
config["edges"] = context.get("edges", [])
config["execution_id"] = context.get("execution_id")
config["root_execution_id"] = context.get("root_execution_id")
+ config["user_id"] = context.get("user_id", "owner")
+
+ if t_input_model is not None:
+ try:
+ validated = t_input_model.model_validate(kwargs)
+ tool_args = validated.model_dump(
+ mode="json",
+ exclude_unset=True,
+ )
+ except Exception as exc:
+ return {
+ "error": "Invalid tool arguments",
+ "details": str(exc),
+ }
+ else:
+ tool_args = dict(kwargs)
async def execute_with_parent_status():
if broadcaster and parent_node_id:
@@ -166,7 +235,11 @@ class ToolBridgeAdapter:
invocation_source="rlm",
)
try:
- result = await execute_tool(t_label, kwargs, config)
+ result = await execute_tool(
+ t_name,
+ tool_args,
+ config,
+ )
if broadcaster and parent_node_id:
await broadcaster.update_node_status(
parent_node_id,
@@ -217,15 +290,125 @@ class ToolBridgeAdapter:
)
raise
- future = asyncio.run_coroutine_threadsafe(execute_with_parent_status(), main_loop)
- return future.result(timeout=60)
+ execution_finished = threading.Event()
+ async def execute_with_completion():
+ try:
+ return await execute_with_parent_status()
+ finally:
+ execution_finished.set()
+
+ future = asyncio.run_coroutine_threadsafe(
+ execute_with_completion(),
+ main_loop,
+ )
+ try:
+ return future.result(
+ timeout=(
+ ToolBridgeAdapter.TOOL_TIMEOUT_SECONDS
+ )
+ )
+ except FutureTimeoutError as exc:
+ # A tool may itself raise TimeoutError. In that case
+ # the concurrent future is already complete and its
+ # exception is an exact failure, not our wait limit.
+ if future.done():
+ return future.result()
+ cancel_requested = future.cancel()
+ cancellation_observed = execution_finished.wait(
+ ToolBridgeAdapter.CANCELLATION_GRACE_SECONDS
+ )
+ ambiguous = {
+ "outcome": "ambiguous",
+ "reason": "tool_timeout",
+ "tool_name": t_name,
+ "tool_node_id": t_id,
+ "tool_node_type": t_type,
+ "arguments": tool_args,
+ "timeout_seconds": (
+ ToolBridgeAdapter.TOOL_TIMEOUT_SECONDS
+ ),
+ "cancel_requested": cancel_requested,
+ "cancellation_observed": (
+ cancellation_observed
+ ),
+ }
+ if t_ambiguous_outcome_sink is not None:
+ receipt = asyncio.run_coroutine_threadsafe(
+ t_ambiguous_outcome_sink(ambiguous),
+ main_loop,
+ )
+ try:
+ receipt.result(
+ timeout=(
+ ToolBridgeAdapter
+ .CONTEXT_EVENT_TIMEOUT_SECONDS
+ )
+ )
+ except Exception as persist_exc:
+ raise RuntimeError(
+ "RLM tool timed out and the "
+ "ambiguous Context event could not "
+ "be committed"
+ ) from persist_exc
+ raise TimeoutError(
+ f"RLM tool {t_name!r} timed out after "
+ f"{ToolBridgeAdapter.TOOL_TIMEOUT_SECONDS:g}s; "
+ "outcome is ambiguous"
+ ) from exc
+
+ if t_input_model is not None:
+ from pydantic_core import PydanticUndefined
+
+ parameters = []
+ for field_name, field_info in (
+ t_input_model.model_fields.items()
+ ):
+ if field_info.is_required():
+ default = inspect.Parameter.empty
+ elif field_info.default_factory is not None:
+ try:
+ default = field_info.default_factory()
+ except Exception:
+ default = None
+ elif field_info.default is PydanticUndefined:
+ default = None
+ else:
+ default = field_info.default
+ parameters.append(
+ inspect.Parameter(
+ field_name,
+ inspect.Parameter.KEYWORD_ONLY,
+ annotation=field_info.annotation,
+ default=default,
+ )
+ )
+ wrapper.__signature__ = inspect.Signature( # type: ignore[attr-defined]
+ parameters
+ )
+ wrapper.__name__ = str(t_name)
return wrapper
- description = params.get("tool_description") or ToolBridgeAdapter.TOOL_DESCRIPTIONS.get(node_type, f"Execute {label} ({node_type})")
+ description = (
+ tool_info.get("_agent_tool_description")
+ or params.get("tool_description")
+ or ToolBridgeAdapter.TOOL_DESCRIPTIONS.get(
+ node_type,
+ f"Execute {label} ({node_type})",
+ )
+ )
tools[tool_name] = {
- "tool": _make_sync_wrapper(node_type, node_id, params, label, tool_name),
+ "tool": _make_sync_wrapper(
+ node_type,
+ node_id,
+ params,
+ label,
+ tool_name,
+ input_model,
+ tool_info.get("_agent_tool_execution"),
+ ambiguous_outcome_sink,
+ ),
"description": description,
}
logger.info(f"[RLM] Bridged tool: {tool_name} ({node_type})")
diff --git a/server/services/rlm/service.py b/server/services/rlm/service.py
index 0428cb52..5d6f8c61 100644
--- a/server/services/rlm/service.py
+++ b/server/services/rlm/service.py
@@ -30,12 +30,14 @@ class RLMService:
node_id: str,
parameters: Dict[str, Any],
memory_data: Optional[Dict[str, Any]] = None,
+ context_data: Optional[Dict[str, Any]] = None,
skill_data: Optional[List[Dict[str, Any]]] = None,
tool_data: Optional[List[Dict[str, Any]]] = None,
broadcaster=None,
workflow_id: Optional[str] = None,
context: Optional[Dict[str, Any]] = None,
database=None,
+ ai_service=None,
) -> Dict[str, Any]:
"""Execute RLM agent completion.
@@ -43,6 +45,23 @@ class RLMService:
Reuses existing OpenCompany utilities for shared concerns.
"""
start_time = time.time()
+ context_bridge = None
+ if (context_data or {}).get("kind") == "context":
+ from services.cli_agent.context_bridge import (
+ SpecializedAgentContextBridge,
+ )
+
+ context_bridge = await SpecializedAgentContextBridge.resolve(
+ database,
+ context_data,
+ provider="rlm",
+ fidelity="observable_only",
+ resumable=False,
+ operation_prefix=(
+ f"rlm-context:"
+ f"{(context or {}).get('execution_id') or 'run'}:{node_id}"
+ ),
+ )
async def broadcast_status(phase: str, details: Dict[str, Any] = None):
if broadcaster:
@@ -119,6 +138,38 @@ class RLMService:
progressive_skill_tool = skill_tool_info(skill_data or [], node_id)
if progressive_skill_tool:
effective_tool_data.append(progressive_skill_tool)
+ if ai_service is not None:
+ canonical_tool_data = []
+ for tool_info in effective_tool_data:
+ tool, execution = await ai_service._build_tool_from_node(
+ tool_info
+ )
+ if tool is None or execution is None:
+ raise ValueError(
+ "Could not build connected tool "
+ f"{tool_info.get('node_type')!r}"
+ )
+ canonical_tool_data.append(
+ {
+ **tool_info,
+ "_agent_tool_name": tool.name,
+ "_agent_tool_description": tool.description,
+ "_agent_tool_schema": tool.parameters,
+ "_agent_tool_input_model": tool.args_schema,
+ "_agent_tool_execution": execution,
+ }
+ )
+ effective_tool_data = canonical_tool_data
+
+ async def record_ambiguous_tool_outcome(
+ payload: Dict[str, Any],
+ ) -> None:
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "tool.ambiguous_outcome",
+ payload,
+ )
+
custom_tools = ToolBridgeAdapter.bridge(
effective_tool_data,
context,
@@ -127,6 +178,11 @@ class RLMService:
parent_node_id=node_id,
workflow_id=workflow_id,
provider=provider,
+ ambiguous_outcome_sink=(
+ record_ambiguous_tool_outcome
+ if context_bridge is not None
+ else None
+ ),
)
# === RLM-specific parameters ===
@@ -161,6 +217,31 @@ class RLMService:
augmented_prompt = f"## Instructions\n{system_message}\n\n## Task\n{augmented_prompt}"
if supplementary_context:
augmented_prompt = f"{supplementary_context}\n\n{augmented_prompt}"
+ if context_bridge is not None:
+ augmented_prompt = context_bridge.augment_prompt(
+ augmented_prompt
+ )
+
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "provider.request",
+ {
+ "provider": provider,
+ "model": model,
+ "prompt": augmented_prompt,
+ "max_iterations": max_iterations,
+ "max_depth": max_depth,
+ "max_budget": max_budget,
+ "max_timeout": max_timeout,
+ "max_tokens": max_tokens,
+ "tool_node_ids": [
+ item.get("node_id")
+ for item in effective_tool_data
+ if isinstance(item, dict)
+ ],
+ },
+ operation_suffix="request",
+ )
def _run_rlm():
from rlm import RLM
@@ -185,6 +266,17 @@ class RLMService:
result = await asyncio.to_thread(_run_rlm)
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "provider.result",
+ {
+ "response": result.response,
+ "metadata": result.metadata,
+ "usage_summary": result.usage_summary,
+ },
+ operation_suffix="result",
+ )
+
# === Memory save (same pattern as execute_chat_agent) ===
if memory_data and memory_data.get("node_id"):
from services.memory_store import add_message
@@ -222,14 +314,32 @@ class RLMService:
}
except Exception as e:
- logger.error(f"[RLM] Execution failed: {e}", exc_info=True)
+ public_error = (
+ str(e)
+ if context_bridge is None
+ else f"{type(e).__name__}: RLM execution failed"
+ )
+ logger.error(
+ "[RLM] Execution failed type=%s",
+ type(e).__name__,
+ exc_info=context_bridge is None,
+ )
execution_time = time.time() - start_time
- await broadcast_status("error", {"message": str(e)})
+ if context_bridge is not None:
+ await context_bridge.append_observable(
+ "provider.error",
+ {
+ "error_type": type(e).__name__,
+ "error": str(e),
+ },
+ operation_suffix="error",
+ )
+ await broadcast_status("error", {"message": public_error})
return {
"success": False,
"node_id": node_id,
"node_type": "rlm_agent",
- "error": str(e),
+ "error": public_error,
"execution_time": execution_time,
}
finally:
diff --git a/server/services/temporal/activities.py b/server/services/temporal/activities.py
index 5005d948..825e9851 100644
--- a/server/services/temporal/activities.py
+++ b/server/services/temporal/activities.py
@@ -221,6 +221,7 @@ class NodeExecutionActivities:
"session_id": context.get("session_id", "default"),
"workflow_id": context.get("workflow_id"),
"execution_id": context.get("execution_id"),
+ "user_id": context.get("user_id", "owner"),
# CRITICAL: Pass upstream node outputs for downstream nodes to access
# This enables taskTrigger -> chatAgent data flow via input-task handle
"outputs": context.get("inputs", {}),
@@ -443,6 +444,7 @@ async def load_persisted_workflow_graph_activity(payload: Dict[str, Any]) -> Dic
)
return {
"found": True,
+ "graphVersion": int(data.get("graphVersion") or 0),
"nodes": nodes,
"edges": normalize_edge_handles(edges),
}
@@ -526,7 +528,7 @@ async def broadcast_trigger_status_activity(payload: Dict[str, Any]) -> None:
activity.logger.warning(f"broadcast_trigger_status_activity failed for " f"node={payload.get('node_id')!r}: {exc}")
[email protected](name="workflow_control.pause_on_failure.v1")
[email protected](name="workflow_control.pause_on_failure")
async def pause_workflow_on_failure_activity(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Circuit breaker: pause a controlled deployment after a failed run.
diff --git a/server/services/temporal/agent_activities.py b/server/services/temporal/agent_activities.py
index 0f3a92ba..7307bc02 100644
--- a/server/services/temporal/agent_activities.py
+++ b/server/services/temporal/agent_activities.py
@@ -37,10 +37,15 @@ from __future__ import annotations
import asyncio
from dataclasses import asdict
import hashlib
+import json
import logging
-from typing import Any, Dict, List, Optional
+import re
+from typing import Any, Dict, Iterable, List, Mapping, Optional
from temporalio import activity
+from temporalio.exceptions import ApplicationError
+
+from models.agent_context import AgentContextRef
logger = logging.getLogger(__name__)
@@ -288,6 +293,7 @@ async def _execute_native_llm_step(payload: Dict[str, Any]) -> Dict[str, Any]:
max_tokens=payload.get("max_tokens", 4096),
thinking=thinking,
tools=tool_defs,
+ context_management=payload.get("context_management"),
# Temporal owns the one-shot retry contract. SDK retries could
# otherwise re-bill a request after an ambiguous transport loss.
sdk_max_retries=0,
@@ -324,26 +330,37 @@ async def _execute_native_llm_step(payload: Dict[str, Any]) -> Dict[str, Any]:
if parse_error:
call["parse_error"] = parse_error
calls.append(call)
- return {
+ result = {
"kind": "tool_calls",
"assistant_message": assistant_wire,
"calls": calls,
"usage": usage,
}
+ if payload.get("include_finish_reason"):
+ result["finish_reason"] = response.finish_reason
+ return result
- return {
+ result = {
"kind": "final",
"assistant_message": assistant_wire,
"content": response.content or "",
"thinking": response.thinking,
"usage": usage,
}
+ if payload.get("include_finish_reason"):
+ result["finish_reason"] = response.finish_reason
+ return result
[email protected](name="agent.execute_llm_step.v1")
[email protected](name="agent.execute_llm_step")
async def execute_llm_step(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Run one LLM turn with bound tools and return the structured response.
+ The single LLM-step activity. A ``context_ref`` in the payload selects
+ the journal-backed path (transcript reconstructed from the Context
+ store rather than carried in the payload); everything else is
+ identical, so callers do not pick an implementation.
+
``payload`` shape::
{
@@ -371,6 +388,10 @@ async def execute_llm_step(payload: Dict[str, Any]) -> Dict[str, Any]:
Heartbeats every 30 s so long LLM streams don't trip
``heartbeat_timeout``.
"""
+
+ if payload.get("context_ref"):
+ return await _execute_context_llm_step(payload)
+
activity.logger.info(
f"Agent LLM step: provider={payload.get('provider')} " f"model={payload.get('model')} messages={len(payload.get('messages', []))}"
)
@@ -403,7 +424,7 @@ async def execute_llm_step(payload: Dict[str, Any]) -> Dict[str, Any]:
return result
[email protected](name="agent.persist_turn.v1")
[email protected](name="agent.persist_turn")
async def persist_agent_turn(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Append one ``(human, assistant)`` pair to the connected memory.
@@ -482,7 +503,7 @@ async def persist_agent_turn(payload: Dict[str, Any]) -> Dict[str, Any]:
}
[email protected](name="agent.compact_memory.v1")
[email protected](name="agent.compact_memory")
async def compact_agent_memory(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Compact the running conversation when token budget exceeded.
@@ -537,7 +558,7 @@ async def compact_agent_memory(payload: Dict[str, Any]) -> Dict[str, Any]:
)
[email protected](name="agent.broadcast_progress.v1")
[email protected](name="agent.broadcast_progress")
async def broadcast_agent_progress(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Emit a CloudEvents-shaped ``agent_progress`` broadcast per turn.
@@ -660,7 +681,7 @@ async def broadcast_agent_progress(payload: Dict[str, Any]) -> Dict[str, Any]:
return {"emitted": True}
[email protected](name="agent.store_output.v1")
[email protected](name="agent.store_output")
async def store_agent_output(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Persist the agent's final ``result`` dict via the existing
``WorkflowService.store_node_output`` so ``ParameterResolver`` can
@@ -691,7 +712,7 @@ async def store_agent_output(payload: Dict[str, Any]) -> Dict[str, Any]:
return {"stored": True}
[email protected](name="agent.prepare_payload.v1")
[email protected](name="agent.prepare_payload")
async def prepare_agent_payload(context: Dict[str, Any]) -> Dict[str, Any]:
"""Resolve everything ``AgentWorkflow`` needs from the canvas + DB.
@@ -978,9 +999,16 @@ async def prepare_agent_payload(context: Dict[str, Any]) -> Dict[str, Any]:
system_message = skill_prompt if has_personality else f"{system_message}\n\n{skill_prompt}"
# ---- Memory ---------------------------------------------------------
+ # The descriptor is forwarded verbatim as ``context_descriptor``. Reading
+ # it only through the legacy markdown keys below silently yields an empty
+ # transcript for any descriptor that does not carry them, which is how a
+ # journal-backed agent ends up starting every run with no history. The
+ # keys are read with ``.get`` and never interpreted here — whichever
+ # plugin produced the descriptor owns its meaning.
memory_node_id = ""
memory_content = ""
memory_window_size = 10
+ context_descriptor: Dict[str, Any] = dict(memory_data or {})
if memory_data:
memory_node_id = memory_data.get("node_id") or ""
memory_content = memory_data.get("memory_content") or ""
@@ -1171,6 +1199,7 @@ async def prepare_agent_payload(context: Dict[str, Any]) -> Dict[str, Any]:
"memory_node_id": memory_node_id,
"memory_content": memory_content,
"memory_window_size": memory_window_size,
+ "context_descriptor": context_descriptor,
"max_iterations": effective_recursion_limit,
"thinking_config": thinking_config_dict,
"compaction_threshold": compaction_threshold,
@@ -1193,7 +1222,7 @@ async def prepare_agent_payload(context: Dict[str, Any]) -> Dict[str, Any]:
}
[email protected](name="agent.refresh_tools.v1")
[email protected](name="agent.refresh_tools")
async def refresh_agent_tools(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Build fresh ``tool_payload`` entries from a workflow_ops batch.
@@ -1289,7 +1318,7 @@ async def refresh_agent_tools(payload: Dict[str, Any]) -> Dict[str, Any]:
return {"tools": new_tools_payload}
[email protected](name="agent.skill.invoke.v1")
[email protected](name="agent.skill.invoke")
async def invoke_agent_skill(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Retry-safe, history-recorded progressive skill invocation."""
from services.skill_runtime import execute_skill_tool
@@ -1319,7 +1348,7 @@ async def invoke_agent_skill(payload: Dict[str, Any]) -> Dict[str, Any]:
)
[email protected](name="agent.skill.clear.v1")
[email protected](name="agent.skill.clear")
async def clear_agent_skills(payload: Dict[str, Any]) -> Dict[str, Any]:
from services.skill_runtime import clear_skill_turn
@@ -1331,7 +1360,7 @@ async def clear_agent_skills(payload: Dict[str, Any]) -> Dict[str, Any]:
return {"cleared": True}
[email protected](name="agent.begin_delegation.v1")
[email protected](name="agent.begin_delegation")
async def begin_agent_delegation(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Idempotently persist and claim a delegation before child startup."""
from services.agent_team import get_agent_team_service
@@ -1422,7 +1451,7 @@ async def begin_agent_delegation(payload: Dict[str, Any]) -> Dict[str, Any]:
return {"team_id": team_id, "team_task_id": task_id, "claimed": True}
[email protected](name="agent.queue_delegation.v1")
[email protected](name="agent.queue_delegation")
async def queue_agent_delegation(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Create a pending task before it waits for a root-wide permit."""
from services.agent_team import get_agent_team_service
@@ -1455,7 +1484,7 @@ async def queue_agent_delegation(payload: Dict[str, Any]) -> Dict[str, Any]:
return {"team_id": team_id, "team_task_id": task_id, "status": "queued"}
[email protected](name="agent.cancel_delegation.v1")
[email protected](name="agent.cancel_delegation")
async def cancel_agent_delegation(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Persist cancellation as a terminal state without failure requeueing."""
from services.agent_team import get_agent_team_service
@@ -1523,7 +1552,7 @@ def _subagent_lease_id(
return f"subagent-lease-v2-{hashlib.sha256(identity).hexdigest()}"
[email protected](name="agent.acquire_subagent_permit.v1")
[email protected](name="agent.acquire_subagent_permit")
async def acquire_subagent_permit(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Poll the durable root coordinator until this delegation is admitted."""
from services.agent_team import get_agent_team_service
@@ -1618,7 +1647,7 @@ async def acquire_subagent_permit(payload: Dict[str, Any]) -> Dict[str, Any]:
raise
[email protected](name="agent.release_subagent_permit.v1")
[email protected](name="agent.release_subagent_permit")
async def release_subagent_permit(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Idempotently release a durable root-wide concurrency permit."""
from services.agent_team import get_agent_team_service
@@ -1645,7 +1674,7 @@ async def release_subagent_permit(payload: Dict[str, Any]) -> Dict[str, Any]:
return result
[email protected](name="agent.finish_delegation.v1")
[email protected](name="agent.finish_delegation")
async def finish_agent_delegation(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Idempotently persist a delegated child's terminal result."""
from services.agent_team import get_agent_team_service
@@ -1745,7 +1774,7 @@ async def finish_agent_delegation(payload: Dict[str, Any]) -> Dict[str, Any]:
return {"team_id": team_id, "team_task_id": task_id, "status": target_status}
[email protected](name="agent.register_task_execution.v1")
[email protected](name="agent.register_task_execution")
async def register_task_execution(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Persist actual runner/child Temporal identities for trace inspection."""
from services.agent_team import get_agent_team_service
@@ -1772,7 +1801,7 @@ async def register_task_execution(payload: Dict[str, Any]) -> Dict[str, Any]:
return {"team_id": team_id, "team_task_id": task_id, "registered": True}
[email protected](name="agent.finalize_team.v1")
[email protected](name="agent.finalize_team")
async def finalize_agent_team(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Finalize a lead's team after all delegated tasks become terminal."""
from services.agent_team import get_agent_team_service
@@ -1818,9 +1847,1291 @@ def collect_agent_activities() -> List[Any]:
clear_agent_skills,
begin_agent_delegation,
queue_agent_delegation,
+ # Scheduled unconditionally by the cancellation-unwind paths in
+ # AgentWorkflow/DelegatedTaskWorkflow. Its absence here left
+ # agent.cancel_delegation.v1 unregistered, so every cancelled
+ # delegation burned DELEGATION_CLEANUP_RETRY (10 attempts) against
+ # an unknown activity type and the failure was swallowed, leaving
+ # team task rows non-terminal.
+ cancel_agent_delegation,
acquire_subagent_permit,
release_subagent_permit,
register_task_execution,
finish_agent_delegation,
finalize_agent_team,
+ # Context journal surface (formerly collect_agent_v2_activities).
+ # NOTE: execute_tool_activity is deliberately absent — it is a
+ # plain coroutine invoked by each plugin's own activity wrapper in
+ # services/plugin/base.py, not a Temporal activity of its own.
+ prepare_context,
+ append_context,
+ compact_context,
]
+
+
+# ---------------------------------------------------------------------------
+# Agent Context journal surface.
+#
+# Moved here from the retired ``agent_activities_v2`` module so there is a
+# single agent-activity module. These five activities own the backend
+# execution journal: the request snapshot, the LLM step, tool-result and
+# assistant appends, and compaction.
+#
+# ``execute_tool_activity`` is NOT Temporal-only -- ``services/plugin/base.py``
+# calls it from every plugin's ``as_activity()`` when the node context carries
+# ``protocol == "agent-context-tool-v2"``, so it must stay importable from here.
+# ---------------------------------------------------------------------------
+
+_SENSITIVE_GRAPH_KEYS = frozenset(
+ {
+ "apikey",
+ "accesstoken",
+ "refreshtoken",
+ "password",
+ "secret",
+ "clientsecret",
+ "authorization",
+ }
+)
+def _normalise_key(value: Any) -> str:
+ return re.sub(r"[^a-z0-9]", "", str(value).lower())
+def _strip_credentials(value: Any) -> Any:
+ """Copy graph/runtime metadata without credential-shaped fields."""
+
+ if isinstance(value, Mapping):
+ return {
+ str(key): _strip_credentials(item)
+ for key, item in value.items()
+ if _normalise_key(key) not in _SENSITIVE_GRAPH_KEYS
+ }
+ if isinstance(value, list):
+ return [_strip_credentials(item) for item in value]
+ if isinstance(value, tuple):
+ return [_strip_credentials(item) for item in value]
+ return value
+def _context_ref(value: Mapping[str, Any]) -> AgentContextRef:
+ return AgentContextRef.model_validate(dict(value))
+def _ref_dict(ref: AgentContextRef) -> Dict[str, Any]:
+ return ref.model_dump(mode="json")
+def _hash_from_ref(payload_ref: str) -> str:
+ return str(payload_ref).removeprefix("sha256:")
+def _context_store():
+ from core.container import container
+ from services.agent_context import AgentContextStore
+
+ return AgentContextStore(container.database())
+def _non_retryable(message: str, error_type: str) -> ApplicationError:
+ return ApplicationError(
+ message,
+ type=error_type,
+ non_retryable=True,
+ )
+def _context_node_id(context: Mapping[str, Any]) -> str:
+ """Return the exactly-one Context companion wired to this agent."""
+
+ node_id = str(context.get("node_id") or "")
+ nodes = {
+ str(node.get("id")): node
+ for node in context.get("nodes") or []
+ if isinstance(node, dict) and node.get("id")
+ }
+ candidates: list[str] = []
+ for edge in context.get("edges") or []:
+ if not isinstance(edge, dict) or str(edge.get("target") or "") != node_id:
+ continue
+ handle = edge.get("targetHandle") or edge.get("target_handle")
+ if handle != "input-context":
+ continue
+ source_id = str(edge.get("source") or "")
+ if (nodes.get(source_id) or {}).get("type") == "context":
+ candidates.append(source_id)
+ unique = sorted(set(candidates))
+ if len(unique) != 1:
+ raise _non_retryable(
+ (
+ f"Context V2 agent {node_id!r} requires exactly one Context "
+ f"companion; found {len(unique)}."
+ ),
+ "InvalidAgentContextTopology",
+ )
+ return unique[0]
+def _thread_inputs(context: Mapping[str, Any]) -> Dict[str, Optional[str]]:
+ """Resolve a delegated-task, explicit-session, or execution thread.
+
+ Delegation always gets an isolated task thread even when the parent
+ propagates its chat session id into the child context.
+ """
+
+ raw_session = str(
+ context.get("context_session_id")
+ or context.get("session_id")
+ or ""
+ ).strip()
+ # ``default`` is the UI placeholder, not an explicit chat identity.
+ session_id = raw_session if raw_session and raw_session != "default" else None
+ # Controlled deployments deliberately replace the caller's session with
+ # their generation-scoped data namespace. That namespace is not an
+ # explicit conversation and must not collapse independent trigger
+ # firings into one persistent session thread.
+ data_scope_id = str(context.get("data_scope_id") or "").strip()
+ if session_id and data_scope_id and session_id == data_scope_id:
+ session_id = None
+ delegated_task_id = str(
+ context.get("delegated_task_id")
+ or context.get("task_id")
+ or context.get("parent_task_id")
+ or context.get("team_task_id")
+ or (context.get("invocation") or {}).get("task_id")
+ or ""
+ ).strip() or None
+ if delegated_task_id is not None:
+ session_id = None
+ execution_id = str(
+ context.get("context_execution_id")
+ or context.get("execution_id")
+ or context.get("data_scope_id")
+ or ""
+ ).strip() or None
+ return {
+ "session_id": session_id,
+ "delegated_task_id": delegated_task_id,
+ "execution_id": execution_id,
+ }
+def _tool_identity(tool: Mapping[str, Any]) -> Dict[str, Any]:
+ return {
+ "name": str(tool.get("name") or ""),
+ "node_id": str(tool.get("tool_node_id") or ""),
+ "node_type": str(tool.get("node_type") or ""),
+ "version": int(tool.get("version") or 1),
+ "task_queue": str(tool.get("dispatch_task_queue") or ""),
+ }
+def _validate_unique_tool_names(
+ tools: Iterable[Mapping[str, Any]],
+ *,
+ phase: str,
+) -> None:
+ """Reject an ambiguous provider-visible tool surface.
+
+ A dict comprehension would otherwise silently select the last connected
+ node. Validation belongs in this backend preparation/rebind boundary,
+ before another provider request can be billed.
+ """
+
+ seen: Dict[str, str] = {}
+ conflicts: Dict[str, set[str]] = {}
+ for tool in tools:
+ if tool.get("llm_hidden"):
+ continue
+ name = str(tool.get("name") or "").strip()
+ if not name:
+ continue
+ node_id = str(tool.get("tool_node_id") or "")
+ previous = seen.get(name)
+ if previous is None:
+ seen[name] = node_id
+ continue
+ conflicts.setdefault(name, {previous}).add(node_id)
+ if conflicts:
+ details = ", ".join(
+ f"{name!r} ({', '.join(sorted(node_ids))})"
+ for name, node_ids in sorted(conflicts.items())
+ )
+ raise _non_retryable(
+ (
+ f"Context V2 {phase} rejected duplicate canonical tool "
+ f"name(s): {details}"
+ ),
+ "DuplicateAgentToolName",
+ )
+async def _runtime_config(
+ store: Any,
+ payload_ref: str,
+) -> Dict[str, Any]:
+ value = await store.get_blob(payload_ref)
+ if not isinstance(value, dict) or value.get("protocol") != "agent-context-v2":
+ raise _non_retryable(
+ "The Context V2 runtime snapshot is missing or invalid.",
+ "InvalidAgentContextRuntimeSnapshot",
+ )
+ return value
+async def _append_event(
+ store: Any,
+ ref: AgentContextRef,
+ *,
+ event_type: str,
+ operation_id: str,
+ provider: Optional[str] = None,
+ message_wire_v2: Optional[Dict[str, Any]] = None,
+ payload_ref: Optional[str] = None,
+) -> AgentContextRef:
+ result = await store.append_transition(
+ ref,
+ event_type=event_type,
+ operation_id=operation_id,
+ provider=provider,
+ message_wire_v2=message_wire_v2,
+ payload_ref=payload_ref,
+ )
+ return result.ref
[email protected](name="agent.prepare_context")
+async def prepare_context(payload: Dict[str, Any]) -> Dict[str, Any]:
+ """Resolve one Context thread and persist the exact runtime snapshot."""
+
+ from core.container import container
+ from services.llm.protocol import Message, message_to_wire
+ from services.temporal.agent_activities import prepare_agent_payload
+
+ context = dict(payload.get("context") or payload)
+ workflow_id = str(context.get("workflow_id") or "")
+ generation = int(context.get("generation") or 0)
+ graph_version = int(
+ context.get("graphVersion")
+ or context.get("graph_version")
+ or 0
+ )
+ if not workflow_id or generation <= 0 or graph_version < 2:
+ raise _non_retryable(
+ "Context V2 requires a normalized graph and a positive workflow generation.",
+ "InvalidAgentContextGeneration",
+ )
+
+ context_node_id = _context_node_id(context)
+ context["generation"] = generation
+ context["graphVersion"] = graph_version
+
+ # Reuse the canonical backend preparation path. Its result contains no
+ # credential; provider keys are resolved afresh inside each LLM activity.
+ prepared = dict(await prepare_agent_payload(context))
+ prepared.pop("api_key", None)
+ # The first edge-walker slot is Context in V2, not legacy Markdown memory.
+ prepared["memory_node_id"] = ""
+ prepared["memory_content"] = ""
+ prepared["memory_window_size"] = 0
+ worker_pool_enabled = context.get(
+ "temporal_worker_pool_enabled",
+ True,
+ ) is not False
+ for tool in prepared.get("tools") or []:
+ tool["dispatch_task_queue"] = (
+ str(tool.get("task_queue") or "machina-default")
+ if worker_pool_enabled
+ else ""
+ )
+ _validate_unique_tool_names(
+ prepared.get("tools") or [],
+ phase="prepare",
+ )
+
+ database = container.database()
+ policy = await database.get_node_parameters(context_node_id) or {}
+ operation_id = str(
+ payload.get("operation_id")
+ or f"context-v2:{workflow_id}:{context.get('node_id')}:prepare"
+ )
+ runtime_snapshot = {
+ "protocol": "agent-context-v2",
+ "workflow_id": workflow_id,
+ "generation": generation,
+ "graph_version": graph_version,
+ "context_node_id": context_node_id,
+ "agent_node_id": str(context.get("node_id") or ""),
+ "prepared": _strip_credentials(prepared),
+ "context_policy": _strip_credentials(policy),
+ # Canvas-aware tools load this inside an activity. It never enters
+ # Temporal history or ordinary workflow outputs.
+ "source_context": _strip_credentials(
+ {
+ "nodes": context.get("nodes") or [],
+ "edges": context.get("edges") or [],
+ "workspace_dir": context.get("workspace_dir") or "",
+ "session_id": context.get("session_id"),
+ "execution_id": context.get("execution_id"),
+ "root_execution_id": context.get("root_execution_id"),
+ "data_scope_id": context.get("data_scope_id"),
+ "generation": generation,
+ "user_id": context.get("user_id"),
+ "delegated_task_id": (
+ _thread_inputs(context).get("delegated_task_id")
+ ),
+ "delegation_depth": context.get("delegation_depth", 0),
+ "team_id": context.get("team_id"),
+ }
+ ),
+ "operation_prefix": operation_id.rsplit(":prepare", 1)[0],
+ }
+
+ store = _context_store()
+ thread = await store.resolve_thread(
+ workflow_id=workflow_id,
+ context_node_id=context_node_id,
+ generation=generation,
+ **_thread_inputs(context),
+ )
+ runtime_config_ref = await store.put_blob(runtime_snapshot)
+ thread = await _append_event(
+ store,
+ thread,
+ event_type="request.snapshot",
+ operation_id=f"{operation_id}:snapshot",
+ provider=str(prepared.get("provider") or ""),
+ payload_ref=runtime_config_ref,
+ )
+
+ prompt = str(prepared.get("user_prompt") or "")
+ if prompt:
+ thread = await _append_event(
+ store,
+ thread,
+ event_type="message.user",
+ operation_id=f"{operation_id}:user",
+ provider=str(prepared.get("provider") or ""),
+ message_wire_v2=message_to_wire(
+ Message(role="user", content=prompt)
+ ),
+ )
+
+ return {
+ "context_ref": _ref_dict(thread),
+ "runtime_config_ref": runtime_config_ref,
+ "runtime_config_hash": _hash_from_ref(runtime_config_ref),
+ "max_iterations": max(1, int(prepared.get("max_iterations") or 1)),
+ "tool_identities": [
+ _tool_identity(tool)
+ for tool in prepared.get("tools") or []
+ if not tool.get("llm_hidden")
+ ],
+ }
+def _pending_tool_identities(
+ calls: Iterable[Mapping[str, Any]],
+ configured_tools: Iterable[Mapping[str, Any]],
+) -> List[Dict[str, Any]]:
+ index = {
+ str(tool.get("name") or ""): tool
+ for tool in configured_tools
+ if not tool.get("llm_hidden")
+ }
+ pending: List[Dict[str, Any]] = []
+ for call_index, call in enumerate(calls, start=1):
+ name = str(call.get("name") or "")
+ tool = index.get(name) or {}
+ pending.append(
+ {
+ "call_index": call_index,
+ "call_id": str(
+ call.get("id") or f"call-{call_index}"
+ ),
+ "name": name,
+ "node_id": str(tool.get("tool_node_id") or ""),
+ "node_type": str(tool.get("node_type") or ""),
+ "version": int(tool.get("version") or 1),
+ "task_queue": str(
+ tool.get("dispatch_task_queue") or ""
+ ),
+ "known": bool(tool),
+ }
+ )
+ return pending
+async def _execute_context_llm_step(payload: Dict[str, Any]) -> Dict[str, Any]:
+ """Journal-backed variant: load checkpoint-plus-tail, call once, commit.
+
+ Not an activity of its own — ``execute_llm_step`` dispatches here when
+ the payload carries a ``context_ref``, so there is exactly one LLM-step
+ activity regardless of whether the agent is journal-backed.
+ """
+
+ from services.agent_context import (
+ ContextCompactionPolicy,
+ provider_context_request_options,
+ reconstruct_message_wire_v2,
+ )
+ from services.llm.protocol import Message, message_to_wire
+
+ store = _context_store()
+ ref = _context_ref(payload["context_ref"])
+ config = await _runtime_config(
+ store,
+ str(payload["runtime_config_ref"]),
+ )
+ prepared = dict(config["prepared"])
+ provider = str(prepared.get("provider") or "")
+ policy = ContextCompactionPolicy.from_mapping(
+ dict(config.get("context_policy") or {})
+ )
+ operation_id = str(payload["operation_id"])
+ iteration = int(payload.get("iteration") or 1)
+
+ current_ref, wires = await reconstruct_message_wire_v2(store, ref)
+ system_message = str(prepared.get("system_message") or "")
+ messages = list(wires)
+ if system_message:
+ messages.insert(
+ 0,
+ message_to_wire(
+ Message(role="system", content=system_message)
+ ),
+ )
+ llm_payload = {
+ "node_id": prepared.get("node_id"),
+ "provider": provider,
+ "model": prepared.get("model"),
+ "messages": messages,
+ "tools": [
+ tool.get("definition") or {}
+ for tool in prepared.get("tools") or []
+ if not tool.get("llm_hidden")
+ ],
+ "temperature": prepared.get("temperature", 0.7),
+ "max_tokens": prepared.get("max_tokens", 4096),
+ "thinking_config": prepared.get("thinking_config"),
+ "message_wire_version": prepared.get("message_wire_version", 2),
+ "include_finish_reason": True,
+ "context_management": provider_context_request_options(
+ provider=provider,
+ model=str(prepared.get("model") or ""),
+ policy=policy,
+ ),
+ }
+
+ activity.heartbeat(
+ f"Context V2 LLM step starting: {prepared.get('model')}"
+ )
+ try:
+ result = dict(await _execute_native_llm_step(llm_payload))
+ except BaseException as exc:
+ # The provider may have accepted a request even though the caller did
+ # not receive a response. Record the ambiguity and let the one-shot
+ # Temporal policy surface it; never silently rebill.
+ ambiguous_ref = await store.put_blob(
+ {
+ "iteration": iteration,
+ "error_type": type(exc).__name__,
+ "ambiguous": True,
+ }
+ )
+ try:
+ await _append_event(
+ store,
+ current_ref,
+ event_type="provider.outcome_ambiguous",
+ operation_id=f"{operation_id}:ambiguous",
+ provider=provider,
+ payload_ref=ambiguous_ref,
+ )
+ except Exception:
+ activity.logger.exception(
+ "Could not persist ambiguous Context V2 provider outcome"
+ )
+ raise
+
+ response_ref = await store.put_blob(
+ {
+ "kind": result.get("kind"),
+ "assistant_message": result.get("assistant_message"),
+ "calls": result.get("calls") or [],
+ "content": result.get("content") or "",
+ "thinking": result.get("thinking"),
+ "usage": result.get("usage") or {},
+ "finish_reason": result.get("finish_reason"),
+ "iteration": iteration,
+ }
+ )
+ current_ref = await _append_event(
+ store,
+ current_ref,
+ event_type="message.assistant",
+ operation_id=f"{operation_id}:assistant",
+ provider=provider,
+ message_wire_v2=dict(result["assistant_message"]),
+ payload_ref=response_ref,
+ )
+ finish_reason = str(result.get("finish_reason") or "").strip().lower()
+ if finish_reason == "compaction":
+ current_ref = await _append_event(
+ store,
+ current_ref,
+ event_type="provider.compaction_paused",
+ operation_id=f"{operation_id}:compaction-pause",
+ provider=provider,
+ payload_ref=response_ref,
+ )
+
+ usage = dict(result.get("usage") or {})
+ pending = _pending_tool_identities(
+ result.get("calls") or [],
+ prepared.get("tools") or [],
+ )
+ return {
+ "context_ref": _ref_dict(current_ref),
+ "response_ref": response_ref,
+ "response_hash": _hash_from_ref(response_ref),
+ "iteration": iteration,
+ "status": (
+ "compaction_pause"
+ if finish_reason == "compaction"
+ else (
+ "tool_calls"
+ if result.get("kind") == "tool_calls"
+ else "final"
+ )
+ ),
+ "pending_tools": pending,
+ # Delegation builds its child prompt from candidate["args"] in the
+ # workflow; pending_tools is identity-only and cannot carry them.
+ "calls": list(result.get("calls") or []),
+ "assistant_message": result.get("assistant_message"),
+ "kind": result.get("kind"),
+ "active_input_tokens": int(usage.get("input_tokens") or 0),
+ "output_tokens": int(usage.get("output_tokens") or 0),
+ }
+def _find_call(
+ response: Mapping[str, Any],
+ pending: Mapping[str, Any],
+) -> Dict[str, Any]:
+ call_index = int(pending.get("call_index") or 0)
+ calls = list(response.get("calls") or [])
+ if call_index < 1 or call_index > len(calls):
+ raise _non_retryable(
+ "Pending Context V2 tool identity does not match its response.",
+ "InvalidPendingAgentTool",
+ )
+ call = dict(calls[call_index - 1])
+ if (
+ str(call.get("name") or "") != str(pending.get("name") or "")
+ or str(call.get("id") or f"call-{call_index}")
+ != str(pending.get("call_id") or "")
+ ):
+ raise _non_retryable(
+ "Pending Context V2 tool identity hash boundary was violated.",
+ "InvalidPendingAgentTool",
+ )
+ return call
+async def _validate_tool_args(
+ tool: Mapping[str, Any],
+ call: Mapping[str, Any],
+) -> tuple[Optional[Dict[str, Any]], Optional[Dict[str, Any]]]:
+ if call.get("parse_error"):
+ return None, {
+ "error": "Tool arguments were invalid JSON",
+ "detail": str(call.get("parse_error") or ""),
+ "raw_arguments": str(call.get("raw_arguments") or ""),
+ }
+ args = call.get("args") or {}
+ if not isinstance(args, dict):
+ return None, {
+ "error": "Tool arguments must be an object",
+ }
+
+ definition = dict(tool.get("definition") or {})
+ schema = dict(
+ definition.get("parameters")
+ or {"type": "object", "properties": {}}
+ )
+ try:
+ from jsonschema import Draft202012Validator
+
+ Draft202012Validator(schema).validate(args)
+ except Exception as exc:
+ return None, {
+ "error": "Invalid tool arguments",
+ "details": str(exc),
+ }
+
+ # Security-sensitive ToolInput models also validate through the plugin
+ # boundary so model arguments cannot replace persisted configuration.
+ from services.node_registry import get_node_class
+
+ node_cls = get_node_class(str(tool.get("node_type") or ""))
+ if node_cls is not None:
+ model_factory = getattr(node_cls, "tool_input_model", None)
+ if callable(model_factory):
+ try:
+ model = model_factory()
+ value = model.model_validate(args)
+ return (
+ value.model_dump(mode="json", exclude_unset=True),
+ None,
+ )
+ except Exception as exc:
+ return None, {
+ "error": "Invalid tool arguments",
+ "details": str(exc),
+ }
+ return dict(args), None
+async def _execute_tool_and_append(
+ *,
+ store: Any,
+ ref: AgentContextRef,
+ runtime_config: Dict[str, Any],
+ runtime_config_ref: str,
+ response_ref: str,
+ pending: Dict[str, Any],
+ operation_id: str,
+ iteration: int,
+) -> Dict[str, Any]:
+ from core.container import container
+ from services.handlers.tools import execute_tool
+ from services.llm.protocol import Message, message_to_wire
+ from services.node_registry import get_node_class
+
+ prepared = dict(runtime_config["prepared"])
+ provider = str(prepared.get("provider") or "")
+ response = await store.get_blob(response_ref)
+ if not isinstance(response, dict):
+ raise _non_retryable(
+ "Context V2 response reference is invalid.",
+ "InvalidAgentContextResponse",
+ )
+ call = _find_call(response, pending)
+ tool = next(
+ (
+ item
+ for item in prepared.get("tools") or []
+ if str(item.get("name") or "") == str(call.get("name") or "")
+ and not item.get("llm_hidden")
+ ),
+ None,
+ )
+
+ if tool is None:
+ tool_result: Any = {
+ "error": (
+ f"Tool {call.get('name')!r} is not connected to this agent."
+ )
+ }
+ else:
+ args, validation_error = await _validate_tool_args(tool, call)
+ if validation_error is not None:
+ tool_result = validation_error
+ else:
+ source_context = dict(runtime_config.get("source_context") or {})
+ node_cls = get_node_class(str(tool.get("node_type") or ""))
+ needs_canvas = bool(
+ node_cls is not None
+ and getattr(node_cls, "needs_canvas", False)
+ )
+ tool_info = dict(tool.get("tool_info") or {})
+ execution = {
+ "node_type": str(tool.get("node_type") or ""),
+ "node_id": str(tool.get("tool_node_id") or ""),
+ "parameters": dict(tool_info.get("parameters") or {}),
+ "label": str(
+ tool_info.get("label")
+ or tool.get("node_type")
+ or call.get("name")
+ or "tool"
+ ),
+ "connected_services": list(
+ tool_info.get("connected_services") or []
+ ),
+ "workflow_id": runtime_config.get("workflow_id"),
+ "ai_service": container.ai_service(),
+ "database": container.database(),
+ "parent_node_id": runtime_config.get("agent_node_id"),
+ "provider": provider,
+ "nodes": source_context.get("nodes") if needs_canvas else [],
+ "edges": source_context.get("edges") if needs_canvas else [],
+ "workspace_dir": source_context.get("workspace_dir") or "",
+ "session_id": source_context.get("session_id") or "default",
+ "user_id": source_context.get("user_id") or "owner",
+ "execution_id": source_context.get("execution_id"),
+ "root_execution_id": source_context.get("root_execution_id"),
+ "tool_call_id": str(call.get("id") or ""),
+ "operation_id": operation_id,
+ "delegation_depth": source_context.get("delegation_depth", 0),
+ "team_id": source_context.get("team_id"),
+ "max_concurrent_subagents": prepared.get(
+ "max_concurrent_subagents", 3
+ ),
+ "max_delegation_depth": prepared.get(
+ "max_delegation_depth", 2
+ ),
+ "auto_rebind_tools": bool(
+ prepared.get("auto_rebind_tools", True)
+ ),
+ }
+ try:
+ tool_result = await execute_tool(
+ str(call.get("name") or ""),
+ args or {},
+ execution,
+ )
+ except Exception as exc:
+ # Tool failures are exact observable outcomes and are returned
+ # to the model rather than failing the whole agent loop.
+ tool_result = {
+ "error": str(exc),
+ "error_type": type(exc).__name__,
+ }
+
+ try:
+ tool_content = json.dumps(
+ tool_result,
+ ensure_ascii=False,
+ default=str,
+ )
+ except Exception:
+ tool_content = str(tool_result)
+ result_ref = await store.put_blob(
+ {
+ "tool_call_id": str(call.get("id") or pending.get("call_id") or ""),
+ "tool_name": str(call.get("name") or ""),
+ "result": tool_result,
+ "iteration": iteration,
+ }
+ )
+ tool_message = Message(
+ role="tool",
+ content=tool_content,
+ tool_call_id=str(call.get("id") or pending.get("call_id") or ""),
+ name=str(call.get("name") or ""),
+ )
+ ref = await _append_event(
+ store,
+ ref,
+ event_type="message.tool_result",
+ operation_id=f"{operation_id}:result",
+ provider=provider,
+ message_wire_v2=message_to_wire(tool_message),
+ payload_ref=result_ref,
+ )
+
+ # Canvas-mutating tools can rebind newly-created tool identities without
+ # placing their schemas or operation results in Event History.
+ next_runtime_ref = runtime_config_ref
+ operations = (
+ tool_result.get("operations") or []
+ if isinstance(tool_result, dict)
+ else []
+ )
+ if operations and prepared.get("auto_rebind_tools", True):
+ from services.temporal.agent_activities import refresh_agent_tools
+
+ refreshed = await refresh_agent_tools(
+ {
+ "operations": operations,
+ "agent_node_type": prepared.get("node_type"),
+ }
+ )
+ additions = list(refreshed.get("tools") or [])
+ _validate_unique_tool_names(
+ [*(prepared.get("tools") or []), *additions],
+ phase="hot rebind",
+ )
+ if additions:
+ prepared["tools"] = [
+ *(prepared.get("tools") or []),
+ *additions,
+ ]
+ runtime_config = {
+ **runtime_config,
+ "prepared": prepared,
+ }
+ next_runtime_ref = await store.put_blob(runtime_config)
+ ref = await _append_event(
+ store,
+ ref,
+ event_type="request.tools_rebound",
+ operation_id=f"{operation_id}:rebind",
+ provider=provider,
+ payload_ref=next_runtime_ref,
+ )
+
+ return {
+ "context_ref": _ref_dict(ref),
+ "runtime_config_ref": next_runtime_ref,
+ "result_ref": result_ref,
+ "result_hash": _hash_from_ref(result_ref),
+ }
+async def execute_tool_activity(
+ payload: Dict[str, Any],
+ *,
+ expected_node_type: str,
+ expected_version: int,
+) -> Dict[str, Any]:
+ """Execute one Context V2 tool from its plugin-owned activity.
+
+ The Temporal payload contains only hashes, Context references, and the
+ pending tool identity. Tool arguments and persisted configuration remain
+ in ``AgentContextStore`` and are loaded inside the activity.
+ """
+
+ if payload.get("protocol") != "agent-context-tool-v2":
+ raise _non_retryable(
+ "The plugin activity received an invalid Context V2 tool payload.",
+ "InvalidAgentToolActivityPayload",
+ )
+ pending = dict(payload.get("pending_tool") or {})
+ if (
+ not pending.get("known")
+ or str(pending.get("node_type") or "") != expected_node_type
+ or int(pending.get("version") or 0) != int(expected_version)
+ ):
+ raise _non_retryable(
+ "The pending tool identity does not match the dispatched plugin.",
+ "InvalidPendingAgentTool",
+ )
+
+ store = _context_store()
+ ref = _context_ref(payload["context_ref"])
+ runtime_config_ref = str(payload["runtime_config_ref"])
+ config = await _runtime_config(store, runtime_config_ref)
+ configured = next(
+ (
+ item
+ for item in config["prepared"].get("tools") or []
+ if str(item.get("name") or "") == str(pending.get("name") or "")
+ ),
+ None,
+ )
+ if (
+ not isinstance(configured, dict)
+ or str(configured.get("tool_node_id") or "")
+ != str(pending.get("node_id") or "")
+ or str(configured.get("node_type") or "") != expected_node_type
+ or int(configured.get("version") or 1) != int(expected_version)
+ ):
+ raise _non_retryable(
+ "The persisted tool binding no longer matches the pending identity.",
+ "InvalidPendingAgentTool",
+ )
+
+ return await _execute_tool_and_append(
+ store=store,
+ ref=ref,
+ runtime_config=config,
+ runtime_config_ref=runtime_config_ref,
+ response_ref=str(payload["response_ref"]),
+ pending=pending,
+ operation_id=str(payload["operation_id"]),
+ iteration=int(payload.get("iteration") or 0),
+ )
+async def _lifetime_usage(
+ store: Any,
+ ref: AgentContextRef,
+ operation_prefix: str,
+) -> Dict[str, int]:
+ totals: Dict[str, int] = {}
+ after = 0
+ while True:
+ page, next_after = await store.load_journal_page(
+ ref,
+ after_sequence=after,
+ limit=200,
+ epoch=ref.epoch,
+ )
+ for event in page:
+ if (
+ event.event_type
+ not in {"message.assistant", "context.compacted"}
+ or not event.payload_ref
+ or not event.operation_id.startswith(operation_prefix)
+ ):
+ continue
+ value = await store.get_blob(event.payload_ref)
+ if not isinstance(value, dict):
+ continue
+ for key, amount in (value.get("usage") or {}).items():
+ if isinstance(amount, int):
+ totals[key] = totals.get(key, 0) + amount
+ if next_after is None:
+ break
+ after = next_after
+ return totals
+async def _finalize(
+ *,
+ store: Any,
+ ref: AgentContextRef,
+ runtime_config: Dict[str, Any],
+ response_ref: str,
+ operation_id: str,
+ truncated: bool,
+) -> Dict[str, Any]:
+ from core.container import container
+
+ response = await store.get_blob(response_ref)
+ if not isinstance(response, dict):
+ raise _non_retryable(
+ "Context V2 final response reference is invalid.",
+ "InvalidAgentContextResponse",
+ )
+ prepared = dict(runtime_config["prepared"])
+ operation_prefix = str(runtime_config.get("operation_prefix") or "")
+ usage = await _lifetime_usage(store, ref, operation_prefix)
+ result = {
+ "response": str(response.get("content") or ""),
+ "thinking": response.get("thinking"),
+ "model": prepared.get("model"),
+ "provider": prepared.get("provider"),
+ "usage": usage,
+ "execution_id": (
+ runtime_config.get("source_context") or {}
+ ).get("execution_id"),
+ "root_execution_id": (
+ runtime_config.get("source_context") or {}
+ ).get("root_execution_id"),
+ "truncated": truncated,
+ }
+ output_ref = await store.put_blob(result)
+ ref = await _append_event(
+ store,
+ ref,
+ event_type=(
+ "response.truncated" if truncated else "response.final"
+ ),
+ operation_id=f"{operation_id}:response",
+ provider=str(prepared.get("provider") or ""),
+ payload_ref=output_ref,
+ )
+
+ workflow_service = container.workflow_service()
+ session_id = str(
+ (runtime_config.get("source_context") or {}).get("session_id")
+ or "default"
+ )
+ node_id = str(runtime_config.get("agent_node_id") or "")
+ for output_name in ("output_main", "output_top", "output_0"):
+ await workflow_service.store_node_output(
+ session_id,
+ node_id,
+ output_name,
+ result,
+ )
+
+ try:
+ from services.skill_runtime import clear_skill_turn
+
+ await clear_skill_turn(
+ str(runtime_config.get("workflow_id") or ""),
+ str(
+ (runtime_config.get("source_context") or {}).get(
+ "execution_id"
+ )
+ or ""
+ ),
+ node_id,
+ )
+ except Exception:
+ activity.logger.exception(
+ "Context V2 finalization could not clear active skills"
+ )
+
+ return {
+ "context_ref": _ref_dict(ref),
+ "output_ref": output_ref,
+ "response_hash": _hash_from_ref(response_ref),
+ }
[email protected](name="agent.append_context")
+async def append_context(payload: Dict[str, Any]) -> Dict[str, Any]:
+ """Record an unknown tool outcome or finalize a persisted response."""
+
+ from services.llm.protocol import Message, message_to_wire
+
+ store = _context_store()
+ ref = _context_ref(payload["context_ref"])
+ runtime_config_ref = str(payload["runtime_config_ref"])
+ config = await _runtime_config(store, runtime_config_ref)
+ action = str(payload.get("action") or "")
+ operation_id = str(payload["operation_id"])
+ iteration = int(payload.get("iteration") or 0)
+
+ if action == "record_unknown_tool":
+ pending = dict(payload["pending_tool"])
+ if pending.get("known"):
+ raise _non_retryable(
+ "Known tools must execute on their plugin-owned activity.",
+ "InvalidAgentToolActivityPayload",
+ )
+ return await _execute_tool_and_append(
+ store=store,
+ ref=ref,
+ runtime_config=config,
+ runtime_config_ref=runtime_config_ref,
+ response_ref=str(payload["response_ref"]),
+ pending=pending,
+ operation_id=operation_id,
+ iteration=iteration,
+ )
+ if action == "append_messages":
+ # Journal turns that were produced outside a journaling activity.
+ # The agent loop executes tools through its own per-type and
+ # delegation paths, so their results never pass through
+ # ``_execute_tool_and_append``; without this the journal loses every
+ # tool result and the next reconstruction replays an assistant turn
+ # whose tool calls have no answers.
+ wires = list(payload.get("messages") or [])
+ current = ref
+ for offset, wire in enumerate(wires):
+ if not isinstance(wire, dict):
+ continue
+ current = await _append_event(
+ store,
+ current,
+ event_type=str(payload.get("event_type") or "message.tool"),
+ operation_id=f"{operation_id}:{offset}",
+ provider=str(config.get("prepared", {}).get("provider") or ""),
+ message_wire_v2=wire,
+ )
+ return {"context_ref": _ref_dict(current), "appended": len(wires)}
+
+ if action == "finalize":
+ return await _finalize(
+ store=store,
+ ref=ref,
+ runtime_config=config,
+ response_ref=str(payload["response_ref"]),
+ operation_id=operation_id,
+ truncated=False,
+ )
+ if action == "truncate":
+ prepared = dict(config["prepared"])
+ content = (
+ "[Recursion limit reached. Simplify the task or increase the "
+ "agent recursion limit.]"
+ )
+ message = Message(role="assistant", content=content)
+ response_ref = await store.put_blob(
+ {
+ "kind": "final",
+ "assistant_message": message_to_wire(message),
+ "content": content,
+ "thinking": None,
+ "usage": {},
+ "iteration": iteration,
+ "truncated": True,
+ }
+ )
+ ref = await _append_event(
+ store,
+ ref,
+ event_type="message.assistant",
+ operation_id=f"{operation_id}:assistant",
+ provider=str(prepared.get("provider") or ""),
+ message_wire_v2=message_to_wire(message),
+ payload_ref=response_ref,
+ )
+ return await _finalize(
+ store=store,
+ ref=ref,
+ runtime_config=config,
+ response_ref=response_ref,
+ operation_id=operation_id,
+ truncated=True,
+ )
+ raise _non_retryable(
+ f"Unknown Context V2 append action {action!r}.",
+ "InvalidAgentContextAppendAction",
+ )
+def _estimate_tokens(value: Any) -> int:
+ encoded = json.dumps(
+ value,
+ ensure_ascii=False,
+ separators=(",", ":"),
+ default=str,
+ )
+ return max(1, (len(encoded) + 3) // 4)
[email protected](name="agent.compact_context")
+async def compact_context(
+ payload: Dict[str, Any],
+) -> Dict[str, Any]:
+ """Run Temporal compaction through the shared provider adapter service."""
+
+ from core.container import container
+ from services.agent_context import (
+ AgentContextCompactionService,
+ CompactionConflictError,
+ ContextCompactionCandidate,
+ ContextCompactionPolicy,
+ reconstruct_message_wire_v2,
+ )
+ from services.agent_runtime import run_native_llm_step
+ from services.llm.protocol import (
+ Message,
+ message_from_wire,
+ message_to_wire,
+ )
+ from services.temporal.agent_activities import (
+ _await_with_llm_heartbeats,
+ _resolve_activity_api_key,
+ )
+
+ store = _context_store()
+ ref = _context_ref(payload["context_ref"])
+ config = await _runtime_config(
+ store,
+ str(payload["runtime_config_ref"]),
+ )
+ prepared = dict(config["prepared"])
+ provider = str(prepared.get("provider") or "")
+ model = str(prepared.get("model") or "")
+ policy = ContextCompactionPolicy.from_mapping(
+ dict(config.get("context_policy") or {})
+ )
+ response = await store.get_blob(str(payload["response_ref"]))
+ usage = (
+ dict(response.get("usage") or {})
+ if isinstance(response, dict)
+ else {}
+ )
+ active_input_tokens = int(usage.get("input_tokens") or 0) + int(
+ usage.get("output_tokens") or 0
+ )
+ output_headroom = max(0, int(prepared.get("max_tokens") or 0))
+ rendered_request: Optional[List[Dict[str, Any]]] = None
+ try:
+ ref, rendered_request = await reconstruct_message_wire_v2(store, ref)
+ except Exception:
+ # Provider-native checkpoints may be intentionally opaque. Exact
+ # provider usage still drives pressure in that case.
+ rendered_request = None
+
+ async def _portable_compactor(
+ wires: list[dict[str, Any]],
+ _policy: ContextCompactionPolicy,
+ ) -> ContextCompactionCandidate:
+ transcript: list[str] = []
+ for wire in wires:
+ message = message_from_wire(wire)
+ line = f"{message.role.upper()}: {message.content}"
+ if message.tool_calls:
+ line += "\nTOOL_CALLS: " + json.dumps(
+ [
+ {
+ "id": call.id,
+ "name": call.name,
+ "args": call.args,
+ "raw_arguments": call.raw_arguments,
+ "parse_error": call.parse_error,
+ }
+ for call in message.tool_calls
+ ],
+ ensure_ascii=False,
+ default=str,
+ )
+ if message.tool_call_id:
+ line += (
+ f"\nTOOL_RESULT_FOR: {message.tool_call_id} "
+ f"({message.name or 'tool'})"
+ )
+ transcript.append(line)
+ prompt = (
+ "Create a compact, provider-portable continuation checkpoint "
+ "for the exact agent transcript below. Preserve instructions, "
+ "decisions, constraints, identifiers, completed and pending "
+ "tool work, errors, open questions, and the next action. Never "
+ "claim a tool ran unless its result appears. Return only the "
+ "checkpoint.\n\n"
+ + "\n\n".join(transcript)
+ )
+ api_key = await _resolve_activity_api_key(prepared)
+ compacted = await _await_with_llm_heartbeats(
+ run_native_llm_step(
+ container.chat_unifier(),
+ provider=provider,
+ api_key=api_key,
+ messages=[Message(role="user", content=prompt)],
+ model=model,
+ temperature=0.2,
+ max_tokens=min(4096, output_headroom or 4096),
+ sdk_max_retries=0,
+ explicit_max_retries=0,
+ translate_errors=False,
+ ),
+ detail=f"Context V2 compaction waiting: {model}",
+ )
+ summary = str(compacted.content or "").strip()
+ if not summary:
+ raise ValueError("empty_compaction_candidate")
+ summary_wire = dict(
+ message_to_wire(
+ Message(
+ role="user",
+ content=(
+ "<agent_context_checkpoint>\n"
+ f"{summary}\n"
+ "</agent_context_checkpoint>"
+ ),
+ )
+ )
+ )
+ message_from_wire(summary_wire)
+ billing_usage = compacted.billing_usage or compacted.usage
+ return ContextCompactionCandidate(
+ strategy="portable_structured",
+ replay_payload={
+ "format": "agent-context-portable-v1",
+ "messages": [summary_wire],
+ },
+ active_token_count=_estimate_tokens([summary_wire]),
+ lifetime_usage=asdict(billing_usage),
+ )
+
+ operation_id = str(payload["operation_id"])
+ try:
+ result = await AgentContextCompactionService(
+ store
+ ).update_pressure_and_compact(
+ ref,
+ operation_id=operation_id,
+ provider=provider,
+ model=model,
+ policy=policy,
+ active_input_tokens=active_input_tokens,
+ output_headroom=output_headroom,
+ rendered_request=rendered_request,
+ portable_compactor=_portable_compactor,
+ )
+ except CompactionConflictError:
+ latest = await store.load_active(ref)
+ return {
+ "context_ref": _ref_dict(latest.ref),
+ "status": "cas_conflict",
+ }
+ except Exception as exc:
+ activity.logger.warning(
+ "Context V2 compaction failed; prior checkpoint retained: %s",
+ type(exc).__name__,
+ )
+ latest = await store.load_active(ref)
+ return {
+ "context_ref": _ref_dict(latest.ref),
+ "status": "failed",
+ "error_code": type(exc).__name__,
+ }
+
+ latest_ref = result.ref
+ response_payload: Dict[str, Any] = {
+ "context_ref": _ref_dict(latest_ref),
+ "status": result.reason,
+ "active_pressure": result.pressure_tokens,
+ "context_window": result.context_window,
+ }
+ if result.compacted and result.checkpoint is not None:
+ usage_ref = await store.put_blob(
+ {
+ "usage": result.lifetime_usage,
+ "active_tokens_before": active_input_tokens + output_headroom,
+ "active_tokens_after": result.pressure_tokens,
+ "checkpoint_ref": result.checkpoint.replay_payload_ref,
+ "strategy": result.checkpoint.strategy,
+ }
+ )
+ latest_ref = await _append_event(
+ store,
+ latest_ref,
+ event_type="context.compacted",
+ operation_id=f"{operation_id}:event",
+ provider=provider,
+ payload_ref=usage_ref,
+ )
+ response_payload.update(
+ {
+ "context_ref": _ref_dict(latest_ref),
+ "status": "compacted",
+ "strategy": result.checkpoint.strategy,
+ "checkpoint_hash": _hash_from_ref(
+ result.checkpoint.replay_payload_ref
+ ),
+ "active_tokens_after": result.pressure_tokens,
+ }
+ )
+ return response_payload
diff --git a/server/services/temporal/agent_workflow.py b/server/services/temporal/agent_workflow.py
index e69c2358..2e97ba45 100644
--- a/server/services/temporal/agent_workflow.py
+++ b/server/services/temporal/agent_workflow.py
@@ -111,36 +111,12 @@ def _default_max_iterations() -> int:
# failures inside the LLM step fail fast instead of burning 3 retries.
AGENT_ACTIVITY_RETRY: RetryPolicy = DEFAULT_ACTIVITY_RETRY
-# Temporal patch marker for per-call command identity and duplicate-name
-# validation. Existing histories must retain their recorded activity/child
-# ids and last-wins tool index; new histories take the isolated path.
-TOOL_CALL_IDENTITY_V2_PATCH = "agent-tool-call-identity-v2"
-TASK_MANAGER_DELEGATION_PATCH = "agent-task-manager-delegation-v1"
-COOPERATIVE_PAUSE_SCHEDULING_PATCH = (
- "agent-cooperative-pause-scheduling-v1"
-)
-DELEGATED_TASK_COOPERATIVE_PAUSE_SCHEDULING_PATCH = (
- "delegated-task-cooperative-pause-scheduling-v1"
-)
-DELEGATION_PERMIT_CLEANUP_PATCH = "agent-delegation-permit-cleanup-v1"
-DELEGATION_ACQUIRE_CANCELLATION_PATCH = (
- "agent-delegation-acquire-cancellation-v1"
-)
-DELEGATED_TASK_CANCELLATION_TERMINAL_PATCH = (
- "delegated-task-cancellation-terminal-v1"
-)
-DELEGATED_TASK_SEARCH_ATTRIBUTES_PATCH = (
- "agent-delegated-task-search-attributes-v1"
-)
-# Agent/delegated children used to carry 1-2h execution/run timeouts and
-# the permit wait a ~3h total retry budget. Runs may execute — or stay
+# Agent/delegated children start with NO execution/run timeout, and the
+# permit wait retries indefinitely. Runs may execute — or stay
# cooperatively paused — for months, and Temporal's timeout timers keep
-# ticking through a pause, so the caps silently terminated long/paused
-# work (skipping the compensation blocks: leaked permits, stuck task
-# rows). New executions start children unbounded and wait for permits
-# indefinitely; the patch keeps replay compatibility for histories
-# recorded with the old caps.
-UNBOUNDED_LIFETIMES_PATCH = "agent-unbounded-lifetimes-v1"
+# ticking through a pause, so any lifetime cap silently terminated
+# long/paused work (skipping the compensation blocks: leaked permits,
+# stuck task rows). Liveness is the activity heartbeat's job.
DUPLICATE_TOOL_NAME_ERROR_TYPE = "DuplicateToolNameError"
@@ -221,12 +197,12 @@ def _native_assistant_thinking(message: Any) -> Optional[str]:
return "\n\n".join(parts) or None
-def _tool_activity_id_v2(tool_node_id: str, iteration: int, call_index: int) -> str:
+def _tool_activity_id(tool_node_id: str, iteration: int, call_index: int) -> str:
"""Return a stable, unique id for one tool call in one agent turn."""
return f"tool-{tool_node_id}-{iteration + 1}-{call_index + 1}"
-def _delegation_child_id_v2(
+def _delegation_child_id(
agent_workflow_id: str,
tool_node_id: str,
iteration: int,
@@ -236,11 +212,45 @@ def _delegation_child_id_v2(
return f"{agent_workflow_id}-delegate-{tool_node_id}-{iteration + 1}-{call_index + 1}"
-def _refresh_tools_activity_id_v2(tool_node_id: str, iteration: int, call_index: int) -> str:
+def _refresh_tools_activity_id(tool_node_id: str, iteration: int, call_index: int) -> str:
"""Return a stable id for the hot-refresh owned by one tool call."""
return f"refresh-tools-{tool_node_id}-{iteration + 1}-{call_index + 1}"
+# Keys that identify the durable scope a run executes in. They are injected
+# into a root node's context by MachinaWorkflow and must be inherited by
+# delegated children: without them a subagent resolves no context of its own
+# and its turns are silently never journalled. Forwarded verbatim — the
+# framework does not interpret any of them.
+_INHERITED_SCOPE_KEYS = (
+ "graphVersion",
+ "generation",
+ "data_scope_id",
+ "context_execution_id",
+ "context_session_id",
+ "user_id",
+ "workspace_dir",
+ "temporal_worker_pool_enabled",
+)
+
+
+# Roll the agent run over before Temporal's ~51,200-event hard terminate.
+# The loop carries the transcript and a growing tool list, so a long agent
+# run reaches that ceiling; the server's own suggestion is the primary
+# signal and this soft cap is the deterministic backstop.
+_AGENT_HISTORY_SOFT_CAP = 10_000
+
+# State carried across a rollover. Deliberately all O(1) or bounded: the
+# transcript is NOT carried -- a journal-backed run reconstructs it from
+# the Context store, which is the whole point of holding refs.
+_RESUME_MARKER = "_agent_resume"
+
+
+def _inherited_scope(context: Dict[str, Any]) -> Dict[str, Any]:
+ """Return the scope keys a delegated child must inherit from its parent."""
+ return {key: context[key] for key in _INHERITED_SCOPE_KEYS if key in context}
+
+
def _tool_call_metadata(
*,
agent_node_id: str,
@@ -328,25 +338,11 @@ class DelegatedTaskWorkflow:
@workflow.run
async def run(self, request: Dict[str, Any]) -> Dict[str, Any]:
- use_cooperative_pause_schedule_gate = workflow.patched(
- DELEGATED_TASK_COOPERATIVE_PAUSE_SCHEDULING_PATCH
- )
- use_acquire_cancellation_completion = workflow.patched(
- DELEGATION_ACQUIRE_CANCELLATION_PATCH
- )
- use_cancellation_terminal = workflow.patched(
- DELEGATED_TASK_CANCELLATION_TERMINAL_PATCH
- )
- use_unbounded_lifetimes = workflow.patched(UNBOUNDED_LIFETIMES_PATCH)
- acquire_cancellation_options = (
- {
- "cancellation_type": (
- ActivityCancellationType.WAIT_CANCELLATION_COMPLETED
- )
- }
- if use_acquire_cancellation_completion
- else {}
- )
+ acquire_cancellation_options = {
+ "cancellation_type": (
+ ActivityCancellationType.WAIT_CANCELLATION_COMPLETED
+ )
+ }
lifecycle = request["lifecycle"]
task_id = lifecycle["team_task_id"]
root_id = lifecycle["root_execution_id"]
@@ -356,7 +352,7 @@ class DelegatedTaskWorkflow:
await self._wait_until_resumed()
info = workflow.info()
await workflow.execute_activity(
- "agent.register_task_execution.v1",
+ "agent.register_task_execution",
args=[{**lifecycle, "runner_workflow_id": info.workflow_id,
"runner_run_id": info.run_id,
"child_workflow_id": request["child_workflow_id"]}],
@@ -364,56 +360,43 @@ class DelegatedTaskWorkflow:
retry_policy=AGENT_ACTIVITY_RETRY,
**acquire_cancellation_options,
)
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
acquire_payload = {
"root_execution_id": root_id,
"permit_id": task_id,
"limit": request.get("limit", 3),
+ "lease_version": 2,
}
- if use_acquire_cancellation_completion:
- acquire_payload["lease_version"] = 2
acquire_result = await workflow.execute_activity(
- "agent.acquire_subagent_permit.v1",
+ "agent.acquire_subagent_permit",
args=[acquire_payload],
start_to_close_timeout=timedelta(hours=1),
heartbeat_timeout=timedelta(seconds=10),
- # Unlimited attempts on the patched path — a queued
- # delegation waits as long as admission takes.
- retry_policy=(
- PERMIT_WAIT_RETRY if use_unbounded_lifetimes else AGENT_ACTIVITY_RETRY
- ),
+ # Unlimited attempts — a queued delegation waits as long
+ # as admission takes.
+ retry_policy=PERMIT_WAIT_RETRY,
**acquire_cancellation_options,
)
acquired_permit_id = str(
(acquire_result or {}).get("lease_id")
or task_id
)
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
await workflow.execute_activity(
- "agent.begin_delegation.v1", args=[lifecycle],
+ "agent.begin_delegation", args=[lifecycle],
start_to_close_timeout=PERSIST_TURN_TIMEOUT,
retry_policy=AGENT_ACTIVITY_RETRY,
**acquire_cancellation_options,
)
began = True
await self._wait_until_resumed()
- if use_acquire_cancellation_completion:
- request["child_context"]["team_permit_id"] = (
- acquired_permit_id
- )
- runner_child_kwargs: Dict[str, Any] = {"id": request["child_workflow_id"]}
- if not use_unbounded_lifetimes:
- # Replay-only: pre-patch histories recorded 1h caps.
- runner_child_kwargs["execution_timeout"] = timedelta(hours=1)
- runner_child_kwargs["run_timeout"] = timedelta(hours=1)
+ request["child_context"]["team_permit_id"] = acquired_permit_id
child_handle = await workflow.start_child_workflow(
"AgentWorkflow", args=[request["child_context"]],
- **runner_child_kwargs,
+ id=request["child_workflow_id"],
)
await workflow.execute_activity(
- "agent.register_task_execution.v1",
+ "agent.register_task_execution",
args=[{**lifecycle, "runner_workflow_id": info.workflow_id,
"runner_run_id": info.run_id,
"child_workflow_id": child_handle.id,
@@ -425,7 +408,7 @@ class DelegatedTaskWorkflow:
succeeded = bool(result.get("success", True)) if isinstance(result, dict) else True
_response, summary = _normalise_delegated_result(result)
return await workflow.execute_activity(
- "agent.finish_delegation.v1",
+ "agent.finish_delegation",
args=[{**lifecycle, "success": succeeded, "result": summary,
"error": result.get("error") if isinstance(result, dict) else None,
"terminal_event_id": f"{task_id}:terminal"}],
@@ -433,77 +416,76 @@ class DelegatedTaskWorkflow:
retry_policy=AGENT_ACTIVITY_RETRY,
)
except asyncio.CancelledError:
- if use_cancellation_terminal:
- if began:
- try:
- await workflow.execute_activity(
- "agent.cancel_delegation.v1",
- args=[{
- **lifecycle,
- "reason": (
- "Delegated task workflow cancelled"
- ),
- "terminal_event_id": f"{task_id}:terminal",
- }],
- activity_id="cancel-delegation",
- start_to_close_timeout=PERSIST_TURN_TIMEOUT,
- retry_policy=DELEGATION_CLEANUP_RETRY,
- **acquire_cancellation_options,
- )
- except Exception as cleanup_exc: # noqa: BLE001
- workflow.logger.error(
- "DelegatedTaskWorkflow failed to persist "
- f"cancellation for {task_id}: {cleanup_exc}"
- )
- else:
- # Queue/acquire/begin may have completed even when their
- # result lost the cancellation race. The dedicated
- # transition is idempotent for absent/already-terminal
- # tasks and never invokes the normal failure/requeue path.
- try:
- await workflow.execute_activity(
- "agent.cancel_delegation.v1",
- args=[{
- **lifecycle,
- "reason": (
- "Delegated task workflow cancelled"
- ),
- "terminal_event_id": f"{task_id}:terminal",
- }],
- activity_id="cancel-delegation-before-begin",
- start_to_close_timeout=PERSIST_TURN_TIMEOUT,
- retry_policy=DELEGATION_CLEANUP_RETRY,
- **acquire_cancellation_options,
- )
- except Exception as cleanup_exc: # noqa: BLE001
- workflow.logger.error(
- "DelegatedTaskWorkflow failed to persist early "
- f"cancellation for {task_id}: {cleanup_exc}"
- )
- if acquired_permit_id:
- try:
- await workflow.execute_activity(
- "agent.release_subagent_permit.v1",
- args=[{
- "root_execution_id": root_id,
- "permit_id": acquired_permit_id,
- }],
- activity_id="release-permit-cancelled",
- start_to_close_timeout=PERSIST_TURN_TIMEOUT,
- retry_policy=DELEGATION_CLEANUP_RETRY,
- **acquire_cancellation_options,
- )
- acquired_permit_id = None
- except Exception as cleanup_exc: # noqa: BLE001
- workflow.logger.error(
- "DelegatedTaskWorkflow failed to release "
- f"cancelled permit {task_id}: {cleanup_exc}"
- )
+ if began:
+ try:
+ await workflow.execute_activity(
+ "agent.cancel_delegation",
+ args=[{
+ **lifecycle,
+ "reason": (
+ "Delegated task workflow cancelled"
+ ),
+ "terminal_event_id": f"{task_id}:terminal",
+ }],
+ activity_id="cancel-delegation",
+ start_to_close_timeout=PERSIST_TURN_TIMEOUT,
+ retry_policy=DELEGATION_CLEANUP_RETRY,
+ **acquire_cancellation_options,
+ )
+ except Exception as cleanup_exc: # noqa: BLE001
+ workflow.logger.error(
+ "DelegatedTaskWorkflow failed to persist "
+ f"cancellation for {task_id}: {cleanup_exc}"
+ )
+ else:
+ # Queue/acquire/begin may have completed even when their
+ # result lost the cancellation race. The dedicated
+ # transition is idempotent for absent/already-terminal
+ # tasks and never invokes the normal failure/requeue path.
+ try:
+ await workflow.execute_activity(
+ "agent.cancel_delegation",
+ args=[{
+ **lifecycle,
+ "reason": (
+ "Delegated task workflow cancelled"
+ ),
+ "terminal_event_id": f"{task_id}:terminal",
+ }],
+ activity_id="cancel-delegation-before-begin",
+ start_to_close_timeout=PERSIST_TURN_TIMEOUT,
+ retry_policy=DELEGATION_CLEANUP_RETRY,
+ **acquire_cancellation_options,
+ )
+ except Exception as cleanup_exc: # noqa: BLE001
+ workflow.logger.error(
+ "DelegatedTaskWorkflow failed to persist early "
+ f"cancellation for {task_id}: {cleanup_exc}"
+ )
+ if acquired_permit_id:
+ try:
+ await workflow.execute_activity(
+ "agent.release_subagent_permit",
+ args=[{
+ "root_execution_id": root_id,
+ "permit_id": acquired_permit_id,
+ }],
+ activity_id="release-permit-cancelled",
+ start_to_close_timeout=PERSIST_TURN_TIMEOUT,
+ retry_policy=DELEGATION_CLEANUP_RETRY,
+ **acquire_cancellation_options,
+ )
+ acquired_permit_id = None
+ except Exception as cleanup_exc: # noqa: BLE001
+ workflow.logger.error(
+ "DelegatedTaskWorkflow failed to release "
+ f"cancelled permit {task_id}: {cleanup_exc}"
+ )
raise
except Exception as exc:
if began:
await workflow.execute_activity(
- "agent.finish_delegation.v1",
+ "agent.finish_delegation",
args=[{**lifecycle, "success": False,
"error": f"{type(exc).__name__}: {exc}",
"terminal_event_id": f"{task_id}:terminal"}],
@@ -513,28 +495,19 @@ class DelegatedTaskWorkflow:
raise
finally:
if acquired_permit_id:
- release_options = (
- {
- "activity_id": "release-permit-final",
- "retry_policy": DELEGATION_CLEANUP_RETRY,
- }
- if use_cancellation_terminal
- else {"retry_policy": AGENT_ACTIVITY_RETRY}
- )
try:
await workflow.execute_activity(
- "agent.release_subagent_permit.v1",
+ "agent.release_subagent_permit",
args=[{
"root_execution_id": root_id,
"permit_id": acquired_permit_id,
}],
+ activity_id="release-permit-final",
start_to_close_timeout=PERSIST_TURN_TIMEOUT,
+ retry_policy=DELEGATION_CLEANUP_RETRY,
**acquire_cancellation_options,
- **release_options,
)
except Exception as release_exc:
- if not use_cancellation_terminal:
- raise
workflow.logger.error(
"DelegatedTaskWorkflow final permit release failed "
f"for {task_id}: {release_exc}"
@@ -636,37 +609,17 @@ class AgentWorkflow:
``services/ai.py:execute_agent`` returns today so downstream
code (OutputPanel, edge inputs, etc.) doesn't change.
"""
- # Activity inputs and command ids are recorded in Temporal Event
- # History. Old histories take the exact legacy branches below; new
- # runs record this marker and receive per-call command identity plus
- # deterministic duplicate-name validation.
- use_tool_call_identity_v2 = workflow.patched(TOOL_CALL_IDENTITY_V2_PATCH)
- use_task_manager_delegation = workflow.patched(TASK_MANAGER_DELEGATION_PATCH)
- use_cooperative_pause_schedule_gate = workflow.patched(
- COOPERATIVE_PAUSE_SCHEDULING_PATCH
- )
- use_delegation_permit_cleanup = workflow.patched(
- DELEGATION_PERMIT_CLEANUP_PATCH
- )
- use_unbounded_lifetimes = workflow.patched(UNBOUNDED_LIFETIMES_PATCH)
- use_acquire_cancellation_completion = workflow.patched(
- DELEGATION_ACQUIRE_CANCELLATION_PATCH
- )
- acquire_cancellation_options = (
- {
- "cancellation_type": (
- ActivityCancellationType.WAIT_CANCELLATION_COMPLETED
- )
- }
- if use_acquire_cancellation_completion
- else {}
- )
+ acquire_cancellation_options = {
+ "cancellation_type": (
+ ActivityCancellationType.WAIT_CANCELLATION_COMPLETED
+ )
+ }
# ---- Step 0: Resolve payload via the prep activity --------------
# DB lookups + edge walking + tool schema build happen here, NOT
# in the workflow body (workflows must be deterministic).
payload = await workflow.execute_activity(
- "agent.prepare_payload.v1",
+ "agent.prepare_payload",
args=[context],
activity_id="prepare-payload",
start_to_close_timeout=PERSIST_TURN_TIMEOUT * 2, # 60s default
@@ -690,7 +643,15 @@ class AgentWorkflow:
# (node_executor.py fallback). Delegation children inherit it via
# the ``child_context`` spread below. ``workflow.info().run_id``
# is deterministic — safe inside workflow code.
- execution_id = str(context.get("execution_id") or "") or workflow.info().run_id[:8]
+ resume = dict(context.get(_RESUME_MARKER) or {})
+ # run_id CHANGES on continue-as-new, so a resumed run would mint a
+ # different execution id and break browser-session reuse, permit
+ # scoping and root_execution_id fallback. Carry it explicitly.
+ execution_id = (
+ str(resume.get("execution_id") or "")
+ or str(context.get("execution_id") or "")
+ or workflow.info().run_id[:8]
+ )
task_scope_execution_id = str(
payload.get("team_execution_id") or execution_id
)
@@ -710,16 +671,11 @@ class AgentWorkflow:
self._root_execution_id = root_execution_id
self._provider = str(payload.get("provider") or "")
- # The provider-visible name is the dispatch key. The legacy dict
- # comprehension silently selected the last connected node when two
- # tools shared a name. New histories fail before the first billed LLM
- # call and identify every conflicting canvas node.
+ # The provider-visible name is the dispatch key. Duplicate names fail
+ # before the first billed LLM call and identify every conflicting
+ # canvas node, instead of silently selecting the last connected one.
tools = payload.get("tools") or []
- duplicate_tool_error = (
- _duplicate_visible_tool_name_error(tools)
- if use_tool_call_identity_v2
- else None
- )
+ duplicate_tool_error = _duplicate_visible_tool_name_error(tools)
duplicate_tool_conflicts = (
_duplicate_visible_tool_name_conflicts(tools)
if duplicate_tool_error
@@ -753,6 +709,36 @@ class AgentWorkflow:
# ---- Build initial message list ---------------------------------
# Native executions use the SDK-neutral v2 wire codec. Legacy
# histories keep the legacy canonical {type, data} shape exactly.
+ # ---- Context journal -------------------------------------------
+ # A Context node on the agent's input makes the backend journal the
+ # source of truth for the transcript: the LLM activity reconstructs
+ # from the store rather than from `messages`, so a run resumes the
+ # prior conversation instead of starting empty. The workflow holds
+ # only references.
+ context_ref: Optional[Dict[str, Any]] = None
+ runtime_config_ref = ""
+ journalled = 0
+ if payload.get("context_descriptor"):
+ await self._wait_until_resumed()
+ prepared_context = await workflow.execute_activity(
+ "agent.prepare_context",
+ args=[{"context": context, "operation_id": f"{execution_id}:prepare"}],
+ activity_id="prepare-context",
+ start_to_close_timeout=PERSIST_TURN_TIMEOUT * 2,
+ retry_policy=AGENT_ACTIVITY_RETRY,
+ )
+ context_ref = prepared_context.get("context_ref")
+ runtime_config_ref = str(
+ prepared_context.get("runtime_config_ref") or ""
+ )
+ if resume:
+ # A resumed run reconstructs its transcript from the journal, so
+ # only references and counters cross the rollover boundary.
+ context_ref = resume.get("context_ref") or context_ref
+ runtime_config_ref = (
+ str(resume.get("runtime_config_ref") or "") or runtime_config_ref
+ )
+
messages: List[Dict[str, Any]] = []
system = payload.get("system_message") or ""
@@ -817,10 +803,39 @@ class AgentWorkflow:
)
# ---- Main loop --------------------------------------------------
- for iteration in range(max_iterations):
+ iteration_offset = int(resume.get("iteration") or 0)
+ for iteration in range(iteration_offset, max_iterations):
await self._wait_until_resumed()
workflow.logger.info(f"AgentWorkflow iteration {iteration} " f"(messages={len(messages)} tools={len(tools)})")
+ # Commit anything produced since the last turn — tool results
+ # above all. Tools run through this workflow's own per-type and
+ # delegation paths, which never touch the journal, so without
+ # this the next reconstruction replays an assistant turn whose
+ # tool calls have no answers.
+ if context_ref and len(messages) > journalled:
+ pending_messages = messages[journalled:]
+ appended = await workflow.execute_activity(
+ "agent.append_context",
+ args=[
+ {
+ "action": "append_messages",
+ "context_ref": context_ref,
+ "runtime_config_ref": runtime_config_ref,
+ "messages": pending_messages,
+ "iteration": iteration,
+ "operation_id": (
+ f"{execution_id}:iter:{iteration}:append"
+ ),
+ }
+ ],
+ activity_id=f"append-context-{iteration + 1}",
+ start_to_close_timeout=PERSIST_TURN_TIMEOUT,
+ retry_policy=AGENT_ACTIVITY_RETRY,
+ )
+ context_ref = appended.get("context_ref") or context_ref
+ journalled = len(messages)
+
# CloudEvents-shaped agent_progress per LLM turn. Mirrors the
# the in-process agent loop's per-turn broadcast (RFC §6.4).
# FE consumes the typed envelope and updates the canvas
@@ -832,8 +847,7 @@ class AgentWorkflow:
max_iterations,
phase="llm_step",
)
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
# Strip per-turn fields the activity doesn't need. Native turns
# receive only provider-neutral JSON tool definitions. Legacy
@@ -857,6 +871,22 @@ class AgentWorkflow:
"thinking_config": payload.get("thinking_config"),
"llm_engine": NATIVE_LLM_ENGINE,
"message_wire_version": message_wire_version,
+ # Present only for journal-backed agents; the activity
+ # reconstructs the transcript from the store instead of
+ # trusting `messages`.
+ **(
+ {
+ "context_ref": context_ref,
+ "runtime_config_ref": runtime_config_ref,
+ }
+ if context_ref
+ else {}
+ ),
+ # A provider may stop a turn to compact rather than to
+ # answer. Without the finish reason that response is
+ # indistinguishable from a normal completion and gets
+ # returned to the user as a truncated final answer.
+ "include_finish_reason": True,
}
else:
if not has_recorded_engine:
@@ -906,7 +936,7 @@ class AgentWorkflow:
try:
if has_recorded_engine:
step_result = await workflow.execute_activity(
- "agent.execute_llm_step.v1",
+ "agent.execute_llm_step",
args=[llm_payload],
activity_id=f"llm-step-{iteration + 1}",
start_to_close_timeout=LLM_STEP_TIMEOUT,
@@ -915,7 +945,7 @@ class AgentWorkflow:
)
else:
step_result = await workflow.execute_activity(
- "agent.execute_llm_step.v1",
+ "agent.execute_llm_step",
args=[llm_payload],
activity_id=f"llm-step-{iteration + 1}",
start_to_close_timeout=LLM_STEP_TIMEOUT,
@@ -964,7 +994,7 @@ class AgentWorkflow:
# stuck on its last capability while the workflow has already
# ended. Keep the public event free of provider error text.
await workflow.execute_activity(
- "agent.skill.clear.v1",
+ "agent.skill.clear",
args=[{
"workflow_id": payload.get("workflow_id"),
"execution_id": task_scope_execution_id,
@@ -1024,6 +1054,23 @@ class AgentWorkflow:
assistant_message = step_result.get("assistant_message")
if assistant_message:
messages.append(assistant_message)
+ if context_ref and step_result.get("context_ref"):
+ # The step committed the assistant turn; nothing for the
+ # delta journaller below to re-append.
+ context_ref = step_result["context_ref"]
+ journalled = len(messages)
+
+ # A provider that stops to compact has not answered. It carries
+ # no tool calls, so it would otherwise be classified "final" and
+ # its truncated content returned to the user as the response.
+ # Treat it as a no-op turn and let the loop request again.
+ finish_reason = str(step_result.get("finish_reason") or "").strip().lower()
+ if kind != "tool_calls" and finish_reason == "compaction":
+ workflow.logger.info(
+ f"Provider paused to compact at iteration {iteration + 1}; "
+ "continuing without treating the stop as a final answer"
+ )
+ continue
if kind == "final":
final_content = step_result.get("content", "")
@@ -1034,7 +1081,7 @@ class AgentWorkflow:
# invocation to wait. Completion will emit taskTrigger and
# start the separately scoped review invocation.
await workflow.execute_activity(
- "agent.finalize_team.v1",
+ "agent.finalize_team",
args=[{"team_id": team_id}],
activity_id="finalize-agent-team",
start_to_close_timeout=PERSIST_TURN_TIMEOUT,
@@ -1048,11 +1095,10 @@ class AgentWorkflow:
break
# ---- Schedule tool activities -------------------------------
- if use_cooperative_pause_schedule_gate:
- # The LLM activity and phase broadcasts yield long enough for
- # a pause signal to arrive after the iteration-level check.
- # Admit the resulting tool/delegation command batch afresh.
- await self._wait_until_resumed()
+ # The LLM activity and phase broadcasts yield long enough for
+ # a pause signal to arrive after the iteration-level check.
+ # Admit the resulting tool/delegation command batch afresh.
+ await self._wait_until_resumed()
calls = step_result.get("calls") or []
workflow.logger.info(f"AgentWorkflow scheduling {len(calls)} tool call(s)")
@@ -1107,18 +1153,14 @@ class AgentWorkflow:
release_attempt + 1
)
await workflow.execute_activity(
- "agent.release_subagent_permit.v1",
+ "agent.release_subagent_permit",
args=[{
"root_execution_id": root_execution_id,
"permit_id": permit_id,
}],
activity_id=release_activity_id,
start_to_close_timeout=PERSIST_TURN_TIMEOUT,
- retry_policy=(
- DELEGATION_CLEANUP_RETRY
- if use_delegation_permit_cleanup
- else AGENT_ACTIVITY_RETRY
- ),
+ retry_policy=DELEGATION_CLEANUP_RETRY,
)
delegation_permits.pop(call_index, None)
@@ -1150,7 +1192,7 @@ class AgentWorkflow:
lifecycle = delegation_lifecycles[pending_index]
try:
await workflow.execute_activity(
- "agent.cancel_delegation.v1",
+ "agent.cancel_delegation",
args=[{
**lifecycle,
"reason": (
@@ -1198,7 +1240,7 @@ class AgentWorkflow:
)
if delegation_call_indices and own_permit_id:
await workflow.execute_activity(
- "agent.release_subagent_permit.v1",
+ "agent.release_subagent_permit",
args=[{
"root_execution_id": root_execution_id,
"permit_id": own_permit_id,
@@ -1215,17 +1257,15 @@ class AgentWorkflow:
candidate_args = candidate.get("args") or {}
task = str(candidate_args.get("task", "") or "")
task_context = str(candidate_args.get("context", "") or "")
- call_metadata = (
- _tool_call_metadata(
- agent_node_id=agent_node_id,
- iteration=iteration,
- call_index=call_index,
- call=candidate,
- )
- if use_tool_call_identity_v2
- else {}
+ call_metadata = _tool_call_metadata(
+ agent_node_id=agent_node_id,
+ iteration=iteration,
+ call_index=call_index,
+ call=candidate,
)
child_context = {
+ # Inherited scope first: explicit keys below win.
+ **_inherited_scope(context),
"node_id": candidate_tool["tool_node_id"],
"node_type": candidate_tool["node_type"],
"node_data": {
@@ -1280,10 +1320,9 @@ class AgentWorkflow:
delegation_lifecycles[call_index] = (
lifecycle_payload
)
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
await workflow.execute_activity(
- "agent.queue_delegation.v1",
+ "agent.queue_delegation",
args=[{
**lifecycle_payload,
"queued_event_id": (
@@ -1297,46 +1336,40 @@ class AgentWorkflow:
retry_policy=AGENT_ACTIVITY_RETRY,
**acquire_cancellation_options,
)
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
acquire_payload = {
"root_execution_id": root_execution_id,
"permit_id": permit_id,
"limit": max_concurrent_subagents,
+ "lease_version": 2,
}
- if use_acquire_cancellation_completion:
- acquire_payload["lease_version"] = 2
acquire_result = await workflow.execute_activity(
- "agent.acquire_subagent_permit.v1",
+ "agent.acquire_subagent_permit",
args=[acquire_payload],
activity_id=(
f"acquire-permit-{iteration + 1}-{call_index + 1}"
),
start_to_close_timeout=timedelta(hours=1),
heartbeat_timeout=timedelta(seconds=10),
- # Unlimited attempts on the patched path — a queued
- # delegation waits as long as admission takes.
- retry_policy=(
- PERMIT_WAIT_RETRY if use_unbounded_lifetimes else AGENT_ACTIVITY_RETRY
- ),
+ # Unlimited attempts — a queued delegation waits as
+ # long as admission takes.
+ retry_policy=PERMIT_WAIT_RETRY,
**acquire_cancellation_options,
)
delegation_permits[call_index] = str(
(acquire_result or {}).get("lease_id")
or permit_id
)
- if use_acquire_cancellation_completion:
- child_context["team_permit_id"] = (
- delegation_permits[call_index]
- )
+ child_context["team_permit_id"] = (
+ delegation_permits[call_index]
+ )
# Claim only after admission. Persistence failure still
# prevents child startup and the task remains pending
# while the coordinator queues it.
try:
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
await workflow.execute_activity(
- "agent.begin_delegation.v1",
+ "agent.begin_delegation",
args=[lifecycle_payload],
activity_id=(
f"begin-delegation-{iteration + 1}-{call_index + 1}"
@@ -1346,39 +1379,30 @@ class AgentWorkflow:
**acquire_cancellation_options,
)
except asyncio.CancelledError:
- if not use_delegation_permit_cleanup:
- await _release_delegation_permit(call_index)
+ # Cancellation cleanup is owned by the caller's
+ # _cleanup_cancelled_delegations() sweep; releasing
+ # here as well would double-release the permit.
raise
except BaseException:
await _release_delegation_permit(call_index)
raise
- child_id = (
- _delegation_child_id_v2(
- workflow.info().workflow_id,
- candidate_tool["tool_node_id"],
- iteration,
- call_index,
- )
- if use_tool_call_identity_v2
- else f"{workflow.info().workflow_id}-delegate-{candidate_tool['tool_node_id']}-{iteration}"
+ child_id = _delegation_child_id(
+ workflow.info().workflow_id,
+ candidate_tool["tool_node_id"],
+ iteration,
+ call_index,
)
try:
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
- delegation_child_kwargs: Dict[str, Any] = {"id": child_id}
- if not use_unbounded_lifetimes:
- # Replay-only: pre-patch histories recorded 1h caps.
- delegation_child_kwargs["execution_timeout"] = timedelta(hours=1)
- delegation_child_kwargs["run_timeout"] = timedelta(hours=1)
+ await self._wait_until_resumed()
delegation_handles[call_index] = await workflow.start_child_workflow(
"AgentWorkflow",
args=[child_context],
- **delegation_child_kwargs,
+ id=child_id,
)
if team_id:
child_handle = delegation_handles[call_index]
await workflow.execute_activity(
- "agent.register_task_execution.v1",
+ "agent.register_task_execution",
args=[{**lifecycle_payload,
"child_workflow_id": child_handle.id,
"child_run_id": child_handle.first_execution_run_id}],
@@ -1388,12 +1412,8 @@ class AgentWorkflow:
**acquire_cancellation_options,
)
except asyncio.CancelledError:
- if (
- not use_delegation_permit_cleanup
- and team_id
- and call_index in delegation_permits
- ):
- await _release_delegation_permit(call_index)
+ # Cancellation cleanup is owned by the caller's
+ # _cleanup_cancelled_delegations() sweep.
raise
except BaseException:
if team_id and call_index in delegation_permits:
@@ -1440,7 +1460,7 @@ class AgentWorkflow:
# root-wide slot is occupied by leads assigning descendants.
if own_permit_id and not yielded_own_permit:
await workflow.execute_activity(
- "agent.release_subagent_permit.v1",
+ "agent.release_subagent_permit",
args=[{"root_execution_id": root_execution_id, "permit_id": own_permit_id}],
activity_id=f"yield-own-permit-task-manager-{iteration + 1}",
start_to_close_timeout=PERSIST_TURN_TIMEOUT,
@@ -1475,10 +1495,9 @@ class AgentWorkflow:
# queue_delegation is intentionally retained: it is
# idempotent for the pre-created task and records the same
# lifecycle event as direct delegation without duplicating it.
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
await workflow.execute_activity(
- "agent.queue_delegation.v1",
+ "agent.queue_delegation",
args=[{**lifecycle, "queued_event_id": f"{task_id}:queued"}],
activity_id=f"queue-task-manager-{iteration + 1}-{call_index + 1}",
start_to_close_timeout=PERSIST_TURN_TIMEOUT,
@@ -1486,6 +1505,8 @@ class AgentWorkflow:
)
context_text = request_context if isinstance(request_context, str) else _serialise_tool_result(request_context)
child_context = {
+ # Inherited scope first: explicit keys below win.
+ **_inherited_scope(context),
"node_id": assignee_id, "node_type": delegate["node_type"],
"node_data": {**(delegate.get("parameters") or {}),
"system_message": mission, "prompt": context_text or mission},
@@ -1497,37 +1518,26 @@ class AgentWorkflow:
"nodes": context.get("nodes") or [], "edges": context.get("edges") or [],
"invocation": {"task": mission, "context": context_text},
}
- child_id = _delegation_child_id_v2(
+ child_id = _delegation_child_id(
workflow.info().workflow_id, assignee_id, iteration, call_index
) + f"-{task_id}"
runner_id = f"{child_id}-runner"
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
delegated_task_options: Dict[str, Any] = {
"id": runner_id,
"parent_close_policy": ParentClosePolicy.ABANDON,
}
- if not use_unbounded_lifetimes:
- # Replay-only: pre-patch histories recorded 2h caps.
- # A capped runner that timed out skipped its finally
- # compensation (leaked permit + stuck task row).
- delegated_task_options["execution_timeout"] = timedelta(hours=2)
- delegated_task_options["run_timeout"] = timedelta(hours=2)
- use_delegated_task_search_attributes = workflow.patched(
- DELEGATED_TASK_SEARCH_ATTRIBUTES_PATCH
+ from services.temporal.trigger_listener_workflow import (
+ event_workflow_search_attributes,
)
- if use_delegated_task_search_attributes:
- from services.temporal.trigger_listener_workflow import (
- event_workflow_search_attributes,
- )
- search_attributes = event_workflow_search_attributes(
- payload.get("workflow_id")
+ search_attributes = event_workflow_search_attributes(
+ payload.get("workflow_id")
+ )
+ if search_attributes is not None:
+ delegated_task_options["search_attributes"] = (
+ search_attributes
)
- if search_attributes is not None:
- delegated_task_options["search_attributes"] = (
- search_attributes
- )
await workflow.start_child_workflow(
"DelegatedTaskWorkflow",
args=[{"lifecycle": lifecycle, "child_context": child_context,
@@ -1542,63 +1552,55 @@ class AgentWorkflow:
# are allowed to reach the child-workflow bridge below.
task_manager_preflight_indices: List[int] = []
task_manager_preflight_handles: List[Any] = []
- if use_task_manager_delegation:
- for preflight_index, preflight_call in enumerate(calls):
- preflight_tool = tool_index.get(preflight_call.get("name", ""))
- preflight_args = preflight_call.get("args") or {}
- if (
- preflight_tool
- and preflight_tool.get("node_type") == "taskManager"
- and preflight_args.get("operation") == "assign_task"
- ):
- preflight_metadata = (
- _tool_call_metadata(
- agent_node_id=agent_node_id,
- iteration=iteration,
- call_index=preflight_index,
- call=preflight_call,
- )
- if use_tool_call_identity_v2
- else {}
- )
- preflight_payload = {
- "node_id": preflight_tool["tool_node_id"],
- "node_type": "taskManager",
- "node_data": {
- **(preflight_tool.get("parameters") or {}),
- **preflight_args,
- },
- "inputs": {},
- "workflow_id": payload.get("workflow_id"),
- "session_id": payload.get("session_id", "default"),
- "execution_id": task_scope_execution_id,
- "root_execution_id": root_execution_id,
- "parent_node_id": agent_node_id,
- "team_lead_node_id": agent_node_id,
- "nodes": context.get("nodes") or [],
- "edges": context.get("edges") or [],
- **preflight_metadata,
- }
- task_manager_preflight_indices.append(preflight_index)
- if (
- not task_manager_preflight_handles
- and use_cooperative_pause_schedule_gate
- ):
- # ``start_activity`` does not yield; one admission
- # check protects this whole concurrent batch.
- await self._wait_until_resumed()
- task_manager_preflight_handles.append(
- workflow.start_activity(
- f"node.taskManager.v{preflight_tool['version']}",
- args=[preflight_payload],
- activity_id=(
- f"task-manager-preflight-{iteration + 1}-"
- f"{preflight_index + 1}"
- ),
- start_to_close_timeout=TOOL_STEP_TIMEOUT,
- heartbeat_timeout=TOOL_HEARTBEAT_TIMEOUT,
- )
+ for preflight_index, preflight_call in enumerate(calls):
+ preflight_tool = tool_index.get(preflight_call.get("name", ""))
+ preflight_args = preflight_call.get("args") or {}
+ if (
+ preflight_tool
+ and preflight_tool.get("node_type") == "taskManager"
+ and preflight_args.get("operation") == "assign_task"
+ ):
+ preflight_metadata = _tool_call_metadata(
+ agent_node_id=agent_node_id,
+ iteration=iteration,
+ call_index=preflight_index,
+ call=preflight_call,
+ )
+ preflight_payload = {
+ "node_id": preflight_tool["tool_node_id"],
+ "node_type": "taskManager",
+ "node_data": {
+ **(preflight_tool.get("parameters") or {}),
+ **preflight_args,
+ },
+ "inputs": {},
+ "workflow_id": payload.get("workflow_id"),
+ "session_id": payload.get("session_id", "default"),
+ "execution_id": task_scope_execution_id,
+ "root_execution_id": root_execution_id,
+ "parent_node_id": agent_node_id,
+ "team_lead_node_id": agent_node_id,
+ "nodes": context.get("nodes") or [],
+ "edges": context.get("edges") or [],
+ **preflight_metadata,
+ }
+ task_manager_preflight_indices.append(preflight_index)
+ if not task_manager_preflight_handles:
+ # ``start_activity`` does not yield; one admission
+ # check protects this whole concurrent batch.
+ await self._wait_until_resumed()
+ task_manager_preflight_handles.append(
+ workflow.start_activity(
+ f"node.taskManager.v{preflight_tool['version']}",
+ args=[preflight_payload],
+ activity_id=(
+ f"task-manager-preflight-{iteration + 1}-"
+ f"{preflight_index + 1}"
+ ),
+ start_to_close_timeout=TOOL_STEP_TIMEOUT,
+ heartbeat_timeout=TOOL_HEARTBEAT_TIMEOUT,
)
+ )
task_manager_preflight_results: Dict[int, Any] = {}
task_manager_delegation_tasks: Dict[int, asyncio.Task[Any]] = {}
@@ -1615,7 +1617,7 @@ class AgentWorkflow:
# assignment coroutines contend for root-wide permits.
if own_permit_id and not yielded_own_permit:
await workflow.execute_activity(
- "agent.release_subagent_permit.v1",
+ "agent.release_subagent_permit",
args=[{
"root_execution_id": root_execution_id,
"permit_id": own_permit_id,
@@ -1648,8 +1650,7 @@ class AgentWorkflow:
await _start_delegation(delegation_index)
next_delegation_to_start += 1
except BaseException:
- if use_delegation_permit_cleanup:
- await _cleanup_cancelled_delegations()
+ await _cleanup_cancelled_delegations()
raise
for call_index, call in enumerate(calls):
@@ -1776,15 +1777,11 @@ class AgentWorkflow:
child_nodes = []
child_edges = []
- call_metadata = (
- _tool_call_metadata(
- agent_node_id=agent_node_id,
- iteration=iteration,
- call_index=call_index,
- call=call,
- )
- if use_tool_call_identity_v2
- else {}
+ call_metadata = _tool_call_metadata(
+ agent_node_id=agent_node_id,
+ iteration=iteration,
+ call_index=call_index,
+ call=call,
)
tool_payload = {
"node_id": tool_info["tool_node_id"],
@@ -1815,7 +1812,7 @@ class AgentWorkflow:
}
tool_activity_name = (
- "agent.skill.invoke.v1"
+ "agent.skill.invoke"
if tool_info["node_type"] == "_builtin_skill"
else f"node.{tool_info['node_type']}.v{tool_info['version']}"
)
@@ -1837,8 +1834,7 @@ class AgentWorkflow:
},
)
except asyncio.CancelledError:
- if use_delegation_permit_cleanup:
- await _cleanup_cancelled_delegations()
+ await _cleanup_cancelled_delegations()
raise
try:
@@ -1847,8 +1843,8 @@ class AgentWorkflow:
try:
tool_result = await handle
except asyncio.CancelledError:
- if not use_delegation_permit_cleanup:
- await _release_delegation_permit(call_index)
+ # Cancellation cleanup is owned by the outer
+ # _cleanup_cancelled_delegations() sweep.
raise
except BaseException:
await _release_delegation_permit(call_index)
@@ -1873,7 +1869,7 @@ class AgentWorkflow:
f"{iteration + 1}-{call_index + 1}"
)
await workflow.execute_activity(
- "agent.finish_delegation.v1",
+ "agent.finish_delegation",
args=[{
"team_id": team_id,
"team_task_id": task_id,
@@ -1916,22 +1912,17 @@ class AgentWorkflow:
)
next_delegation_to_start += 1
else:
- tool_activity_id = (
- _tool_activity_id_v2(
- tool_info["tool_node_id"],
- iteration,
- call_index,
- )
- if use_tool_call_identity_v2
- else f"tool-{tool_info['tool_node_id']}-{iteration + 1}"
+ tool_activity_id = _tool_activity_id(
+ tool_info["tool_node_id"],
+ iteration,
+ call_index,
)
if call_index in task_manager_preflight_results:
tool_result = task_manager_preflight_results[call_index]
if isinstance(tool_result, BaseException):
raise tool_result
else:
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
tool_result = await workflow.execute_activity(
tool_activity_name,
args=[tool_payload],
@@ -1940,8 +1931,7 @@ class AgentWorkflow:
heartbeat_timeout=TOOL_HEARTBEAT_TIMEOUT,
)
if (
- use_task_manager_delegation
- and tool_info["node_type"] == "taskManager"
+ tool_info["node_type"] == "taskManager"
and isinstance(tool_result, dict)
and isinstance(tool_result.get("delegation_request"), dict)
):
@@ -1992,34 +1982,27 @@ class AgentWorkflow:
# step's one-shot LLM_STEP_RETRY): rebuilding
# the tool surface from canvas state is fully
# idempotent, so retries are free.
- refresh_activity_id = (
- _refresh_tools_activity_id_v2(
- tool_info["tool_node_id"],
- iteration,
- call_index,
- )
- if use_tool_call_identity_v2
- else f"refresh-tools-{tool_info['tool_node_id']}-{iteration + 1}"
+ refresh_activity_id = _refresh_tools_activity_id(
+ tool_info["tool_node_id"],
+ iteration,
+ call_index,
)
refresh_payload = {
"operations": ops_from_tool,
"agent_node_type": payload.get("node_type") or context.get("node_type"),
**call_metadata,
}
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
refresh_result = await workflow.execute_activity(
- "agent.refresh_tools.v1",
+ "agent.refresh_tools",
args=[refresh_payload],
activity_id=refresh_activity_id,
start_to_close_timeout=timedelta(seconds=30),
retry_policy=AGENT_ACTIVITY_RETRY,
)
added_tools = refresh_result.get("tools") or []
- refresh_duplicate_error = (
- _duplicate_visible_tool_name_error([*tools, *added_tools])
- if use_tool_call_identity_v2
- else None
+ refresh_duplicate_error = _duplicate_visible_tool_name_error(
+ [*tools, *added_tools]
)
refresh_duplicate_conflicts = (
_duplicate_visible_tool_name_conflicts([*tools, *added_tools])
@@ -2062,8 +2045,7 @@ class AgentWorkflow:
len(tools),
)
except asyncio.CancelledError:
- if use_delegation_permit_cleanup:
- await _cleanup_cancelled_delegations()
+ await _cleanup_cancelled_delegations()
raise
except Exception as e: # noqa: BLE001 — Temporal handles retries
# After all retries exhausted, surface the error to
@@ -2078,7 +2060,7 @@ class AgentWorkflow:
)
try:
await workflow.execute_activity(
- "agent.finish_delegation.v1",
+ "agent.finish_delegation",
args=[{
"team_id": team_id,
"team_task_id": task_id,
@@ -2113,8 +2095,7 @@ class AgentWorkflow:
None,
)
except asyncio.CancelledError:
- if use_delegation_permit_cleanup:
- await _cleanup_cancelled_delegations()
+ await _cleanup_cancelled_delegations()
raise
tool_content = f'{{"error": "{type(e).__name__}: {e}"}}'
try:
@@ -2139,8 +2120,7 @@ class AgentWorkflow:
},
)
except asyncio.CancelledError:
- if use_delegation_permit_cleanup:
- await _cleanup_cancelled_delegations()
+ await _cleanup_cancelled_delegations()
raise
_append_tool_result_message(
@@ -2152,10 +2132,9 @@ class AgentWorkflow:
)
if yielded_own_permit:
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
await workflow.execute_activity(
- "agent.acquire_subagent_permit.v1",
+ "agent.acquire_subagent_permit",
args=[{
"root_execution_id": root_execution_id,
"permit_id": own_permit_id,
@@ -2224,10 +2203,9 @@ class AgentWorkflow:
"api_key": payload["api_key"],
"model": payload["model"],
}
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
compact_result = await workflow.execute_activity(
- "agent.compact_memory.v1",
+ "agent.compact_memory",
args=[compact_payload],
activity_id=f"compact-memory-{iteration + 1}",
start_to_close_timeout=COMPACT_MEMORY_TIMEOUT,
@@ -2298,6 +2276,39 @@ class AgentWorkflow:
# Exact pre-cutover completion shape.
usage_total = {}
+ # ---- Continue-as-new -------------------------------------
+ # Only at a clean turn boundary, and never while a delegation
+ # is in flight: delegation_handles and the Task-Manager task
+ # map hold live ChildWorkflowHandle / asyncio.Task objects
+ # that cannot cross the boundary. The own-permit reacquire
+ # above is the permit-safe point.
+ from services.temporal.trigger_listener_workflow import (
+ _history_pressure,
+ )
+
+ if context_ref and _history_pressure(_AGENT_HISTORY_SOFT_CAP):
+ delegations_live = bool(
+ delegation_handles or task_manager_delegation_tasks
+ )
+ if not delegations_live:
+ workflow.logger.info(
+ f"AgentWorkflow continue_as_new at iteration "
+ f"{iteration + 1} (history pressure)"
+ )
+ workflow.continue_as_new(
+ args=[
+ {
+ **context,
+ _RESUME_MARKER: {
+ "context_ref": context_ref,
+ "runtime_config_ref": runtime_config_ref,
+ "iteration": iteration + 1,
+ "execution_id": execution_id,
+ },
+ }
+ ]
+ )
+
else:
# Loop exited without break -- hit max_iterations.
workflow.logger.warning(f"AgentWorkflow hit max_iterations={max_iterations}; truncating")
@@ -2322,7 +2333,7 @@ class AgentWorkflow:
# NodeExecutor; F4.B needs an explicit activity because we
# bypass NodeExecutor entirely.
await workflow.execute_activity(
- "agent.store_output.v1",
+ "agent.store_output",
args=[
{
"node_id": agent_node_id,
@@ -2336,7 +2347,7 @@ class AgentWorkflow:
)
await workflow.execute_activity(
- "agent.skill.clear.v1",
+ "agent.skill.clear",
args=[{"workflow_id": payload.get("workflow_id"), "execution_id": task_scope_execution_id,
"agent_node_id": agent_node_id}],
activity_id="clear-active-skills",
@@ -2379,7 +2390,7 @@ class AgentWorkflow:
parent's canvas badge advances alongside the child.
"""
await workflow.execute_activity(
- "agent.broadcast_progress.v1",
+ "agent.broadcast_progress",
args=[
{
"node_id": node_id,
@@ -2408,7 +2419,7 @@ class AgentWorkflow:
# this phase-only event merely indicates that descendant work is
# advancing.
await workflow.execute_activity(
- "agent.broadcast_progress.v1",
+ "agent.broadcast_progress",
args=[
{
"node_id": self._parent_node_id,
@@ -2443,7 +2454,7 @@ class AgentWorkflow:
if interim and not assistant_text:
return
await workflow.execute_activity(
- "agent.persist_turn.v1",
+ "agent.persist_turn",
args=[
{
"memory_node_id": memory_node_id,
diff --git a/server/services/temporal/executor.py b/server/services/temporal/executor.py
index a806529e..6faff253 100644
--- a/server/services/temporal/executor.py
+++ b/server/services/temporal/executor.py
@@ -17,11 +17,36 @@ from temporalio.common import (
)
from core.logging import get_logger
-from .workflow import MachinaWorkflow
+from .workflow import (
+ MachinaWorkflow,
+ TEMPORAL_ROUTING_INPUT_KEY,
+ TEMPORAL_ROUTING_INPUT_VERSION,
+)
logger = get_logger(__name__)
+def capture_temporal_routing_input() -> Dict[str, Any]:
+ """Capture command-shaping flags before entering workflow execution."""
+ from core.config import Settings
+
+ settings = Settings()
+ return {
+ TEMPORAL_ROUTING_INPUT_KEY: {
+ "version": TEMPORAL_ROUTING_INPUT_VERSION,
+ "agent_workflow_enabled": bool(
+ getattr(settings, "temporal_agent_workflow_enabled", False)
+ ),
+ "per_type_dispatch_enabled": bool(
+ getattr(settings, "temporal_per_type_dispatch", False)
+ ),
+ "worker_pool_enabled": bool(
+ getattr(settings, "temporal_worker_pool_enabled", False)
+ ),
+ }
+ }
+
+
class TemporalExecutor:
"""Workflow executor that uses Temporal for durable execution.
@@ -54,6 +79,9 @@ class TemporalExecutor:
session_id: str = "default",
enable_caching: bool = True,
workflow_slug: Optional[str] = None,
+ graph_version: int = 0,
+ generation: int = 0,
+ user_id: str = "owner",
) -> Dict[str, Any]:
"""Execute a workflow using Temporal.
@@ -66,6 +94,9 @@ class TemporalExecutor:
workflow_slug: Human-readable slug used in the Temporal Web UI
listing prefix. Optional — falls back to ``"workflow"`` for
one-off Runs without a saved DB row.
+ graph_version: Normalized graph version for Context V2
+ generation: Durable workflow-control generation for Context V2
+ user_id: Authenticated server-owned user identity
Returns:
Dict with success, outputs, execution_trace, and timing info
@@ -90,16 +121,28 @@ class TemporalExecutor:
try:
# Execute workflow via Temporal
+ workflow_payload = {
+ "nodes": nodes,
+ "edges": edges,
+ "session_id": session_id,
+ "workflow_id": workflow_id,
+ "workflow_slug": workflow_slug,
+ "execution_id": execution_id,
+ "user_id": str(user_id or "owner"),
+ **capture_temporal_routing_input(),
+ }
+ if int(graph_version or 0) >= 2 and int(generation or 0) > 0:
+ workflow_payload.update(
+ {
+ "graphVersion": int(graph_version),
+ "generation": int(generation),
+ "root_execution_id": execution_id,
+ }
+ )
+
result = await self.client.execute_workflow(
MachinaWorkflow.run,
- {
- "nodes": nodes,
- "edges": edges,
- "session_id": session_id,
- "workflow_id": workflow_id,
- "workflow_slug": workflow_slug,
- "execution_id": execution_id,
- },
+ workflow_payload,
id=execution_id,
task_queue=self.task_queue,
search_attributes=TypedSearchAttributes(
diff --git a/server/services/temporal/polling_trigger_workflow.py b/server/services/temporal/polling_trigger_workflow.py
index 7b2c9acd..129c0d63 100644
--- a/server/services/temporal/polling_trigger_workflow.py
+++ b/server/services/temporal/polling_trigger_workflow.py
@@ -41,6 +41,7 @@ from temporalio.common import WorkflowIDReusePolicy
from temporalio.workflow import ParentClosePolicy
from ._retry_policies import DEFAULT_ACTIVITY_RETRY
+from .workflow import TEMPORAL_ROUTING_INPUT_KEY
# Same continueAsNew threshold as TriggerListenerWorkflow — keeps Event
@@ -57,23 +58,18 @@ _DEFAULT_POLL_INTERVAL_S = 60
# headroom for slow Gmail / Twitter responses without hanging forever.
# Workflow ``RetryPolicy`` (default) handles transient failures.
_ACTIVITY_TIMEOUT_MULT = 4
-COOPERATIVE_PAUSE_SCHEDULING_PATCH = (
- "polling-trigger-cooperative-pause-scheduling-v1"
-)
-# Same contract as trigger_listener_workflow.UNBOUNDED_CHILD_RUNS_PATCH:
-# child runs may execute or stay paused for months, so new executions
-# start them without lifetime caps; the patch keeps replay
-# compatibility for histories recorded with the old 1h caps.
-UNBOUNDED_CHILD_RUNS_PATCH = "polling-unbounded-child-runs-v1"
-# _processed_count above increments only per EMITTED event, but every
-# poll cycle burns ~8-11 history events (sleep timer + activity +
-# workflow tasks) regardless — a quiet mailbox at the 60s default
-# reached the server's ~51,200-event hard termination in ~3 days with
-# the counter frozen at 0 and continue-as-new never firing. The patched
-# path checks history pressure every cycle (server suggestion + the
-# soft cap below), clamps user-supplied poll intervals to a sane floor,
-# and carries the pause flag across the rollover instead of blocking it.
-HISTORY_BOUNDED_CAN_PATCH = "polling-history-bounded-can-v1"
+# Child runs may execute — or stay cooperatively paused — for months,
+# so they start without lifetime caps; Temporal's timeout timer keeps
+# ticking through a pause. Liveness comes from activity heartbeats.
+#
+# _processed_count increments only per EMITTED event, but every poll
+# cycle burns ~8-11 history events (sleep timer + activity + workflow
+# tasks) regardless — a quiet mailbox at the 60s default reached the
+# server's ~51,200-event hard termination in ~3 days with the counter
+# frozen at 0. History pressure is therefore checked every cycle
+# (server suggestion + the soft cap below), poll intervals are clamped
+# to a sane floor, and the pause flag is carried across the rollover
+# instead of blocking it behind a resume that may be months away.
_HISTORY_SOFT_CAP = 10_000
# Defensive floor for user-supplied poll intervals: a few-second
# interval burns ~100K+ history events/day. Mirrors the plugin-side
@@ -138,26 +134,12 @@ class PollingTriggerWorkflow:
node_type = listener_data["node_type"]
version = listener_data.get("version", 1)
activity_name = f"poll.{node_type}.v{version}"
- try:
- use_cooperative_pause_schedule_gate = workflow.patched(
- COOPERATIVE_PAUSE_SCHEDULING_PATCH
- )
- use_history_bounded_can = workflow.patched(HISTORY_BOUNDED_CAN_PATCH)
- except Exception: # direct unit invocation outside Temporal runtime
- if workflow.in_workflow():
- raise
- use_cooperative_pause_schedule_gate = False
- use_history_bounded_can = True
-
- # Rehydrate rollover-carried pause state. Command-free, so
- # pre-patch histories (whose payloads lack the key) replay
- # identically.
+ # Rehydrate rollover-carried pause state.
self._control_paused = bool(listener_data.get("control_paused"))
params = listener_data.get("filter_params", {}) or {}
poll_interval = int(params.get("poll_interval") or _DEFAULT_POLL_INTERVAL_S)
- if use_history_bounded_can:
- poll_interval = max(_MIN_POLL_INTERVAL_S, poll_interval)
+ poll_interval = max(_MIN_POLL_INTERVAL_S, poll_interval)
activity_timeout_s = max(30, poll_interval * _ACTIVITY_TIMEOUT_MULT)
# Carry seen_ids across continueAsNew. First-start payload has
@@ -186,8 +168,7 @@ class PollingTriggerWorkflow:
}
else:
await workflow.sleep(timedelta(seconds=poll_interval))
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
cycle_payload = {
"node_id": listener_data["trigger_node_id"],
"params": params,
@@ -224,8 +205,7 @@ class PollingTriggerWorkflow:
continue
self._seen_event_ids.add(event_id)
try:
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
from services.temporal.trigger_listener_workflow import (
event_workflow_search_attributes,
)
@@ -233,17 +213,9 @@ class PollingTriggerWorkflow:
await self._spawn_child_run(
event,
listener_data,
- admission_check=(
- self._wait_until_resumed
- if use_cooperative_pause_schedule_gate
- else None
- ),
- search_attributes=(
- event_workflow_search_attributes(
- listener_data.get("workflow_id")
- )
- if use_cooperative_pause_schedule_gate
- else None
+ admission_check=self._wait_until_resumed,
+ search_attributes=event_workflow_search_attributes(
+ listener_data.get("workflow_id")
),
)
except Exception as spawn_exc: # noqa: BLE001
@@ -254,7 +226,7 @@ class PollingTriggerWorkflow:
self._processed_count += 1
should_rollover = self._processed_count >= _MAX_EVENTS_BEFORE_CONTINUE_AS_NEW
- if use_history_bounded_can and not should_rollover:
+ if not should_rollover:
# Poll cycles burn history even with zero emitted events —
# check pressure every cycle, not only per processed event.
from services.temporal.trigger_listener_workflow import _history_pressure
@@ -262,16 +234,10 @@ class PollingTriggerWorkflow:
should_rollover = _history_pressure(_HISTORY_SOFT_CAP)
if should_rollover:
workflow.logger.info(f"PollingTriggerWorkflow continue_as_new: processed={self._processed_count}")
- if use_history_bounded_can:
- # Carry the pause flag instead of blocking the rollover
- # behind a resume that may be months away (signal
- # traffic would overflow the history mid-pause).
- listener_data["control_paused"] = self._control_paused
- elif use_cooperative_pause_schedule_gate:
- # A pause may arrive during the final waiting-status
- # broadcast. Continue only after Resume so the next run
- # cannot reset a still-active pause flag.
- await self._wait_until_resumed()
+ # Carry the pause flag instead of blocking the rollover
+ # behind a resume that may be months away (signal traffic
+ # would overflow the history mid-pause).
+ listener_data["control_paused"] = self._control_paused
# Carry seen_ids forward so the new run doesn't re-emit
# what's already been seen by the provider.
listener_data["seen_ids"] = list(seen_ids)
@@ -296,7 +262,6 @@ class PollingTriggerWorkflow:
"""
from services.temporal._retry_policies import QUICK_ACTIVITY_RETRY
from services.temporal.trigger_listener_workflow import (
- BOUNDED_STATUS_BROADCASTS_PATCH,
_broadcast_trigger_idle,
_broadcast_trigger_waiting,
_build_run_graph,
@@ -307,11 +272,15 @@ class PollingTriggerWorkflow:
edges = listener_data["edges"]
session_id = listener_data.get("session_id", "default")
workflow_id = listener_data.get("workflow_id")
- try:
- use_latest_graph = bool(workflow_id) and workflow.patched("trigger-latest-graph-v1")
- except RuntimeError: # direct unit invocation outside Temporal runtime
- use_latest_graph = False
- if use_latest_graph:
+ graph_version = int(
+ listener_data.get("graphVersion")
+ or listener_data.get("graph_version")
+ or 0
+ )
+ generation = int(listener_data.get("generation") or 0)
+ # Controlled generations execute their immutable admitted snapshot.
+ # Legacy uncontrolled deployments retain hot graph lookup.
+ if bool(workflow_id) and not listener_data.get("data_scope_id"):
try:
latest = await workflow.execute_activity(
"load_persisted_workflow_graph_activity",
@@ -321,6 +290,9 @@ class PollingTriggerWorkflow:
if latest.get("found"):
nodes = latest.get("nodes") or []
edges = latest.get("edges") or []
+ graph_version = int(
+ latest.get("graphVersion") or graph_version
+ )
except Exception as exc: # snapshot remains a safe fallback
workflow.logger.warning(
f"Current graph lookup failed for {workflow_id}; using deployment snapshot: {exc}"
@@ -357,13 +329,10 @@ class PollingTriggerWorkflow:
event_id = event.get("id") or workflow.uuid4().hex
child_id = f"{workflow_slug}-{trigger_label}-{event_id}"
- try:
- unbounded_child_runs = workflow.patched(UNBOUNDED_CHILD_RUNS_PATCH)
- bounded_broadcasts = workflow.patched(BOUNDED_STATUS_BROADCASTS_PATCH)
- except RuntimeError: # direct unit invocation outside Temporal runtime
- unbounded_child_runs = True
- bounded_broadcasts = True
- broadcast_retry = QUICK_ACTIVITY_RETRY if bounded_broadcasts else None
+ # Status broadcasts are cosmetic UI signalling; without an explicit
+ # policy Temporal retries a failing activity forever, wedging the
+ # serialized spawn loop behind a dead broadcaster.
+ broadcast_retry = QUICK_ACTIVITY_RETRY
try:
await _broadcast_trigger_idle(
@@ -386,26 +355,44 @@ class PollingTriggerWorkflow:
WorkflowIDReusePolicy.ALLOW_DUPLICATE_FAILED_ONLY
),
}
- if not unbounded_child_runs:
- # Replay-only: pre-patch histories recorded 1h caps on the
- # child-start command. See UNBOUNDED_CHILD_RUNS_PATCH.
- child_options["execution_timeout"] = timedelta(hours=1)
- child_options["run_timeout"] = timedelta(hours=1)
if search_attributes is not None:
child_options["search_attributes"] = search_attributes
- await workflow.start_child_workflow(
- "MachinaWorkflow",
- args=[
+ child_payload = {
+ "nodes": filtered_nodes,
+ "edges": filtered_edges,
+ "session_id": session_id,
+ "workflow_id": workflow_id,
+ "workflow_slug": workflow_slug,
+ "tenant_id": tenant_id,
+ }
+ frozen_routing = listener_data.get(TEMPORAL_ROUTING_INPUT_KEY)
+ if isinstance(frozen_routing, dict):
+ child_payload[TEMPORAL_ROUTING_INPUT_KEY] = dict(
+ frozen_routing
+ )
+ if "user_id" in listener_data:
+ child_payload["user_id"] = listener_data.get("user_id")
+ if graph_version >= 2 and generation > 0:
+ event_session_id = str(event.get("session_id") or "").strip()
+ child_payload.update(
{
- "nodes": filtered_nodes,
- "edges": filtered_edges,
- "session_id": session_id,
- "workflow_id": workflow_id,
- "workflow_slug": workflow_slug,
- "tenant_id": tenant_id,
+ "graphVersion": graph_version,
+ "generation": generation,
+ "context_execution_id": child_id,
+ "execution_id": listener_data.get("execution_id"),
+ "root_execution_id": listener_data.get(
+ "root_execution_id"
+ ),
+ "data_scope_id": listener_data.get("data_scope_id"),
}
- ],
+ )
+ if event_session_id and event_session_id != "default":
+ child_payload["context_session_id"] = event_session_id
+
+ await workflow.start_child_workflow(
+ "MachinaWorkflow",
+ args=[child_payload],
**child_options,
)
diff --git a/server/services/temporal/trigger_listener_workflow.py b/server/services/temporal/trigger_listener_workflow.py
index 8f04fcf8..3411b979 100644
--- a/server/services/temporal/trigger_listener_workflow.py
+++ b/server/services/temporal/trigger_listener_workflow.py
@@ -43,6 +43,8 @@ from temporalio.common import (
)
from temporalio.workflow import ParentClosePolicy
+from .workflow import TEMPORAL_ROUTING_INPUT_KEY
+
# 50K events is the Temporal Event-History soft ceiling per the
# very-long-running-workflows blog post — past it the workflow
@@ -51,42 +53,26 @@ from temporalio.workflow import ParentClosePolicy
# so this caps at ~16K triggers between continueAsNew checkpoints.
# Per-event histogram is empirical; tune later.
_MAX_EVENTS_BEFORE_CONTINUE_AS_NEW = 16_000
-CHILD_SEARCH_ATTRIBUTES_PATCH = "trigger-child-search-attributes-v1"
-COOPERATIVE_PAUSE_SCHEDULING_PATCH = (
- "trigger-cooperative-pause-scheduling-v1"
-)
-# Child MachinaWorkflow runs used to carry 1h execution/run timeouts.
-# A run may legitimately execute — or stay cooperatively paused — for
-# months, and Temporal's timeout timer keeps ticking through a pause,
-# so any pause longer than the cap silently terminated the run.
-# Liveness is enforced at the activity layer (heartbeats), not by
-# workflow lifetime caps; new executions start children unbounded
-# (Temporal's default). The patch keeps replay compatibility for
-# histories whose recorded child starts carried the old caps.
-UNBOUNDED_CHILD_RUNS_PATCH = "trigger-unbounded-child-runs-v1"
+# Child MachinaWorkflow runs start without execution/run timeouts. A run
+# may legitimately execute — or stay cooperatively paused — for months,
+# and Temporal's timeout timer keeps ticking through a pause, so any cap
+# silently terminated paused runs. Liveness is enforced at the activity
+# layer (heartbeats), not by workflow lifetime caps.
+#
# The _processed_count threshold above was calibrated assuming ~2-3
# history events per firing, but the real spawn path costs ~15-25
# (signal + two broadcast activities + optional graph-load + child
# start/close + workflow tasks) — the server's ~51,200-event hard
# termination fires around 2,000-3,500 firings, long before the
-# 16,000-processed checkpoint could. The patched path rolls over on
-# the server's own is_continue_as_new_suggested signal (with the soft
-# cap below as a deterministic backstop), carries still-queued events
-# instead of dropping them, and carries the pause flag instead of
-# blocking the rollover behind a resume that may be months away.
-HISTORY_BOUNDED_CAN_PATCH = "trigger-history-bounded-can-v1"
+# 16,000-processed checkpoint could. Rollover therefore rides the
+# server's own is_continue_as_new_suggested signal (with the soft cap
+# below as a deterministic backstop), carries still-queued events, and
+# carries the pause flag rather than blocking the rollover behind a
+# resume that may be months away.
_HISTORY_SOFT_CAP = 10_000
# Continue-as-new argument blobs are capped at 2MiB; carry at most this
# many still-queued events across a rollover (oldest dropped, logged).
_MAX_CARRIED_EVENTS = 256
-# Status broadcasts are cosmetic UI signalling. Without an explicit
-# policy Temporal retries a failing activity forever, wedging the
-# serialized spawn loop behind a dead broadcaster. Bounded + swallowed
-# on the patched path (BOUNDED_STATUS_BROADCASTS_PATCH gates the
-# command-visible retry-policy change; the try/except is command-free).
-# Policy: the shared QUICK_ACTIVITY_RETRY constant (cheap side-effect
-# activities fail fast) — never an inline RetryPolicy construction.
-BOUNDED_STATUS_BROADCASTS_PATCH = "trigger-bounded-status-broadcasts-v1"
def _history_pressure(soft_cap: int) -> bool:
@@ -193,16 +179,7 @@ class TriggerListenerWorkflow:
f"node={listener_data.get('trigger_node_id')} "
f"event_type={listener_data.get('event_type')}"
)
- use_child_search_attributes = workflow.patched(
- CHILD_SEARCH_ATTRIBUTES_PATCH
- )
- use_cooperative_pause_schedule_gate = workflow.patched(
- COOPERATIVE_PAUSE_SCHEDULING_PATCH
- )
- use_history_bounded_can = workflow.patched(HISTORY_BOUNDED_CAN_PATCH)
-
- # Rehydrate rollover-carried state. Command-free, so pre-patch
- # histories (whose payloads lack the keys) replay identically.
+ # Rehydrate rollover-carried state.
self._control_paused = bool(listener_data.get("control_paused"))
for carried_event in listener_data.get("pending_events") or []:
carried_id = carried_event.get("id")
@@ -222,17 +199,9 @@ class TriggerListenerWorkflow:
await self._spawn_child_run(
event,
listener_data,
- admission_check=(
- self._wait_until_resumed
- if use_cooperative_pause_schedule_gate
- else None
- ),
- search_attributes=(
- event_workflow_search_attributes(
- listener_data.get("workflow_id")
- )
- if use_child_search_attributes
- else None
+ admission_check=self._wait_until_resumed,
+ search_attributes=event_workflow_search_attributes(
+ listener_data.get("workflow_id")
),
)
except Exception as exc: # noqa: BLE001
@@ -245,27 +214,21 @@ class TriggerListenerWorkflow:
self._processed_count += 1
should_rollover = self._processed_count >= _MAX_EVENTS_BEFORE_CONTINUE_AS_NEW
- if use_history_bounded_can and not should_rollover:
+ if not should_rollover:
should_rollover = _history_pressure(_HISTORY_SOFT_CAP)
if should_rollover:
workflow.logger.info(f"TriggerListener continue_as_new: processed={self._processed_count}")
- if use_history_bounded_can:
- # Carry queued events (previously dropped at rollover)
- # and the pause flag (previously the rollover blocked
- # behind a resume that could be months away, letting
- # signal traffic overflow the history mid-pause).
- dropped = max(0, len(self._matched_events) - _MAX_CARRIED_EVENTS)
- if dropped:
- workflow.logger.warning(
- f"TriggerListener rollover dropping {dropped} oldest queued "
- f"event(s) beyond the {_MAX_CARRIED_EVENTS} carry cap"
- )
- listener_data["pending_events"] = self._matched_events[-_MAX_CARRIED_EVENTS:]
- listener_data["control_paused"] = self._control_paused
- elif use_cooperative_pause_schedule_gate:
- # Do not let continue-as-new reset a pause that landed
- # during the final spawn/status broadcast.
- await self._wait_until_resumed()
+ # Carry queued events and the pause flag so the rollover
+ # never blocks behind a resume that could be months away,
+ # which would let signal traffic overflow the history.
+ dropped = max(0, len(self._matched_events) - _MAX_CARRIED_EVENTS)
+ if dropped:
+ workflow.logger.warning(
+ f"TriggerListener rollover dropping {dropped} oldest queued "
+ f"event(s) beyond the {_MAX_CARRIED_EVENTS} carry cap"
+ )
+ listener_data["pending_events"] = self._matched_events[-_MAX_CARRIED_EVENTS:]
+ listener_data["control_paused"] = self._control_paused
workflow.continue_as_new(args=[listener_data])
async def _spawn_child_run(
@@ -294,13 +257,15 @@ class TriggerListenerWorkflow:
edges = listener_data["edges"]
session_id = listener_data.get("session_id", "default")
workflow_id = listener_data.get("workflow_id")
- try:
- use_latest_graph = bool(workflow_id) and workflow.patched("trigger-latest-graph-v1")
- except RuntimeError: # direct unit invocation outside Temporal runtime
- use_latest_graph = False
+ graph_version = int(
+ listener_data.get("graphVersion")
+ or listener_data.get("graph_version")
+ or 0
+ )
+ generation = int(listener_data.get("generation") or 0)
# Controlled generations execute their immutable admitted snapshot.
# Legacy deployments retain hot graph lookup for compatibility.
- if use_latest_graph and not listener_data.get("data_scope_id"):
+ if bool(workflow_id) and not listener_data.get("data_scope_id"):
try:
latest = await workflow.execute_activity(
"load_persisted_workflow_graph_activity",
@@ -310,6 +275,9 @@ class TriggerListenerWorkflow:
if latest.get("found"):
nodes = latest.get("nodes") or []
edges = latest.get("edges") or []
+ graph_version = int(
+ latest.get("graphVersion") or graph_version
+ )
except Exception as exc: # snapshot remains a safe fallback
workflow.logger.warning(
f"Current graph lookup failed for {workflow_id}; using deployment snapshot: {exc}"
@@ -351,13 +319,11 @@ class TriggerListenerWorkflow:
from services.temporal._retry_policies import QUICK_ACTIVITY_RETRY
- try:
- unbounded_child_runs = workflow.patched(UNBOUNDED_CHILD_RUNS_PATCH)
- bounded_broadcasts = workflow.patched(BOUNDED_STATUS_BROADCASTS_PATCH)
- except RuntimeError: # direct unit invocation outside Temporal runtime
- unbounded_child_runs = True
- bounded_broadcasts = True
- broadcast_retry = QUICK_ACTIVITY_RETRY if bounded_broadcasts else None
+ # Status broadcasts are cosmetic UI signalling; without an explicit
+ # policy Temporal retries a failing activity forever, wedging the
+ # serialized spawn loop behind a dead broadcaster. Always the shared
+ # QUICK_ACTIVITY_RETRY constant — never an inline RetryPolicy.
+ broadcast_retry = QUICK_ACTIVITY_RETRY
try:
await _broadcast_trigger_idle(
@@ -380,29 +346,47 @@ class TriggerListenerWorkflow:
WorkflowIDReusePolicy.ALLOW_DUPLICATE_FAILED_ONLY
),
}
- if not unbounded_child_runs:
- # Replay-only: pre-patch histories recorded 1h caps on the
- # child-start command. See UNBOUNDED_CHILD_RUNS_PATCH.
- child_options["execution_timeout"] = timedelta(hours=1)
- child_options["run_timeout"] = timedelta(hours=1)
if search_attributes is not None:
child_options["search_attributes"] = search_attributes
- await workflow.start_child_workflow(
- "MachinaWorkflow",
- args=[
+ child_payload = {
+ "nodes": filtered_nodes,
+ "edges": filtered_edges,
+ "session_id": session_id,
+ "workflow_id": workflow_id,
+ "workflow_slug": workflow_slug,
+ "tenant_id": tenant_id,
+ "execution_id": listener_data.get("execution_id"),
+ "root_execution_id": listener_data.get("root_execution_id"),
+ "data_scope_id": listener_data.get("data_scope_id"),
+ }
+ frozen_routing = listener_data.get(TEMPORAL_ROUTING_INPUT_KEY)
+ if isinstance(frozen_routing, dict):
+ child_payload[TEMPORAL_ROUTING_INPUT_KEY] = dict(
+ frozen_routing
+ )
+ if "user_id" in listener_data:
+ child_payload["user_id"] = listener_data.get("user_id")
+ if graph_version >= 2 and generation > 0:
+ event_session_id = str(
+ trigger_output.get("session_id") or ""
+ ).strip()
+ child_payload.update(
{
- "nodes": filtered_nodes,
- "edges": filtered_edges,
- "session_id": session_id,
- "workflow_id": workflow_id,
- "workflow_slug": workflow_slug,
- "tenant_id": tenant_id,
- "execution_id": listener_data.get("execution_id"),
- "root_execution_id": listener_data.get("root_execution_id"),
- "data_scope_id": listener_data.get("data_scope_id"),
+ "graphVersion": graph_version,
+ "generation": generation,
+ # The Temporal child id is the execution-scoped Context
+ # identity for this firing. The control-plane
+ # ``execution_id`` remains the generation/root identity.
+ "context_execution_id": child_id,
}
- ],
+ )
+ if event_session_id and event_session_id != "default":
+ child_payload["context_session_id"] = event_session_id
+
+ await workflow.start_child_workflow(
+ "MachinaWorkflow",
+ args=[child_payload],
**child_options,
)
diff --git a/server/services/temporal/worker.py b/server/services/temporal/worker.py
index 0f6780c4..83dc9512 100644
--- a/server/services/temporal/worker.py
+++ b/server/services/temporal/worker.py
@@ -38,6 +38,31 @@ from .activities import (
create_shared_session,
)
+
+
+def _framework_workflows() -> list:
+ """The framework workflow classes every worker must register.
+
+ Single source of truth: the three worker constructions below used to
+ hand-maintain identical copies of this list, which is how they drifted.
+ Imports stay lazy — the workflow modules pull in the plugin registry.
+ """
+ from services.temporal.workflow import MachinaWorkflow
+ from services.temporal.agent_workflow import AgentWorkflow, DelegatedTaskWorkflow
+ from services.temporal.trigger_listener_workflow import TriggerListenerWorkflow
+ from services.temporal.polling_trigger_workflow import PollingTriggerWorkflow
+ from services.temporal.workflow_control_workflow import WorkflowControlWorkflow
+
+ return [
+ MachinaWorkflow,
+ AgentWorkflow,
+ DelegatedTaskWorkflow,
+ TriggerListenerWorkflow,
+ PollingTriggerWorkflow,
+ WorkflowControlWorkflow,
+ ]
+
+
logger = get_logger(__name__)
tracer = trace.get_tracer(__name__)
@@ -195,7 +220,6 @@ class TemporalWorkerManager:
collect_polling_activities,
)
from services.temporal.agent_activities import collect_agent_activities
- from services.temporal.agent_workflow import AgentWorkflow, DelegatedTaskWorkflow
from services.temporal.activities import (
broadcast_trigger_status_activity,
load_persisted_workflow_graph_activity,
@@ -241,14 +265,7 @@ class TemporalWorkerManager:
self.client,
task_queue=self.task_queue,
plugins=plugin_list,
- workflows=[
- MachinaWorkflow,
- AgentWorkflow,
- DelegatedTaskWorkflow,
- TriggerListenerWorkflow,
- PollingTriggerWorkflow,
- WorkflowControlWorkflow,
- ],
+ workflows=_framework_workflows(),
activities=[
self._activities.execute_node_activity,
broadcast_trigger_status_activity,
@@ -709,22 +726,33 @@ async def run_standalone_worker(
pause_workflow_on_failure_activity,
store_node_output_activity,
)
+ from services.temporal.agent_activities import collect_agent_activities
+ from services.temporal.agent_workflow import (
+ AgentWorkflow,
+ DelegatedTaskWorkflow,
+ )
+ from services.temporal.plugin_activities import (
+ collect_plugin_activities,
+ )
+
+ registered_agent_activities = [
+ *collect_agent_activities(),
+ ]
+ registered_plugin_activities = collect_plugin_activities()
try:
worker = Worker(
client,
task_queue=task_queue,
- workflows=[
- MachinaWorkflow,
- TriggerListenerWorkflow,
- PollingTriggerWorkflow,
- WorkflowControlWorkflow,
- ],
+ workflows=_framework_workflows(),
activities=[
activities.execute_node_activity,
broadcast_trigger_status_activity,
load_persisted_workflow_graph_activity,
+ pause_workflow_on_failure_activity,
store_node_output_activity,
+ *registered_plugin_activities,
+ *registered_agent_activities,
],
max_concurrent_activities=pool_size,
max_concurrent_workflow_tasks=10,
@@ -775,22 +803,32 @@ async def create_worker(
pause_workflow_on_failure_activity,
store_node_output_activity,
)
+ from services.temporal.agent_activities import collect_agent_activities
+ from services.temporal.agent_workflow import (
+ AgentWorkflow,
+ DelegatedTaskWorkflow,
+ )
+ from services.temporal.plugin_activities import (
+ collect_plugin_activities,
+ )
+
+ registered_agent_activities = [
+ *collect_agent_activities(),
+ ]
+ registered_plugin_activities = collect_plugin_activities()
return Worker(
client,
task_queue=task_queue,
- workflows=[
- MachinaWorkflow,
- TriggerListenerWorkflow,
- PollingTriggerWorkflow,
- WorkflowControlWorkflow,
- ],
+ workflows=_framework_workflows(),
activities=[
activities.execute_node_activity,
broadcast_trigger_status_activity,
load_persisted_workflow_graph_activity,
pause_workflow_on_failure_activity,
store_node_output_activity,
+ *registered_plugin_activities,
+ *registered_agent_activities,
],
max_concurrent_activities=100,
max_concurrent_workflow_tasks=10,
diff --git a/server/services/temporal/workflow.py b/server/services/temporal/workflow.py
index 3566768e..8d5980b7 100644
--- a/server/services/temporal/workflow.py
+++ b/server/services/temporal/workflow.py
@@ -20,9 +20,17 @@ from ._retry_policies import DEFAULT_ACTIVITY_RETRY, QUICK_ACTIVITY_RETRY
from services.workflow_naming import node_label_slug
# Config handles - nodes connecting via these are config nodes (not executed)
-# AI Agent handles: input-memory, input-tools, input-model, input-task, input-teammates
+# AI Agent handles: input-context, input-tools, input-model, input-task, input-teammates
# Zeenie handles: input-skill, input-tools
-CONFIG_HANDLES = {"input-tools", "input-memory", "input-model", "input-skill", "input-task", "input-teammates"}
+CONFIG_HANDLES = {
+ "input-context",
+ "input-tools",
+ "input-memory", # replay/import compatibility for V1 graph snapshots
+ "input-model",
+ "input-skill",
+ "input-task",
+ "input-teammates",
+}
def _is_config_edge(edge: Dict[str, Any], node_map: Dict[str, Dict[str, Any]]) -> bool:
@@ -87,14 +95,22 @@ AGENT_WORKFLOW_TYPES = frozenset(
]
)
-# Temporal patch marker for the root -> agent child-workflow identity change.
-# Keep the legacy branch until every pre-patch history is outside the
-# namespace's replay/reset window, then deprecate the marker in a later
-# release before removing that branch.
-AGENT_CHILD_ID_V2_PATCH = "machina-agent-child-id-v2"
-COOPERATIVE_PAUSE_SCHEDULING_PATCH = (
- "machina-cooperative-pause-scheduling-v1"
-)
+AGENT_CONTEXT_GRAPH_VERSION = 2
+# Root dispatch consumes an immutable routing snapshot carried in workflow
+# input rather than process-local Settings, so flipping an environment
+# value can never change how an in-flight run dispatches.
+TEMPORAL_ROUTING_INPUT_KEY = "_temporal_routing_v1"
+TEMPORAL_ROUTING_INPUT_VERSION = 1
+# Direct Temporal callers predating the input helper may omit the snapshot.
+# Keep that path deterministic and on the manager's queue while retaining the
+# shipped child/per-type protocols. Normal executor/deployment starts always
+# provide an explicit snapshot.
+_SAFE_FROZEN_ROUTING = {
+ "version": TEMPORAL_ROUTING_INPUT_VERSION,
+ "agent_workflow_enabled": True,
+ "per_type_dispatch_enabled": True,
+ "worker_pool_enabled": False,
+}
# Agent child workflows used to carry 1h execution/run timeouts, and node
# activities a 10-minute start_to_close. A run may legitimately execute —
# or stay cooperatively paused — for months, and Temporal's timeout
@@ -102,9 +118,7 @@ COOPERATIVE_PAUSE_SCHEDULING_PATCH = (
# long/paused runs. New executions start agent children unbounded and
# give node activities a generous start_to_close; liveness is enforced
# by the 2-minute heartbeat timeout (activities self-heartbeat every
-# 30s), not by lifetime caps. Patch keeps replay compatibility for
-# histories recorded with the old caps.
-UNBOUNDED_LIFETIMES_PATCH = "machina-unbounded-lifetimes-v1"
+# 30s), not by lifetime caps.
# Circuit breaker: a failed trigger-spawned run schedules the
# workflow_control.pause_on_failure.v1 activity so the deployment pauses
# (user fixes + resumes) instead of the trigger firing into the same
@@ -112,19 +126,38 @@ UNBOUNDED_LIFETIMES_PATCH = "machina-unbounded-lifetimes-v1"
# evaluated on the activity side, so flipping config never touches
# recorded workflow commands; this patch only gates the activity
# scheduling itself for replay compatibility with older histories.
-PAUSE_ON_FAILURE_PATCH = "machina-pause-on-failure-v1"
-# start_to_close for node activities on the patched path. Generous by
+# start_to_close for node activities. Generous by
# design: a single node step (agent turn batch, browser op, long shell
# command) may run for hours; worker-death detection is the heartbeat
# timeout's job, so a long ceiling costs nothing in liveness.
_NODE_ACTIVITY_START_TO_CLOSE = timedelta(hours=24)
-def _agent_child_workflow_id_v2(root_workflow_id: str, agent_node_id: str) -> str:
+def _agent_child_workflow_id(root_workflow_id: str, agent_node_id: str) -> str:
"""Return a deterministic child id unique to one canvas agent and run."""
return f"{root_workflow_id}-agent-{agent_node_id}"
+def _frozen_routing_from_input(
+ workflow_data: Dict[str, Any],
+) -> Dict[str, Any]:
+ """Normalize the immutable routing snapshot without consulting Settings."""
+ raw = workflow_data.get(TEMPORAL_ROUTING_INPUT_KEY)
+ if (
+ not isinstance(raw, dict)
+ or raw.get("version") != TEMPORAL_ROUTING_INPUT_VERSION
+ ):
+ raw = _SAFE_FROZEN_ROUTING
+ return {
+ "version": TEMPORAL_ROUTING_INPUT_VERSION,
+ "agent_workflow_enabled": raw.get("agent_workflow_enabled") is True,
+ "per_type_dispatch_enabled": (
+ raw.get("per_type_dispatch_enabled") is True
+ ),
+ "worker_pool_enabled": raw.get("worker_pool_enabled") is True,
+ }
+
+
# Durable protocol identifier: existing Temporal histories and child-workflow
# start calls replay against this exact type string. It intentionally retains
# the pre-rebrand name even though the product is now OpenCompany.
@@ -192,7 +225,7 @@ class MachinaWorkflow:
return
try:
await workflow.execute_activity(
- "workflow_control.pause_on_failure.v1",
+ "workflow_control.pause_on_failure",
{
"workflow_id": workflow_id,
"reason": str((errors[0] or {}).get("error", "run_failed"))[:500],
@@ -299,6 +332,12 @@ class MachinaWorkflow:
# (one-off Runs without a saved DB row).
workflow_slug = workflow_data.get("workflow_slug") or workflow_id
tenant_id = workflow_data.get("tenant_id")
+ graph_version = int(
+ workflow_data.get("graphVersion")
+ or workflow_data.get("graph_version")
+ or 0
+ )
+ generation = int(workflow_data.get("generation") or 0)
# Stable per-run execution id. The executor passes the same value
# it uses as this workflow's Temporal id (``<slug>-<uuid8>``), so
# the fallback to ``workflow.info().workflow_id`` is identical by
@@ -310,12 +349,7 @@ class MachinaWorkflow:
# histories must keep the label-derived child ids they recorded;
# new histories use the root Temporal id + exact canvas node id so
# two same-label agents can start in the same graph execution.
- use_agent_child_id_v2 = workflow.patched(AGENT_CHILD_ID_V2_PATCH)
- use_cooperative_pause_schedule_gate = workflow.patched(
- COOPERATIVE_PAUSE_SCHEDULING_PATCH
- )
- use_unbounded_lifetimes = workflow.patched(UNBOUNDED_LIFETIMES_PATCH)
- use_pause_on_failure = workflow.patched(PAUSE_ON_FAILURE_PATCH)
+ frozen_routing = _frozen_routing_from_input(workflow_data)
workflow.logger.info(f"Starting workflow orchestration: {len(nodes)} nodes, {len(edges)} edges")
@@ -372,8 +406,7 @@ class MachinaWorkflow:
# ``_trigger_output`` is ``{not_triggered: True}``, no
# downstream template should resolve against them.
if not trigger_output.get("not_triggered"):
- if use_cooperative_pause_schedule_gate:
- await self._wait_until_resumed()
+ await self._wait_until_resumed()
await workflow.execute_activity(
"store_node_output_activity",
{
@@ -450,17 +483,57 @@ class MachinaWorkflow:
"pre_executed": node.get("_pre_executed", False),
"trigger_output": node.get("_trigger_output"),
}
+ if (
+ graph_version >= AGENT_CONTEXT_GRAPH_VERSION
+ and generation > 0
+ ):
+ context.update(
+ {
+ "graphVersion": graph_version,
+ "generation": generation,
+ "root_execution_id": workflow_data.get(
+ "root_execution_id"
+ )
+ or execution_id,
+ "data_scope_id": workflow_data.get(
+ "data_scope_id"
+ ),
+ # Context thread identity is distinct from the
+ # output/data namespace. Trigger listeners provide
+ # a per-firing execution id and, for chat events,
+ # the explicit conversation session.
+ "context_execution_id": workflow_data.get(
+ "context_execution_id"
+ ),
+ "context_session_id": workflow_data.get(
+ "context_session_id"
+ ),
+ }
+ )
+ if "user_id" in workflow_data:
+ context["user_id"] = workflow_data.get("user_id")
+ context["temporal_worker_pool_enabled"] = bool(
+ frozen_routing.get("worker_pool_enabled")
+ )
# F4.B: agent-as-child-workflow takes precedence over the
# activity path for the 15 migrating agent types when its
# flag is on. Tool calls inside the agent loop become
# per-type activities (F4.A path) automatically.
- dispatch = self._resolve_dispatch(node_type)
- if use_cooperative_pause_schedule_gate:
- # A preceding child start yields to the workflow event
- # loop, so a pause signal may have landed since ``ready``
- # was computed. Re-admit every command in the batch.
- await self._wait_until_resumed()
+ dispatch = self._resolve_dispatch(
+ node_type,
+ graph_version=graph_version,
+ generation=generation,
+ context_v2_enabled=(
+ graph_version >= AGENT_CONTEXT_GRAPH_VERSION
+ and generation > 0
+ ),
+ routing_snapshot=frozen_routing,
+ )
+ # A preceding child start yields to the workflow event loop,
+ # so a pause signal may have landed since ``ready`` was
+ # computed. Re-admit every command in the batch.
+ await self._wait_until_resumed()
if dispatch["kind"] == "child_workflow":
# ``workflow.start_child_workflow`` is ``async def`` —
# awaiting it returns the ``ChildWorkflowHandle`` once
@@ -473,23 +546,17 @@ class MachinaWorkflow:
# ``machina-agent-child-id-v2``. New runs use the actual
# root Temporal workflow id plus the exact canvas node id;
# labels are mutable and need not be unique.
- if use_agent_child_id_v2:
- child_workflow_id = _agent_child_workflow_id_v2(
- workflow.info().workflow_id,
- node_id,
- )
- else:
- child_workflow_id = f"{workflow_slug}-{node_label_slug(node)}"
+ child_workflow_id = _agent_child_workflow_id(
+ workflow.info().workflow_id,
+ node_id,
+ )
+ # Unbounded by design: a run may execute — or stay
+ # cooperatively paused — for months, and Temporal's
+ # timeout timers keep ticking through a pause.
child_start_kwargs: Dict[str, Any] = dict(
args=[context],
id=child_workflow_id,
)
- if not use_unbounded_lifetimes:
- # Replay-only: pre-patch histories recorded 1h caps
- # on the child-start command. New runs are unbounded
- # (see UNBOUNDED_LIFETIMES_PATCH).
- child_start_kwargs["execution_timeout"] = timedelta(hours=1)
- child_start_kwargs["run_timeout"] = timedelta(hours=1)
handle = await workflow.start_child_workflow(
dispatch["name"],
**child_start_kwargs,
@@ -502,6 +569,14 @@ class MachinaWorkflow:
# execute_node_activity otherwise. ``activity_id`` =
# node_id so the Temporal Web UI history tab labels
# each activity by the canvas node it ran.
+ # Honor each plugin's declared RetryPolicy.
+ activity_retry_policy = retry_policy
+ from services.node_registry import get_node_class
+
+ node_cls = get_node_class(node_type)
+ declared_retry = getattr(node_cls, "retry_policy", None)
+ if declared_retry is not None:
+ activity_retry_policy = declared_retry.to_temporal()
start_kwargs: Dict[str, Any] = dict(
args=[context],
activity_id=node_id,
@@ -509,13 +584,9 @@ class MachinaWorkflow:
# self-heartbeat every 30s); start_to_close only
# bounds a single legitimate step. Pre-patch
# histories replay against the old 10min cap.
- start_to_close_timeout=(
- _NODE_ACTIVITY_START_TO_CLOSE
- if use_unbounded_lifetimes
- else timedelta(minutes=10)
- ),
+ start_to_close_timeout=_NODE_ACTIVITY_START_TO_CLOSE,
heartbeat_timeout=timedelta(minutes=2),
- retry_policy=retry_policy,
+ retry_policy=activity_retry_policy,
)
if dispatch.get("queue") is not None:
start_kwargs["task_queue"] = dispatch["queue"]
@@ -560,7 +631,7 @@ class MachinaWorkflow:
# Build final result
success = len(errors) == 0 and len(completed) == len(node_map)
- if errors and use_pause_on_failure:
+ if errors:
await self._pause_deployment_on_failure(workflow_data, nodes, errors)
workflow.logger.info(f"Workflow complete: success={success}, " f"executed={len(execution_trace)}/{len(node_map)}")
@@ -585,7 +656,15 @@ class MachinaWorkflow:
inputs[dep_id] = outputs[dep_id].get("result", {})
return inputs
- def _resolve_dispatch(self, node_type: str) -> Dict[str, Any]:
+ def _resolve_dispatch(
+ self,
+ node_type: str,
+ *,
+ graph_version: int = 0,
+ generation: int = 0,
+ context_v2_enabled: bool = False,
+ routing_snapshot: Optional[Dict[str, Any]] = None,
+ ) -> Dict[str, Any]:
"""Resolve dispatch kind for a node type.
Returns one of:
@@ -595,20 +674,56 @@ class MachinaWorkflow:
— F4.A per-type activity OR legacy fallback, depending on
``temporal_per_type_dispatch``.
- Deterministic: all lookups go through frozen dicts (Settings,
- _NODE_CLASS_REGISTRY, AGENT_WORKFLOW_TYPES). Safe inside
- ``MachinaWorkflow.run`` per the workflow-definition contract.
+ New histories pass ``routing_snapshot`` from workflow input so no
+ mutable Settings value can alter their command shape. ``None`` is the
+ replay-only compatibility path for pre-patch histories.
"""
- from core.config import Settings
-
- settings = Settings()
- if getattr(settings, "temporal_agent_workflow_enabled", False) and node_type in AGENT_WORKFLOW_TYPES:
+ if routing_snapshot is None:
+ from core.config import Settings
+
+ settings = Settings()
+ agent_workflow_enabled = getattr(
+ settings,
+ "temporal_agent_workflow_enabled",
+ False,
+ )
+ per_type_dispatch_enabled = getattr(
+ settings,
+ "temporal_per_type_dispatch",
+ False,
+ )
+ worker_pool_enabled = getattr(
+ settings,
+ "temporal_worker_pool_enabled",
+ False,
+ )
+ else:
+ agent_workflow_enabled = (
+ routing_snapshot.get("agent_workflow_enabled") is True
+ )
+ per_type_dispatch_enabled = (
+ routing_snapshot.get("per_type_dispatch_enabled") is True
+ )
+ worker_pool_enabled = (
+ routing_snapshot.get("worker_pool_enabled") is True
+ )
+ if agent_workflow_enabled and node_type in AGENT_WORKFLOW_TYPES:
return {"kind": "child_workflow", "name": "AgentWorkflow"}
- name, queue = self._resolve_activity(node_type)
+ name, queue = self._resolve_activity(
+ node_type,
+ per_type_dispatch_enabled=per_type_dispatch_enabled,
+ worker_pool_enabled=worker_pool_enabled,
+ )
return {"kind": "activity", "name": name, "queue": queue}
- def _resolve_activity(self, node_type: str) -> tuple[str, str | None]:
+ def _resolve_activity(
+ self,
+ node_type: str,
+ *,
+ per_type_dispatch_enabled: Optional[bool] = None,
+ worker_pool_enabled: Optional[bool] = None,
+ ) -> tuple[str, str | None]:
"""Resolve (activity_name, task_queue) for a node type.
F4.A: when ``settings.temporal_per_type_dispatch`` is on AND the
@@ -635,18 +750,32 @@ class MachinaWorkflow:
Imports are inside the method to keep the workflow module's
top-level import set minimal and to avoid import-cycle drift.
"""
- from core.config import Settings
from services.node_registry import get_node_class
- settings = Settings()
- if not settings.temporal_per_type_dispatch:
+ if (
+ per_type_dispatch_enabled is None
+ or worker_pool_enabled is None
+ ):
+ from core.config import Settings
+
+ settings = Settings()
+ if per_type_dispatch_enabled is None:
+ per_type_dispatch_enabled = (
+ settings.temporal_per_type_dispatch
+ )
+ if worker_pool_enabled is None:
+ worker_pool_enabled = (
+ settings.temporal_worker_pool_enabled
+ )
+
+ if not per_type_dispatch_enabled:
return "execute_node_activity", None
cls = get_node_class(node_type)
if cls is None:
return "execute_node_activity", None
- queue = cls.task_queue if settings.temporal_worker_pool_enabled else None
+ queue = cls.task_queue if worker_pool_enabled else None
return f"node.{cls.type}.v{cls.version}", queue
async def _wait_any_complete(self, running: Dict[str, Any]) -> tuple:
diff --git a/server/services/temporal/workflow_control_workflow.py b/server/services/temporal/workflow_control_workflow.py
index 9ca480a7..7c1b303b 100644
--- a/server/services/temporal/workflow_control_workflow.py
+++ b/server/services/temporal/workflow_control_workflow.py
@@ -11,24 +11,15 @@ from temporalio.exceptions import ApplicationError
from services.temporal._retry_policies import DEFAULT_ACTIVITY_RETRY
-COOPERATIVE_PAUSE_SCHEDULING_PATCH = (
- "workflow-control-cooperative-pause-scheduling-v1"
-)
# The controller is the ONE workflow expected to live for months, and it
# multiplexes every trigger's signals, poll timers/activities, and child
# spawns into a single event history. Temporal terminates any workflow
# around ~51,200 history events, so without continue-as-new a single
# polling trigger at the default 60s interval killed the whole
-# deployment's control plane in days. The patched path rolls the run
-# over (carrying triggers, control state, queued events, and per-trigger
-# seen-id baselines) whenever the server suggests it or the history
-# crosses the soft cap below. Pre-patch histories replay unchanged.
-CONTINUE_AS_NEW_PATCH = "workflow-control-continue-as-new-v1"
-# Upsert the ControlEventTypes keyword-list Search Attribute so
-# dispatch.emit can skip controllers whose deployment has no matching
-# push trigger (otherwise every platform event lands ~4 history events
-# in every running controller). Patch-gated: upsert is a command.
-CONTROL_EVENT_TYPES_SA_PATCH = "workflow-control-event-types-sa-v1"
+# deployment's control plane in days. The run rolls over (carrying
+# triggers, control state, queued events, and per-trigger seen-id
+# baselines) whenever the server suggests it or the history crosses the
+# soft cap below.
# Roll over before the server's hard ceiling. is_continue_as_new_suggested
# is the primary signal; this cap is the deterministic backstop.
@@ -72,8 +63,6 @@ class WorkflowControlWorkflow:
# the newest entries when trimming to _MAX_CARRIED_SEEN_IDS.
self._seen_event_ids: dict[str, None] = {}
self._poll_tasks: dict[str, asyncio.Task] = {}
- self._use_cooperative_pause_schedule_gate = False
- self._can_enabled = False
self._can_requested = False
@workflow.signal
@@ -167,16 +156,6 @@ class WorkflowControlWorkflow:
@workflow.run
async def run(self, control_data: Dict[str, Any]) -> Dict[str, Any]:
- try:
- self._use_cooperative_pause_schedule_gate = workflow.patched(
- COOPERATIVE_PAUSE_SCHEDULING_PATCH
- )
- self._can_enabled = workflow.patched(CONTINUE_AS_NEW_PATCH)
- except Exception: # direct unit invocation outside Temporal runtime
- if workflow.in_workflow():
- raise
- self._use_cooperative_pause_schedule_gate = False
- self._can_enabled = True
self._state = control_data.get("state", "running")
self._seed_carried_state(control_data)
while not self._closed:
@@ -255,7 +234,7 @@ class WorkflowControlWorkflow:
return _history_pressure(_HISTORY_SOFT_CAP)
def _maybe_request_rollover(self) -> None:
- if self._can_enabled and not self._closed and not self._can_requested:
+ if not self._closed and not self._can_requested:
if self._history_pressure():
self._can_requested = True
@@ -299,13 +278,14 @@ class WorkflowControlWorkflow:
Lets ``dispatch.emit`` skip controllers with no matching trigger
instead of signalling every running controller with every
- platform event. Patch-gated (upsert is a command); controllers
- without the attribute are treated as match-all by dispatch.
+ platform event. Controllers without the attribute are treated as
+ match-all by dispatch.
"""
- try:
- if not workflow.patched(CONTROL_EVENT_TYPES_SA_PATCH):
- return
- except Exception: # direct unit invocation outside Temporal runtime
+ # Upsert is a workflow command and the failure path logs through
+ # ``workflow.logger``; both need the workflow event loop. Direct
+ # unit invocation has no loop, so no-op there rather than raising
+ # out of the exception handler.
+ if not workflow.in_workflow():
return
event_types: set[str] = set()
for spec in self._triggers.values():
@@ -341,16 +321,11 @@ class WorkflowControlWorkflow:
await listener._spawn_child_run(
event,
listener_args,
- # This admission check predates the patch marker and must remain
- # unconditional for replay compatibility with existing controller
- # histories. The marker gates only the new child metadata.
admission_check=self._wait_until_running,
search_attributes=(
event_workflow_search_attributes(
listener_args.get("workflow_id")
)
- if self._use_cooperative_pause_schedule_gate
- else None
),
)
@@ -375,11 +350,8 @@ class WorkflowControlWorkflow:
activity_name = f"poll.{node_type}.v{listener_data.get('version', 1)}"
params = listener_data.get("filter_params", {}) or {}
poll_interval = int(params.get("poll_interval") or _DEFAULT_POLL_INTERVAL_S)
- if self._can_enabled:
- # A few-second user-supplied interval overflows history in
- # hours. Timer durations are recorded commands, so the clamp
- # rides the same patch as continue-as-new.
- poll_interval = max(_MIN_POLL_INTERVAL_S, poll_interval)
+ # A few-second user-supplied interval overflows history in hours.
+ poll_interval = max(_MIN_POLL_INTERVAL_S, poll_interval)
activity_timeout_s = max(30, poll_interval * _ACTIVITY_TIMEOUT_MULT)
seen_ids: set[str] = set(listener_data.get("seen_ids") or [])
baseline = not seen_ids
@@ -433,17 +405,9 @@ class WorkflowControlWorkflow:
await runner._spawn_child_run(
event,
listener_data,
- admission_check=(
- self._wait_until_running
- if self._use_cooperative_pause_schedule_gate
- else None
- ),
- search_attributes=(
- event_workflow_search_attributes(
- listener_data.get("workflow_id")
- )
- if self._use_cooperative_pause_schedule_gate
- else None
+ admission_check=self._wait_until_running,
+ search_attributes=event_workflow_search_attributes(
+ listener_data.get("workflow_id")
),
)
except asyncio.CancelledError:
@@ -462,8 +426,5 @@ class WorkflowControlWorkflow:
__all__ = [
- "CONTINUE_AS_NEW_PATCH",
- "CONTROL_EVENT_TYPES_SA_PATCH",
- "COOPERATIVE_PAUSE_SCHEDULING_PATCH",
"WorkflowControlWorkflow",
]
diff --git a/server/services/workflow.py b/server/services/workflow.py
index c13c59ed..f5079825 100644
--- a/server/services/workflow.py
+++ b/server/services/workflow.py
@@ -11,6 +11,7 @@ Following n8n/Conductor patterns for clean separation of concerns.
"""
import time
+from contextvars import ContextVar
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, List, Optional, TYPE_CHECKING
@@ -35,6 +36,11 @@ if TYPE_CHECKING:
logger = get_logger(__name__)
+_parallel_user_id: ContextVar[str] = ContextVar(
+ "workflow_parallel_user_id",
+ default="owner",
+)
+
class WorkflowService:
"""Workflow execution and deployment service.
@@ -185,6 +191,7 @@ class WorkflowService:
workflow_slug: str = None,
outputs: Dict[str, Any] = None,
extras: Optional[Dict[str, Any]] = None,
+ user_id: str = "owner",
) -> Dict[str, Any]:
"""Execute a single workflow node.
@@ -204,6 +211,7 @@ class WorkflowService:
"execution_id": execution_id,
"workflow_id": workflow_id, # UUID — stable system identity, FK target
"workflow_slug": workflow_slug, # Human-readable, mutable on rename
+ "user_id": str(user_id or "owner"),
"workspace_dir": workspace_dir, # Per-workflow filesystem for nodes and agents
"get_output_fn": self.get_node_output,
"outputs": outputs or {}, # Upstream node outputs for data flow (e.g., taskTrigger -> chatAgent)
@@ -236,6 +244,10 @@ class WorkflowService:
execution_id=context.get("execution_id"),
workflow_id=context.get("workflow_id"),
workflow_slug=context.get("workflow_slug"),
+ user_id=(
+ context.get("user_id")
+ or _parallel_user_id.get()
+ ),
)
# =========================================================================
@@ -252,6 +264,9 @@ class WorkflowService:
skip_clear_outputs: bool = False,
workflow_id: Optional[str] = None,
use_temporal: bool = None,
+ graph_version: int = 0,
+ generation: int = 0,
+ user_id: str = "owner",
) -> Dict[str, Any]:
"""Execute entire workflow.
@@ -264,8 +279,12 @@ class WorkflowService:
skip_clear_outputs: Skip clearing outputs (for deployment runs)
workflow_id: Workflow ID for per-workflow status scoping (n8n pattern)
use_temporal: Force Temporal execution (None = use settings default)
+ graph_version: Normalized graph version for Context V2 cutover
+ generation: Durable deployment generation for Context V2 cutover
+ user_id: Authenticated server-owned user identity
"""
start_time = time.time()
+ user_id = str(user_id or "owner")
# Clear outputs unless skipped
if not skip_clear_outputs:
@@ -310,7 +329,17 @@ class WorkflowService:
# Use Temporal if enabled and executor is configured
if use_temporal and self._temporal_executor is not None:
- return await self._execute_temporal(nodes, edges, session_id, status_callback, start_time, workflow_id)
+ return await self._execute_temporal(
+ nodes,
+ edges,
+ session_id,
+ status_callback,
+ start_time,
+ workflow_id,
+ graph_version,
+ generation,
+ user_id,
+ )
# Loud error if Temporal was requested but the executor never finished
# wiring. Previously a WARNING; bumped to ERROR because a silent
@@ -328,12 +357,39 @@ class WorkflowService:
# Use parallel executor if enabled and Redis available
if use_parallel and self.settings.redis_enabled:
- return await self._execute_parallel(nodes, edges, session_id, status_callback, start_time, workflow_id)
+ return await self._execute_parallel(
+ nodes,
+ edges,
+ session_id,
+ status_callback,
+ start_time,
+ workflow_id,
+ user_id,
+ )
# Fall back to sequential
- return await self._execute_sequential(nodes, edges, session_id, status_callback, start_time, workflow_id)
+ return await self._execute_sequential(
+ nodes,
+ edges,
+ session_id,
+ status_callback,
+ start_time,
+ workflow_id,
+ user_id,
+ )
- async def _execute_temporal(self, nodes, edges, session_id, status_callback, start_time, workflow_id: Optional[str] = None) -> Dict:
+ async def _execute_temporal(
+ self,
+ nodes,
+ edges,
+ session_id,
+ status_callback,
+ start_time,
+ workflow_id: Optional[str] = None,
+ graph_version: int = 0,
+ generation: int = 0,
+ user_id: str = "owner",
+ ) -> Dict:
"""Execute with Temporal for durable workflow orchestration."""
# Use passed workflow_id (from deployment) or generate new one
if not workflow_id:
@@ -357,6 +413,9 @@ class WorkflowService:
session_id=session_id,
enable_caching=True,
workflow_slug=workflow_slug,
+ graph_version=graph_version,
+ generation=generation,
+ user_id=user_id,
)
# Notify status callback for completed nodes if provided
@@ -382,20 +441,33 @@ class WorkflowService:
"timestamp": datetime.now().isoformat(),
}
- async def _execute_parallel(self, nodes, edges, session_id, status_callback, start_time, workflow_id: Optional[str] = None) -> Dict:
+ async def _execute_parallel(
+ self,
+ nodes,
+ edges,
+ session_id,
+ status_callback,
+ start_time,
+ workflow_id: Optional[str] = None,
+ user_id: str = "owner",
+ ) -> Dict:
"""Execute with parallel orchestration engine."""
# Use passed workflow_id (from deployment) or generate new one
if not workflow_id:
workflow_id = f"workflow_{session_id}_{int(time.time() * 1000)}"
executor = self._get_workflow_executor(status_callback)
- result = await executor.execute_workflow(
- workflow_id=workflow_id,
- nodes=nodes,
- edges=edges,
- session_id=session_id,
- enable_caching=True,
- )
+ token = _parallel_user_id.set(str(user_id or "owner"))
+ try:
+ result = await executor.execute_workflow(
+ workflow_id=workflow_id,
+ nodes=nodes,
+ edges=edges,
+ session_id=session_id,
+ enable_caching=True,
+ )
+ finally:
+ _parallel_user_id.reset(token)
return {
"success": result.get("success", False),
@@ -408,7 +480,16 @@ class WorkflowService:
"timestamp": datetime.now().isoformat(),
}
- async def _execute_sequential(self, nodes, edges, session_id, status_callback, start_time, workflow_id: Optional[str] = None) -> Dict:
+ async def _execute_sequential(
+ self,
+ nodes,
+ edges,
+ session_id,
+ status_callback,
+ start_time,
+ workflow_id: Optional[str] = None,
+ user_id: str = "owner",
+ ) -> Dict:
"""Execute nodes sequentially (fallback mode)."""
execution_id = uuid4().hex
start_node = self._find_start_node(nodes)
@@ -454,6 +535,7 @@ class WorkflowService:
session_id=session_id,
execution_id=execution_id,
workflow_id=workflow_id,
+ user_id=user_id,
)
results[node_id] = result
@@ -491,6 +573,9 @@ class WorkflowService:
session_id: str = "default",
status_callback=None,
workflow_id: Optional[str] = None,
+ graph_version: int = 0,
+ generation: int = 0,
+ user_id: str = "owner",
) -> Dict[str, Any]:
"""Deploy workflow in event-driven mode.
@@ -500,9 +585,30 @@ class WorkflowService:
session_id: Session identifier
status_callback: Status update callback
workflow_id: Workflow ID for per-workflow deployment tracking
+ graph_version: Normalized graph contract version
+ generation: Durable workflow-control generation
+ user_id: Authenticated server-owned user identity
"""
manager = self._get_deployment_manager()
- return await manager.deploy(nodes, edges, session_id, status_callback, workflow_id)
+ if int(graph_version or 0) < 2 or int(generation or 0) <= 0:
+ return await manager.deploy(
+ nodes,
+ edges,
+ session_id,
+ status_callback,
+ workflow_id,
+ user_id=user_id,
+ )
+ return await manager.deploy(
+ nodes,
+ edges,
+ session_id,
+ status_callback,
+ workflow_id,
+ graph_version,
+ generation,
+ user_id=user_id,
+ )
async def cancel_deployment(self, workflow_id: Optional[str] = None) -> Dict[str, Any]:
"""Cancel active deployment.
diff --git a/server/services/workflow_context_migration.py b/server/services/workflow_context_migration.py
new file mode 100644
index 00000000..1bfeb1fc
--- /dev/null
+++ b/server/services/workflow_context_migration.py
@@ -0,0 +1,214 @@
+"""Durable second phase for Context V2 workflow normalization.
+
+The pure graph migration returns state-import receipts after canonical IDs are
+known. This module commits those receipts before the normalized graph replaces
+its legacy topology, so a failed import never silently discards the only copy
+of legacy conversation state.
+"""
+
+from __future__ import annotations
+
+import asyncio
+from typing import Any, Iterable, Mapping
+
+from services.agent_context import AgentContextStore
+
+
+_LEGACY_RUNTIME_FIELDS = frozenset(
+ {
+ "memory_content",
+ "memory_jsonl",
+ "last_session_id",
+ "vertex_interaction_id",
+ "vertex_environment_id",
+ "session_id",
+ "window_size",
+ "long_term_enabled",
+ "retrieval_count",
+ "embedding_provider",
+ "embedding_model",
+ "embedding_endpoint",
+ }
+)
+
+
+async def load_node_parameters(
+ database: Any,
+ nodes: Iterable[Mapping[str, Any]],
+) -> dict[str, dict[str, Any]]:
+ """Hydrate parameters without serial reads at graph boundaries."""
+
+ node_ids = [
+ str(node["id"])
+ for node in nodes
+ if node.get("id") is not None
+ ]
+ getter = getattr(database, "get_node_parameters", None)
+ if not callable(getter):
+ return {}
+ values = await asyncio.gather(
+ *(getter(node_id) for node_id in node_ids)
+ )
+ return {
+ node_id: dict(value or {})
+ for node_id, value in zip(node_ids, values)
+ if value
+ }
+
+
+async def import_legacy_context_receipts(
+ database: Any,
+ receipts: Iterable[Mapping[str, Any]],
+) -> int:
+ """Idempotently preserve Markdown and provider bindings as artifacts."""
+
+ store = AgentContextStore(database)
+ imported = 0
+ for receipt in receipts:
+ workflow_id = str(receipt.get("workflow_id") or "")
+ context_node_id = str(receipt.get("context_node_id") or "")
+ operation_id = str(receipt.get("operation_id") or "")
+ if not workflow_id or not context_node_id or not operation_id:
+ raise ValueError("invalid_context_import_receipt")
+
+ # Generation zero is the immutable migration epoch. A new execution
+ # imports it as a portable handoff into its own generation/thread.
+ ref = await store.resolve_thread(
+ workflow_id=workflow_id,
+ context_node_id=context_node_id,
+ generation=0,
+ session_id=str(
+ receipt.get("legacy_session_id")
+ or receipt.get("agent_node_id")
+ or "legacy"
+ ),
+ )
+ markdown = receipt.get("markdown")
+ payload_ref = None
+ if markdown:
+ payload_ref = await store.put_blob(
+ {
+ "format": "legacy_markdown",
+ "fidelity": "legacy_partial",
+ "content": str(markdown),
+ "source_memory_node_id": receipt.get(
+ "legacy_memory_node_id"
+ ),
+ }
+ )
+ await store.append_transition(
+ ref,
+ event_type="legacy_partial",
+ operation_id=operation_id,
+ payload_ref=payload_ref,
+ provider="legacy",
+ )
+
+ bindings = dict(receipt.get("provider_bindings") or {})
+ claude_session = bindings.get("last_session_id")
+ if claude_session:
+ await store.bind_provider(
+ ref,
+ provider="claude_code",
+ binding_type="session_uuid",
+ binding={"session_uuid": str(claude_session)},
+ operation_id=f"{operation_id}:claude-session",
+ fidelity="provider_bound",
+ )
+ vertex_interaction = bindings.get("vertex_interaction_id")
+ vertex_environment = bindings.get("vertex_environment_id")
+ if vertex_interaction or vertex_environment:
+ await store.bind_provider(
+ ref,
+ provider="vertex",
+ binding_type="interaction_environment",
+ binding={
+ "interaction_id": vertex_interaction,
+ "environment_id": vertex_environment,
+ },
+ operation_id=f"{operation_id}:vertex-binding",
+ fidelity="provider_bound",
+ )
+ imported += 1
+ return imported
+
+
+async def persist_parameter_aliases(
+ database: Any,
+ *,
+ aliases: Mapping[str, str],
+ parameters: Mapping[str, Mapping[str, Any]],
+ context_import_completed: bool,
+) -> None:
+ """Rekey node configuration and retire imported legacy runtime fields."""
+
+ reverse_aliases = {new: old for old, new in aliases.items()}
+ save = getattr(database, "save_node_parameters", None)
+ remove = getattr(database, "delete_node_parameters", None)
+ if not callable(save):
+ return
+ for node_id, raw_params in parameters.items():
+ params = dict(raw_params or {})
+ if context_import_completed and any(
+ key in params for key in _LEGACY_RUNTIME_FIELDS
+ ):
+ params = {
+ key: value
+ for key, value in params.items()
+ if key not in _LEGACY_RUNTIME_FIELDS
+ }
+ params.setdefault("reset_policy", "preserve")
+ await save(node_id, params)
+ old_id = reverse_aliases.get(node_id)
+ if old_id and old_id != node_id and callable(remove):
+ await remove(old_id)
+
+
+async def archive_removed_contexts(
+ database: Any,
+ *,
+ workflow_id: str,
+ previous_nodes: Iterable[Mapping[str, Any]],
+ normalized_nodes: Iterable[Mapping[str, Any]],
+ aliases: Mapping[str, str],
+) -> int:
+ """Archive journals whose system Context node left the saved graph."""
+
+ previous = {
+ str(node.get("id") or "")
+ for node in previous_nodes
+ if node.get("type") == "context" and node.get("id")
+ }
+ current = {
+ str(node.get("id") or "")
+ for node in normalized_nodes
+ if node.get("type") == "context" and node.get("id")
+ }
+ # Canonicalization is a rename, not deletion.
+ removed = {
+ context_id
+ for context_id in previous
+ if context_id not in current
+ and aliases.get(context_id) not in current
+ }
+ if not removed:
+ return 0
+ store = AgentContextStore(database)
+ for context_id in sorted(removed):
+ await store.archive_context(
+ workflow_id=workflow_id,
+ context_node_id=context_id,
+ generation=None,
+ operation_id=(
+ f"context-node-removed:{workflow_id}:{context_id}"
+ ),
+ )
+ return len(removed)
+
+
+__all__ = [
+ "archive_removed_contexts",
+ "import_legacy_context_receipts",
+ "load_node_parameters",
+ "persist_parameter_aliases",
+]
diff --git a/server/services/workflow_import.py b/server/services/workflow_import.py
index b884a535..f6793247 100644
--- a/server/services/workflow_import.py
+++ b/server/services/workflow_import.py
@@ -35,8 +35,11 @@ from typing import Any, Dict, List, Optional, Tuple
from core.logging import get_logger
from services.node_registry import get_node_class
-from services.workflow_migrations import normalize_legacy_android_toolkit
-from services.workflow_naming import canonicalize_node_ids, next_available_slug
+from services.workflow_migrations import (
+ normalize_legacy_android_toolkit,
+ normalize_workflow_graph,
+)
+from services.workflow_naming import next_available_slug
from services.workflow_validator import validate_workflow
logger = get_logger(__name__)
@@ -262,6 +265,26 @@ async def import_workflow(
nodes, edges, node_parameters, migration_warnings = normalize_legacy_android_toolkit(
nodes, edges, node_parameters
)
+ # Validation sees the exact V2 topology, but preview does not allocate a
+ # workflow identity or write state. Keep the pre-Context source so the
+ # commit pass can generate canonical import receipts after ID allocation.
+ migration_source_nodes = [dict(node) for node in nodes]
+ migration_source_edges = [dict(edge) for edge in edges]
+ migration_source_params = {
+ str(node_id): dict(params or {})
+ for node_id, params in node_parameters.items()
+ }
+ preview_normalization = normalize_workflow_graph(
+ "",
+ nodes,
+ edges,
+ node_parameters,
+ canonicalize_ids=False,
+ )
+ nodes = preview_normalization.nodes
+ edges = preview_normalization.edges
+ node_parameters = preview_normalization.node_parameters
+ migration_warnings.extend(preview_normalization.warnings)
# 1. Validate. Errors block the import; warnings flow through.
report = await validate_workflow(
@@ -306,46 +329,69 @@ async def import_workflow(
# 6. Remap node ids — protects the node_parameters table from
# duplicate-id collisions across imports.
remapped_nodes, remapped_edges, remapped_params = remap_node_ids(
- nodes,
- edges,
- node_parameters,
+ migration_source_nodes,
+ migration_source_edges,
+ migration_source_params,
)
# 7. Save. Incremental system identity + name-derived slug for
# human-visible surfaces (folder names, Temporal Web UI).
workflow_id = await database.allocate_workflow_id()
- remapped_nodes, remapped_edges, canonical_aliases = canonicalize_node_ids(
- workflow_id, remapped_nodes, remapped_edges
+ normalization = normalize_workflow_graph(
+ workflow_id,
+ remapped_nodes,
+ remapped_edges,
+ remapped_params,
)
- remapped_params = {
- canonical_aliases.get(node_id, node_id): params
- for node_id, params in remapped_params.items()
- }
+ remapped_nodes = normalization.nodes
+ remapped_edges = normalization.edges
+ remapped_params = normalization.node_parameters
+ from services.workflow_context_migration import (
+ import_legacy_context_receipts,
+ )
+
+ imported = await import_legacy_context_receipts(
+ database,
+ normalization.state_imports,
+ )
+ if imported != len(normalization.state_imports):
+ return {
+ "success": False,
+ "error": "context_state_import_failed",
+ "report": report,
+ }
slug = await next_available_slug(proposed_name, database)
+ from services.workflow_sanitizer import sanitize_workflow_graph
+
+ normalized_data = sanitize_workflow_graph(
+ {
+ "graphVersion": normalization.graph_version,
+ "nodes": remapped_nodes,
+ "edges": remapped_edges,
+ }
+ )
saved = await database.save_workflow(
workflow_id=workflow_id,
name=proposed_name,
slug=slug,
description=workflow.get("description"),
- data={"nodes": remapped_nodes, "edges": remapped_edges},
+ data=normalized_data,
)
if not saved:
return {"success": False, "error": "save_failed", "report": report}
- # 8. Per-node parameter saves under the remapped ids.
- saved_params = 0
- for node_id, params in remapped_params.items():
- if not params:
- continue
- try:
- await database.save_node_parameters(node_id, params)
- saved_params += 1
- except Exception as e:
- logger.error(
- "[workflow_import] save_node_parameters failed for %s: %s",
- node_id,
- e,
- )
+ # 8. Per-node configuration saves. Legacy transcript/provider fields have
+ # already been preserved as immutable Context artifacts and are retired
+ # from the Memory tool's configuration surface.
+ from services.workflow_context_migration import persist_parameter_aliases
+
+ await persist_parameter_aliases(
+ database,
+ aliases={},
+ parameters=remapped_params,
+ context_import_completed=True,
+ )
+ saved_params = sum(bool(params) for params in remapped_params.values())
# 9. CloudEvents broadcast — ``workflow.imported`` envelope. Other
# connected clients (browser tabs) listen for this and invalidate
diff --git a/server/services/workflow_migrations.py b/server/services/workflow_migrations.py
index 823665f9..b9e141b4 100644
--- a/server/services/workflow_migrations.py
+++ b/server/services/workflow_migrations.py
@@ -1,12 +1,95 @@
-"""Compatibility migrations for persisted workflow graphs."""
+"""Versioned, idempotent compatibility migrations for workflow graphs.
+
+Graph normalization is deliberately pure. Durable Context import happens
+after canonical node IDs have been assigned and is driven by the returned
+``state_imports`` receipts. This keeps topology normalization safe for
+editor previews while allowing persistence boundaries to commit topology
+and imported runtime state transactionally.
+"""
from __future__ import annotations
+from dataclasses import dataclass, field
from typing import Any, Dict, List, Mapping, Optional, Tuple
from constants import AI_AGENT_TYPES, ANDROID_SERVICE_NODE_TYPES
+WORKFLOW_GRAPH_VERSION = 2
+
+
+@dataclass(frozen=True)
+class WorkflowGraphNormalization:
+ """Result of the V2 graph migration pipeline."""
+
+ nodes: List[Dict[str, Any]]
+ edges: List[Dict[str, Any]]
+ node_parameters: Dict[str, Dict[str, Any]]
+ warnings: List[str] = field(default_factory=list)
+ aliases: Dict[str, str] = field(default_factory=dict)
+ state_imports: List[Dict[str, Any]] = field(default_factory=list)
+ graph_version: int = WORKFLOW_GRAPH_VERSION
+
+ def graph_data(self, original: Optional[Mapping[str, Any]] = None) -> Dict[str, Any]:
+ return {
+ **dict(original or {}),
+ "graphVersion": self.graph_version,
+ "nodes": self.nodes,
+ "edges": self.edges,
+ }
+
+
+def _target_handle(edge: Mapping[str, Any]) -> Optional[str]:
+ value = edge.get("targetHandle") or edge.get("target_handle")
+ return str(value) if value else None
+
+
+def _source_handle(edge: Mapping[str, Any]) -> Optional[str]:
+ value = edge.get("sourceHandle") or edge.get("source_handle")
+ return str(value) if value else None
+
+
+def _requires_context(node_type: str) -> bool:
+ """Read the declared plugin capability without type-name heuristics."""
+ from services.node_registry import get_node_class
+
+ node_cls = get_node_class(node_type)
+ if node_cls is None:
+ return False
+ if bool(getattr(node_cls, "requires_context", False)):
+ return True
+ # Transitional support for plugins which shipped the capability as a
+ # NodeSpec hint before gaining the BaseNode ClassVar.
+ return bool((getattr(node_cls, "ui_hints", {}) or {}).get("requiresContext"))
+
+
+def _context_node_for(agent: Mapping[str, Any], node_id: str) -> Dict[str, Any]:
+ position = dict(agent.get("position") or {})
+ x = position.get("x")
+ y = position.get("y")
+ context_position: Dict[str, Any] = {}
+ if isinstance(x, (int, float)):
+ context_position["x"] = x - 360
+ if isinstance(y, (int, float)):
+ context_position["y"] = y
+ return {
+ "id": node_id,
+ "type": "context",
+ "position": context_position,
+ "data": {
+ "label": "Context",
+ "systemManaged": True,
+ "agentNodeId": str(agent.get("id") or ""),
+ },
+ }
+
+
+def _edge_id(prefix: str, source: str, target: str) -> str:
+ safe_source = source.replace(":", "-")
+ safe_target = target.replace(":", "-")
+ return f"{prefix}-{safe_source}-{safe_target}"
+
+
def normalize_edge_handles(edges: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Canonicalize legacy ReactFlow handle keys without changing topology."""
normalized: List[Dict[str, Any]] = []
@@ -35,9 +118,7 @@ def normalize_legacy_android_toolkit(
"""
params = dict(node_parameters or {})
node_by_id = {node.get("id"): node for node in nodes if node.get("id")}
- toolkit_ids = {
- node_id for node_id, node in node_by_id.items() if node.get("type") == "androidTool"
- }
+ toolkit_ids = {node_id for node_id, node in node_by_id.items() if node.get("type") == "androidTool"}
if not toolkit_ids:
return list(nodes), list(edges), params, []
@@ -54,11 +135,7 @@ def normalize_legacy_android_toolkit(
if target_node.get("type") in AI_AGENT_TYPES:
outgoing[source].append(target)
- migrated_edges = [
- dict(edge)
- for edge in edges
- if edge.get("source") not in toolkit_ids and edge.get("target") not in toolkit_ids
- ]
+ migrated_edges = [dict(edge) for edge in edges if edge.get("source") not in toolkit_ids and edge.get("target") not in toolkit_ids]
direct_pairs = {
(edge.get("source"), edge.get("target"))
for edge in migrated_edges
@@ -69,9 +146,7 @@ def normalize_legacy_android_toolkit(
agents = list(dict.fromkeys(outgoing[toolkit_id]))
services = list(dict.fromkeys(incoming[toolkit_id]))
if not agents:
- warnings.append(
- f"Removed legacy androidTool '{toolkit_id}' without a valid destination agent"
- )
+ warnings.append(f"Removed legacy androidTool '{toolkit_id}' without a valid destination agent")
continue
for service_id in services:
for agent_id in agents:
@@ -92,3 +167,370 @@ def normalize_legacy_android_toolkit(
for toolkit_id in toolkit_ids:
params.pop(toolkit_id, None)
return migrated_nodes, migrated_edges, params, warnings
+
+
+def normalize_workflow_graph(
+ workflow_id: str,
+ nodes: List[Dict[str, Any]],
+ edges: List[Dict[str, Any]],
+ node_parameters: Optional[Mapping[str, Dict[str, Any]]] = None,
+ *,
+ canonicalize_ids: bool = True,
+) -> WorkflowGraphNormalization:
+ """Normalize a graph to Context V2.
+
+ The transform is idempotent and preserves unknown edges for validation.
+ It performs the following ordered stages:
+
+ 1. canonicalize legacy handle field names;
+ 2. migrate the retired Android toolkit;
+ 3. convert every legacy Memory continuity edge into a Context edge plus
+ an ordinary Memory tool edge;
+ 4. pair every plugin declaring ``requires_context`` with a fresh Context;
+ 5. assign canonical node IDs and return aliases/import receipts.
+
+ Raw legacy Markdown is returned only in ``state_imports``. It is never
+ copied into the Context node or workflow graph.
+ """
+ normalized_edges = normalize_edge_handles(edges or [])
+ normalized_nodes, normalized_edges, params, warnings = normalize_legacy_android_toolkit(
+ nodes or [],
+ normalized_edges,
+ node_parameters,
+ )
+ normalized_nodes = [dict(node) for node in normalized_nodes]
+ normalized_edges = [dict(edge) for edge in normalized_edges]
+ params = {str(node_id): dict(value or {}) for node_id, value in params.items()}
+
+ node_by_id = {str(node.get("id")): node for node in normalized_nodes if node.get("id") is not None}
+ context_ids = {node_id for node_id, node in node_by_id.items() if node.get("type") == "context"}
+
+ legacy_by_agent: Dict[str, List[str]] = {}
+ retained_edges: List[Dict[str, Any]] = []
+ for edge in normalized_edges:
+ source = str(edge.get("source") or "")
+ target = str(edge.get("target") or "")
+ source_node = node_by_id.get(source) or {}
+ if source_node.get("type") == "simpleMemory" and _target_handle(edge) == "input-memory" and target in node_by_id:
+ legacy_by_agent.setdefault(target, []).append(source)
+ continue
+ retained_edges.append(edge)
+ normalized_edges = retained_edges
+
+ # Reconnect legacy Simple Memory nodes as normal tools. A shared Memory
+ # remains shared, while each destination agent receives isolated Context.
+ tool_pairs = {
+ (str(edge.get("source") or ""), str(edge.get("target") or "")) for edge in normalized_edges if _target_handle(edge) == "input-tools"
+ }
+ for agent_id, memory_ids in sorted(legacy_by_agent.items()):
+ for memory_id in dict.fromkeys(memory_ids):
+ if (memory_id, agent_id) in tool_pairs:
+ continue
+ normalized_edges.append(
+ {
+ "id": _edge_id("memory-tool", memory_id, agent_id),
+ "source": memory_id,
+ "target": agent_id,
+ "sourceHandle": "output-tool",
+ "targetHandle": "input-tools",
+ }
+ )
+ tool_pairs.add((memory_id, agent_id))
+
+ required_agent_ids = {node_id for node_id, node in node_by_id.items() if _requires_context(str(node.get("type") or ""))}
+ # A legacy edge is itself an explicit continuity declaration. It remains
+ # migratable even if an optional plugin is not installed on this host.
+ required_agent_ids.update(legacy_by_agent)
+
+ # Resolve system companion ownership before repairing edges. This prevents
+ # a stale Context whose owner was deleted from being silently adopted by a
+ # different agent merely because an unrelated edge still points at it.
+ context_targets: Dict[str, List[str]] = {}
+ for edge in normalized_edges:
+ source = str(edge.get("source") or "")
+ target = str(edge.get("target") or "")
+ if (
+ source in context_ids
+ and _source_handle(edge) == "output-context"
+ and _target_handle(edge) == "input-context"
+ and target in required_agent_ids
+ ):
+ context_targets.setdefault(source, []).append(target)
+
+ declared_owners: Dict[str, str] = {}
+ resolved_system_owners: Dict[str, str] = {}
+ orphaned_system_contexts: set[str] = set()
+ for context_id in sorted(context_ids):
+ context_node = node_by_id[context_id]
+ data = dict(context_node.get("data") or {})
+ if data.get("systemManaged") is not True:
+ continue
+ declared_owner = str(data.get("agentNodeId") or "")
+ if declared_owner:
+ if declared_owner not in required_agent_ids:
+ orphaned_system_contexts.add(context_id)
+ warnings.append(f"Removed orphaned system Context {context_id!r}")
+ continue
+ declared_owners[context_id] = declared_owner
+ resolved_system_owners[context_id] = declared_owner
+ continue
+
+ inferred = sorted(set(context_targets.get(context_id, [])))
+ if len(inferred) == 1:
+ resolved_system_owners[context_id] = inferred[0]
+ elif not inferred:
+ orphaned_system_contexts.add(context_id)
+ warnings.append(f"Removed orphaned system Context {context_id!r}")
+ # A shared system Context without ownership metadata is ambiguous.
+ # Preserve it so the validation boundary rejects the graph instead of
+ # guessing which agent owns provider state.
+
+ # If retries or an older client produced two system companions for one
+ # agent, retain one deterministically and archive the rest after save.
+ contexts_by_owner: Dict[str, List[str]] = {}
+ for context_id, owner_id in resolved_system_owners.items():
+ if context_id not in orphaned_system_contexts:
+ contexts_by_owner.setdefault(owner_id, []).append(context_id)
+ for owner_id, candidates in sorted(contexts_by_owner.items()):
+ if len(candidates) < 2:
+ continue
+ keeper = min(
+ candidates,
+ key=lambda context_id: (
+ context_id not in declared_owners,
+ owner_id not in context_targets.get(context_id, []),
+ context_id,
+ ),
+ )
+ for context_id in sorted(candidates):
+ if context_id == keeper:
+ continue
+ orphaned_system_contexts.add(context_id)
+ resolved_system_owners.pop(context_id, None)
+ warnings.append(f"Removed duplicate system Context {context_id!r} for agent {owner_id!r}; retained {keeper!r}")
+
+ if orphaned_system_contexts:
+ normalized_nodes = [node for node in normalized_nodes if str(node.get("id") or "") not in orphaned_system_contexts]
+ normalized_edges = [
+ edge
+ for edge in normalized_edges
+ if str(edge.get("source") or "") not in orphaned_system_contexts
+ and str(edge.get("target") or "") not in orphaned_system_contexts
+ ]
+ for context_id in orphaned_system_contexts:
+ params.pop(context_id, None)
+ node_by_id.pop(context_id, None)
+ context_ids.discard(context_id)
+
+ # A system-owned Context may only point at its recorded owner. Repair
+ # stale/shared system edges, but leave ambiguous user-authored topology
+ # untouched so validation can reject it.
+ repaired_edges: List[Dict[str, Any]] = []
+ owner_pairs: set[tuple[str, str]] = set()
+ for edge in normalized_edges:
+ source = str(edge.get("source") or "")
+ target = str(edge.get("target") or "")
+ owner_id = resolved_system_owners.get(source)
+ is_context_edge = _source_handle(edge) == "output-context" and _target_handle(edge) == "input-context"
+ if owner_id and is_context_edge:
+ if target != owner_id:
+ warnings.append(f"Removed stale system Context edge {source!r} -> {target!r}; owner is {owner_id!r}")
+ continue
+ owner_pairs.add((source, target))
+ repaired_edges.append(edge)
+ normalized_edges = repaired_edges
+ for context_id, owner_id in sorted(resolved_system_owners.items()):
+ if (context_id, owner_id) in owner_pairs:
+ continue
+ normalized_edges.append(
+ {
+ "id": _edge_id("context", context_id, owner_id),
+ "source": context_id,
+ "target": owner_id,
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ "data": {"systemManaged": True},
+ }
+ )
+
+ existing_contexts: Dict[str, List[str]] = {}
+ for edge in normalized_edges:
+ source = str(edge.get("source") or "")
+ target = str(edge.get("target") or "")
+ if source in context_ids and _source_handle(edge) == "output-context" and _target_handle(edge) == "input-context":
+ existing_contexts.setdefault(target, []).append(source)
+
+ # A required system edge removed in the editor is repaired by the
+ # backend. The companion records its owner as non-runtime UI metadata so
+ # repair does not rely on geometry or agent type-name heuristics.
+ existing_pairs = {(source, target) for target, sources in existing_contexts.items() for source in sources}
+ for context_id in sorted(context_ids):
+ context_node = node_by_id[context_id]
+ data = context_node.get("data") or {}
+ owner_id = str(data.get("agentNodeId") or "")
+ if (
+ data.get("systemManaged") is True
+ and owner_id in node_by_id
+ and _requires_context(str((node_by_id.get(owner_id) or {}).get("type") or ""))
+ and (context_id, owner_id) not in existing_pairs
+ ):
+ normalized_edges.append(
+ {
+ "id": _edge_id("context", context_id, owner_id),
+ "source": context_id,
+ "target": owner_id,
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ "data": {"systemManaged": True},
+ }
+ )
+ existing_contexts.setdefault(owner_id, []).append(context_id)
+ existing_pairs.add((context_id, owner_id))
+
+ occupied_ids = set(node_by_id)
+ for ordinal, agent_id in enumerate(sorted(required_agent_ids), start=1):
+ if existing_contexts.get(agent_id):
+ continue
+ temporary_id = f"__context__:{ordinal}:{agent_id}"
+ suffix = 1
+ while temporary_id in occupied_ids:
+ suffix += 1
+ temporary_id = f"__context__:{ordinal}:{agent_id}:{suffix}"
+ occupied_ids.add(temporary_id)
+ context_node = _context_node_for(node_by_id[agent_id], temporary_id)
+ normalized_nodes.append(context_node)
+ node_by_id[temporary_id] = context_node
+ context_ids.add(temporary_id)
+ existing_contexts[agent_id] = [temporary_id]
+ normalized_edges.append(
+ {
+ "id": _edge_id("context", temporary_id, agent_id),
+ "source": temporary_id,
+ "target": agent_id,
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ "data": {"systemManaged": True},
+ }
+ )
+
+ # Persist the ownership marker for backend edge repair/cascade semantics.
+ # It is UI metadata only; runtime state remains in AgentContextStore.
+ agents_for_context: Dict[str, List[str]] = {}
+ for agent_id, sources in existing_contexts.items():
+ for source_id in dict.fromkeys(sources):
+ agents_for_context.setdefault(source_id, []).append(agent_id)
+ for agent_id, sources in existing_contexts.items():
+ unique_sources = list(dict.fromkeys(sources))
+ if len(unique_sources) != 1:
+ continue
+ source_id = unique_sources[0]
+ if len(set(agents_for_context.get(source_id, []))) != 1:
+ continue
+ context_node = node_by_id.get(source_id)
+ if context_node is None:
+ continue
+ context_node["data"] = {
+ **dict(context_node.get("data") or {}),
+ "systemManaged": True,
+ "agentNodeId": agent_id,
+ }
+
+ # Cascade-delete an orphaned system companion when its owning agent is
+ # absent. User-authored/unowned Context nodes are left for validation.
+ claimed_context_ids = {source_id for sources in existing_contexts.values() for source_id in sources}
+ unclaimed_system_contexts = {
+ node_id
+ for node_id, node in node_by_id.items()
+ if node.get("type") == "context" and (node.get("data") or {}).get("systemManaged") is True and node_id not in claimed_context_ids
+ }
+ if unclaimed_system_contexts:
+ normalized_nodes = [node for node in normalized_nodes if str(node.get("id") or "") not in unclaimed_system_contexts]
+ normalized_edges = [
+ edge
+ for edge in normalized_edges
+ if str(edge.get("source") or "") not in unclaimed_system_contexts
+ and str(edge.get("target") or "") not in unclaimed_system_contexts
+ ]
+ for context_id in unclaimed_system_contexts:
+ params.pop(context_id, None)
+ warnings.append(f"Removed orphaned system Context {context_id!r}")
+
+ aliases: Dict[str, str] = {}
+ if canonicalize_ids and workflow_id:
+ from services.workflow_naming import canonicalize_node_ids
+
+ normalized_nodes, normalized_edges, aliases = canonicalize_node_ids(
+ str(workflow_id),
+ normalized_nodes,
+ normalized_edges,
+ )
+ params = {aliases.get(node_id, node_id): value for node_id, value in params.items()}
+
+ # Resolve agent -> Context after canonicalization so persistence can use
+ # stable IDs. Receipts are operation-id friendly and safe to replay.
+ context_for_agent: Dict[str, str] = {}
+ canonical_node_by_id = {str(node.get("id")): node for node in normalized_nodes if node.get("id") is not None}
+ for edge in normalized_edges:
+ source = str(edge.get("source") or "")
+ target = str(edge.get("target") or "")
+ if (
+ (canonical_node_by_id.get(source) or {}).get("type") == "context"
+ and _source_handle(edge) == "output-context"
+ and _target_handle(edge) == "input-context"
+ ):
+ context_for_agent[target] = source
+
+ state_imports: List[Dict[str, Any]] = []
+ for legacy_agent_id, memory_ids in sorted(legacy_by_agent.items()):
+ agent_id = aliases.get(legacy_agent_id, legacy_agent_id)
+ context_id = context_for_agent.get(agent_id)
+ if not context_id:
+ continue
+ for legacy_memory_id in dict.fromkeys(memory_ids):
+ memory_id = aliases.get(legacy_memory_id, legacy_memory_id)
+ legacy = params.get(memory_id, {})
+ markdown = legacy.get("memory_content")
+ bindings = {
+ key: legacy[key]
+ for key in (
+ "last_session_id",
+ "vertex_interaction_id",
+ "vertex_environment_id",
+ )
+ if legacy.get(key)
+ }
+ if markdown or bindings:
+ state_imports.append(
+ {
+ "operation_id": (f"legacy-context-import:{workflow_id}:{context_id}:{memory_id}"),
+ "workflow_id": str(workflow_id),
+ "context_node_id": context_id,
+ "agent_node_id": agent_id,
+ "legacy_memory_node_id": memory_id,
+ "event_type": "legacy_partial",
+ "markdown": str(markdown) if markdown else None,
+ "legacy_session_id": str(legacy.get("session_id") or agent_id),
+ "provider_bindings": bindings,
+ }
+ )
+ warnings.append(
+ f"Migrated legacy Simple Memory edge {memory_id!r} -> {agent_id!r}; process-local vector entries could not be imported"
+ )
+
+ return WorkflowGraphNormalization(
+ nodes=normalized_nodes,
+ edges=normalized_edges,
+ node_parameters=params,
+ warnings=warnings,
+ aliases=aliases,
+ state_imports=state_imports,
+ )
+
+
+__all__ = [
+ "WORKFLOW_GRAPH_VERSION",
+ "WorkflowGraphNormalization",
+ "normalize_edge_handles",
+ "normalize_legacy_android_toolkit",
+ "normalize_workflow_graph",
+]
diff --git a/server/services/workflow_naming.py b/server/services/workflow_naming.py
index 6443bc6c..5fe47273 100644
--- a/server/services/workflow_naming.py
+++ b/server/services/workflow_naming.py
@@ -171,6 +171,16 @@ def canonicalize_node_ids(
if isinstance(value, str) and value in mapping:
item[key] = mapping[value]
normalized_edges.append(item)
+ # System-managed companions retain their owner link as UI metadata so the
+ # backend can repair a deleted required edge. It is topology identity and
+ # must follow the same alias map as edge endpoints.
+ for node in normalized_nodes:
+ data = node.get("data")
+ if not isinstance(data, dict):
+ continue
+ owner_id = data.get("agentNodeId")
+ if isinstance(owner_id, str) and owner_id in mapping:
+ node["data"] = {**data, "agentNodeId": mapping[owner_id]}
return normalized_nodes, normalized_edges, mapping
diff --git a/server/services/workflow_sanitizer.py b/server/services/workflow_sanitizer.py
new file mode 100644
index 00000000..d96ad2f3
--- /dev/null
+++ b/server/services/workflow_sanitizer.py
@@ -0,0 +1,236 @@
+"""Authoritative server-side redaction for workflow graph surfaces.
+
+Context and Memory contents live in normalized backend tables. This sanitizer
+is defense in depth for legacy/imported graphs and for any future export/log
+path that receives an untrusted workflow-shaped mapping.
+"""
+
+from __future__ import annotations
+
+import re
+from copy import deepcopy
+from typing import Any, Mapping
+
+
+def _key(value: Any) -> str:
+ return re.sub(r"[^a-z0-9]", "", str(value).lower())
+
+
+_BLOCKED_EXACT = frozenset(
+ {
+ "memorycontent",
+ "memoryjsonl",
+ "lastsessionid",
+ "vertexinteractionid",
+ "vertexenvironmentid",
+ "contextjournal",
+ "contextevents",
+ "contextcheckpoint",
+ "contextcheckpoints",
+ "providerbinding",
+ "providerbindings",
+ "providerstate",
+ "providersession",
+ "rawcontext",
+ "rawjournal",
+ "rawtranscript",
+ "activereplay",
+ "replaystate",
+ "replaypayload",
+ "checkpointplustail",
+ "memoryitems",
+ "recalledmemories",
+ "recalledsecrets",
+ "thoughtsignature",
+ "reasoningsignature",
+ "encryptedreasoning",
+ "encryptedcontent",
+ }
+)
+
+_BLOCKED_CONTEXT_SUFFIXES = (
+ "providerstate",
+ "replaypayload",
+ "journalpayload",
+ "bindingpayload",
+)
+
+_SENSITIVE_EXACT = frozenset(
+ {
+ "apikey",
+ "accesstoken",
+ "refreshtoken",
+ "secret",
+ "password",
+ "passwd",
+ "clientid",
+ "clientsecret",
+ "token",
+ "bearertoken",
+ "privatekey",
+ "encryptionkey",
+ "oauthtoken",
+ "authtoken",
+ }
+)
+
+_SAFE_TOKEN_FIELDS = frozenset(
+ {
+ "maxtokens",
+ "budgettokens",
+ "pagetoken",
+ "nextpagetoken",
+ "tokencount",
+ "totaltokens",
+ "inputtokens",
+ "outputtokens",
+ }
+)
+
+_SENSITIVE_PARTS = (
+ "apikey",
+ "secret",
+ "password",
+ "privatekey",
+ "accesstoken",
+ "refreshtoken",
+ "bearertoken",
+ "oauthtoken",
+ "authtoken",
+)
+
+# React Flow graph persistence is intentionally narrower than its runtime node
+# objects. Selection, measurements, execution status, outputs, and parameters
+# are maintained elsewhere and must not be copied into workflow JSON.
+_NODE_FIELDS = frozenset(
+ {
+ "id",
+ "type",
+ "position",
+ "parentId",
+ "parentNode",
+ "extent",
+ "expandParent",
+ "hidden",
+ "draggable",
+ "selectable",
+ "connectable",
+ "deletable",
+ "focusable",
+ "dragHandle",
+ "className",
+ "sourcePosition",
+ "targetPosition",
+ "zIndex",
+ "origin",
+ }
+)
+
+_COMMON_NODE_DATA_FIELDS = frozenset({"label", "disabled", "condition"})
+_CONTEXT_NODE_DATA_FIELDS = frozenset({"label", "disabled", "systemManaged", "agentNodeId"})
+
+_EDGE_FIELDS = frozenset(
+ {
+ "id",
+ "source",
+ "target",
+ "sourceHandle",
+ "targetHandle",
+ "source_handle",
+ "target_handle",
+ "type",
+ "animated",
+ "hidden",
+ "selectable",
+ "deletable",
+ "focusable",
+ "updatable",
+ "label",
+ "zIndex",
+ }
+)
+_EDGE_DATA_FIELDS = frozenset({"label", "condition", "conditionLogic", "conditions", "systemManaged"})
+
+
+def _blocked_key(normalized: str) -> bool:
+ if normalized in _BLOCKED_EXACT:
+ return True
+ if normalized.endswith(_BLOCKED_CONTEXT_SUFFIXES):
+ return True
+ if normalized in _SAFE_TOKEN_FIELDS:
+ return False
+ if normalized in _SENSITIVE_EXACT:
+ return True
+ if any(part in normalized for part in _SENSITIVE_PARTS):
+ return True
+ return normalized.endswith("token")
+
+
+def sanitize_runtime_payload(value: Any) -> Any:
+ """Deep-copy JSON-like data while dropping runtime-only secret fields."""
+
+ if isinstance(value, Mapping):
+ cleaned: dict[str, Any] = {}
+ for raw_key, raw_value in value.items():
+ normalized = _key(raw_key)
+ if _blocked_key(normalized):
+ continue
+ cleaned[str(raw_key)] = sanitize_runtime_payload(raw_value)
+ return cleaned
+ if isinstance(value, list):
+ return [sanitize_runtime_payload(item) for item in value]
+ if isinstance(value, tuple):
+ return [sanitize_runtime_payload(item) for item in value]
+ return deepcopy(value)
+
+
+def _sanitize_node(node: Mapping[str, Any]) -> dict[str, Any]:
+ cleaned = {key: sanitize_runtime_payload(node[key]) for key in sorted(_NODE_FIELDS) if key in node}
+ position = node.get("position")
+ if isinstance(position, Mapping):
+ cleaned["position"] = {key: deepcopy(position[key]) for key in ("x", "y") if key in position}
+
+ raw_data = node.get("data")
+ allowed_data = _CONTEXT_NODE_DATA_FIELDS if node.get("type") == "context" else _COMMON_NODE_DATA_FIELDS
+ if isinstance(raw_data, Mapping):
+ cleaned["data"] = {key: sanitize_runtime_payload(raw_data[key]) for key in sorted(allowed_data) if key in raw_data}
+ else:
+ cleaned["data"] = {}
+ return cleaned
+
+
+def _sanitize_edge(edge: Mapping[str, Any]) -> dict[str, Any]:
+ cleaned = {key: sanitize_runtime_payload(edge[key]) for key in sorted(_EDGE_FIELDS) if key in edge}
+ raw_data = edge.get("data")
+ if isinstance(raw_data, Mapping):
+ cleaned["data"] = {key: sanitize_runtime_payload(raw_data[key]) for key in sorted(_EDGE_DATA_FIELDS) if key in raw_data}
+ return cleaned
+
+
+def sanitize_workflow_graph(data: Mapping[str, Any]) -> dict[str, Any]:
+ """Return an export/response-safe workflow graph.
+
+ Workflow JSON has a deliberately small schema: graph version, React Flow
+ topology, and UI-only node/edge metadata. Context policy lives in the
+ node-parameter table, while journals, replay payloads, provider bindings,
+ Memory items, outputs, and credentials live in their dedicated stores.
+ """
+
+ raw_nodes = data.get("nodes")
+ raw_edges = data.get("edges")
+ cleaned: dict[str, Any] = {
+ "nodes": [_sanitize_node(node) for node in raw_nodes if isinstance(node, Mapping)] if isinstance(raw_nodes, list) else [],
+ "edges": [_sanitize_edge(edge) for edge in raw_edges if isinstance(edge, Mapping)] if isinstance(raw_edges, list) else [],
+ }
+ if "graphVersion" in data:
+ cleaned["graphVersion"] = deepcopy(data["graphVersion"])
+ owner_id = data.get("owner_id")
+ if isinstance(owner_id, str) and owner_id:
+ # Injected by authenticated backend boundaries; required to scope
+ # durable Memory namespaces without placing any Memory data in graph
+ # JSON.
+ cleaned["owner_id"] = owner_id
+ return cleaned
+
+
+__all__ = ["sanitize_runtime_payload", "sanitize_workflow_graph"]
diff --git a/server/services/workflow_storage/handlers.py b/server/services/workflow_storage/handlers.py
index 1e2cd77e..52e1e45b 100644
--- a/server/services/workflow_storage/handlers.py
+++ b/server/services/workflow_storage/handlers.py
@@ -20,6 +20,318 @@ from services.ws_handler_registry import ws_handler
logger = get_logger(__name__)
+_CONTEXT_TOPOLOGY_ERROR_CODES = frozenset(
+ {
+ "INVALID_CONTEXT_EDGE",
+ "MISSING_CONTEXT",
+ "MULTIPLE_CONTEXTS",
+ "SHARED_CONTEXT",
+ }
+)
+
+
+class _AssumeCredentialsPresent:
+ """Avoid credential lookups while validating graph-only invariants."""
+
+ async def has_valid_key(self, provider_id: str) -> bool:
+ return True
+
+
+async def _context_topology_errors(
+ nodes: list[Dict[str, Any]],
+ edges: list[Dict[str, Any]],
+ parameters_by_id: Dict[str, Dict[str, Any]],
+) -> list[Dict[str, Any]]:
+ """Run the shared validator and retain only Context V2 invariants."""
+
+ from services.workflow_validator import validate_workflow
+
+ report = await validate_workflow(
+ nodes,
+ edges,
+ parameters_by_id=parameters_by_id,
+ auth_service=_AssumeCredentialsPresent(),
+ enforce_context=True,
+ )
+ return [issue for issue in report.get("errors", []) if issue.get("code") in _CONTEXT_TOPOLOGY_ERROR_CODES]
+
+
+def _trusted_owner_id(websocket: WebSocket, existing: Any) -> str:
+ """Resolve workflow ownership without trusting client graph fields."""
+
+ state = getattr(websocket, "state", None)
+ authenticated = getattr(state, "user_id", None)
+ if authenticated is not None and str(authenticated).strip():
+ return str(authenticated).strip()
+ existing_data = getattr(existing, "data", None)
+ if isinstance(existing_data, dict):
+ stored = existing_data.get("owner_id")
+ if stored is not None and str(stored).strip():
+ return str(stored).strip()
+ return "owner"
+
+
+def _reconcile_context_submission(
+ workflow_data: Dict[str, Any],
+ existing: Any,
+) -> tuple[list[Dict[str, Any]], list[Dict[str, Any]], list[str]]:
+ """Restore backend-owned Context companions before normalization.
+
+ Client graph data may preserve ownership metadata for round-tripping, but
+ it is never authoritative. Existing companions survive while their owner
+ agent remains, and client-created/replacement Context nodes are discarded
+ so normalization creates fresh backend companions where needed.
+ """
+
+ submitted_nodes = [
+ dict(node)
+ for node in workflow_data.get("nodes") or []
+ if isinstance(node, dict)
+ ]
+ submitted_edges = [
+ dict(edge)
+ for edge in workflow_data.get("edges") or []
+ if isinstance(edge, dict)
+ ]
+ existing_graph = getattr(existing, "data", None)
+ if not isinstance(existing_graph, dict):
+ existing_graph = {}
+ stored_nodes = {
+ str(node.get("id")): dict(node)
+ for node in existing_graph.get("nodes") or []
+ if isinstance(node, dict) and node.get("id")
+ }
+ stored_contexts = {
+ node_id: node
+ for node_id, node in stored_nodes.items()
+ if node.get("type") == "context"
+ }
+ submitted_context_targets: Dict[str, set[str]] = {}
+ for edge in submitted_edges:
+ source = str(edge.get("source") or "")
+ target = str(edge.get("target") or "")
+ source_handle = edge.get("sourceHandle") or edge.get(
+ "source_handle"
+ )
+ target_handle = edge.get("targetHandle") or edge.get(
+ "target_handle"
+ )
+ if (
+ source
+ and target
+ and source_handle == "output-context"
+ and target_handle == "input-context"
+ ):
+ submitted_context_targets.setdefault(source, set()).add(
+ target
+ )
+
+ edge_owners: Dict[str, set[str]] = {}
+ for edge in existing_graph.get("edges") or []:
+ if not isinstance(edge, dict):
+ continue
+ source = str(edge.get("source") or "")
+ target = str(edge.get("target") or "")
+ source_handle = edge.get("sourceHandle") or edge.get(
+ "source_handle"
+ )
+ target_handle = edge.get("targetHandle") or edge.get(
+ "target_handle"
+ )
+ if (
+ source in stored_contexts
+ and source_handle == "output-context"
+ and target_handle == "input-context"
+ and target
+ ):
+ edge_owners.setdefault(source, set()).add(target)
+
+ protected_owners: Dict[str, str] = {}
+ for context_id, node in stored_contexts.items():
+ data = dict(node.get("data") or {})
+ declared = str(data.get("agentNodeId") or "")
+ if (
+ data.get("systemManaged") is True
+ and declared in stored_nodes
+ ):
+ protected_owners[context_id] = declared
+ continue
+ inferred = sorted(edge_owners.get(context_id, set()))
+ if len(inferred) == 1:
+ protected_owners[context_id] = inferred[0]
+
+ submitted_owner_ids = {
+ str(node.get("id"))
+ for node in submitted_nodes
+ if node.get("id")
+ and node.get("type") != "context"
+ and str(node.get("id")) not in stored_contexts
+ }
+ retained_context_ids = {
+ context_id
+ for context_id, owner_id in protected_owners.items()
+ if owner_id in submitted_owner_ids
+ }
+ warnings: list[str] = []
+ reconciled_nodes: list[Dict[str, Any]] = []
+ seen_context_ids: set[str] = set()
+ rejected_context_ids: set[str] = set()
+ for node in submitted_nodes:
+ node_id = str(node.get("id") or "")
+ is_context_submission = (
+ node.get("type") == "context"
+ or node_id in stored_contexts
+ )
+ if not is_context_submission:
+ reconciled_nodes.append(node)
+ continue
+ if node_id not in stored_contexts and not stored_contexts:
+ # A new/legacy graph has no protected companion to replace.
+ # Ignore client ownership claims and derive ownership only from
+ # an unambiguous submitted topology. Ambiguous topology is left
+ # intact so the shared validator rejects it.
+ submitted_data = dict(node.get("data") or {})
+ submitted_data.pop("systemManaged", None)
+ submitted_data.pop("agentNodeId", None)
+ targets = sorted(
+ submitted_context_targets.get(node_id, set())
+ )
+ if len(targets) == 1:
+ submitted_data["systemManaged"] = True
+ submitted_data["agentNodeId"] = targets[0]
+ reconciled_nodes.append(
+ {
+ **node,
+ "type": "context",
+ "data": submitted_data,
+ }
+ )
+ continue
+ if node_id not in retained_context_ids:
+ if node_id:
+ rejected_context_ids.add(node_id)
+ warnings.append(
+ f"Ignored untrusted Context companion {node_id!r}"
+ )
+ continue
+
+ stored = stored_contexts[node_id]
+ owner_id = protected_owners[node_id]
+ submitted_data = dict(node.get("data") or {})
+ stored_data = dict(stored.get("data") or {})
+ item = {
+ **stored,
+ **node,
+ "id": node_id,
+ "type": "context",
+ "data": {
+ **stored_data,
+ **{
+ key: submitted_data[key]
+ for key in ("label", "disabled")
+ if key in submitted_data
+ },
+ "systemManaged": True,
+ "agentNodeId": owner_id,
+ },
+ }
+ reconciled_nodes.append(item)
+ seen_context_ids.add(node_id)
+
+ for context_id in sorted(retained_context_ids - seen_context_ids):
+ stored = stored_contexts[context_id]
+ owner_id = protected_owners[context_id]
+ reconciled_nodes.append(
+ {
+ **stored,
+ "type": "context",
+ "data": {
+ **dict(stored.get("data") or {}),
+ "systemManaged": True,
+ "agentNodeId": owner_id,
+ },
+ }
+ )
+ warnings.append(
+ f"Restored protected Context companion {context_id!r}"
+ )
+
+ reconciled_edges = [
+ edge
+ for edge in submitted_edges
+ if str(edge.get("source") or "") not in rejected_context_ids
+ and str(edge.get("target") or "") not in rejected_context_ids
+ ]
+ return reconciled_nodes, reconciled_edges, warnings
+
+
+def _supports_context_archive_outbox(database: Any) -> bool:
+ return callable(
+ getattr(
+ database,
+ "list_workflow_context_archive_outbox",
+ None,
+ )
+ ) and callable(
+ getattr(
+ database,
+ "complete_workflow_context_archive_outbox",
+ None,
+ )
+ )
+
+
+async def _drain_context_archive_outbox(
+ database: Any,
+ workflow_id: str,
+) -> tuple[int, int]:
+ """Archive pending Context identities and acknowledge each durably."""
+
+ if not _supports_context_archive_outbox(database):
+ return 0, 0
+ pending = await database.list_workflow_context_archive_outbox(workflow_id)
+ if not pending:
+ return 0, 0
+
+ from services.agent_context import AgentContextStore
+
+ store = AgentContextStore(database)
+ completed = 0
+ for receipt in pending:
+ outbox_id = str(receipt.get("id") or "")
+ context_node_id = str(receipt.get("context_node_id") or "")
+ if not outbox_id or not context_node_id:
+ logger.error(
+ "[workflow] invalid Context archive outbox identity for workflow %s",
+ workflow_id,
+ )
+ continue
+ try:
+ await store.archive_context(
+ workflow_id=workflow_id,
+ context_node_id=context_node_id,
+ generation=None,
+ operation_id=f"context-archive-outbox:{outbox_id}",
+ )
+ acknowledged = await database.complete_workflow_context_archive_outbox(
+ outbox_id=outbox_id,
+ workflow_id=workflow_id,
+ )
+ if acknowledged:
+ completed += 1
+ except Exception:
+ # The graph mutation is already durable. Leave the receipt pending
+ # so a later save/get/delete boundary safely retries it.
+ logger.exception(
+ "[workflow] Context archive outbox drain failed (workflow=%s context=%s receipt=%s)",
+ workflow_id,
+ context_node_id,
+ outbox_id,
+ )
+
+ remaining = await database.list_workflow_context_archive_outbox(workflow_id)
+ return completed, len(remaining)
+
def _move_workspace(old_slug: str, new_slug: str) -> None:
"""Rename ``<workspace_base>/<old_slug>/`` on disk. No-op when the
@@ -62,12 +374,16 @@ async def handle_save_workflow(data: Dict[str, Any], websocket: WebSocket) -> Di
``updateWorkflow({name})`` -> debounced save) flows through this
handler, so renaming happens here — no dedicated rename endpoint.
"""
- from services.workflow_naming import canonicalize_node_ids, next_available_slug
+ from services.workflow_naming import next_available_slug
database = container.database()
requested_id = str(data.get("workflow_id") or "").strip()
is_new_marker = requested_id.lower() in {"", "new"}
- existing = await database.get_workflow(requested_id) if requested_id and not is_new_marker else None
+ if requested_id and not is_new_marker:
+ await _drain_context_archive_outbox(database, requested_id)
+ existing = await database.get_workflow(requested_id)
+ else:
+ existing = None
if is_new_marker:
workflow_id = await database.allocate_workflow_id()
elif existing is None:
@@ -83,25 +399,96 @@ async def handle_save_workflow(data: Dict[str, Any], websocket: WebSocket) -> Di
slug = await next_available_slug(name, database, exclude_id=workflow_id)
workflow_data = data.get("data", {})
- from services.workflow_migrations import normalize_legacy_android_toolkit
+ from services.workflow_context_migration import (
+ archive_removed_contexts,
+ import_legacy_context_receipts,
+ load_node_parameters,
+ persist_parameter_aliases,
+ )
+ from services.workflow_migrations import normalize_workflow_graph
- nodes, edges, _, migration_warnings = normalize_legacy_android_toolkit(
- workflow_data.get("nodes") or [], workflow_data.get("edges") or []
+ source_nodes, source_edges, ownership_warnings = (
+ _reconcile_context_submission(
+ workflow_data,
+ existing,
+ )
)
- # New workflows use canonical IDs from their first persistence boundary.
- # Existing legacy workflows are deliberately left byte-for-byte compatible;
- # there is no startup or save-time migration.
- if is_new_marker:
- nodes, edges, node_id_aliases = canonicalize_node_ids(workflow_id, nodes, edges)
- else:
- node_id_aliases = {}
- normalized_data = {**workflow_data, "nodes": nodes, "edges": edges}
+ source_params = await load_node_parameters(database, source_nodes)
+ normalization = normalize_workflow_graph(
+ workflow_id,
+ source_nodes,
+ source_edges,
+ source_params,
+ )
+ migration_warnings = [
+ *ownership_warnings,
+ *normalization.warnings,
+ ]
+ context_errors = await _context_topology_errors(
+ normalization.nodes,
+ normalization.edges,
+ normalization.node_parameters,
+ )
+ from services.workflow_sanitizer import sanitize_workflow_graph
+
+ normalized_graph = normalization.graph_data(workflow_data)
+ normalized_graph["owner_id"] = _trusted_owner_id(websocket, existing)
+ normalized_data = sanitize_workflow_graph(normalized_graph)
+ if context_errors:
+ return {
+ "success": False,
+ "error": "invalid_context_topology",
+ "workflow_id": workflow_id,
+ "validation_errors": context_errors,
+ "migration_warnings": migration_warnings,
+ "node_id_aliases": normalization.aliases,
+ "data": normalized_data,
+ }
+
+ # Import exact legacy artifacts before replacing their only topology
+ # pointer. Receipts are idempotent, so a failed graph save is safe to retry.
+ imported = await import_legacy_context_receipts(
+ database,
+ normalization.state_imports,
+ )
+ save_kwargs: Dict[str, Any] = {
+ "workflow_id": storage_id,
+ "name": name,
+ "slug": slug,
+ "data": normalized_data,
+ }
+ if _supports_context_archive_outbox(database):
+ save_kwargs["context_id_aliases"] = normalization.aliases
success = await database.save_workflow(
- workflow_id=storage_id,
- name=name,
- slug=slug,
- data=normalized_data,
+ **save_kwargs,
)
+ if success:
+ await persist_parameter_aliases(
+ database,
+ aliases=normalization.aliases,
+ parameters=normalization.node_parameters,
+ context_import_completed=(not normalization.state_imports or imported == len(normalization.state_imports)),
+ )
+ if _supports_context_archive_outbox(database):
+ context_archives_completed, context_archives_pending = await _drain_context_archive_outbox(
+ database,
+ workflow_id,
+ )
+ else:
+ # Compatibility for isolated test doubles and older storage
+ # implementations. Production Database commits the outbox in the
+ # same transaction as the graph.
+ context_archives_completed = await archive_removed_contexts(
+ database,
+ workflow_id=workflow_id,
+ previous_nodes=((existing.data or {}).get("nodes") or [] if existing is not None else []),
+ normalized_nodes=normalization.nodes,
+ aliases=normalization.aliases,
+ )
+ context_archives_pending = 0
+ else:
+ context_archives_completed = 0
+ context_archives_pending = 0
if existing and existing.slug and existing.slug != slug:
_move_workspace(existing.slug, slug)
@@ -113,7 +500,13 @@ async def handle_save_workflow(data: Dict[str, Any], websocket: WebSocket) -> Di
"name": name,
"slug": slug,
"migration_warnings": migration_warnings,
- "node_id_aliases": node_id_aliases,
+ "node_id_aliases": normalization.aliases,
+ "context_archives_completed": context_archives_completed,
+ "context_archives_pending": context_archives_pending,
+ # The server owns topology normalization. The editor replaces its
+ # local draft with this authoritative graph instead of reimplementing
+ # Context lifecycle rules.
+ "data": normalized_data,
}
@@ -154,41 +547,100 @@ async def handle_import_workflow(data: Dict[str, Any], websocket: WebSocket) ->
async def handle_get_workflow(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
"""Get workflow by ID."""
database = container.database()
- workflow = await database.get_workflow(data["workflow_id"])
+ workflow_id = str(data["workflow_id"])
+ recovered_archives, pending_archives = await _drain_context_archive_outbox(database, workflow_id)
+ workflow = await database.get_workflow(workflow_id)
if workflow:
- from services.workflow_migrations import normalize_legacy_android_toolkit
-
workflow_data = workflow.data or {}
- nodes, edges, _, migration_warnings = normalize_legacy_android_toolkit(
- workflow_data.get("nodes") or [], workflow_data.get("edges") or []
- )
- if nodes != (workflow_data.get("nodes") or []) or edges != (workflow_data.get("edges") or []):
- normalized_data = {**workflow_data, "nodes": nodes, "edges": edges}
- await database.save_workflow(
- workflow_id=workflow.id,
- name=workflow.name,
- slug=workflow.slug,
- description=getattr(workflow, "description", None),
- data=normalized_data,
+ from services.workflow_context_migration import (
+ archive_removed_contexts,
+ import_legacy_context_receipts,
+ load_node_parameters,
+ persist_parameter_aliases,
+ )
+ from services.workflow_migrations import normalize_workflow_graph
+
+ source_nodes = workflow_data.get("nodes") or []
+ source_params = await load_node_parameters(database, source_nodes)
+ normalization = normalize_workflow_graph(
+ workflow.id,
+ source_nodes,
+ workflow_data.get("edges") or [],
+ source_params,
+ )
+ context_errors = await _context_topology_errors(
+ normalization.nodes,
+ normalization.edges,
+ normalization.node_parameters,
+ )
+ from services.workflow_sanitizer import sanitize_workflow_graph
+
+ normalized_data = sanitize_workflow_graph(normalization.graph_data(workflow_data))
+ if not context_errors:
+ imported = await import_legacy_context_receipts(
+ database,
+ normalization.state_imports,
)
- legacy_ids = {
- node.get("id")
- for node in workflow_data.get("nodes", [])
- if node.get("type") == "androidTool" and node.get("id")
- }
- for legacy_id in legacy_ids:
- await database.delete_node_parameters(legacy_id)
+ normalization_persisted = True
+ if normalized_data != workflow_data:
+ save_kwargs = {
+ "workflow_id": workflow.id,
+ "name": workflow.name,
+ "slug": workflow.slug,
+ "description": getattr(
+ workflow,
+ "description",
+ None,
+ ),
+ "data": normalized_data,
+ }
+ if _supports_context_archive_outbox(database):
+ save_kwargs["context_id_aliases"] = (
+ normalization.aliases
+ )
+ normalization_persisted = (
+ await database.save_workflow(**save_kwargs)
+ )
+ if normalization_persisted and _supports_context_archive_outbox(database):
+ drained, pending_archives = await _drain_context_archive_outbox(
+ database,
+ workflow.id,
+ )
+ recovered_archives += drained
+ elif normalization_persisted:
+ recovered_archives += await archive_removed_contexts(
+ database,
+ workflow_id=workflow.id,
+ previous_nodes=source_nodes,
+ normalized_nodes=normalization.nodes,
+ aliases=normalization.aliases,
+ )
+ else:
+ # Keep the response aligned with the authoritative row.
+ # A later read retries normalization and outbox creation.
+ normalized_data = sanitize_workflow_graph(workflow_data)
+ if normalization_persisted:
+ await persist_parameter_aliases(
+ database,
+ aliases=normalization.aliases,
+ parameters=normalization.node_parameters,
+ context_import_completed=(not normalization.state_imports or imported == len(normalization.state_imports)),
+ )
return {
"success": True,
"workflow": {
"id": workflow.id,
"name": workflow.name,
"slug": workflow.slug,
- "data": {**workflow_data, "nodes": nodes, "edges": edges},
+ "data": normalized_data,
"created_at": workflow.created_at.isoformat() if workflow.created_at else None,
"updated_at": workflow.updated_at.isoformat() if workflow.updated_at else None,
},
- "migration_warnings": migration_warnings,
+ "migration_warnings": normalization.warnings,
+ "node_id_aliases": normalization.aliases,
+ "context_validation_errors": context_errors,
+ "context_archives_completed": recovered_archives,
+ "context_archives_pending": pending_archives,
}
return {"success": False, "error": "Workflow not found"}
@@ -213,11 +665,76 @@ async def handle_get_all_workflows(data: Dict[str, Any], websocket: WebSocket) -
}
+async def _archive_workflow_contexts(database: Any, workflow_id: str) -> int:
+ """Fence every persisted Context before its workflow graph is deleted."""
+
+ workflow = await database.get_workflow(workflow_id)
+ graph = getattr(workflow, "data", None)
+ if not isinstance(graph, dict):
+ return 0
+ context_ids = sorted(
+ {
+ str(node.get("id"))
+ for node in graph.get("nodes") or []
+ if isinstance(node, dict) and node.get("type") == "context" and node.get("id")
+ }
+ )
+ if not context_ids:
+ return 0
+
+ from services.agent_context import AgentContextStore
+
+ store = AgentContextStore(database)
+ for context_id in context_ids:
+ await store.archive_context(
+ workflow_id=workflow_id,
+ context_node_id=context_id,
+ generation=None,
+ operation_id=f"workflow-deleted:{workflow_id}:{context_id}",
+ )
+ return len(context_ids)
+
+
+async def delete_workflow_with_context_archival(
+ database: Any,
+ workflow_id: str,
+) -> Dict[str, Any]:
+ """Delete a workflow through the durable Context lifecycle boundary."""
+
+ if _supports_context_archive_outbox(database):
+ # Production Database commits graph deletion and archive identities in
+ # one transaction. A failed delete therefore cannot fence a Context
+ # that is still referenced by the workflow.
+ success = await database.delete_workflow(workflow_id)
+ completed, pending = await _drain_context_archive_outbox(
+ database,
+ workflow_id,
+ )
+ else:
+ # Compatibility for isolated test doubles. Production never takes
+ # this archive-before-delete path.
+ completed = await _archive_workflow_contexts(
+ database,
+ workflow_id,
+ )
+ success = await database.delete_workflow(workflow_id)
+ pending = 0
+ return {
+ "success": success,
+ "workflow_id": workflow_id,
+ "contexts_archived": completed,
+ "context_archives_pending": pending,
+ }
+
+
async def handle_delete_workflow(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
- """Delete workflow."""
+ """Archive durable Context threads, then delete their workflow graph."""
database = container.database()
- success = await database.delete_workflow(data["workflow_id"])
- return {"success": success, "workflow_id": data["workflow_id"]}
+ workflow_id = str(data["workflow_id"])
+ return await delete_workflow_with_context_archival(
+ database,
+ workflow_id,
+ )
WS_HANDLERS: Dict[str, Any] = {
@@ -231,6 +748,7 @@ WS_HANDLERS: Dict[str, Any] = {
__all__ = [
"WS_HANDLERS",
+ "delete_workflow_with_context_archival",
"handle_delete_workflow",
"handle_get_all_workflows",
"handle_get_workflow",
diff --git a/server/services/workflow_validator.py b/server/services/workflow_validator.py
index 7e824f56..f5dfde23 100644
--- a/server/services/workflow_validator.py
+++ b/server/services/workflow_validator.py
@@ -15,7 +15,7 @@ Issue shape (flat dict):
{
"code": "CYCLE" | "DANGLING_EDGE" | "UNKNOWN_NODE_TYPE"
- | "INVALID_PARAM" | "MISSING_CREDENTIAL",
+ | "INVALID_PARAM" | "MISSING_CREDENTIAL" | Context topology codes,
"node_id": str | None, # node where the issue surfaces, if applicable
"message": str, # user-facing description
# plus code-specific extras:
@@ -54,6 +54,7 @@ async def validate_workflow(
*,
parameters_by_id: Optional[Dict[str, Dict[str, Any]]] = None,
auth_service: Any = None,
+ enforce_context: bool = True,
) -> Dict[str, List[Dict[str, Any]]]:
"""Validate a workflow graph + per-node params + credential presence.
@@ -80,6 +81,17 @@ async def validate_workflow(
node_by_id: Dict[str, Dict[str, Any]] = {n["id"]: n for n in nodes if n.get("id")}
+ def requires_context(node_id: str) -> bool:
+ if not enforce_context:
+ return False
+ node = node_by_id.get(node_id) or {}
+ node_cls = get_node_class(str(node.get("type") or ""))
+ if node_cls is None:
+ return False
+ if bool(getattr(node_cls, "requires_context", False)):
+ return True
+ return bool((getattr(node_cls, "ui_hints", {}) or {}).get("requiresContext"))
+
# 1. Dangling edges — references to non-existent nodes. Errors.
for e in edges:
src = e.get("source")
@@ -101,6 +113,93 @@ async def validate_workflow(
}
)
+ # Context V2 topology. These checks are capability-driven: renderer
+ # component kinds and legacy AI_AGENT_TYPES are intentionally not used.
+ valid_contexts_by_agent: Dict[str, List[str]] = {}
+ agents_by_context: Dict[str, List[str]] = {}
+ for edge in edges:
+ source_id = str(edge.get("source") or "")
+ target_id = str(edge.get("target") or "")
+ source_node = node_by_id.get(source_id) or {}
+ source_handle = edge.get("sourceHandle") or edge.get("source_handle")
+ target_handle = edge.get("targetHandle") or edge.get("target_handle")
+ source_is_context = source_node.get("type") == "context"
+ touches_context_handle = (
+ source_handle == "output-context" or target_handle == "input-context"
+ )
+
+ if (
+ source_node.get("type") == "simpleMemory"
+ and target_handle == "input-memory"
+ ):
+ warnings.append(
+ {
+ "code": "LEGACY_MEMORY_EDGE",
+ "node_id": target_id or None,
+ "message": (
+ "Legacy input-memory continuity must be normalized to "
+ "a Context edge and an ordinary Memory tool edge"
+ ),
+ }
+ )
+
+ if not (source_is_context or touches_context_handle):
+ continue
+ valid = (
+ source_is_context
+ and source_handle == "output-context"
+ and target_handle == "input-context"
+ and requires_context(target_id)
+ )
+ if not valid:
+ errors.append(
+ {
+ "code": "INVALID_CONTEXT_EDGE",
+ "node_id": target_id or source_id or None,
+ "message": (
+ "Context edges must connect context.output-context "
+ "to a node declaring requiresContext at input-context"
+ ),
+ }
+ )
+ continue
+ valid_contexts_by_agent.setdefault(target_id, []).append(source_id)
+ agents_by_context.setdefault(source_id, []).append(target_id)
+
+ for node_id in sorted(node_by_id):
+ if not requires_context(node_id):
+ continue
+ context_ids = list(dict.fromkeys(valid_contexts_by_agent.get(node_id, [])))
+ if not context_ids:
+ errors.append(
+ {
+ "code": "MISSING_CONTEXT",
+ "node_id": node_id,
+ "message": "This agent requires exactly one Context node",
+ }
+ )
+ elif len(context_ids) > 1:
+ errors.append(
+ {
+ "code": "MULTIPLE_CONTEXTS",
+ "node_id": node_id,
+ "contexts": sorted(context_ids),
+ "message": "An agent cannot be connected to multiple Context nodes",
+ }
+ )
+
+ for context_id, agent_ids in sorted(agents_by_context.items()):
+ unique_agents = list(dict.fromkeys(agent_ids))
+ if len(unique_agents) > 1:
+ errors.append(
+ {
+ "code": "SHARED_CONTEXT",
+ "node_id": context_id,
+ "agents": sorted(unique_agents),
+ "message": "A Context node cannot serve multiple agents",
+ }
+ )
+
# Team topology is validated independently from ordinary execution edges.
# ReactFlow stores teammate edges as worker -> lead, while delegation runs
# lead -> worker, so cycle/depth analysis below reverses them.
diff --git a/server/tests/llm/test_providers.py b/server/tests/llm/test_providers.py
index f226a466..1c7fbd8e 100644
--- a/server/tests/llm/test_providers.py
+++ b/server/tests/llm/test_providers.py
@@ -16,6 +16,33 @@ from services.llm.protocol import Message, ThinkingConfig, ToolCall, ToolDef
# ---------------------------------------------------------------------------
+def _stream_returning(response):
+ """Stand-in for ``client.messages.stream``.
+
+ The real call is a plain method returning an async context manager whose
+ ``get_final_message()`` yields the accumulated ``Message``.
+ """
+ stream = MagicMock()
+ stream.get_final_message = AsyncMock(return_value=response)
+ ctx = MagicMock()
+ ctx.__aenter__ = AsyncMock(return_value=stream)
+ ctx.__aexit__ = AsyncMock(return_value=False)
+ return MagicMock(return_value=ctx)
+
+
+def _text_response(text: str = "ok"):
+ resp = MagicMock()
+ resp.content = [MagicMock(type="text", text=text)]
+ resp.usage = MagicMock(
+ input_tokens=5,
+ output_tokens=2,
+ cache_creation_input_tokens=0,
+ cache_read_input_tokens=0,
+ )
+ resp.stop_reason = "end_turn"
+ return resp
+
+
class TestAnthropicProvider:
@pytest.fixture
def provider(self):
@@ -73,11 +100,7 @@ class TestAnthropicProvider:
@pytest.mark.asyncio
async def test_chat_sets_thinking_params(self, provider):
"""When thinking enabled, budget_tokens and temperature=1 are set."""
- mock_resp = MagicMock()
- mock_resp.content = [MagicMock(type="text", text="ok")]
- mock_resp.usage = MagicMock(input_tokens=5, output_tokens=2, cache_creation_input_tokens=0, cache_read_input_tokens=0)
- mock_resp.stop_reason = "end_turn"
- provider._client.messages.create = AsyncMock(return_value=mock_resp)
+ provider._client.messages.stream = _stream_returning(_text_response())
thinking = ThinkingConfig(enabled=True, budget=4096)
await provider.chat(
@@ -86,7 +109,7 @@ class TestAnthropicProvider:
thinking=thinking,
)
- call_kwargs = provider._client.messages.create.call_args[1]
+ call_kwargs = provider._client.messages.stream.call_args[1]
assert call_kwargs["thinking"]["budget_tokens"] == 4096
assert call_kwargs["temperature"] == 1
@@ -94,16 +117,7 @@ class TestAnthropicProvider:
async def test_chat_clamps_thinking_budget_and_expands_max_tokens(
self, provider
):
- mock_resp = MagicMock()
- mock_resp.content = [MagicMock(type="text", text="ok")]
- mock_resp.usage = MagicMock(
- input_tokens=5,
- output_tokens=2,
- cache_creation_input_tokens=0,
- cache_read_input_tokens=0,
- )
- mock_resp.stop_reason = "end_turn"
- provider._client.messages.create = AsyncMock(return_value=mock_resp)
+ provider._client.messages.stream = _stream_returning(_text_response())
await provider.chat(
[Message(role="user", content="test")],
@@ -112,10 +126,125 @@ class TestAnthropicProvider:
thinking=ThinkingConfig(enabled=True, budget=100),
)
- call_kwargs = provider._client.messages.create.call_args.kwargs
+ call_kwargs = provider._client.messages.stream.call_args.kwargs
assert call_kwargs["thinking"]["budget_tokens"] == 1024
assert call_kwargs["max_tokens"] == 2048
+ @pytest.mark.asyncio
+ async def test_sampling_params_dropped_on_current_models(self, provider):
+ """`temperature` is a 400 on Claude 4.7+ generations, not a warning."""
+ provider._client.messages.stream = _stream_returning(_text_response())
+
+ await provider.chat(
+ [Message(role="user", content="test")],
+ model="claude-opus-5",
+ temperature=0.7,
+ )
+
+ call_kwargs = provider._client.messages.stream.call_args.kwargs
+ assert "temperature" not in call_kwargs
+ assert "top_p" not in call_kwargs
+ assert "top_k" not in call_kwargs
+
+ @pytest.mark.asyncio
+ async def test_adaptive_thinking_replaces_budget_tokens(self, provider):
+ """`budget_tokens` was removed on these models; adaptive replaces it."""
+ provider._client.messages.stream = _stream_returning(_text_response())
+
+ await provider.chat(
+ [Message(role="user", content="test")],
+ model="claude-opus-5",
+ thinking=ThinkingConfig(enabled=True, budget=8000),
+ )
+
+ call_kwargs = provider._client.messages.stream.call_args.kwargs
+ assert call_kwargs["thinking"] == {
+ "type": "adaptive",
+ "display": "summarized",
+ }
+ assert "temperature" not in call_kwargs
+
+ @pytest.mark.asyncio
+ async def test_thinking_omitted_rather_than_disabled(self, provider):
+ """`{"type": "disabled"}` is itself a 400 on Fable/Mythos."""
+ provider._client.messages.stream = _stream_returning(_text_response())
+
+ await provider.chat(
+ [Message(role="user", content="test")],
+ model="claude-fable-5",
+ thinking=ThinkingConfig(enabled=False),
+ )
+
+ assert "thinking" not in provider._client.messages.stream.call_args.kwargs
+
+ @pytest.mark.asyncio
+ async def test_legacy_models_keep_budget_thinking_and_temperature(
+ self, provider
+ ):
+ """4.6 and older still accept both — don't change what works."""
+ provider._client.messages.stream = _stream_returning(_text_response())
+
+ await provider.chat(
+ [Message(role="user", content="test")],
+ model="claude-sonnet-4-6",
+ temperature=0.3,
+ thinking=ThinkingConfig(enabled=True, budget=4096),
+ )
+
+ call_kwargs = provider._client.messages.stream.call_args.kwargs
+ assert call_kwargs["thinking"] == {
+ "type": "enabled",
+ "budget_tokens": 4096,
+ }
+ assert call_kwargs["temperature"] == 1
+
+ @pytest.mark.asyncio
+ async def test_chat_streams_instead_of_create(self, provider):
+ """The non-streaming path raises for the max_tokens we actually send.
+
+ ``resolve_max_tokens`` hands the provider the model's full output cap
+ (128K on every current Claude model), and the SDK rejects any
+ non-streaming request that large before it leaves the process, so
+ ``chat`` must never reach ``messages.create``.
+ """
+ provider._client.messages.stream = _stream_returning(_text_response())
+ provider._client.messages.create = AsyncMock(
+ side_effect=AssertionError("chat() must not use the non-streaming path")
+ )
+
+ result = await provider.chat(
+ [Message(role="user", content="test")],
+ model="claude-opus-5",
+ max_tokens=128_000,
+ )
+
+ assert result.content == "ok"
+ provider._client.messages.create.assert_not_awaited()
+ assert provider._client.messages.stream.call_args.kwargs["max_tokens"] == 128_000
+
+ @pytest.mark.asyncio
+ async def test_compaction_streams_on_the_beta_endpoint(self, provider):
+ provider._client.beta.messages.stream = _stream_returning(_text_response())
+ provider._client.beta.messages.create = AsyncMock(
+ side_effect=AssertionError("compaction must not use the non-streaming path")
+ )
+
+ await provider.chat(
+ [Message(role="user", content="test")],
+ model="claude-opus-5",
+ max_tokens=128_000,
+ context_management={
+ "type": "compaction",
+ "compact_threshold": 150_000,
+ },
+ )
+
+ call_kwargs = provider._client.beta.messages.stream.call_args.kwargs
+ assert call_kwargs["betas"] == ["compact-2026-01-12"]
+ edit = call_kwargs["context_management"]["edits"][0]
+ assert edit["type"] == "compact_20260112"
+ assert edit["trigger"] == {"type": "input_tokens", "value": 150_000}
+
# ---------------------------------------------------------------------------
# OpenAI: message formatting + response normalization
diff --git a/server/tests/nodes/test_agent_builder.py b/server/tests/nodes/test_agent_builder.py
index fc034e8b..5a6512e3 100644
--- a/server/tests/nodes/test_agent_builder.py
+++ b/server/tests/nodes/test_agent_builder.py
@@ -439,9 +439,11 @@ class TestAddSubagent:
):
result = await node.add_subagent(ctx, params)
- assert len(result.operations) == 2
+ assert len(result.operations) == 4
assert result.operations[0]["node_type"] == "coding_agent"
- assert result.operations[1]["target_handle"] == "input-teammates"
+ assert result.operations[1]["node_type"] == "context"
+ assert result.operations[2]["target_handle"] == "input-context"
+ assert result.operations[3]["target_handle"] == "input-teammates"
# ============================================================================
diff --git a/server/tests/nodes/test_agent_context_node.py b/server/tests/nodes/test_agent_context_node.py
new file mode 100644
index 00000000..d112ce76
--- /dev/null
+++ b/server/tests/nodes/test_agent_context_node.py
@@ -0,0 +1,196 @@
+from __future__ import annotations
+
+import pytest
+from pydantic import ValidationError
+
+from nodes.context import (
+ AgentContextNode,
+ AgentContextParams,
+)
+from nodes._visuals import get_plugin_icon_path, get_plugin_meta
+from services.plugin.context import NodeContext
+from services.plugin.edge_walker import collect_agent_connections
+from services.ws_handler_registry import get_ws_handlers
+
+
+def test_context_node_declares_only_policy_and_dedicated_handle():
+ schema = AgentContextParams.model_json_schema()
+ assert set(schema["properties"]) == {
+ "compaction_mode",
+ "trigger_ratio",
+ "context_window_override",
+ "exact_tail_retention_count",
+ }
+ forbidden = {
+ "journal",
+ "messages",
+ "checkpoint",
+ "provider_binding",
+ "payload_ref",
+ "epoch",
+ "revision",
+ }
+ assert forbidden.isdisjoint(schema["properties"])
+ assert AgentContextNode.handles == (
+ {
+ "name": "output-context",
+ "kind": "output",
+ "position": "top",
+ "label": "Context",
+ "role": "context",
+ },
+ )
+ assert AgentContextNode.ui_hints["systemManaged"] is True
+ assert AgentContextNode.ui_hints["isContextPanel"] is True
+ assert AgentContextNode.ui_hints["hideInputSection"] is True
+ assert AgentContextNode.ui_hints["hideOutputSection"] is True
+
+
+def test_context_node_ships_backend_visual_assets():
+ icon_path = get_plugin_icon_path("context")
+ assert icon_path is not None
+ assert icon_path.name == "icon.svg"
+ assert icon_path.parent.name == "context"
+ assert icon_path.is_file()
+ assert (
+ AgentContextNode._metadata_dict()["icon"]
+ == "/api/schemas/nodes/context/icon"
+ )
+ assert get_plugin_meta("context", "color") == "#6272a4"
+
+
+def test_context_policy_rejects_runtime_payloads():
+ with pytest.raises(ValidationError):
+ AgentContextParams.model_validate(
+ {
+ "compaction_mode": "auto",
+ "journal": [{"role": "user", "content": "secret"}],
+ }
+ )
+
+
+def test_context_panel_handlers_self_register():
+ handlers = get_ws_handlers()
+ assert {
+ "get_agent_context",
+ "clear_agent_context",
+ "fork_agent_context",
+ "export_agent_context",
+ }.issubset(handlers)
+
+
+async def test_context_node_operation_is_policy_only_and_metadata_only():
+ assert set(AgentContextNode._operations) == {"policy"}
+ result = await AgentContextNode().policy(
+ NodeContext(node_id="context-1", node_type="context"),
+ AgentContextParams(),
+ )
+ assert result.model_dump() == {
+ "configured": True,
+ "compaction_mode": "auto",
+ "trigger_ratio": 0.8,
+ "exact_tail_retention_count": 8,
+ }
+
+
+async def test_context_connection_activates_only_for_admitted_generation():
+ class Database:
+ async def get_node_parameters(self, node_id):
+ assert node_id == "context-1"
+ return {"compaction_mode": "auto"}
+
+ graph = {
+ "workflow_id": "workflow-1",
+ "execution_id": "execution-1",
+ "nodes": [
+ {"id": "agent-1", "type": "aiAgent"},
+ {"id": "context-1", "type": "context"},
+ ],
+ "edges": [
+ {
+ "source": "context-1",
+ "target": "agent-1",
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ }
+ ],
+ }
+ generation_zero = await collect_agent_connections(
+ "agent-1",
+ graph,
+ Database(),
+ )
+ assert generation_zero[0] is None
+
+ live = await collect_agent_connections(
+ "agent-1",
+ {**graph, "generation": 1},
+ Database(),
+ )
+ assert live[0]["kind"] == "context"
+ assert live[0]["generation"] == 1
+
+
+class TestContextPluginOwnsItsDescriptor:
+ """The framework must not carry Context-plugin knowledge.
+
+ ``context`` is a declarative UI/policy surface onto an agent's context
+ scope (RFC-0002 section 3). The edge walker knows only that something
+ may be registered for ``input-context``; the descriptor's shape, the
+ node's parameters and the thread-selection rules belong to the plugin.
+ """
+
+ def test_edge_walker_has_no_context_plugin_knowledge(self):
+ import inspect
+
+ from services.plugin import edge_walker
+
+ source = inspect.getsource(edge_walker)
+ # The framework may reference the handle name; what it must NOT do
+ # is know the descriptor's shape or branch on the node type.
+ assert '"kind": "context"' not in source, (
+ "edge_walker hardcodes the Context descriptor shape; it should "
+ "call the registered builder instead"
+ )
+ assert 'source_node.get("type") == "context"' not in source
+ assert '"policy"' not in source, "node parameters belong to the plugin"
+
+ def test_plugin_registers_the_builder(self):
+ import nodes # noqa: F401 - triggers plugin discovery
+ from services.plugin.edge_walker import get_agent_context_builder
+
+ builder = get_agent_context_builder()
+ assert builder is not None
+ assert builder.__module__.startswith("nodes.context")
+
+ @pytest.mark.asyncio
+ async def test_generation_zero_contributes_no_context(self):
+ from nodes.context._descriptor import build_agent_context_descriptor
+
+ class _Db:
+ async def get_node_parameters(self, _node_id):
+ return {}
+
+ # Generation zero is a migration/import artifact, not a live scope.
+ assert await build_agent_context_descriptor("ctx-1", {"generation": 0}, _Db()) is None
+
+ @pytest.mark.asyncio
+ async def test_delegated_task_does_not_inherit_parent_session(self):
+ from nodes.context._descriptor import build_agent_context_descriptor
+
+ class _Db:
+ async def get_node_parameters(self, _node_id):
+ return {"compaction_mode": "auto"}
+
+ got = await build_agent_context_descriptor(
+ "ctx-1",
+ {"generation": 2, "session_id": "parent-session", "delegated_task_id": "task-9"},
+ _Db(),
+ )
+ # Sharing the parent's session would collapse every subagent onto
+ # one thread; the delegated task is the isolation boundary.
+ assert got["session_id"] is None
+ assert got["delegated_task_id"] == "task-9"
+ assert got["policy"] == {"compaction_mode": "auto"}
diff --git a/server/tests/nodes/test_ai_agents.py b/server/tests/nodes/test_ai_agents.py
index 98008340..d8b30f25 100644
--- a/server/tests/nodes/test_ai_agents.py
+++ b/server/tests/nodes/test_ai_agents.py
@@ -437,58 +437,72 @@ class TestChatAgent:
class TestSimpleMemory:
- """handle_simple_memory reads from services.memory_store (NOT markdown)."""
-
- @pytest.fixture(autouse=True)
- def _reset_memory_store(self):
- """Snapshot + restore the module-global session dict."""
- from services import memory_store as ms
-
- backup = dict(ms._sessions)
- ms._sessions.clear()
- try:
- yield ms
- finally:
- ms._sessions.clear()
- ms._sessions.update(backup)
-
- async def test_happy_path_empty_session(self, harness, _reset_memory_store):
+ """Simple Memory V2 is an explicit tool, never a transcript node."""
+
+ @staticmethod
+ def _stub_store(monkeypatch, *, items=None):
+ import nodes.tool.simple_memory as memory_plugin
+
+ store = MagicMock()
+ store.list = AsyncMock(
+ return_value={
+ "operation": "list",
+ "items": list(items or []),
+ "count": len(items or []),
+ "next_cursor": None,
+ "retrieval": "lexical",
+ }
+ )
+ monkeypatch.setattr(
+ memory_plugin,
+ "MemoryToolStore",
+ MagicMock(return_value=store),
+ )
+ return store
+
+ async def test_framework_run_is_harmless_diagnostic_list(
+ self,
+ harness,
+ monkeypatch,
+ ):
+ store = self._stub_store(monkeypatch)
result = await harness.execute(
"simpleMemory",
- {"session_id": "sess-a"},
+ {"reset_policy": "preserve"},
)
- harness.assert_envelope(result, success=True)
- payload = result["result"]
- assert payload["session_id"] == "sess-a"
- assert payload["messages"] == []
- assert payload["message_count"] == 0
- # window_size always defaults to 100 now (no buffer/window split)
- assert payload["window_size"] == 100
-
- async def test_window_size_returns_last_n_messages(self, harness, _reset_memory_store):
- ms = _reset_memory_store
- for i in range(5):
- ms.add_message("sess-b", "human", f"h{i}")
- ms.add_message("sess-b", "ai", f"a{i}")
-
+ payload = result
+ assert payload["operation"] == "list"
+ assert payload["items"] == []
+ assert payload["count"] == 0
+ store.list.assert_awaited_once()
+
+ async def test_legacy_transcript_fields_are_ignored(
+ self,
+ harness,
+ monkeypatch,
+ ):
+ self._stub_store(monkeypatch)
result = await harness.execute(
"simpleMemory",
- {"session_id": "sess-b", "window_size": 2},
+ {
+ "session_id": "legacy-session",
+ "window_size": 2,
+ "memory_content": "must not be injected",
+ },
)
- harness.assert_envelope(result, success=True)
- payload = result["result"]
- assert payload["window_size"] == 2
- # last 2 messages from a 10-message session
- assert payload["message_count"] == 2
- contents = [m["content"] for m in payload["messages"]]
- assert contents == ["h4", "a4"]
-
- async def test_default_session_when_omitted(self, harness, _reset_memory_store):
+ payload = result
+ assert "session_id" not in payload
+ assert "messages" not in payload
+ assert "memory_content" not in payload
+
+ async def test_default_config_does_not_create_transcript(
+ self,
+ harness,
+ monkeypatch,
+ ):
+ self._stub_store(monkeypatch)
result = await harness.execute("simpleMemory", {})
- harness.assert_envelope(result, success=True)
- # Empty session_id resolves to "default" sentinel
- assert result["result"]["session_id"] == "default"
- assert result["result"]["window_size"] == 100
+ assert result["operation"] == "list"
diff --git a/server/tests/nodes/test_specialized_context_bridges.py b/server/tests/nodes/test_specialized_context_bridges.py
new file mode 100644
index 00000000..3df395f4
--- /dev/null
+++ b/server/tests/nodes/test_specialized_context_bridges.py
@@ -0,0 +1,228 @@
+"""Thin-node wiring tests for specialized Context V2 backends."""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from services.cli_agent.protocol import BatchResult, SessionResult
+from tests.nodes._mocks import patched_broadcaster, patched_container
+
+pytestmark = pytest.mark.node_contract
+
+
+def _node(node_id: str, node_type: str) -> dict:
+ return {"id": node_id, "type": node_type, "data": {"label": node_type}}
+
+
+def _context_graph(agent_id: str, agent_type: str) -> tuple[list, list]:
+ context_id = "wf:context:1"
+ return (
+ [_node(agent_id, agent_type), _node(context_id, "context")],
+ [
+ {
+ "source": context_id,
+ "target": agent_id,
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ }
+ ],
+ )
+
+
+async def test_rlm_node_passes_context_descriptor_without_legacy_memory(
+ harness,
+):
+ nodes, edges = _context_graph("rlm-1", "rlm_agent")
+ rlm = MagicMock()
+ rlm.execute = AsyncMock(
+ return_value={"success": True, "result": {"response": "done"}}
+ )
+ harness.ai_service.rlm_service = rlm
+
+ with patched_container(auth_api_keys={}), patched_broadcaster():
+ result = await harness.execute(
+ "rlm_agent",
+ {"prompt": "solve", "provider": "openai", "model": "gpt-4o"},
+ node_id="rlm-1",
+ context=harness.build_context(
+ nodes=nodes,
+ edges=edges,
+ extra={"generation": 1},
+ ),
+ )
+
+ harness.assert_envelope(result, success=True)
+ kwargs = rlm.execute.await_args.kwargs
+ assert kwargs["memory_data"] is None
+ assert kwargs["context_data"]["kind"] == "context"
+ assert kwargs["context_data"]["context_node_id"] == "wf:context:1"
+
+
+async def test_claude_node_routes_context_separately_from_v1_memory(harness):
+ nodes, edges = _context_graph("claude-1", "claude_code_agent")
+ service = MagicMock()
+ service.run_batch = AsyncMock(
+ return_value=BatchResult(
+ tasks=[
+ SessionResult(
+ task_id="t1",
+ provider="claude",
+ response="done",
+ session_id="uuid-1",
+ success=True,
+ )
+ ],
+ n_tasks=1,
+ n_succeeded=1,
+ provider="claude",
+ )
+ )
+
+ with (
+ patched_container(auth_api_keys={}),
+ patched_broadcaster(),
+ patch(
+ "services.cli_agent.service.get_ai_cli_service",
+ return_value=service,
+ ),
+ ):
+ result = await harness.execute(
+ "claude_code_agent",
+ {"prompt": "edit"},
+ node_id="claude-1",
+ context=harness.build_context(
+ nodes=nodes,
+ edges=edges,
+ extra={"generation": 1},
+ ),
+ )
+
+ harness.assert_envelope(result, success=True)
+ kwargs = service.run_batch.await_args.kwargs
+ assert kwargs["connected_memory"] is None
+ assert kwargs["connected_context"]["kind"] == "context"
+ task = list(kwargs["tasks"])[0]
+ assert task.continue_session is False
+ assert result["result"]["session_id"] is None
+ assert result["result"]["tasks"][0]["session_id"] is None
+
+
+async def test_codex_node_routes_observable_context_without_resume(harness):
+ nodes, edges = _context_graph("codex-1", "codex_agent")
+ service = MagicMock()
+ service.run_batch = AsyncMock(
+ return_value=BatchResult(
+ tasks=[
+ SessionResult(
+ task_id="t1",
+ provider="codex",
+ response="done",
+ session_id=None,
+ success=True,
+ )
+ ],
+ n_tasks=1,
+ n_succeeded=1,
+ provider="codex",
+ )
+ )
+
+ with (
+ patched_container(auth_api_keys={}),
+ patched_broadcaster(),
+ patch(
+ "services.cli_agent.service.get_ai_cli_service",
+ return_value=service,
+ ),
+ ):
+ result = await harness.execute(
+ "codex_agent",
+ {"prompt": "inspect"},
+ node_id="codex-1",
+ context=harness.build_context(
+ nodes=nodes,
+ edges=edges,
+ extra={"generation": 1},
+ ),
+ )
+
+ harness.assert_envelope(result, success=True)
+ kwargs = service.run_batch.await_args.kwargs
+ assert kwargs["connected_context"]["kind"] == "context"
+ assert kwargs.get("connected_memory") is None
+ assert result["result"].get("session_id") is None
+ assert result["result"]["tasks"][0]["session_id"] is None
+
+
+async def test_vertex_uses_context_binding_and_redacts_ids_from_output(harness):
+ nodes, edges = _context_graph("vertex-1", "vertex_managed_agent")
+ bridge = MagicMock()
+ bridge.load_binding = AsyncMock(
+ return_value={
+ "interaction_id": "ix-prev",
+ "environment_id": "env-prev",
+ }
+ )
+ bridge.append_observable = AsyncMock()
+ bridge.bind_provider = AsyncMock()
+ bridge.augment_prompt = MagicMock(side_effect=lambda prompt: prompt)
+ interaction = SimpleNamespace(
+ id="ix-next",
+ status="completed",
+ environment_id="env-next",
+ output_text="answer",
+ steps=[],
+ usage=None,
+ )
+
+ with (
+ patched_container(auth_api_keys={}),
+ patched_broadcaster() as broadcaster,
+ patch(
+ "services.cli_agent.context_bridge."
+ "SpecializedAgentContextBridge.resolve",
+ AsyncMock(return_value=bridge),
+ ),
+ patch(
+ "nodes.agent.vertex_managed_agent.build_genai_client",
+ return_value=MagicMock(),
+ ),
+ patch(
+ "nodes.agent.vertex_managed_agent.stream_interaction",
+ AsyncMock(return_value=interaction),
+ ) as stream,
+ ):
+ broadcaster.broadcast_agent_progress = AsyncMock()
+ broadcaster.broadcast = AsyncMock()
+ result = await harness.execute(
+ "vertex_managed_agent",
+ {
+ "prompt": "continue",
+ "project_id": "project",
+ "visualize_cloud_tools": False,
+ },
+ node_id="vertex-1",
+ context=harness.build_context(
+ nodes=nodes,
+ edges=edges,
+ extra={"generation": 1},
+ ),
+ )
+
+ harness.assert_envelope(result, success=True)
+ _, kwargs = stream.call_args
+ assert kwargs["previous_interaction_id"] == "ix-prev"
+ assert kwargs["environment"] == "env-prev"
+ assert result["result"]["interaction_id"] is None
+ assert result["result"]["environment_id"] is None
+ bridge.bind_provider.assert_awaited_once()
+ binding = bridge.bind_provider.await_args.args[1]
+ assert binding["interaction_id"] == "ix-next"
+ assert binding["environment_id"] == "env-next"
diff --git a/server/tests/nodes/test_vertex_agents.py b/server/tests/nodes/test_vertex_agents.py
index 2df58df2..f7d62566 100644
--- a/server/tests/nodes/test_vertex_agents.py
+++ b/server/tests/nodes/test_vertex_agents.py
@@ -678,10 +678,9 @@ class TestVertexManagedAgent:
ai_service._build_tool_from_node.assert_awaited_once()
assert [t["name"] for t in create_mock.call_args.kwargs["tools"]] == ["fake_tool"]
- async def test_duplicate_tool_names_deduped(self, harness):
- """Two nodes resolving to the same tool name get deterministic
- _2 suffixes; the suffixed name dispatches with the SECOND node's
- config."""
+ async def test_duplicate_tool_names_fail_before_provider_call(self, harness):
+ """Vertex uses the same canonical-name ambiguity gate as every
+ other agent runtime; it must not invent provider-only aliases."""
agent_id = "vx-1"
def make_tool(node_id):
@@ -692,25 +691,13 @@ class TestVertexManagedAgent:
ai_service = MagicMock(name="AIService")
ai_service._build_tool_from_node = AsyncMock(side_effect=[make_tool("tool-1"), make_tool("tool-2")])
- execute_tool_mock = AsyncMock(return_value={"answer": 1})
-
- turns = [
- _interaction(
- status="requires_action",
- interaction_id="ix-1",
- steps=[_fc_step("fake_tool_2", "c1", {"query": "x"})],
- ),
- _interaction(status="completed", interaction_id="ix-2"),
- ]
- client_patch, create_patch = _patched_interactions(turns)
+ client_patch, create_patch = _patched_interactions([_interaction()])
with (
patched_container(auth_api_keys={}),
patched_broadcaster() as bc,
client_patch,
create_patch as create_mock,
patch("services.plugin.deps.get_ai_service", return_value=ai_service),
- patch("services.handlers.tools.execute_tool", execute_tool_mock),
- patch(f"{_NODE_MODULE}._ops.record_tool_output", AsyncMock()),
):
_wire_async_broadcasts(bc)
result = await harness.execute(
@@ -728,12 +715,9 @@ class TestVertexManagedAgent:
],
)
- harness.assert_envelope(result, success=True)
- declared_names = [t["name"] for t in create_mock.call_args_list[0].kwargs["tools"]]
- assert declared_names == ["fake_tool", "fake_tool_2"]
- execute_tool_mock.assert_awaited_once()
- config_arg = execute_tool_mock.await_args.args[2]
- assert config_arg["node_id"] == "tool-2"
+ harness.assert_envelope(result, success=False)
+ assert "Duplicate LLM-visible tool names" in result["error"]
+ create_mock.assert_not_called()
async def test_unanswerable_requires_action_surfaces_warning(self, harness):
"""When requires_action only carries calls we never declared, the
diff --git a/server/tests/services/agent_context/__init__.py b/server/tests/services/agent_context/__init__.py
new file mode 100644
index 00000000..8b137891
--- /dev/null
+++ b/server/tests/services/agent_context/__init__.py
@@ -0,0 +1 @@
+
diff --git a/server/tests/services/agent_context/test_compaction.py b/server/tests/services/agent_context/test_compaction.py
new file mode 100644
index 00000000..8d0f2953
--- /dev/null
+++ b/server/tests/services/agent_context/test_compaction.py
@@ -0,0 +1,577 @@
+from __future__ import annotations
+
+from typing import Any
+
+import pytest
+
+from models.agent_context import AgentContextEvent
+from services.agent_context.compaction import (
+ AgentContextCompactionService,
+ ContextCompactionCandidate,
+ ContextCompactionPolicy,
+ get_provider_context_adapter,
+ provider_context_request_options,
+ select_compaction_boundary,
+)
+from services.llm.protocol import Message, ToolCall, message_to_wire
+
+from .test_store import ( # noqa: F401, F811
+ _resolve,
+ _wire,
+ context_database,
+)
+
+
+def _event(
+ sequence: int,
+ event_type: str,
+ wire: dict[str, Any] | None = None,
+) -> AgentContextEvent:
+ return AgentContextEvent(
+ sequence=sequence,
+ event_type=event_type,
+ message_wire_v2=wire,
+ operation_id=f"operation-{sequence}",
+ provider="openai",
+ previous_hash=f"{sequence - 1:064x}",
+ payload_hash=f"{sequence:064x}",
+ )
+
+
+def test_boundary_requires_complete_tool_transaction_and_keeps_exact_tail():
+ assistant = dict(
+ message_to_wire(
+ Message(
+ role="assistant",
+ tool_calls=[
+ ToolCall(id="a", name="lookup", args={}),
+ ToolCall(id="b", name="lookup", args={}),
+ ],
+ )
+ )
+ )
+ first_tool = dict(
+ message_to_wire(
+ Message(role="tool", tool_call_id="a", name="lookup")
+ )
+ )
+ second_tool = dict(
+ message_to_wire(
+ Message(role="tool", tool_call_id="b", name="lookup")
+ )
+ )
+ events = [
+ _event(1, "message.assistant", assistant),
+ _event(2, "message.tool_result", first_tool),
+ _event(3, "message.tool_result", second_tool),
+ _event(4, "request.snapshot"),
+ _event(5, "message.assistant", _wire("tail")),
+ ]
+
+ assert (
+ select_compaction_boundary(
+ events,
+ exact_tail_retention_count=2,
+ )
+ == 3
+ )
+ assert (
+ select_compaction_boundary(
+ events[:2],
+ exact_tail_retention_count=1,
+ )
+ is None
+ )
+
+
+def test_final_only_response_is_a_valid_compaction_boundary():
+ events = [
+ _event(1, "message.assistant", _wire("final answer")),
+ _event(2, "response.final"),
+ _event(3, "request.snapshot"),
+ ]
+ assert (
+ select_compaction_boundary(
+ events,
+ exact_tail_retention_count=1,
+ )
+ == 2
+ )
+
+
+async def test_pressure_is_idempotent_and_portable_checkpoint_keeps_tail(
+ context_database, # noqa: F811
+):
+ from services.agent_context import AgentContextStore
+
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ assistant_tool = _wire("tool request", with_tool=True)
+ tool_result = dict(
+ message_to_wire(
+ Message(
+ role="tool",
+ content='{"ok":true}',
+ tool_call_id="call-1",
+ name="lookup",
+ )
+ )
+ )
+ transitions = [
+ ("message.assistant", assistant_tool),
+ ("message.tool_result", tool_result),
+ ("message.assistant", _wire("final answer")),
+ ("response.final", None),
+ ("request.snapshot", None),
+ ]
+ for index, (event_type, wire) in enumerate(transitions, start=1):
+ ref = (
+ await store.append_transition(
+ ref,
+ event_type=event_type,
+ operation_id=f"transition-{index}",
+ message_wire_v2=wire,
+ provider="gemini",
+ )
+ ).ref
+
+ async def portable_compactor(_wires, _policy):
+ return ContextCompactionCandidate(
+ strategy="portable_structured",
+ replay_payload={
+ "messages": [
+ dict(
+ message_to_wire(
+ Message(
+ role="user",
+ content=(
+ "Portable checkpoint: tool completed and "
+ "the final answer was delivered."
+ ),
+ )
+ )
+ )
+ ]
+ },
+ active_token_count=20,
+ lifetime_usage={"input_tokens": 100, "output_tokens": 20},
+ )
+
+ result = await AgentContextCompactionService(
+ store
+ ).update_pressure_and_compact(
+ ref,
+ operation_id="execution-1:context",
+ provider="gemini",
+ model="gemini-2.5-pro",
+ policy=ContextCompactionPolicy(
+ mode="portable",
+ trigger_ratio=0.8,
+ context_window_override=1024,
+ exact_tail_retention_count=1,
+ ),
+ active_input_tokens=0,
+ output_headroom=200,
+ rendered_request={"messages": ["x" * 3_000]},
+ portable_compactor=portable_compactor,
+ )
+
+ assert result.compacted is True
+ assert result.checkpoint is not None
+ assert result.checkpoint.covers_through_sequence == 4
+ assert result.pressure_tokens == 220
+ state = await store.load_active(result.ref)
+ assert [event.sequence for event in state.tail] == [5]
+ assert state.active_token_count == 220
+
+ same_ref = await store.record_active_pressure(
+ result.ref,
+ operation_id="same-pressure-operation",
+ active_token_count=333,
+ )
+ duplicate = await store.record_active_pressure(
+ same_ref,
+ operation_id="same-pressure-operation",
+ active_token_count=999,
+ )
+ assert duplicate == same_ref
+ assert (await store.load_active(duplicate)).active_token_count == 333
+
+
+async def test_openai_native_candidate_preserves_compacted_output_unchanged():
+ wire = _wire("continued answer")
+ wire["provider_state"] = {
+ "provider": "openai",
+ "payload": {
+ "api": "responses",
+ "output": [
+ {"type": "message", "role": "user", "content": []},
+ {
+ "id": "cmp_1",
+ "type": "compaction",
+ "encrypted_content": "opaque",
+ },
+ {
+ "id": "msg_2",
+ "type": "message",
+ "role": "assistant",
+ "content": [],
+ },
+ ],
+ },
+ }
+ adapter = get_provider_context_adapter("openai")
+ candidate = await adapter.compact(
+ [_event(1, "message.assistant", wire)],
+ ContextCompactionPolicy(mode="native"),
+ )
+
+ assert candidate is not None
+ output = candidate.replay_payload["messages"][0]["provider_state"][
+ "payload"
+ ]["output"]
+ assert output == wire["provider_state"]["payload"]["output"][1:]
+ assert output[0]["encrypted_content"] == "opaque"
+
+
+async def test_native_checkpoint_stops_at_marker_and_keeps_later_events_exact(
+ context_database, # noqa: F811
+):
+ from services.agent_context import AgentContextStore
+
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ marker_wire = _wire("call the tool", with_tool=True)
+ marker_wire["provider_state"] = {
+ "provider": "openai",
+ "payload": {
+ "api": "responses",
+ "output": [
+ {
+ "id": "cmp_1",
+ "type": "compaction",
+ "encrypted_content": "opaque",
+ },
+ {
+ "id": "call_1",
+ "type": "function_call",
+ "name": "lookup",
+ "arguments": '{"query":"call the tool"}',
+ },
+ ],
+ },
+ }
+ tool_wire = dict(
+ message_to_wire(
+ Message(
+ role="tool",
+ content='{"ok":true}',
+ tool_call_id="call-1",
+ name="lookup",
+ )
+ )
+ )
+ transitions = [
+ ("message.assistant", marker_wire),
+ ("message.tool_result", tool_wire),
+ ("message.assistant", _wire("final after tool")),
+ ("response.final", None),
+ ]
+ for index, (event_type, wire) in enumerate(transitions, start=1):
+ ref = (
+ await store.append_transition(
+ ref,
+ event_type=event_type,
+ operation_id=f"native-boundary-{index}",
+ message_wire_v2=wire,
+ provider="openai",
+ )
+ ).ref
+
+ result = await AgentContextCompactionService(
+ store
+ ).update_pressure_and_compact(
+ ref,
+ operation_id="native-boundary-compaction",
+ provider="openai",
+ model="gpt-5.6",
+ policy=ContextCompactionPolicy(
+ mode="native",
+ trigger_ratio=0.8,
+ context_window_override=1024,
+ exact_tail_retention_count=1,
+ ),
+ active_input_tokens=900,
+ output_headroom=100,
+ )
+
+ assert result.compacted is True
+ assert result.checkpoint is not None
+ assert result.checkpoint.covers_through_sequence == 1
+ state = await store.load_active(result.ref)
+ assert state.ref == result.ref
+ assert [event.sequence for event in state.tail] == [2, 3, 4]
+ assert [event.message_wire_v2 for event in state.tail] == [
+ tool_wire,
+ _wire("final after tool"),
+ None,
+ ]
+
+
+async def test_compaction_returns_authoritative_ref_after_concurrent_append(
+ context_database, # noqa: F811
+):
+ from services.agent_context import AgentContextStore
+
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ for index in range(1, 4):
+ ref = (
+ await store.append_transition(
+ ref,
+ event_type="message.assistant",
+ operation_id=f"authoritative-ref-{index}",
+ message_wire_v2=_wire(f"message-{index}"),
+ provider="gemini",
+ )
+ ).ref
+
+ async def compact_with_concurrent_append(_wires, _policy):
+ await store.append_transition(
+ ref,
+ event_type="message.assistant",
+ operation_id="authoritative-ref-concurrent",
+ message_wire_v2=_wire("concurrent tail"),
+ provider="gemini",
+ )
+ return ContextCompactionCandidate(
+ strategy="portable_structured",
+ replay_payload={"messages": [_wire("summary")]},
+ active_token_count=10,
+ )
+
+ result = await AgentContextCompactionService(
+ store
+ ).update_pressure_and_compact(
+ ref,
+ operation_id="authoritative-ref-compaction",
+ provider="gemini",
+ model="gemini-2.5-pro",
+ policy=ContextCompactionPolicy(
+ mode="portable",
+ trigger_ratio=0.8,
+ context_window_override=1024,
+ exact_tail_retention_count=1,
+ ),
+ active_input_tokens=900,
+ output_headroom=100,
+ portable_compactor=compact_with_concurrent_append,
+ )
+
+ state = await store.load_active(result.ref)
+ assert result.compacted is True
+ assert result.ref == state.ref
+ assert [event.sequence for event in state.tail] == [3, 4]
+
+
+def test_native_request_options_are_capability_driven():
+ openai = provider_context_request_options(
+ provider="openai",
+ model="gpt-5.6",
+ policy=ContextCompactionPolicy(
+ mode="auto",
+ trigger_ratio=0.8,
+ context_window_override=100_000,
+ ),
+ )
+ anthropic = provider_context_request_options(
+ provider="anthropic",
+ model="claude-sonnet-4-6",
+ policy=ContextCompactionPolicy(
+ mode="native",
+ trigger_ratio=0.6,
+ context_window_override=200_000,
+ ),
+ )
+ compatible = provider_context_request_options(
+ provider="openrouter",
+ model="openai/gpt-5.6",
+ policy=ContextCompactionPolicy(mode="auto"),
+ )
+
+ assert openai == {
+ "type": "compaction",
+ "compact_threshold": 80_000,
+ "pause_after_compaction": False,
+ "strategy": "openai_responses_compaction",
+ }
+ assert anthropic == {
+ "type": "compaction",
+ "compact_threshold": 120_000,
+ "pause_after_compaction": True,
+ "strategy": "anthropic_compact_20260112",
+ }
+ assert compatible is None
+
+
+class _ContextUnifier:
+ def __init__(self, responses):
+ self.responses = list(responses)
+ self.calls: list[dict[str, Any]] = []
+
+ async def chat(self, **kwargs):
+ from services.llm.protocol import LLMResponse, Usage
+
+ self.calls.append(kwargs)
+ content = self.responses.pop(0)
+ return LLMResponse(
+ content=content,
+ usage=Usage(
+ input_tokens=20,
+ output_tokens=5,
+ total_tokens=25,
+ ),
+ )
+
+
+ "method_name",
+ ["execute_agent", "execute_chat_agent"],
+)
+async def test_ai_service_wires_context_sink_for_both_native_paths(
+ context_database, # noqa: F811
+ method_name,
+):
+ from services.agent_context import AgentContextStore
+ from services.ai import AIService
+
+ unifier = _ContextUnifier(["context-backed answer"])
+ service = AIService(
+ auth_service=object(),
+ database=context_database,
+ cache=None,
+ settings=object(),
+ chat_unifier=unifier,
+ )
+ descriptor = {
+ "kind": "context",
+ "workflow_id": f"workflow-{method_name}",
+ "context_node_id": f"context-{method_name}",
+ "generation": 1,
+ "execution_id": "execution-1",
+ "session_id": "chat-1",
+ "delegated_task_id": None,
+ "policy": {"compaction_mode": "disabled"},
+ }
+ result = await getattr(service, method_name)(
+ node_id="agent-1",
+ parameters={
+ "provider": "openai",
+ "model": "gpt-4o-mini",
+ "api_key": "secret",
+ "prompt": "exact user prompt",
+ "system_message": "exact system prompt",
+ },
+ memory_data=descriptor,
+ workflow_id=descriptor["workflow_id"],
+ context={
+ "execution_id": "execution-1",
+ "workflow_id": descriptor["workflow_id"],
+ },
+ database=context_database,
+ )
+
+ assert result["success"] is True, result
+ assert result["result"]["context"]["thread_id"] == "session:chat-1"
+ assert "memory" not in result["result"]
+ assert [message.role for message in unifier.calls[0]["messages"]] == [
+ "system",
+ "user",
+ ]
+
+ store = AgentContextStore(context_database)
+ ref = await store.resolve_thread(
+ workflow_id=descriptor["workflow_id"],
+ context_node_id=descriptor["context_node_id"],
+ generation=1,
+ session_id="chat-1",
+ )
+ journal, _ = await store.load_journal_page(ref)
+ assert [event.event_type for event in journal] == [
+ "request.snapshot",
+ "message.assistant",
+ "response.final",
+ ]
+ assert all(
+ "exact user prompt"
+ not in event.model_dump_json()
+ for event in journal
+ if event.event_type != "message.assistant"
+ )
+
+
+async def test_session_thread_reconstructs_exact_prior_turn_on_next_run(
+ context_database, # noqa: F811
+):
+ from services.ai import AIService
+
+ unifier = _ContextUnifier(["first answer", "second answer"])
+ service = AIService(
+ auth_service=object(),
+ database=context_database,
+ cache=None,
+ settings=object(),
+ chat_unifier=unifier,
+ )
+ base_descriptor = {
+ "kind": "context",
+ "workflow_id": "workflow-continuity",
+ "context_node_id": "context-continuity",
+ "generation": 1,
+ "session_id": "persistent-chat",
+ "delegated_task_id": None,
+ "policy": {"compaction_mode": "disabled"},
+ }
+ for execution_id, prompt in (
+ ("execution-1", "first prompt"),
+ ("execution-2", "second prompt"),
+ ):
+ descriptor = {
+ **base_descriptor,
+ "execution_id": execution_id,
+ }
+ result = await service.execute_chat_agent(
+ node_id="agent-1",
+ parameters={
+ "provider": "openai",
+ "model": "gpt-4o-mini",
+ "api_key": "secret",
+ "prompt": prompt,
+ "system_message": "stable system",
+ },
+ memory_data=descriptor,
+ workflow_id=descriptor["workflow_id"],
+ context={"execution_id": execution_id},
+ database=context_database,
+ )
+ assert result["success"] is True, result
+
+ second_messages = unifier.calls[1]["messages"]
+ assert [message.role for message in second_messages] == [
+ "system",
+ "user",
+ "assistant",
+ "user",
+ ]
+ assert [message.content for message in second_messages] == [
+ "stable system",
+ "first prompt",
+ "first answer",
+ "second prompt",
+ ]
diff --git a/server/tests/services/agent_context/test_store.py b/server/tests/services/agent_context/test_store.py
new file mode 100644
index 00000000..291cf6c7
--- /dev/null
+++ b/server/tests/services/agent_context/test_store.py
@@ -0,0 +1,1231 @@
+from __future__ import annotations
+
+import asyncio
+import importlib.util
+import sys
+import uuid
+from pathlib import Path
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+from sqlmodel import select
+
+from models.agent_context import (
+ AgentContextBlobRecord,
+ AgentContextCheckpointRecord,
+ AgentContextEventRecord,
+ AgentContextRef,
+ AgentContextThreadRecord,
+)
+from nodes.context import AgentContextNode
+from nodes.context import _handlers as context_handlers
+from services.agent_context import (
+ AgentContextError,
+ AgentContextStore,
+ AgentContextTransitionWriter,
+ ContextArchivedError,
+ StaleEpochError,
+ import_generation_zero_handoff,
+ reconstruct_message_wire_v2,
+)
+from services.llm.protocol import (
+ ContentBlock,
+ Message,
+ ToolCall,
+ message_to_wire,
+)
+
+
+async def context_database():
+ # Root conftest stubs core.database for fast plugin tests. Load the real
+ # module privately for SQLite transaction/restart coverage.
+ module_name = f"tests._real_context_database_{uuid.uuid4().hex}"
+ spec = importlib.util.spec_from_file_location(
+ module_name,
+ Path(__file__).resolve().parents[3] / "core" / "database.py",
+ )
+ assert spec is not None and spec.loader is not None
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[module_name] = module
+ spec.loader.exec_module(module)
+
+ db_path = Path.cwd() / f".agent-context-{uuid.uuid4().hex}.db"
+ settings = SimpleNamespace(
+ database_url=f"sqlite+aiosqlite:///{db_path.as_posix()}",
+ database_echo=False,
+ database_pool_size=5,
+ database_max_overflow=5,
+ )
+ database = module.Database(settings)
+ await database.startup()
+ try:
+ yield database
+ finally:
+ await database.shutdown()
+ sys.modules.pop(module_name, None)
+ for candidate in (
+ db_path,
+ Path(f"{db_path}-wal"),
+ Path(f"{db_path}-shm"),
+ ):
+ candidate.unlink(missing_ok=True)
+
+
+def _wire(content: str, *, with_tool: bool = False) -> dict:
+ calls = (
+ [
+ ToolCall(
+ id="call-1",
+ name="lookup",
+ args={"query": content},
+ raw_arguments=f'{{"query":"{content}"}}',
+ )
+ ]
+ if with_tool
+ else []
+ )
+ blocks = [ContentBlock(type="text", text=content)]
+ if calls:
+ blocks.append(ContentBlock(type="tool_call", tool_call=calls[0]))
+ return dict(
+ message_to_wire(
+ Message(
+ role="assistant",
+ content=content,
+ tool_calls=calls,
+ blocks=blocks,
+ provider_state={
+ "openai": {
+ "output": [{"type": "reasoning", "id": "rs_1"}]
+ }
+ },
+ )
+ )
+ )
+
+
+async def _resolve(
+ store: AgentContextStore,
+ **overrides,
+) -> AgentContextRef:
+ values = {
+ "workflow_id": "workflow-1",
+ "context_node_id": "workflow-1:context:1",
+ "generation": 2,
+ "execution_id": "execution-1",
+ }
+ values.update(overrides)
+ return await store.resolve_thread(**values)
+
+
+async def test_thread_resolution_priority_and_isolation(context_database):
+ store = AgentContextStore(context_database)
+ execution = await _resolve(store)
+ task = await _resolve(
+ store,
+ delegated_task_id="task-1",
+ execution_id="execution-2",
+ )
+ session = await _resolve(
+ store,
+ session_id="chat-1",
+ delegated_task_id="task-2",
+ execution_id="execution-3",
+ )
+ same_session = await _resolve(
+ store,
+ session_id="chat-1",
+ execution_id="execution-99",
+ )
+
+ assert execution.thread_id == "execution:execution-1"
+ assert task.thread_id == "task:task-1"
+ assert session.thread_id == "session:chat-1"
+ assert same_session == session
+ assert len({execution.thread_id, task.thread_id, session.thread_id}) == 3
+ summary = await store.load_thread_summary(task)
+ assert summary.ref == task
+ assert summary.thread_kind == "task"
+
+
+async def test_generation_zero_legacy_artifact_handoff_is_idempotent(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ source = await _resolve(
+ store,
+ generation=0,
+ session_id="legacy-session",
+ execution_id=None,
+ )
+ artifact = {
+ "format": "legacy_markdown",
+ "fidelity": "legacy_partial",
+ "content": "# Conversation History\n\nUser: keep this fact",
+ }
+ artifact_ref = await store.put_blob(artifact)
+ source = (
+ await store.append_transition(
+ source,
+ event_type="legacy_partial",
+ operation_id="legacy-import-1",
+ payload_ref=artifact_ref,
+ provider="legacy",
+ )
+ ).ref
+ await store.bind_provider(
+ source,
+ provider="claude_code",
+ binding_type="session_uuid",
+ binding={"session_uuid": "session-uuid-1"},
+ operation_id="legacy-binding-1",
+ )
+ target = await _resolve(
+ store,
+ generation=2,
+ session_id="legacy-session",
+ execution_id=None,
+ )
+
+ first = await import_generation_zero_handoff(store, target)
+ second = await import_generation_zero_handoff(store, first)
+ _, replay = await reconstruct_message_wire_v2(store, second)
+ bindings = await store.load_provider_bindings(
+ second,
+ provider="claude_code",
+ )
+
+ assert len(replay) == 1
+ assert "keep this fact" in replay[0]["content"]
+ assert (await store.load_active(second)).tail[0].payload_ref == artifact_ref
+ assert await store.get_blob(artifact_ref) == artifact
+ assert len(bindings) == 1
+ assert await store.get_blob(bindings[0].binding_ref) == {
+ "session_uuid": "session-uuid-1"
+ }
+
+ cleared = await store.start_epoch(
+ second,
+ operation_id="clear-after-legacy-handoff",
+ provider="openai",
+ )
+ cleared = await import_generation_zero_handoff(store, cleared)
+ assert (await store.load_active(cleared)).tail == []
+ assert await store.load_provider_bindings(
+ cleared,
+ provider="claude_code",
+ ) == []
+
+ later_target = await _resolve(
+ store,
+ generation=3,
+ session_id="legacy-session",
+ execution_id=None,
+ )
+ later = await import_generation_zero_handoff(store, later_target)
+ _, later_replay = await reconstruct_message_wire_v2(store, later)
+ assert len(later_replay) == 1
+
+
+async def test_workflow_reset_rotates_only_the_active_generation(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ migration = await _resolve(store, generation=0)
+ current = await _resolve(store, generation=2)
+ other = await _resolve(store, generation=3)
+
+ result = await AgentContextNode.reset_execution_state(
+ node_id=current.context_node_id,
+ workflow_id=current.workflow_id,
+ execution_id="execution-reset",
+ generation=2,
+ graph={},
+ database=context_database,
+ )
+ migration_after = (
+ await store.list_threads(
+ workflow_id=migration.workflow_id,
+ context_node_id=migration.context_node_id,
+ generation=0,
+ )
+ )[0].ref
+ current_after = (
+ await store.list_threads(
+ workflow_id=current.workflow_id,
+ context_node_id=current.context_node_id,
+ generation=2,
+ )
+ )[0].ref
+ other_after = (
+ await store.list_threads(
+ workflow_id=other.workflow_id,
+ context_node_id=other.context_node_id,
+ generation=3,
+ )
+ )[0].ref
+
+ assert result["rotated_threads"] == 1
+ assert migration_after.epoch == migration.epoch
+ assert current_after.epoch == current.epoch + 1
+ assert other_after.epoch == other.epoch
+
+
+async def test_workflow_reset_scans_beyond_one_thousand_threads(
+ context_database,
+ monkeypatch,
+):
+ thread_count = 1001
+ async with context_database.get_session() as session:
+ session.add_all(
+ [
+ AgentContextThreadRecord(
+ workflow_id="workflow-paged-reset",
+ context_node_id="context-paged-reset",
+ generation=7,
+ thread_id=f"session:thread-{index:04d}",
+ thread_kind="session",
+ )
+ for index in range(thread_count)
+ ]
+ )
+ await session.commit()
+
+ rotated: list[str] = []
+
+ async def rotate(
+ self,
+ ref,
+ *,
+ operation_id,
+ provider=None,
+ handoff_payload_ref=None,
+ ):
+ del self, operation_id, provider, handoff_payload_ref
+ rotated.append(ref.thread_id)
+ return ref.model_copy(
+ update={
+ "epoch": ref.epoch + 1,
+ "revision": ref.revision + 1,
+ }
+ )
+
+ monkeypatch.setattr(AgentContextStore, "start_epoch", rotate)
+ fence = AsyncMock()
+ dispatch = AsyncMock()
+ monkeypatch.setattr(
+ "services.agent_context.lifecycle."
+ "fence_context_provider_resources",
+ fence,
+ )
+ monkeypatch.setattr(
+ "nodes.context._events.dispatch_context_epoch_started",
+ dispatch,
+ )
+
+ result = await AgentContextNode.reset_execution_state(
+ node_id="context-paged-reset",
+ workflow_id="workflow-paged-reset",
+ execution_id="execution-paged-reset",
+ generation=7,
+ graph={},
+ database=context_database,
+ )
+
+ assert result == {
+ "reset": True,
+ "rotated_threads": thread_count,
+ }
+ assert len(rotated) == thread_count
+ assert len(set(rotated)) == thread_count
+ assert fence.await_count == thread_count
+ assert dispatch.await_count == thread_count
+
+
+async def test_append_is_exact_ordered_hash_chained_and_idempotent(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ first = await store.append_transition(
+ ref,
+ event_type="assistant",
+ operation_id="provider-response-1",
+ message_wire_v2=_wire("first", with_tool=True),
+ provider="openai",
+ )
+ duplicate = await store.append_transition(
+ ref,
+ event_type="assistant",
+ operation_id="provider-response-1",
+ message_wire_v2=_wire("first", with_tool=True),
+ provider="openai",
+ )
+ with pytest.raises(
+ AgentContextError,
+ match="context_operation_id_reuse_mismatch",
+ ):
+ await store.append_transition(
+ ref,
+ event_type="assistant",
+ operation_id="provider-response-1",
+ message_wire_v2=_wire(
+ "this must not replace the committed event"
+ ),
+ provider="openai",
+ )
+ second = await store.append_transition(
+ first.ref,
+ event_type="tool_result",
+ operation_id="tool-result-1",
+ message_wire_v2=dict(
+ message_to_wire(
+ Message(
+ role="tool",
+ content='{"ok":true}',
+ tool_call_id="call-1",
+ name="lookup",
+ )
+ )
+ ),
+ provider="openai",
+ )
+
+ state = await store.load_active(second.ref)
+ assert [event.sequence for event in state.tail] == [1, 2]
+ assert duplicate.applied is False
+ assert duplicate.event.payload_hash == first.event.payload_hash
+ assert state.tail[0].message_wire_v2 == _wire("first", with_tool=True)
+ assert state.tail[1].previous_hash == state.tail[0].payload_hash
+ assert state.ref.revision == 2
+
+
+ ("field", "value", "error_code"),
+ [
+ (
+ "sequence",
+ 3,
+ "context_event_sequence_integrity_broken",
+ ),
+ (
+ "previous_hash",
+ "f" * 64,
+ "context_event_chain_integrity_broken",
+ ),
+ (
+ "payload_hash",
+ "e" * 64,
+ "context_event_hash_integrity_broken",
+ ),
+ ],
+)
+async def test_load_active_rejects_corrupt_event_chain(
+ context_database,
+ field,
+ value,
+ error_code,
+):
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ first = await store.append_transition(
+ ref,
+ event_type="message.assistant",
+ operation_id="integrity-event-1",
+ message_wire_v2=_wire("first"),
+ provider="openai",
+ )
+ second = await store.append_transition(
+ first.ref,
+ event_type="message.assistant",
+ operation_id="integrity-event-2",
+ message_wire_v2=_wire("second"),
+ provider="openai",
+ )
+
+ async with context_database.get_session() as session:
+ result = await session.execute(
+ select(AgentContextEventRecord).where(
+ AgentContextEventRecord.operation_id
+ == "integrity-event-2"
+ )
+ )
+ event = result.scalar_one()
+ setattr(event, field, value)
+ await session.commit()
+
+ with pytest.raises(AgentContextError, match=error_code):
+ await store.load_active(second.ref)
+
+
+ ("field", "value", "error_code"),
+ [
+ (
+ "covers_through_sequence",
+ 2,
+ "active_checkpoint_invariant_broken",
+ ),
+ (
+ "source_hash",
+ "f" * 64,
+ "checkpoint_source_integrity_broken",
+ ),
+ ],
+)
+async def test_load_active_rejects_corrupt_checkpoint_source(
+ context_database,
+ field,
+ value,
+ error_code,
+):
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ for index in range(1, 3):
+ ref = (
+ await store.append_transition(
+ ref,
+ event_type="message.assistant",
+ operation_id=f"checkpoint-integrity-{index}",
+ message_wire_v2=_wire(f"message-{index}"),
+ provider="openai",
+ )
+ ).ref
+ plan = await store.prepare_compaction(
+ ref,
+ operation_id="checkpoint-integrity-prepare",
+ provider="openai",
+ strategy="portable",
+ covers_through_sequence=1,
+ )
+ replay_ref = await store.put_blob({"messages": [_wire("summary")]})
+ await store.commit_checkpoint(
+ ref,
+ attempt_id=plan.attempt_id,
+ operation_id="checkpoint-integrity-commit",
+ replay_payload_ref=replay_ref,
+ active_token_count=10,
+ )
+
+ async with context_database.get_session() as session:
+ result = await session.execute(
+ select(AgentContextCheckpointRecord).where(
+ AgentContextCheckpointRecord.operation_id
+ == "checkpoint-integrity-commit"
+ )
+ )
+ checkpoint = result.scalar_one()
+ setattr(checkpoint, field, value)
+ await session.commit()
+
+ with pytest.raises(AgentContextError, match=error_code):
+ await store.load_active(ref)
+
+
+async def test_blob_digest_is_verified_for_direct_and_checkpoint_replay(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ first = await store.append_transition(
+ ref,
+ event_type="message.assistant",
+ operation_id="blob-integrity-event-1",
+ message_wire_v2=_wire("first"),
+ provider="openai",
+ )
+ second = await store.append_transition(
+ first.ref,
+ event_type="message.assistant",
+ operation_id="blob-integrity-event-2",
+ message_wire_v2=_wire("second"),
+ provider="openai",
+ )
+ plan = await store.prepare_compaction(
+ second.ref,
+ operation_id="blob-integrity-prepare",
+ provider="openai",
+ strategy="portable",
+ covers_through_sequence=1,
+ )
+ replay_ref = await store.put_blob({"messages": [_wire("summary")]})
+ await store.commit_checkpoint(
+ second.ref,
+ attempt_id=plan.attempt_id,
+ operation_id="blob-integrity-commit",
+ replay_payload_ref=replay_ref,
+ active_token_count=10,
+ )
+
+ digest = replay_ref.removeprefix("sha256:")
+ async with context_database.get_session() as session:
+ blob = await session.get(AgentContextBlobRecord, digest)
+ assert blob is not None
+ blob.json_payload = {"messages": [_wire("tampered")]}
+ await session.commit()
+
+ with pytest.raises(
+ AgentContextError,
+ match="context_blob_integrity_broken",
+ ):
+ await store.get_blob(replay_ref)
+ with pytest.raises(
+ AgentContextError,
+ match="context_blob_integrity_broken",
+ ):
+ await store.load_active(second.ref)
+
+
+async def test_parallel_appends_serialize_without_losing_events(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+
+ results = await asyncio.gather(
+ *(
+ store.append_transition(
+ ref,
+ event_type="message.assistant",
+ operation_id=f"parallel-{index}",
+ message_wire_v2=_wire(f"parallel-message-{index}"),
+ )
+ for index in range(8)
+ )
+ )
+ current = max(results, key=lambda result: result.ref.revision).ref
+ state = await store.load_active(current)
+ assert [event.sequence for event in state.tail] == list(range(1, 9))
+ assert {event.operation_id for event in state.tail} == {
+ f"parallel-{index}" for index in range(8)
+ }
+ for previous, current_event in zip(state.tail, state.tail[1:]):
+ assert current_event.previous_hash == previous.payload_hash
+
+
+async def test_epoch_rotation_fences_late_writes_and_keeps_handoff(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ old_ref = await _resolve(store)
+ old_ref = (
+ await store.append_transition(
+ old_ref,
+ event_type="message.assistant",
+ operation_id="before-provider-fork",
+ message_wire_v2=_wire("old epoch exact message"),
+ provider="openai",
+ )
+ ).ref
+ handoff_ref = await store.put_blob(
+ {"messages": [_wire("portable exact tail")]}
+ )
+ new_ref = await store.fork_provider(
+ old_ref,
+ provider="anthropic",
+ operation_id="provider-fork-1",
+ portable_handoff_ref=handoff_ref,
+ )
+
+ assert new_ref.epoch == old_ref.epoch + 1
+ current = await store.load_active(new_ref)
+ assert [event.event_type for event in current.tail] == [
+ "provider_handoff"
+ ]
+ assert current.tail[0].payload_ref == handoff_ref
+ journal, next_cursor = await store.load_journal_page(new_ref)
+ assert [event.event_type for event in journal] == [
+ "message.assistant",
+ "provider_handoff",
+ ]
+ assert next_cursor is None
+ with pytest.raises(StaleEpochError):
+ await store.append_transition(
+ old_ref,
+ event_type="assistant",
+ operation_id="late-provider-response",
+ message_wire_v2=_wire("late"),
+ )
+ # Retrying the already-committed rotation is idempotent even with the old
+ # reference that it deliberately fenced.
+ assert (
+ await store.fork_provider(
+ old_ref,
+ provider="anthropic",
+ operation_id="provider-fork-1",
+ portable_handoff_ref=handoff_ref,
+ )
+ ) == new_ref
+
+
+async def test_epoch_rotation_rejects_conflicting_operation_id_reuse(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ old_ref = await _resolve(store)
+ first_handoff = await store.put_blob(
+ {"messages": [_wire("first portable handoff")]}
+ )
+ second_handoff = await store.put_blob(
+ {"messages": [_wire("different portable handoff")]}
+ )
+ await store.fork_provider(
+ old_ref,
+ provider="anthropic",
+ operation_id="provider-fork-conflict",
+ portable_handoff_ref=first_handoff,
+ )
+
+ with pytest.raises(
+ AgentContextError,
+ match="context_operation_id_reuse_mismatch",
+ ):
+ await store.fork_provider(
+ old_ref,
+ provider="openai",
+ operation_id="provider-fork-conflict",
+ portable_handoff_ref=first_handoff,
+ )
+ with pytest.raises(
+ AgentContextError,
+ match="context_operation_id_reuse_mismatch",
+ ):
+ await store.fork_provider(
+ old_ref,
+ provider="anthropic",
+ operation_id="provider-fork-conflict",
+ portable_handoff_ref=second_handoff,
+ )
+
+
+async def test_compaction_cas_keeps_concurrent_tail(context_database):
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ for index in range(1, 4):
+ result = await store.append_transition(
+ ref,
+ event_type="assistant",
+ operation_id=f"response-{index}",
+ message_wire_v2=_wire(f"message-{index}"),
+ provider="openai",
+ )
+ ref = result.ref
+
+ plan = await store.prepare_compaction(
+ ref,
+ operation_id="compact-prepare-1",
+ provider="openai",
+ strategy="native",
+ covers_through_sequence=2,
+ )
+ concurrent = await store.append_transition(
+ ref,
+ event_type="assistant",
+ operation_id="response-4",
+ message_wire_v2=_wire("message-4"),
+ provider="openai",
+ )
+ replay_ref = await store.put_blob(
+ {"provider_output": ["opaque", "native", "items"]}
+ )
+ checkpoint = await store.commit_checkpoint(
+ concurrent.ref,
+ attempt_id=plan.attempt_id,
+ operation_id="compact-commit-1",
+ replay_payload_ref=replay_ref,
+ active_token_count=120,
+ )
+ state = await store.load_active(concurrent.ref.model_copy(
+ update={"revision": concurrent.ref.revision + 1}
+ ))
+
+ assert checkpoint.covers_through_sequence == 2
+ assert checkpoint.source_hash == plan.source_hash
+ assert [event.sequence for event in state.tail] == [3, 4]
+ assert state.checkpoint == checkpoint
+ assert state.active_token_count == 120
+
+
+async def test_failed_compaction_preserves_prior_active_state(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ for index in range(1, 3):
+ ref = (
+ await store.append_transition(
+ ref,
+ event_type="assistant",
+ operation_id=f"failure-response-{index}",
+ message_wire_v2=_wire(f"failure-{index}"),
+ )
+ ).ref
+ before = await store.load_active(ref)
+ plan = await store.prepare_compaction(
+ ref,
+ operation_id="failed-prepare",
+ provider="openai",
+ strategy="portable",
+ covers_through_sequence=1,
+ )
+ await store.fail_compaction(
+ attempt_id=plan.attempt_id,
+ error_code="candidate_did_not_reduce_tokens",
+ )
+ after = await store.load_active(ref)
+ assert after == before
+
+
+async def test_runtime_writer_blobs_payloads_and_reconstructs_portable_replay(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ writer = AgentContextTransitionWriter(store, await _resolve(store))
+ first_wire = _wire("checkpoint-message-1")
+ second_wire = _wire("checkpoint-message-2")
+ first = await writer.append_transition(
+ event_type="message.assistant",
+ operation_id="writer-1",
+ provider="openai",
+ message_wire_v2=first_wire,
+ payload={"usage": {"input_tokens": 10}},
+ )
+ second = await writer.append_transition(
+ event_type="message.assistant",
+ operation_id="writer-2",
+ provider="openai",
+ message_wire_v2=second_wire,
+ )
+ assert writer.ref == second.ref
+ assert first.event.payload_ref is not None
+ assert await store.get_blob(first.event.payload_ref) == {
+ "usage": {"input_tokens": 10}
+ }
+
+ plan = await store.prepare_compaction(
+ writer.ref,
+ operation_id="writer-prepare",
+ provider="openai",
+ strategy="portable",
+ covers_through_sequence=1,
+ )
+ replay_ref = await store.put_blob({"messages": [first_wire]})
+ await store.commit_checkpoint(
+ writer.ref,
+ attempt_id=plan.attempt_id,
+ operation_id="writer-commit",
+ replay_payload_ref=replay_ref,
+ active_token_count=20,
+ )
+ latest = writer.ref.model_copy(update={"revision": writer.ref.revision + 1})
+ reconstructed_ref, wires = await reconstruct_message_wire_v2(
+ store,
+ latest,
+ )
+ assert reconstructed_ref.revision == latest.revision
+ assert wires == [first_wire, second_wire]
+
+
+async def test_reconstruction_uses_latest_exact_request_snapshot(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ writer = AgentContextTransitionWriter(store, await _resolve(store))
+ system = dict(
+ message_to_wire(Message(role="system", content="exact system"))
+ )
+ user = dict(
+ message_to_wire(Message(role="user", content="exact user"))
+ )
+ assistant = _wire("exact assistant")
+ await writer.append_transition(
+ event_type="request.snapshot",
+ operation_id="snapshot-1",
+ provider="openai",
+ payload={"messages": [system, user], "tools": []},
+ )
+ await writer.append_transition(
+ event_type="message.assistant",
+ operation_id="assistant-after-snapshot",
+ provider="openai",
+ message_wire_v2=assistant,
+ )
+ # Metadata-only final events must not appear in provider replay.
+ await writer.append_transition(
+ event_type="response.final",
+ operation_id="final-after-snapshot",
+ provider="openai",
+ payload={"finish_reason": "stop"},
+ )
+
+ _, wires = await reconstruct_message_wire_v2(store, writer.ref)
+ assert wires == [system, user, assistant]
+
+
+async def test_provider_binding_is_opaque_and_archived_on_epoch_change(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ binding = await store.bind_provider(
+ ref,
+ provider="claude_code",
+ binding_type="session_uuid",
+ binding={"uuid": "secret-provider-session"},
+ operation_id="bind-1",
+ )
+
+ assert "secret-provider-session" not in binding.model_dump_json()
+ assert await store.get_blob(binding.binding_ref) == {
+ "uuid": "secret-provider-session"
+ }
+ assert await store.load_provider_bindings(ref) == [binding]
+ new_ref = await store.start_epoch(
+ ref,
+ provider="openai",
+ operation_id="epoch-after-binding",
+ )
+ assert await store.load_provider_bindings(new_ref) == []
+
+
+async def test_provider_binding_rejects_conflicting_operation_id_reuse(
+ context_database,
+):
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ binding = await store.bind_provider(
+ ref,
+ provider="vertex",
+ binding_type="interaction_id",
+ binding={"interaction_id": "interaction-1"},
+ operation_id="binding-conflict",
+ )
+ duplicate = await store.bind_provider(
+ ref,
+ provider="vertex",
+ binding_type="interaction_id",
+ binding={"interaction_id": "interaction-1"},
+ operation_id="binding-conflict",
+ )
+ assert duplicate == binding
+
+ conflicting_calls = (
+ {
+ "provider": "vertex",
+ "binding_type": "interaction_id",
+ "binding": {"interaction_id": "interaction-2"},
+ },
+ {
+ "provider": "claude_code",
+ "binding_type": "session_uuid",
+ "binding": {"interaction_id": "interaction-1"},
+ },
+ {
+ "provider": "vertex",
+ "binding_type": "interaction_id",
+ "binding": {"interaction_id": "interaction-1"},
+ "fidelity": "observable_only",
+ },
+ )
+ for call in conflicting_calls:
+ with pytest.raises(
+ AgentContextError,
+ match="context_operation_id_reuse_mismatch",
+ ):
+ await store.bind_provider(
+ ref,
+ operation_id="binding-conflict",
+ **call,
+ )
+
+
+async def test_archive_fences_writes_and_purge_removes_state(context_database):
+ store = AgentContextStore(context_database)
+ retained_blob_ref = await store.put_blob({"pending_reference": True})
+ ref = await _resolve(store)
+ ref = (
+ await store.append_transition(
+ ref,
+ event_type="assistant",
+ operation_id="before-archive",
+ message_wire_v2=_wire("preserved until purge"),
+ )
+ ).ref
+ archived = await store.archive(ref, operation_id="archive-1")
+ with pytest.raises((StaleEpochError, ContextArchivedError)):
+ await store.append_transition(
+ ref,
+ event_type="assistant",
+ operation_id="after-archive",
+ message_wire_v2=_wire("must not commit"),
+ )
+
+ # Archive is recoverable and idempotent; purge is the separate,
+ # explicitly destructive operation.
+ assert (
+ await store.archive(ref, operation_id="archive-1")
+ ) == archived
+ assert await store.purge(
+ workflow_id=ref.workflow_id,
+ context_node_id=ref.context_node_id,
+ generation=ref.generation,
+ ) == 1
+ assert await store.get_blob(retained_blob_ref) == {
+ "pending_reference": True
+ }
+ fresh = await _resolve(store)
+ assert fresh.revision == 0
+ assert (await store.load_active(fresh)).tail == []
+
+
+async def test_archive_context_defaults_to_every_generation(context_database):
+ store = AgentContextStore(context_database)
+ generation_two = await _resolve(store, execution_id="execution-g2")
+ generation_three = await _resolve(
+ store,
+ generation=3,
+ execution_id="execution-g3",
+ )
+
+ archived = await store.archive_context(
+ workflow_id="workflow-1",
+ context_node_id="workflow-1:context:1",
+ operation_id="archive-all-generations",
+ )
+ assert {
+ (ref.generation, ref.thread_id) for ref in archived
+ } == {
+ (2, generation_two.thread_id),
+ (3, generation_three.thread_id),
+ }
+ for ref in archived:
+ with pytest.raises(ContextArchivedError):
+ await store.load_active(ref)
+
+
+async def test_authorized_handlers_paginate_raw_data_but_redact_exports(
+ context_database,
+ monkeypatch,
+):
+ await context_database.save_workflow(
+ "workflow-1",
+ "Context test",
+ "Context_test",
+ {
+ "owner_id": "authenticated-user",
+ "nodes": [
+ {
+ "id": "workflow-1:context:1",
+ "type": "context",
+ "data": {"label": "Context"},
+ }
+ ],
+ "edges": [],
+ },
+ )
+ monkeypatch.setattr(
+ context_handlers,
+ "_database",
+ lambda: context_database,
+ )
+ socket = SimpleNamespace(
+ state=SimpleNamespace(user_id="authenticated-user"),
+ scope={"path": "/ws/status"},
+ )
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ secret_payload = {"resolved_system_instruction": "classified prompt"}
+ first = await AgentContextTransitionWriter(
+ store,
+ ref,
+ ).append_transition(
+ event_type="request.snapshot",
+ operation_id="handler-event-1",
+ provider="openai",
+ payload=secret_payload,
+ )
+ await store.append_transition(
+ first.ref,
+ event_type="message.assistant",
+ operation_id="handler-event-2",
+ provider="openai",
+ message_wire_v2=_wire("classified response"),
+ )
+
+ page = await context_handlers.handle_get_agent_context(
+ {
+ "workflow_id": "workflow-1",
+ "context_node_id": "workflow-1:context:1",
+ "view": "journal",
+ "limit": 1,
+ },
+ socket,
+ )
+ assert page["success"] is True
+ assert page["context"]["events"][0]["payload"] == secret_payload
+ assert page["context"]["next_cursor"]
+ second_page = await context_handlers.handle_get_agent_context(
+ {
+ "workflow_id": "workflow-1",
+ "context_node_id": "workflow-1:context:1",
+ "view": "journal",
+ "limit": 1,
+ "cursor": page["context"]["next_cursor"],
+ },
+ socket,
+ )
+ assert [
+ event["sequence"] for event in second_page["context"]["events"]
+ ] == [2]
+
+ exported = await context_handlers.handle_export_agent_context(
+ {
+ "workflow_id": "workflow-1",
+ "context_node_id": "workflow-1:context:1",
+ },
+ socket,
+ )
+ assert exported["success"] is True
+ assert '"redacted": true' in exported["content"]
+ assert "classified prompt" not in exported["content"]
+ assert "classified response" not in exported["content"]
+ assert "message_wire_v2" not in exported["content"]
+ assert "payload_ref" not in exported["content"]
+
+ unauthorized = await context_handlers.handle_get_agent_context(
+ {
+ "workflow_id": "workflow-1",
+ "context_node_id": "another-context",
+ },
+ socket,
+ )
+ assert unauthorized["success"] is False
+ wrong_owner = await context_handlers.handle_get_agent_context(
+ {
+ "workflow_id": "workflow-1",
+ "context_node_id": "workflow-1:context:1",
+ },
+ SimpleNamespace(
+ state=SimpleNamespace(user_id="another-user"),
+ scope={"path": "/ws/status"},
+ ),
+ )
+ assert wrong_owner["success"] is False
+ internal = await context_handlers.handle_get_agent_context(
+ {
+ "workflow_id": "workflow-1",
+ "context_node_id": "workflow-1:context:1",
+ },
+ SimpleNamespace(scope={"path": "/ws/internal"}),
+ )
+ assert internal["success"] is False
+
+
+async def test_clear_and_fork_handlers_rotate_epoch_with_metadata_events(
+ context_database,
+ monkeypatch,
+):
+ await context_database.save_workflow(
+ "workflow-1",
+ "Context lifecycle",
+ "Context_lifecycle",
+ {
+ "nodes": [
+ {
+ "id": "workflow-1:context:1",
+ "type": "context",
+ "data": {},
+ }
+ ],
+ "edges": [],
+ },
+ )
+ monkeypatch.setattr(
+ context_handlers,
+ "_database",
+ lambda: context_database,
+ )
+ dispatch = AsyncMock()
+ monkeypatch.setattr(
+ context_handlers,
+ "dispatch_context_epoch_started",
+ dispatch,
+ )
+ socket = SimpleNamespace(scope={"path": "/ws/status"})
+ store = AgentContextStore(context_database)
+ ref = await _resolve(store)
+ ref = (
+ await store.append_transition(
+ ref,
+ event_type="message.assistant",
+ operation_id="lifecycle-event",
+ provider="openai",
+ message_wire_v2=_wire("before clear"),
+ )
+ ).ref
+
+ cleared = await context_handlers.handle_clear_agent_context(
+ {
+ "workflow_id": "workflow-1",
+ "context_node_id": "workflow-1:context:1",
+ "request_id": "clear-request-1",
+ },
+ socket,
+ )
+ assert cleared["success"] is True
+ assert cleared["context"]["epoch"] == ref.epoch + 1
+ current_ref = ref.model_copy(
+ update={
+ "epoch": cleared["context"]["epoch"],
+ "revision": cleared["context"]["revision"],
+ }
+ )
+ assert (await store.load_active(current_ref)).tail == []
+
+ forked = await context_handlers.handle_fork_agent_context(
+ {
+ "workflow_id": "workflow-1",
+ "context_node_id": "workflow-1:context:1",
+ "provider": "anthropic",
+ "request_id": "fork-request-1",
+ },
+ socket,
+ )
+ assert forked["success"] is True
+ assert forked["context"]["epoch"] == current_ref.epoch + 1
+ latest = current_ref.model_copy(
+ update={
+ "epoch": forked["context"]["epoch"],
+ "revision": forked["context"]["revision"],
+ }
+ )
+ state = await store.load_active(latest)
+ assert [event.event_type for event in state.tail] == [
+ "provider_handoff"
+ ]
+ assert dispatch.await_count == 2
+ assert dispatch.await_args_list[0].kwargs["reason"] == "clear"
+ assert dispatch.await_args_list[1].kwargs["reason"] == "fork"
diff --git a/server/tests/services/cli_agent/test_context_v2_bridge.py b/server/tests/services/cli_agent/test_context_v2_bridge.py
new file mode 100644
index 00000000..335cd9af
--- /dev/null
+++ b/server/tests/services/cli_agent/test_context_v2_bridge.py
@@ -0,0 +1,458 @@
+"""Context V2 contracts for specialized CLI providers."""
+
+from __future__ import annotations
+
+import asyncio
+from pathlib import Path
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from models.agent_context import (
+ AgentContextActiveState,
+ AgentContextEvent,
+ AgentContextRef,
+)
+from nodes.agent.claude_code_agent._pool import (
+ ClaudeSessionPool,
+ PooledClaudeSession,
+)
+from services.agent_context import reconstruct_message_wire_v2
+from services.cli_agent.context_bridge import SpecializedAgentContextBridge
+from services.cli_agent.protocol import CanonicalUsage
+from services.cli_agent.service import AICliService
+from services.cli_agent.types import ClaudeTaskSpec, CodexTaskSpec
+
+
+def _bridge(*, fidelity: str, resumable: bool):
+ ref = AgentContextRef(
+ workflow_id="wf",
+ context_node_id="wf:context:1",
+ generation=2,
+ thread_id="session:chat-1",
+ epoch=3,
+ revision=4,
+ )
+ writer = SimpleNamespace(
+ ref=ref,
+ append_transition=AsyncMock(),
+ )
+ store = SimpleNamespace()
+ return SpecializedAgentContextBridge(
+ store=store,
+ writer=writer,
+ provider="codex",
+ operation_prefix="op",
+ fidelity=fidelity,
+ resumable=resumable,
+ )
+
+
+async def test_specialized_bridge_rejects_generation_zero():
+ with pytest.raises(ValueError, match="admitted generation"):
+ await SpecializedAgentContextBridge.resolve(
+ object(),
+ {
+ "kind": "context",
+ "workflow_id": "wf",
+ "context_node_id": "ctx",
+ "generation": 0,
+ "execution_id": "run-1",
+ },
+ provider="codex",
+ fidelity="observable_only",
+ resumable=False,
+ )
+
+
+async def test_observable_only_event_is_explicitly_non_resumable_and_exact():
+ bridge = _bridge(fidelity="observable_only", resumable=False)
+ long_text = "x" * 10_000
+
+ await bridge.capture_provider_event(
+ {"type": "assistant", "content": long_text}
+ )
+
+ call = bridge.writer.append_transition.await_args.kwargs
+ assert call["event_type"] == "provider.stream"
+ assert call["payload"]["fidelity"] == "observable_only"
+ assert call["payload"]["observable_only"] is True
+ assert call["payload"]["non_resumable"] is True
+ assert call["payload"]["event"]["content"] == long_text
+
+
+async def test_claude_pool_key_contains_context_thread_and_epoch():
+ bridge = _bridge(fidelity="provider_bound", resumable=True)
+ assert bridge.pool_key == (
+ "wf:context:1",
+ "session:chat-1",
+ 3,
+ )
+
+
+async def test_provider_change_forks_epoch_with_portable_handoff(monkeypatch):
+ from services.cli_agent import context_bridge as bridge_module
+
+ old_ref = AgentContextRef(
+ workflow_id="wf",
+ context_node_id="ctx",
+ generation=4,
+ thread_id="execution:run-1",
+ epoch=1,
+ revision=2,
+ )
+ new_ref = old_ref.model_copy(update={"epoch": 2, "revision": 3})
+ event = AgentContextEvent(
+ sequence=1,
+ event_type="provider.result",
+ operation_id="old-result",
+ provider="codex",
+ previous_hash="0" * 64,
+ payload_hash="1" * 64,
+ payload_ref="sha256:old-result",
+ )
+ store = SimpleNamespace(
+ resolve_thread=AsyncMock(return_value=old_ref),
+ load_thread_summary=AsyncMock(
+ return_value=SimpleNamespace(ref=old_ref, provider="codex")
+ ),
+ load_active=AsyncMock(
+ return_value=AgentContextActiveState(
+ ref=old_ref,
+ tail=[event],
+ )
+ ),
+ get_blob=AsyncMock(
+ return_value={
+ "fidelity": "observable_only",
+ "event": {"response": "old provider answer"},
+ }
+ ),
+ put_blob=AsyncMock(return_value="sha256:handoff"),
+ fork_provider=AsyncMock(return_value=new_ref),
+ )
+ monkeypatch.setattr(
+ bridge_module,
+ "AgentContextStore",
+ lambda database: store,
+ )
+ import_handoff = AsyncMock(return_value=old_ref)
+ monkeypatch.setattr(
+ bridge_module,
+ "import_generation_zero_handoff",
+ import_handoff,
+ )
+
+ bridge = await SpecializedAgentContextBridge.resolve(
+ object(),
+ {
+ "kind": "context",
+ "workflow_id": "wf",
+ "context_node_id": "ctx",
+ "generation": 4,
+ "execution_id": "run-1",
+ },
+ provider="rlm",
+ fidelity="observable_only",
+ resumable=False,
+ )
+
+ assert bridge.ref == new_ref
+ import_handoff.assert_awaited_once_with(store, old_ref)
+ kwargs = store.fork_provider.await_args.kwargs
+ assert kwargs["provider"] == "rlm"
+ assert kwargs["portable_handoff_ref"] == "sha256:handoff"
+ handoff = store.put_blob.await_args.args[0]
+ assert handoff["from_provider"] == "codex"
+ assert handoff["to_provider"] == "rlm"
+ assert handoff["messages"]
+ assert "old provider answer" in handoff["messages"][0]["content"]
+ replayed = bridge.augment_prompt("new request")
+ assert "old provider answer" in replayed
+ assert replayed.endswith("new request")
+
+ handoff_event = AgentContextEvent(
+ sequence=2,
+ event_type="provider_handoff",
+ operation_id="fork:handoff",
+ provider="rlm",
+ previous_hash="1" * 64,
+ payload_hash="2" * 64,
+ payload_ref="sha256:handoff",
+ )
+ store.load_active.return_value = AgentContextActiveState(
+ ref=new_ref,
+ tail=[handoff_event],
+ )
+ store.get_blob.return_value = handoff
+ _, reconstructed = await reconstruct_message_wire_v2(
+ store,
+ new_ref,
+ )
+ assert "old provider answer" in reconstructed[0]["content"]
+
+
+async def test_pending_provider_handoff_is_recovered_before_request(
+ monkeypatch,
+):
+ from services.cli_agent import context_bridge as bridge_module
+
+ ref = AgentContextRef(
+ workflow_id="wf",
+ context_node_id="ctx",
+ generation=4,
+ thread_id="execution:run-1",
+ epoch=2,
+ revision=3,
+ )
+ handoff = AgentContextEvent(
+ sequence=4,
+ event_type="provider_handoff",
+ operation_id="fork:handoff",
+ provider="rlm",
+ previous_hash="0" * 64,
+ payload_hash="1" * 64,
+ payload_ref="sha256:handoff",
+ )
+ request = AgentContextEvent(
+ sequence=5,
+ event_type="provider.request",
+ operation_id="retry:request",
+ provider="rlm",
+ previous_hash="1" * 64,
+ payload_hash="2" * 64,
+ )
+ message = {
+ "version": 2,
+ "role": "assistant",
+ "content": "committed prior answer",
+ "blocks": [{"type": "text", "text": "committed prior answer"}],
+ "tool_calls": [],
+ "tool_call_id": None,
+ "name": None,
+ "provider_state": {},
+ }
+ store = SimpleNamespace(
+ resolve_thread=AsyncMock(return_value=ref),
+ load_thread_summary=AsyncMock(
+ return_value=SimpleNamespace(ref=ref, provider="rlm")
+ ),
+ load_active=AsyncMock(
+ return_value=AgentContextActiveState(
+ ref=ref,
+ tail=[handoff, request],
+ )
+ ),
+ get_blob=AsyncMock(
+ return_value={"messages": [message]}
+ ),
+ )
+ monkeypatch.setattr(
+ bridge_module,
+ "AgentContextStore",
+ lambda database: store,
+ )
+ monkeypatch.setattr(
+ bridge_module,
+ "import_generation_zero_handoff",
+ AsyncMock(return_value=ref),
+ )
+
+ bridge = await SpecializedAgentContextBridge.resolve(
+ object(),
+ {
+ "kind": "context",
+ "workflow_id": "wf",
+ "context_node_id": "ctx",
+ "generation": 4,
+ "execution_id": "run-1",
+ },
+ provider="rlm",
+ fidelity="observable_only",
+ resumable=False,
+ )
+
+ assert "committed prior answer" in bridge.augment_prompt("continue")
+
+
+async def test_cli_adapter_sends_portable_handoff_in_effective_prompt(
+ monkeypatch,
+):
+ bridge = SimpleNamespace(
+ augment_prompt=lambda prompt: f"prior portable answer\n{prompt}",
+ append_observable=AsyncMock(),
+ )
+ monkeypatch.setattr(
+ "services.cli_agent.service."
+ "SpecializedAgentContextBridge.resolve",
+ AsyncMock(return_value=bridge),
+ )
+ monkeypatch.setattr(
+ "services.plugin.deps.get_database",
+ lambda: object(),
+ )
+ monkeypatch.setattr(
+ AICliService,
+ "_resolve_repo_root",
+ AsyncMock(return_value=None),
+ )
+
+ non_directory = Path(__file__).resolve()
+ result = await AICliService().run_batch(
+ "codex",
+ tasks=[CodexTaskSpec(prompt="current request")],
+ node_id="codex-1",
+ workflow_id="wf",
+ workspace_dir=non_directory,
+ repo_root=non_directory,
+ broadcaster=None,
+ connected_context={
+ "kind": "context",
+ "workflow_id": "wf",
+ "context_node_id": "ctx",
+ "generation": 2,
+ "execution_id": "run-1",
+ },
+ execution_id="run-1",
+ )
+
+ assert result.tasks[0].prompt == (
+ "prior portable answer\ncurrent request"
+ )
+ request = bridge.append_observable.await_args_list[0].args[1]
+ assert request["tasks"][0].prompt == (
+ "prior portable answer\ncurrent request"
+ )
+
+
+async def test_pool_terminates_previous_epoch_before_new_context_acquire(
+ monkeypatch,
+):
+ old_key = ("wf:context:1", "session:chat-1", 1)
+ new_key = ("wf:context:1", "session:chat-1", 2)
+ old = PooledClaudeSession(
+ memory_node_id=old_key,
+ process=SimpleNamespace(returncode=None, pid=1),
+ cwd=Path.cwd(),
+ )
+ new = PooledClaudeSession(
+ memory_node_id=new_key,
+ process=SimpleNamespace(returncode=None, pid=2),
+ cwd=Path.cwd(),
+ )
+ pool = object.__new__(ClaudeSessionPool)
+ pool._pool = {old_key: old}
+ pool._pool_lock = asyncio.Lock()
+ pool._max_size = 16
+ terminated = []
+
+ async def terminate(key, *, reason="explicit"):
+ terminated.append((key, reason))
+ pool._pool.pop(key, None)
+
+ monkeypatch.setattr(pool, "_terminate_locked", terminate)
+ monkeypatch.setattr(pool, "_spawn", AsyncMock(return_value=new))
+ monkeypatch.setattr(pool, "_emit_event", AsyncMock())
+
+ acquired = await pool.acquire(
+ new_key,
+ spec=ClaudeTaskSpec(prompt="next epoch"),
+ cwd=Path.cwd(),
+ env={},
+ defaults={},
+ mcp_endpoint_url=None,
+ mcp_bearer_token=None,
+ )
+ await pool.release(acquired)
+
+ assert terminated == [(old_key, "epoch_changed")]
+ assert old_key not in pool._pool
+ assert pool._pool[new_key] is new
+
+
+class _FullResponseProvider:
+ name = "claude"
+
+ @staticmethod
+ def is_final_event(event):
+ return event.get("type") == "result"
+
+ @staticmethod
+ def event_to_session_result(events, stderr, exit_code):
+ del stderr, exit_code
+ return {
+ "session_id": "uuid-1",
+ "response": events[-1]["result"],
+ "canonical_usage": CanonicalUsage(),
+ "success": True,
+ }
+
+ @staticmethod
+ def canonical_usage(events):
+ del events
+ return CanonicalUsage()
+
+
+async def test_pool_sends_full_raw_event_to_context_before_ui_truncation():
+ raw = "z" * 10_000
+ captured = []
+
+ async def sink(event):
+ captured.append(event)
+
+ session = PooledClaudeSession(
+ memory_node_id=("ctx", "execution:1", 1),
+ process=SimpleNamespace(returncode=None, pid=1),
+ cwd=Path.cwd(),
+ context_event_sink=sink,
+ )
+ pool = object.__new__(ClaudeSessionPool)
+ pool._provider = _FullResponseProvider()
+
+ event = {"type": "result", "result": raw, "session_id": "uuid-1"}
+ await pool._handle_stream_event(session, event)
+ result = pool._build_result_from_events(
+ session=session,
+ events=[event],
+ prompt="hello",
+ success=True,
+ )
+
+ assert captured[0]["result"] == raw
+ assert len(result.response) == 4_000
+
+
+async def test_pool_surfaces_context_persistence_failure():
+ async def broken_sink(event):
+ del event
+ raise RuntimeError("database unavailable")
+
+ session = PooledClaudeSession(
+ memory_node_id=("ctx", "execution:1", 1),
+ process=SimpleNamespace(returncode=None, pid=1),
+ cwd=Path.cwd(),
+ context_event_sink=broken_sink,
+ )
+ pool = object.__new__(ClaudeSessionPool)
+ pool._provider = _FullResponseProvider()
+
+ await pool._handle_stream_event(
+ session,
+ {"type": "assistant", "message": {"content": "secret"}},
+ )
+
+ assert session.result_event.is_set()
+ assert "context_event_persistence_failed" in (
+ session.context_capture_error or ""
+ )
+ assert session.events_this_turn == []
diff --git a/server/tests/services/cli_agent/test_service.py b/server/tests/services/cli_agent/test_service.py
index ff0ceee3..9244d362 100644
--- a/server/tests/services/cli_agent/test_service.py
+++ b/server/tests/services/cli_agent/test_service.py
@@ -13,12 +13,14 @@ from __future__ import annotations
import tempfile
from pathlib import Path
-from unittest.mock import MagicMock
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
import pytest
from services.cli_agent import ClaudeTaskSpec, CodexTaskSpec
from services.cli_agent.service import AICliService, get_ai_cli_service
+from nodes.search.duckduckgo_search import DuckDuckGoSearchParams
@pytest.mark.asyncio
@@ -272,6 +274,23 @@ async def test_run_batch_registers_mcp_batch_on_happy_path(monkeypatch):
svc = get_ai_cli_service()
workspace = Path(__file__).resolve().parents[3] # the repo root (a git repo)
+ tool_builder = SimpleNamespace(
+ _build_tool_from_node=AsyncMock(
+ return_value=(
+ SimpleNamespace(
+ name="web_search",
+ description="Search the web",
+ parameters=DuckDuckGoSearchParams.model_json_schema(),
+ args_schema=DuckDuckGoSearchParams,
+ ),
+ {
+ "node_id": "ddg_1",
+ "node_type": "duckduckgoSearch",
+ "parameters": {},
+ },
+ )
+ )
+ )
result = await svc.run_batch(
"claude",
tasks=[ClaudeTaskSpec(prompt="ping")],
@@ -284,12 +303,13 @@ async def test_run_batch_registers_mcp_batch_on_happy_path(monkeypatch):
{"node_id": "ddg_1", "node_type": "duckduckgoSearch", "label": "DDG", "parameters": {}},
],
connected_skill_names=["duckduckgo-search-skill"],
+ ai_service=tool_builder,
)
text = _logger_messages(svc_logger, mcp_logger)
# Engine-entry log:
assert "[CC-Agent run_batch] enter" in text, text
- assert "duckduckgoSearch" in text, text
+ assert "web_search" in text, text
# Resolver succeeded:
assert "[CC-Agent run_batch] resolved repo_root=" in text, text
# MCP token registered with our connected tool:
diff --git a/server/tests/services/cli_agent/test_workflow_tool_context.py b/server/tests/services/cli_agent/test_workflow_tool_context.py
index 8215acd8..dd4428ca 100644
--- a/server/tests/services/cli_agent/test_workflow_tool_context.py
+++ b/server/tests/services/cli_agent/test_workflow_tool_context.py
@@ -4,6 +4,7 @@ from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
+import pytest
from pydantic import BaseModel
from services.cli_agent import mcp_server, workflow_tools
@@ -14,6 +15,10 @@ class _Params(BaseModel):
value: str
+class _OtherParams(BaseModel):
+ count: int
+
+
async def test_handler_forwards_execution_workspace_and_parent_identity():
broadcaster = SimpleNamespace(
update_node_status=AsyncMock(),
@@ -30,6 +35,8 @@ async def test_handler_forwards_execution_workspace_and_parent_identity():
"node_id": "tool-1",
"node_type": "contextTestTool",
"label": "Context Tool",
+ "_agent_tool_name": "contextTestTool",
+ "_agent_tool_input_model": _Params,
"parameters": {},
}
],
@@ -58,3 +65,73 @@ async def test_handler_forwards_execution_workspace_and_parent_identity():
assert started.kwargs["target_node_id"] == "tool-1"
assert started.kwargs["workflow_id"] == "workflow-1"
assert started.kwargs["execution_id"] == "execution-1"
+
+
+async def test_handler_validates_the_authenticated_batch_schema():
+ context = BatchContext(
+ workflow_id="workflow-2",
+ node_id="cli-parent-2",
+ execution_id="execution-2",
+ workspace_dir=Path("workspace-2").resolve(),
+ broadcaster=SimpleNamespace(
+ update_node_status=AsyncMock(),
+ broadcast_agent_capability=AsyncMock(),
+ ),
+ connected_tools=[
+ {
+ "node_id": "tool-2",
+ "node_type": "contextTestTool",
+ "_agent_tool_name": "shared_tool",
+ "_agent_tool_input_model": _OtherParams,
+ "parameters": {},
+ }
+ ],
+ )
+ context_token = mcp_server._current_batch.set(context)
+ execute = AsyncMock(return_value={"ok": True})
+ try:
+ with patch("services.handlers.tools.execute_tool", new=execute):
+ handler = workflow_tools._build_handler(
+ "shared_tool",
+ "contextTestTool",
+ _Params,
+ )
+ await handler(count=3)
+ finally:
+ mcp_server._current_batch.reset(context_token)
+
+ assert execute.await_args.args[1] == {"count": 3}
+
+
+def test_global_mcp_name_rejects_incompatible_concurrent_schema(monkeypatch):
+ class _FakeMcp:
+ def add_tool(self, *_args, **_kwargs):
+ return None
+
+ def remove_tool(self, *_args, **_kwargs):
+ return None
+
+ from nodes.tool.current_time_tool import CurrentTimeToolNode
+
+ workflow_tools._reset_for_tests()
+ monkeypatch.setattr(workflow_tools, "_get_mcp", lambda: _FakeMcp())
+ first = {
+ "node_id": "tool-a",
+ "node_type": CurrentTimeToolNode.type,
+ "_agent_tool_name": "shared_tool",
+ "_agent_tool_input_model": _Params,
+ "_agent_tool_schema": _Params.model_json_schema(),
+ }
+ second = {
+ "node_id": "tool-b",
+ "node_type": CurrentTimeToolNode.type,
+ "_agent_tool_name": "shared_tool",
+ "_agent_tool_input_model": _OtherParams,
+ "_agent_tool_schema": _OtherParams.model_json_schema(),
+ }
+ workflow_tools.expose_workflow_tools([first])
+
+ with pytest.raises(ValueError, match="schema conflict"):
+ workflow_tools.expose_workflow_tools([second])
+
+ assert workflow_tools._active_tool_refcounts == {"shared_tool": 1}
diff --git a/server/tests/services/memory/test_tool_store.py b/server/tests/services/memory/test_tool_store.py
new file mode 100644
index 00000000..a02aefa8
--- /dev/null
+++ b/server/tests/services/memory/test_tool_store.py
@@ -0,0 +1,179 @@
+"""Durability/security contracts for the explicit Simple Memory tool."""
+
+from __future__ import annotations
+
+import importlib.util
+import sys
+import uuid
+from datetime import datetime, timedelta, timezone
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+from services.memory.tool_store import (
+ MemoryNotFoundError,
+ MemoryScope,
+ MemoryToolStore,
+ MemoryVersionConflictError,
+)
+
+
+async def memory_database():
+ # Root conftest stubs core.database for fast unit tests. Load the real
+ # implementation privately, matching test_runtime_mutations.py.
+ module_name = f"tests._real_memory_tool_database_{uuid.uuid4().hex}"
+ spec = importlib.util.spec_from_file_location(
+ module_name,
+ Path(__file__).resolve().parents[3] / "core" / "database.py",
+ )
+ assert spec is not None and spec.loader is not None
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[module_name] = module
+ spec.loader.exec_module(module)
+
+ db_path = Path.cwd() / f".memory-tool-{uuid.uuid4().hex}.db"
+ database = module.Database(
+ SimpleNamespace(
+ database_url=f"sqlite+aiosqlite:///{db_path.as_posix()}",
+ database_echo=False,
+ database_pool_size=5,
+ database_max_overflow=5,
+ )
+ )
+ await database.startup()
+ try:
+ yield database
+ finally:
+ await database.shutdown()
+ sys.modules.pop(module_name, None)
+ for candidate in (
+ db_path,
+ Path(f"{db_path}-wal"),
+ Path(f"{db_path}-shm"),
+ ):
+ candidate.unlink(missing_ok=True)
+
+
+async def test_namespace_isolation_and_durable_idempotency(memory_database):
+ store = MemoryToolStore(memory_database)
+ scope_a = MemoryScope("owner", "workflow", "memory-a")
+ scope_b = MemoryScope("owner", "workflow", "memory-b")
+
+ first = await store.remember(
+ scope_a,
+ content="Production database is in Mumbai",
+ tags=["infra"],
+ operation_id="tool-call-1",
+ )
+ replay = await store.remember(
+ scope_a,
+ content="This retry must not be inserted",
+ operation_id="tool-call-1",
+ )
+
+ assert first["receipt"]["applied"] is True
+ assert replay["receipt"]["applied"] is False
+ assert replay["memory"]["id"] == first["memory"]["id"]
+ assert (await store.list(scope_a))["count"] == 1
+ assert (await store.list(scope_b))["count"] == 0
+
+
+async def test_lexical_recall_survives_embedding_failure(memory_database):
+ async def broken_embedder(_content: str):
+ raise RuntimeError("embedding provider unavailable")
+
+ store = MemoryToolStore(memory_database, embedder=broken_embedder)
+ scope = MemoryScope("owner", "workflow", "memory")
+ remembered = await store.remember(
+ scope,
+ content="Customer prefers quarterly PDF invoices",
+ category="preference",
+ tags=["billing"],
+ operation_id="remember-invoice",
+ )
+
+ assert remembered["memory"]["indexing_state"] == "embedding_failed"
+ recalled = await store.recall(
+ scope,
+ query="quarterly invoices",
+ categories=["preference"],
+ tags=["billing"],
+ )
+ assert recalled["count"] == 1
+ assert recalled["items"][0]["id"] == remembered["memory"]["id"]
+ assert recalled["retrieval"] in {"fts", "sql"}
+
+
+async def test_optimistic_update_and_forget(memory_database):
+ store = MemoryToolStore(memory_database)
+ scope = MemoryScope("owner", "workflow", "memory")
+ created = await store.remember(
+ scope,
+ content="Release is Tuesday",
+ operation_id="remember-release",
+ )
+ memory_id = created["memory"]["id"]
+
+ updated = await store.update(
+ scope,
+ memory_id=memory_id,
+ expected_version=1,
+ patch={"content": "Release is Wednesday", "tags": ["release"]},
+ operation_id="update-release",
+ )
+ assert updated["memory"]["version"] == 2
+ assert updated["memory"]["content"] == "Release is Wednesday"
+
+ with pytest.raises(MemoryVersionConflictError):
+ await store.update(
+ scope,
+ memory_id=memory_id,
+ expected_version=1,
+ patch={"content": "stale"},
+ operation_id="stale-update",
+ )
+
+ await store.forget(
+ scope,
+ memory_id=memory_id,
+ expected_version=2,
+ operation_id="forget-release",
+ )
+ with pytest.raises(MemoryNotFoundError):
+ await store.get(scope, memory_id)
+
+
+async def test_expiry_pagination_and_reset_clear(memory_database):
+ store = MemoryToolStore(memory_database)
+ scope = MemoryScope("owner", "workflow", "memory")
+ await store.remember(
+ scope,
+ content="already expired",
+ expires_at=datetime.now(timezone.utc) - timedelta(seconds=1),
+ operation_id="expired",
+ )
+ for index in range(3):
+ await store.remember(
+ scope,
+ content=f"active-{index}",
+ operation_id=f"active-{index}",
+ )
+
+ first_page = await store.list(scope, limit=2)
+ second_page = await store.list(
+ scope, limit=2, cursor=first_page["next_cursor"]
+ )
+ assert first_page["count"] == 2
+ assert second_page["count"] == 1
+ assert all(
+ item["content"] != "already expired"
+ for item in [*first_page["items"], *second_page["items"]]
+ )
+
+ cleared = await store.clear_namespace(
+ scope, operation_id="workflow-reset"
+ )
+ assert cleared["cleared"] == 4
+ assert (await store.list(scope))["count"] == 0
diff --git a/server/tests/services/test_deployment_user_identity.py b/server/tests/services/test_deployment_user_identity.py
new file mode 100644
index 00000000..06640755
--- /dev/null
+++ b/server/tests/services/test_deployment_user_identity.py
@@ -0,0 +1,90 @@
+"""Authenticated user propagation through deployment-owned execution."""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from services.deployment.manager import DeploymentManager
+from services.deployment.state import DeploymentState
+
+
+def _manager(*, execute_workflow=None) -> DeploymentManager:
+ database = MagicMock()
+ database.get_workflow = AsyncMock(
+ return_value=SimpleNamespace(slug="workflow"),
+ )
+ broadcaster = MagicMock()
+ broadcaster.update_node_status = AsyncMock()
+ return DeploymentManager(
+ database=database,
+ execute_workflow_fn=execute_workflow or AsyncMock(),
+ store_output_fn=AsyncMock(),
+ broadcaster=broadcaster,
+ )
+
+
+def test_deployment_state_defaults_to_owner():
+ state = DeploymentState(
+ deployment_id="deploy-1",
+ workflow_id="workflow-1",
+ is_running=True,
+ nodes=[],
+ edges=[],
+ session_id="session",
+ )
+
+ assert state.user_id == "owner"
+ assert state.to_dict()["user_id"] == "owner"
+
+
+async def test_deploy_captures_authenticated_user_in_state():
+ manager = _manager()
+ manager._load_settings = AsyncMock()
+ manager._notify = AsyncMock()
+
+ result = await manager.deploy(
+ nodes=[],
+ edges=[],
+ workflow_id="workflow-1",
+ user_id="account-31",
+ )
+
+ assert result["success"] is True
+ assert manager._deployments["workflow-1"].user_id == "account-31"
+
+
+async def test_trigger_run_preserves_deployment_user():
+ execute_workflow = AsyncMock(return_value={"success": True})
+ manager = _manager(execute_workflow=execute_workflow)
+ manager._deployments["workflow-1"] = DeploymentState(
+ deployment_id="deploy-1",
+ workflow_id="workflow-1",
+ is_running=True,
+ nodes=[
+ {"id": "trigger-1", "type": "webhookTrigger", "data": {}},
+ {"id": "node-1", "type": "console", "data": {}},
+ ],
+ edges=[
+ {
+ "source": "trigger-1",
+ "target": "node-1",
+ "targetHandle": "input-main",
+ }
+ ],
+ session_id="session",
+ user_id="account-32",
+ )
+
+ await manager._execute_from_trigger(
+ "run-1",
+ "trigger-1",
+ {"event_data": {"value": 1}},
+ "workflow-1",
+ )
+
+ assert execute_workflow.await_args.kwargs["user_id"] == "account-32"
diff --git a/server/tests/services/test_native_agent_runtime.py b/server/tests/services/test_native_agent_runtime.py
index 921f0457..6d18d100 100644
--- a/server/tests/services/test_native_agent_runtime.py
+++ b/server/tests/services/test_native_agent_runtime.py
@@ -216,6 +216,118 @@ async def test_native_loop_replays_assistant_and_accumulates_usage():
assert result["thinking_content"] == "plan\n\n--- Iteration 2 ---\nanswer"
+async def test_compaction_pause_can_replace_replay_before_next_request():
+ compaction_message = Message(
+ role="assistant",
+ provider_state={
+ "provider": "anthropic",
+ "payload": {
+ "content": [
+ {
+ "type": "compaction",
+ "content": "durable checkpoint",
+ }
+ ]
+ },
+ },
+ )
+ unifier = _FakeUnifier(
+ [
+ LLMResponse(
+ assistant_message=compaction_message,
+ finish_reason="compaction",
+ ),
+ LLMResponse(content="continued after checkpoint"),
+ ]
+ )
+ pauses: list[int] = []
+
+ async def commit_pause(iteration, _response, _messages):
+ pauses.append(iteration)
+ return [compaction_message]
+
+ result = await run_native_agent_loop(
+ unifier,
+ provider="anthropic",
+ api_key="test",
+ model="claude-sonnet-4-6",
+ temperature=0,
+ max_tokens=100,
+ initial_messages=[Message(role="user", content="large history")],
+ compaction_pause_callback=commit_pause,
+ )
+
+ assert pauses == [1]
+ assert unifier.calls[1]["messages"] == [compaction_message]
+ assert result["response"].content == "continued after checkpoint"
+
+
+async def test_native_loop_persists_context_transitions_before_tool_execution():
+ first_message = Message(
+ role="assistant",
+ tool_calls=[ToolCall(id="call-1", name="one", args={"value": 3})],
+ provider_state={"provider": "openai", "payload": {"opaque": "exact"}},
+ )
+ unifier = _FakeUnifier(
+ [
+ LLMResponse(
+ tool_calls=first_message.tool_calls,
+ assistant_message=first_message,
+ usage=Usage(input_tokens=7, output_tokens=2),
+ ),
+ LLMResponse(
+ content="done",
+ usage=Usage(input_tokens=11, output_tokens=3),
+ ),
+ ]
+ )
+ observed: list[tuple[str, str]] = []
+ tool = _tool("one")
+
+ class _Sink:
+ async def append_transition(self, **kwargs):
+ observed.append(("context", kwargs["event_type"]))
+ if kwargs.get("message_wire_v2"):
+ assert kwargs["message_wire_v2"]["version"] == 2
+ assert kwargs["operation_id"].startswith("execution-9:")
+
+ async def execute(_name, args):
+ assert tool.execution["tool_call_id"] == "call-1"
+ assert (
+ tool.execution["operation_id"]
+ == "execution-9:iteration:1:tool:1:call-1"
+ )
+ observed.append(("tool", str(args["value"])))
+ return {"ok": True}
+
+ await run_native_agent_loop(
+ unifier,
+ provider="openai",
+ api_key="test",
+ model="gpt-test",
+ temperature=0,
+ max_tokens=100,
+ initial_messages=[Message(role="system", content="resolved"), Message(role="user", content="go")],
+ tools=[tool],
+ tool_executor=execute,
+ context_transition_sink=_Sink(),
+ context_operation_id="execution-9",
+ )
+
+ event_names = [value for kind, value in observed if kind == "context"]
+ assert event_names == [
+ "request.snapshot",
+ "message.assistant",
+ "message.tool_result",
+ "request.snapshot",
+ "message.assistant",
+ "response.final",
+ ]
+ assert observed.index(("context", "message.assistant")) < observed.index(("tool", "3"))
+
+
@pytest.mark.asyncio
async def test_native_loop_returns_invalid_arguments_to_model_without_execution():
invalid = ToolCall(
diff --git a/server/tests/services/test_rlm_context_v2.py b/server/tests/services/test_rlm_context_v2.py
new file mode 100644
index 00000000..c77f4eb7
--- /dev/null
+++ b/server/tests/services/test_rlm_context_v2.py
@@ -0,0 +1,195 @@
+"""RLM's honest observable-only Context V2 boundary."""
+
+from __future__ import annotations
+
+import asyncio
+import sys
+from types import ModuleType, SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+from pydantic import BaseModel
+
+from services.rlm.adapters import ToolBridgeAdapter
+from services.rlm.service import RLMService
+
+
+async def test_rlm_records_request_and_result_as_non_resumable_context():
+ bridge = MagicMock()
+ bridge.append_observable = AsyncMock()
+ bridge.augment_prompt = MagicMock(
+ side_effect=lambda prompt: f"portable-history\n{prompt}"
+ )
+ auth = MagicMock()
+ auth.get_api_key = AsyncMock(return_value="secret")
+ prompts = []
+
+ fake_rlm = ModuleType("rlm")
+
+ class RLM:
+ def __init__(self, **kwargs):
+ self.kwargs = kwargs
+
+ def completion(self, prompt):
+ prompts.append(prompt)
+ return SimpleNamespace(
+ response=f"answer:{prompt}",
+ metadata={"iterations": [{"index": 1}]},
+ usage_summary=SimpleNamespace(
+ total_cost=0.01,
+ total_input_tokens=12,
+ total_output_tokens=3,
+ ),
+ )
+
+ fake_rlm.RLM = RLM
+ fake_logger = ModuleType("rlm.logger")
+ fake_logger.RLMLogger = lambda: object()
+
+ with (
+ patch.dict(
+ sys.modules,
+ {"rlm": fake_rlm, "rlm.logger": fake_logger},
+ ),
+ patch(
+ "services.cli_agent.context_bridge."
+ "SpecializedAgentContextBridge.resolve",
+ AsyncMock(return_value=bridge),
+ ) as resolve,
+ patch(
+ "services.llm.config.is_model_valid_for_provider",
+ return_value=True,
+ ),
+ patch(
+ "services.rlm.service.BackendAdapter.adapt",
+ return_value=("backend", {}),
+ ),
+ patch(
+ "services.rlm.service.ChatModelExtractor.extract",
+ AsyncMock(return_value=([], [])),
+ ),
+ patch(
+ "services.rlm.service.ToolBridgeAdapter.bridge",
+ return_value=[],
+ ) as tool_bridge,
+ patch(
+ "services.skill_runtime.skill_tool_info",
+ return_value=None,
+ ),
+ ):
+ result = await RLMService(auth=auth).execute(
+ "rlm-1",
+ {
+ "prompt": "solve",
+ "provider": "openai",
+ "model": "gpt-4o",
+ },
+ context_data={
+ "kind": "context",
+ "workflow_id": "wf",
+ "context_node_id": "ctx",
+ "generation": 2,
+ "execution_id": "run-1",
+ },
+ workflow_id="wf",
+ context={"execution_id": "run-1"},
+ database=MagicMock(),
+ )
+
+ assert result["success"] is True
+ resolve_kwargs = resolve.await_args.kwargs
+ assert resolve_kwargs["fidelity"] == "observable_only"
+ assert resolve_kwargs["resumable"] is False
+ event_types = [
+ call.args[0] for call in bridge.append_observable.await_args_list
+ ]
+ assert event_types == ["provider.request", "provider.result"]
+ assert prompts == ["portable-history\nsolve"]
+ sink = tool_bridge.call_args.kwargs["ambiguous_outcome_sink"]
+ assert sink is not None
+ await sink({"outcome": "ambiguous", "reason": "tool_timeout"})
+ last = bridge.append_observable.await_args
+ assert last.args[0] == "tool.ambiguous_outcome"
+ assert last.args[1]["reason"] == "tool_timeout"
+ assert "session_id" not in result["result"]
+ assert "resume" not in result["result"]
+
+
+class _RememberInput(BaseModel):
+ content: str
+
+
+async def test_rlm_tool_timeout_cancels_and_records_exact_ambiguous_event(
+ monkeypatch,
+):
+ cancelled = asyncio.Event()
+
+ async def execute_tool(name, arguments, config):
+ assert name == "memory"
+ assert config["node_id"] == "memory-1"
+ try:
+ await asyncio.Event().wait()
+ except asyncio.CancelledError:
+ cancelled.set()
+ raise
+
+ monkeypatch.setattr(
+ "services.handlers.tools.execute_tool",
+ execute_tool,
+ )
+ monkeypatch.setattr(
+ ToolBridgeAdapter,
+ "TOOL_TIMEOUT_SECONDS",
+ 0.02,
+ )
+ monkeypatch.setattr(
+ ToolBridgeAdapter,
+ "CANCELLATION_GRACE_SECONDS",
+ 1.0,
+ )
+ events = []
+
+ async def record_ambiguous(payload):
+ events.append(payload)
+
+ tools = ToolBridgeAdapter.bridge(
+ [
+ {
+ "node_type": "simpleMemory",
+ "node_id": "memory-1",
+ "label": "Memory",
+ "parameters": {"reset_policy": "preserve"},
+ "_agent_tool_name": "memory",
+ "_agent_tool_input_model": _RememberInput,
+ "_agent_tool_execution": {
+ "namespace": "owner:wf:memory-1"
+ },
+ }
+ ],
+ context={"execution_id": "run-1"},
+ loop=asyncio.get_running_loop(),
+ ambiguous_outcome_sink=record_ambiguous,
+ )
+
+ with pytest.raises(TimeoutError, match="outcome is ambiguous"):
+ await asyncio.to_thread(
+ tools["memory"]["tool"],
+ content="exact durable value",
+ )
+
+ await asyncio.wait_for(cancelled.wait(), timeout=1)
+ assert events == [
+ {
+ "outcome": "ambiguous",
+ "reason": "tool_timeout",
+ "tool_name": "memory",
+ "tool_node_id": "memory-1",
+ "tool_node_type": "simpleMemory",
+ "arguments": {"content": "exact durable value"},
+ "timeout_seconds": 0.02,
+ "cancel_requested": True,
+ "cancellation_observed": True,
+ }
+ ]
diff --git a/server/tests/services/test_tool_input_security.py b/server/tests/services/test_tool_input_security.py
new file mode 100644
index 00000000..ee9468ed
--- /dev/null
+++ b/server/tests/services/test_tool_input_security.py
@@ -0,0 +1,242 @@
+"""ToolInput and locked-schema security contracts."""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+from pydantic import BaseModel, ConfigDict
+
+from nodes.tool.current_time_tool import CurrentTimeToolNode
+from nodes.tool.simple_memory import (
+ SimpleMemoryNode,
+ SimpleMemoryParams,
+ SimpleMemoryToolInput,
+)
+from services.agent_runtime import AgentToolSpec, _validated_tool_args
+from services.ai import AIService
+from services.llm.protocol import ToolCall, ToolDef
+from services.plugin import ActionNode, NodeContext, Operation, ToolNode
+from services.ws_handler_registry import get_ws_handlers
+
+
+class _ConfiguredToolParams(BaseModel):
+ endpoint: str
+ model_config = ConfigDict(extra="forbid")
+
+
+class _ConfiguredToolOutput(BaseModel):
+ endpoint: str
+ query: str
+
+
+class _DefaultedToolInput(BaseModel):
+ query: str
+ configured_limit: int = 25
+
+
+class _ConfiguredToolNode(ToolNode, abstract=True):
+ type = "_testConfiguredToolInput"
+ display_name = "Configured ToolInput Test"
+ group = ("tool",)
+ description = "Test-only ToolInput boundary"
+ handles = (
+ {
+ "name": "output-tool",
+ "kind": "output",
+ "position": "top",
+ "label": "Tool",
+ "role": "tools",
+ },
+ )
+ ui_hints = {"isToolPanel": True}
+ Params = _ConfiguredToolParams
+ ToolInput = _ConfiguredToolParams
+ Output = _ConfiguredToolOutput
+ server_controlled_fields = frozenset({"endpoint"})
+
+ @Operation("run")
+ async def run(
+ self, ctx: NodeContext, params: _ConfiguredToolParams
+ ) -> _ConfiguredToolOutput:
+ return _ConfiguredToolOutput(
+ endpoint=params.endpoint,
+ query=str(ctx.raw.get("query") or ""),
+ )
+
+
+class _DualPurposeActionNode(ActionNode, abstract=True):
+ type = "_testDualPurposeAction"
+ usable_as_tool = True
+ Params = _ConfiguredToolParams
+ Output = _ConfiguredToolOutput
+
+ @Operation("run")
+ async def run(
+ self, ctx: NodeContext, params: _ConfiguredToolParams
+ ) -> _ConfiguredToolOutput:
+ return _ConfiguredToolOutput(
+ endpoint=params.endpoint,
+ query=str(ctx.raw.get("query") or ""),
+ )
+
+
+def test_tool_input_defaults_to_params():
+ assert CurrentTimeToolNode.ToolInput is CurrentTimeToolNode.Params
+ assert CurrentTimeToolNode.tool_input_model() is CurrentTimeToolNode.Params
+
+
+def test_native_validation_does_not_materialize_omitted_defaults():
+ spec = AgentToolSpec(
+ definition=ToolDef(
+ name="defaulted",
+ description="test",
+ parameters=_DefaultedToolInput.model_json_schema(),
+ ),
+ args_schema=_DefaultedToolInput,
+ )
+ args, error = _validated_tool_args(
+ ToolCall(id="call-1", name="defaulted", args={"query": "hello"}),
+ {"defaulted": spec},
+ )
+ assert error is None
+ assert args == {"query": "hello"}
+
+
+def test_simple_memory_has_split_locked_schema_and_canonical_name():
+ assert SimpleMemoryNode.Params is SimpleMemoryParams
+ assert SimpleMemoryNode.ToolInput is SimpleMemoryToolInput
+ assert SimpleMemoryNode.tool_schema_locked is True
+ assert SimpleMemoryNode.tool_name == "memory"
+ schema = SimpleMemoryNode.as_tool_schema()["parameters"]
+ assert "operation" in schema["properties"]
+ assert "reset_policy" not in schema["properties"]
+
+
+def test_stored_custom_schema_cannot_replace_memory_tool_input():
+ malicious = {
+ "db_schema_config": {
+ "fields": {
+ "workflow_id": {
+ "type": "string",
+ "required": True,
+ }
+ }
+ }
+ }
+ resolved = AIService._get_tool_schema(
+ object(), "simpleMemory", malicious
+ )
+ assert resolved is SimpleMemoryToolInput
+ assert "workflow_id" not in resolved.model_fields
+
+
+def test_memory_panel_handlers_are_plugin_registered():
+ handlers = get_ws_handlers()
+ assert {
+ "list_memory_items",
+ "get_memory_item",
+ "remember_memory",
+ "update_memory_item",
+ "forget_memory_item",
+ "clear_memory_items",
+ }.issubset(handlers)
+
+
+async def test_memory_builder_ignores_stored_name_and_schema():
+ class _Database:
+ async def get_tool_schema(self, _node_id):
+ return {
+ "tool_name": "steal_scope",
+ "tool_description": "untrusted",
+ "schema_config": {
+ "fields": {
+ "workflow_id": {
+ "type": "string",
+ "required": True,
+ }
+ }
+ },
+ }
+
+ service = AIService.__new__(AIService)
+ service.database = _Database()
+ tool, _config = await service._build_tool_from_node(
+ {
+ "node_id": "memory-node",
+ "node_type": "simpleMemory",
+ "parameters": {"reset_policy": "preserve"},
+ "label": "Memory",
+ }
+ )
+ assert tool.definition.name == "memory"
+ assert "operation" in tool.definition.parameters["properties"]
+ assert "workflow_id" not in tool.definition.parameters["properties"]
+
+
+async def test_model_arguments_cannot_override_server_controlled_config():
+ node = _ConfiguredToolNode()
+ context = NodeContext(
+ node_id="configured-tool",
+ node_type=_ConfiguredToolNode.type,
+ raw={"query": "hello"},
+ )
+ result = await node.execute_as_tool(
+ {"endpoint": "https://attacker.invalid"},
+ {"endpoint": "https://trusted.internal"},
+ context,
+ )
+ assert result["endpoint"] == "https://trusted.internal"
+
+
+async def test_action_node_tools_keep_their_existing_merge_contract():
+ node = _DualPurposeActionNode()
+ result = await node.execute_as_tool(
+ {"endpoint": "https://model.example"},
+ {"endpoint": "https://configured.example"},
+ NodeContext(
+ node_id="dual-purpose",
+ node_type=_DualPurposeActionNode.type,
+ raw={"query": "hello"},
+ ),
+ )
+ assert result == {
+ "endpoint": "https://model.example",
+ "query": "hello",
+ }
+
+
+async def test_memory_panel_scope_is_resolved_from_workflow_and_auth(
+ monkeypatch,
+):
+ from nodes.tool.simple_memory import _handlers
+
+ class _Database:
+ async def get_workflow(self, workflow_id):
+ assert workflow_id == "workflow-1"
+ return SimpleNamespace(
+ data={
+ "owner_id": "authenticated-user",
+ "nodes": [
+ {"id": "memory-1", "type": "simpleMemory"}
+ ]
+ }
+ )
+
+ monkeypatch.setattr(_handlers, "get_database", lambda: _Database())
+ websocket = SimpleNamespace(
+ state=SimpleNamespace(user_id="authenticated-user"),
+ scope={},
+ )
+ _store, scope = await _handlers._resolve_store_and_scope(
+ {
+ "workflow_id": "workflow-1",
+ "memory_node_id": "memory-1",
+ "namespace_id": "client-chosen-namespace",
+ "user_id": "client-chosen-user",
+ },
+ websocket,
+ )
+ assert scope.workflow_id == "workflow-1"
+ assert scope.memory_node_id == "memory-1"
+ assert scope.owner_id == "authenticated-user"
+ assert scope.namespace_id != "client-chosen-namespace"
diff --git a/server/tests/services/test_workflow_context_archive_outbox.py b/server/tests/services/test_workflow_context_archive_outbox.py
new file mode 100644
index 00000000..6870481b
--- /dev/null
+++ b/server/tests/services/test_workflow_context_archive_outbox.py
@@ -0,0 +1,357 @@
+from __future__ import annotations
+
+import importlib.util
+import sys
+import uuid
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+from services.agent_context import AgentContextStore
+
+
+async def outbox_database():
+ # The root test configuration replaces core.database for fast contract
+ # tests. Load the real implementation privately for transaction coverage.
+ module_name = f"tests._real_workflow_outbox_database_{uuid.uuid4().hex}"
+ spec = importlib.util.spec_from_file_location(
+ module_name,
+ Path(__file__).resolve().parents[2] / "core" / "database.py",
+ )
+ assert spec is not None and spec.loader is not None
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[module_name] = module
+ spec.loader.exec_module(module)
+
+ db_path = Path.cwd() / f".workflow-outbox-{uuid.uuid4().hex}.db"
+ database = module.Database(
+ SimpleNamespace(
+ database_url=f"sqlite+aiosqlite:///{db_path.as_posix()}",
+ database_echo=False,
+ database_pool_size=5,
+ database_max_overflow=5,
+ )
+ )
+ await database.startup()
+ try:
+ yield database
+ finally:
+ await database.shutdown()
+ sys.modules.pop(module_name, None)
+ for candidate in (
+ db_path,
+ Path(f"{db_path}-wal"),
+ Path(f"{db_path}-shm"),
+ ):
+ candidate.unlink(missing_ok=True)
+
+
+def _graph(*, with_context: bool) -> dict:
+ return {
+ "graphVersion": 2,
+ "owner_id": "owner",
+ "nodes": (
+ [
+ {
+ "id": "ctx",
+ "type": "context",
+ "position": {"x": 0, "y": 0},
+ "data": {
+ "label": "Context",
+ "systemManaged": True,
+ "agentNodeId": "agent",
+ },
+ }
+ ]
+ if with_context
+ else []
+ ),
+ "edges": [],
+ }
+
+
+async def test_graph_save_and_archive_intent_commit_atomically(
+ outbox_database,
+):
+ database = outbox_database
+ assert await database.save_workflow(
+ "1",
+ "First",
+ "first",
+ _graph(with_context=True),
+ )
+ assert await database.save_workflow(
+ "2",
+ "Second",
+ "second",
+ _graph(with_context=False),
+ )
+
+ # The conflicting slug makes the workflow UPDATE fail. Its archive intent
+ # must roll back in the same transaction, leaving Context still referenced.
+ assert not await database.save_workflow(
+ "1",
+ "First",
+ "second",
+ _graph(with_context=False),
+ )
+ persisted = await database.get_workflow("1")
+ assert persisted is not None
+ assert [node["id"] for node in persisted.data["nodes"]] == ["ctx"]
+ assert await database.list_workflow_context_archive_outbox("1") == []
+
+ assert await database.save_workflow(
+ "1",
+ "First",
+ "first",
+ _graph(with_context=False),
+ )
+ pending = await database.list_workflow_context_archive_outbox("1")
+ assert len(pending) == 1
+ assert pending[0]["context_node_id"] == "ctx"
+
+
+async def test_context_id_canonicalization_is_not_a_deletion(
+ outbox_database,
+):
+ database = outbox_database
+ assert await database.save_workflow(
+ "1",
+ "Workflow",
+ "workflow",
+ _graph(with_context=True),
+ )
+ canonical = _graph(with_context=True)
+ canonical["nodes"][0]["id"] = "1:context:1"
+
+ assert await database.save_workflow(
+ "1",
+ "Workflow",
+ "workflow",
+ canonical,
+ context_id_aliases={"ctx": "1:context:1"},
+ )
+
+ assert (
+ await database.list_workflow_context_archive_outbox("1")
+ == []
+ )
+
+
+async def test_atomic_canvas_mutation_enqueues_context_removal(
+ outbox_database,
+):
+ database = outbox_database
+ assert await database.save_workflow(
+ "1",
+ "Workflow",
+ "workflow",
+ _graph(with_context=True),
+ )
+
+ _, metadata, applied = await database.mutate_workflow_data_atomic(
+ "1",
+ lambda _: (
+ _graph(with_context=False),
+ {"removed": "ctx"},
+ ),
+ mutation_id="remove-context",
+ operation="test_remove_context",
+ )
+
+ assert applied is True
+ assert metadata == {"found": True, "removed": "ctx"}
+ pending = await database.list_workflow_context_archive_outbox("1")
+ assert len(pending) == 1
+ assert pending[0]["context_node_id"] == "ctx"
+
+
+async def test_pending_archive_recovers_on_workflow_read(
+ outbox_database,
+ monkeypatch,
+):
+ from services.workflow_storage import handlers
+
+ database = outbox_database
+ assert await database.save_workflow(
+ "1",
+ "Workflow",
+ "workflow",
+ _graph(with_context=True),
+ )
+ store = AgentContextStore(database)
+ await store.resolve_thread(
+ workflow_id="1",
+ context_node_id="ctx",
+ generation=1,
+ session_id="session",
+ )
+
+ # Simulate a process crash after graph commit and before the outbox drain.
+ assert await database.save_workflow(
+ "1",
+ "Workflow",
+ "workflow",
+ _graph(with_context=False),
+ )
+ assert len(await database.list_workflow_context_archive_outbox("1")) == 1
+
+ real_store = AgentContextStore
+
+ class _FailingStore:
+ def __init__(self, database):
+ self.database = database
+
+ async def archive_context(self, **kwargs):
+ raise RuntimeError("simulated_archive_crash")
+
+ monkeypatch.setattr(
+ "services.agent_context.AgentContextStore",
+ _FailingStore,
+ )
+ monkeypatch.setattr(handlers.container, "database", lambda: database)
+ first = await handlers.handle_get_workflow(
+ {"workflow_id": "1"},
+ websocket=None,
+ )
+ assert first["context_archives_pending"] == 1
+ assert (
+ await store.list_threads(
+ workflow_id="1",
+ context_node_id="ctx",
+ include_archived=True,
+ )
+ )[0].status == "active"
+
+ monkeypatch.setattr(
+ "services.agent_context.AgentContextStore",
+ real_store,
+ )
+ recovered = await handlers.handle_get_workflow(
+ {"workflow_id": "1"},
+ websocket=None,
+ )
+ assert recovered["context_archives_completed"] == 1
+ assert recovered["context_archives_pending"] == 0
+ assert await database.list_workflow_context_archive_outbox("1") == []
+ assert (
+ await store.list_threads(
+ workflow_id="1",
+ context_node_id="ctx",
+ include_archived=True,
+ )
+ )[0].status == "archived"
+
+
+async def test_workflow_delete_commits_tombstone_then_archives(
+ outbox_database,
+):
+ from services.workflow_storage.handlers import (
+ delete_workflow_with_context_archival,
+ )
+
+ database = outbox_database
+ assert await database.save_workflow(
+ "1",
+ "Workflow",
+ "workflow",
+ _graph(with_context=True),
+ )
+ store = AgentContextStore(database)
+ await store.resolve_thread(
+ workflow_id="1",
+ context_node_id="ctx",
+ generation=1,
+ session_id="session",
+ )
+
+ result = await delete_workflow_with_context_archival(
+ database,
+ "1",
+ )
+
+ assert result == {
+ "success": True,
+ "workflow_id": "1",
+ "contexts_archived": 1,
+ "context_archives_pending": 0,
+ }
+ assert await database.get_workflow("1") is None
+ assert (
+ await database.list_workflow_context_archive_outbox("1")
+ == []
+ )
+ assert (
+ await store.list_threads(
+ workflow_id="1",
+ context_node_id="ctx",
+ include_archived=True,
+ )
+ )[0].status == "archived"
+
+
+async def test_failed_workflow_delete_rolls_back_archive_tombstone(
+ outbox_database,
+ monkeypatch,
+):
+ database = outbox_database
+ database_module = sys.modules[database.__class__.__module__]
+ monkeypatch.setattr(
+ database_module.secrets,
+ "token_hex",
+ lambda _: "fixed",
+ )
+
+ # Reserve the deterministic outbox primary key.
+ assert await database.save_workflow(
+ "1",
+ "First",
+ "first",
+ _graph(with_context=True),
+ )
+ assert await database.save_workflow(
+ "1",
+ "First",
+ "first",
+ _graph(with_context=False),
+ )
+
+ assert await database.save_workflow(
+ "2",
+ "Second",
+ "second",
+ _graph(with_context=True),
+ )
+ store = AgentContextStore(database)
+ await store.resolve_thread(
+ workflow_id="2",
+ context_node_id="ctx",
+ generation=1,
+ session_id="session",
+ )
+
+ # Enqueuing workflow 2's archive collides with the existing outbox ID.
+ # The workflow DELETE and tombstone INSERT must both roll back.
+ assert not await database.delete_workflow("2")
+ persisted = await database.get_workflow("2")
+ assert persisted is not None
+ assert [node["id"] for node in persisted.data["nodes"]] == ["ctx"]
+ assert (
+ await database.list_workflow_context_archive_outbox("2")
+ == []
+ )
+ assert (
+ await store.list_threads(
+ workflow_id="2",
+ context_node_id="ctx",
+ include_archived=True,
+ )
+ )[0].status == "active"
diff --git a/server/tests/services/test_workflow_control.py b/server/tests/services/test_workflow_control.py
index 45c39a9e..30cb647a 100644
--- a/server/tests/services/test_workflow_control.py
+++ b/server/tests/services/test_workflow_control.py
@@ -107,6 +107,53 @@ async def test_generation_atomically_creates_and_archives_isolated_data_scope(co
assert [item["formatted"] for item in current_logs] == ["new"]
+async def test_active_generation_scope_redacts_context_runtime_payloads(
+ control_database,
+):
+ await control_database.save_node_parameters(
+ "memory-1",
+ {
+ "reset_policy": "preserve",
+ "memory_content": "private legacy transcript",
+ "provider_bindings": {"session_uuid": "private-binding"},
+ "thought_signature": "private-signature",
+ },
+ )
+ service = WorkflowControlService(control_database)
+ control, _ = await service.begin_generation(
+ workflow_id="wf-safe-scope",
+ nodes=[
+ {
+ "id": "memory-1",
+ "type": "simpleMemory",
+ "data": {
+ "label": "Memory",
+ "contextJournal": [{"content": "private journal"}],
+ "providerState": {"opaque": "private-state"},
+ },
+ }
+ ],
+ edges=[],
+ session_id="browser-session",
+ idempotency_key="safe-scope-1",
+ graph_version=2,
+ )
+
+ scope = await control_database.get_workflow_run_data_scope(
+ control.data_scope_id
+ )
+ serialized = str(scope.node_data) + str(scope.runtime_data)
+ assert "private legacy transcript" not in serialized
+ assert "private-binding" not in serialized
+ assert "private-signature" not in serialized
+ assert "private journal" not in serialized
+ assert "private-state" not in serialized
+ assert scope.runtime_data["nodes"]["memory-1"]["parameters"] == {
+ "reset_policy": "preserve"
+ }
+
+
@pytest.mark.asyncio
async def test_reset_archives_every_node_then_calls_plugin_lifecycle(monkeypatch):
database = AsyncMock()
diff --git a/server/tests/services/test_workflow_execution_identity.py b/server/tests/services/test_workflow_execution_identity.py
index bcd258ce..fca1c739 100644
--- a/server/tests/services/test_workflow_execution_identity.py
+++ b/server/tests/services/test_workflow_execution_identity.py
@@ -2,8 +2,10 @@
from __future__ import annotations
+import asyncio
import time
-from types import MethodType
+from types import MethodType, SimpleNamespace
+from unittest.mock import AsyncMock
import pytest
@@ -15,10 +17,10 @@ async def test_sequential_workflow_uses_one_generated_id_for_every_node():
service = WorkflowService.__new__(WorkflowService)
service._settings = {"stop_on_error": False}
- observed: list[str] = []
+ observed: list[tuple[str, str]] = []
async def fake_execute_node(self, **kwargs):
- observed.append(kwargs["execution_id"])
+ observed.append((kwargs["execution_id"], kwargs["user_id"]))
return {"success": True, "result": {"node_id": kwargs["node_id"]}}
service.execute_node = MethodType(fake_execute_node, service)
@@ -38,4 +40,182 @@ async def test_sequential_workflow_uses_one_generated_id_for_every_node():
)
assert result["execution_id"]
- assert observed == [result["execution_id"], result["execution_id"]]
+ assert observed == [
+ (result["execution_id"], "owner"),
+ (result["execution_id"], "owner"),
+ ]
+
+
+async def test_sequential_workflow_preserves_authenticated_user():
+ service = WorkflowService.__new__(WorkflowService)
+ service._settings = {"stop_on_error": False}
+ observed: list[str] = []
+
+ async def fake_execute_node(self, **kwargs):
+ observed.append(kwargs["user_id"])
+ return {"success": True, "result": {}}
+
+ service.execute_node = MethodType(fake_execute_node, service)
+ await service._execute_sequential(
+ [{"id": "start-1", "type": "start", "data": {}}],
+ [],
+ "session-1",
+ None,
+ time.time(),
+ "workflow-1",
+ "account-7",
+ )
+
+ assert observed == ["account-7"]
+
+
+async def test_parallel_workflows_isolate_authenticated_users():
+ service = WorkflowService.__new__(WorkflowService)
+ observed: dict[str, str] = {}
+
+ async def fake_execute_node(self, **kwargs):
+ observed[kwargs["workflow_id"]] = kwargs["user_id"]
+ return {"success": True, "result": {}}
+
+ service.execute_node = MethodType(fake_execute_node, service)
+
+ class _Executor:
+ async def execute_workflow(
+ self,
+ *,
+ workflow_id,
+ nodes,
+ edges,
+ session_id,
+ enable_caching,
+ ):
+ await asyncio.sleep(0)
+ await service._execute_node_adapter(
+ "node-1",
+ "console",
+ {},
+ {
+ "workflow_id": workflow_id,
+ "session_id": session_id,
+ "nodes": nodes,
+ "edges": edges,
+ },
+ )
+ return {
+ "success": True,
+ "execution_id": workflow_id,
+ "nodes_executed": ["node-1"],
+ "outputs": {},
+ "errors": [],
+ }
+
+ executor = _Executor()
+ service._get_workflow_executor = lambda _callback=None: executor
+
+ await asyncio.gather(
+ service._execute_parallel(
+ [],
+ [],
+ "session-a",
+ None,
+ time.time(),
+ "workflow-a",
+ "account-a",
+ ),
+ service._execute_parallel(
+ [],
+ [],
+ "session-b",
+ None,
+ time.time(),
+ "workflow-b",
+ "account-b",
+ ),
+ )
+
+ assert observed == {
+ "workflow-a": "account-a",
+ "workflow-b": "account-b",
+ }
+
+
+ ("supplied", "expected"),
+ [("account-9", "account-9"), ("", "owner")],
+)
+async def test_execute_node_builds_user_scoped_context(
+ supplied,
+ expected,
+):
+ service = WorkflowService.__new__(WorkflowService)
+ service._get_workspace_dir = lambda _slug: "workspace"
+ service._node_executor = SimpleNamespace(
+ execute=AsyncMock(return_value={"success": True}),
+ )
+ service._param_resolver = SimpleNamespace(resolve=AsyncMock())
+
+ await service.execute_node(
+ node_id="node-1",
+ node_type="console",
+ parameters={},
+ workflow_slug="workflow",
+ user_id=supplied,
+ )
+
+ context = service._node_executor.execute.await_args.kwargs["context"]
+ assert context["user_id"] == expected
+
+
+async def test_temporal_workflow_preserves_authenticated_user():
+ service = WorkflowService.__new__(WorkflowService)
+ service._resolve_workflow_slug = AsyncMock(return_value="workflow")
+ service._temporal_executor = SimpleNamespace(
+ execute_workflow=AsyncMock(
+ return_value={
+ "success": True,
+ "execution_id": "run-1",
+ "nodes_executed": [],
+ "outputs": {},
+ "errors": [],
+ }
+ )
+ )
+
+ await service._execute_temporal(
+ [],
+ [],
+ "session",
+ None,
+ time.time(),
+ "workflow-1",
+ 2,
+ 1,
+ "account-11",
+ )
+
+ kwargs = service._temporal_executor.execute_workflow.await_args.kwargs
+ assert kwargs["user_id"] == "account-11"
+
+
+async def test_deploy_workflow_preserves_authenticated_user():
+ service = WorkflowService.__new__(WorkflowService)
+ manager = SimpleNamespace(
+ deploy=AsyncMock(return_value={"success": True}),
+ )
+ service._get_deployment_manager = lambda: manager
+
+ await service.deploy_workflow(
+ [],
+ [],
+ workflow_id="workflow-1",
+ graph_version=2,
+ generation=1,
+ user_id="account-12",
+ )
+
+ assert manager.deploy.await_args.kwargs["user_id"] == "account-12"
diff --git a/server/tests/temporal/test_agent_workflow.py b/server/tests/temporal/test_agent_workflow.py
index 32f2e6d2..e73aa7ca 100644
--- a/server/tests/temporal/test_agent_workflow.py
+++ b/server/tests/temporal/test_agent_workflow.py
@@ -99,7 +99,7 @@ class TestDurableTeamDelegationContract:
source = inspect.getsource(AgentWorkflow.run)
initialization = source.index("root_execution_id = str(")
- tool_loop = source.index("for iteration in range(max_iterations)")
+ tool_loop = source.index("for iteration in range(iteration_offset, max_iterations)")
result_payload = source.index('"root_execution_id": root_execution_id')
assert initialization < tool_loop < result_payload
@@ -124,8 +124,8 @@ class TestDurableTeamDelegationContract:
assert '"DelegatedTaskWorkflow"' in lead_source
assert "ParentClosePolicy.ABANDON" in lead_source
assert 'return {"status": "queued"' in lead_source
- assert '"agent.finish_delegation.v1"' in runner_source
- assert '"agent.release_subagent_permit.v1"' in runner_source
+ assert '"agent.finish_delegation"' in runner_source
+ assert '"agent.release_subagent_permit"' in runner_source
def test_queued_work_does_not_block_lead_final_response(self):
import inspect
@@ -162,13 +162,13 @@ class TestDurableTeamDelegationContract:
from services.temporal.agent_workflow import AgentWorkflow
source = inspect.getsource(AgentWorkflow.run)
- queue_at = source.index('"agent.queue_delegation.v1"')
- acquire_at = source.index('"agent.acquire_subagent_permit.v1"')
- claim_at = source.index('"agent.begin_delegation.v1"')
+ queue_at = source.index('"agent.queue_delegation"')
+ acquire_at = source.index('"agent.acquire_subagent_permit"')
+ claim_at = source.index('"agent.begin_delegation"')
start_at = source.index("workflow.start_child_workflow")
assert queue_at < acquire_at < claim_at < start_at
- assert '"agent.finish_delegation.v1"' in source
- assert '"agent.release_subagent_permit.v1"' in source
+ assert '"agent.finish_delegation"' in source
+ assert '"agent.release_subagent_permit"' in source
assert "assignment_event_id" in source
assert "terminal_event_id" in source
@@ -196,15 +196,14 @@ class TestDurableTeamDelegationContract:
detached = source.index('"DelegatedTaskWorkflow"', queue)
assert queue < detached
runner = inspect.getsource(DelegatedTaskWorkflow.run)
- permit = runner.index('"agent.acquire_subagent_permit.v1"')
- claim = runner.index('"agent.begin_delegation.v1"')
+ permit = runner.index('"agent.acquire_subagent_permit"')
+ claim = runner.index('"agent.begin_delegation"')
child = runner.index('"AgentWorkflow"', claim)
- finish = runner.index('"agent.finish_delegation.v1"', child)
- release = runner.index('"agent.release_subagent_permit.v1"', finish)
+ finish = runner.index('"agent.finish_delegation"', child)
+ release = runner.index('"agent.release_subagent_permit"', finish)
assert permit < claim < child < finish < release
assert '"permit_id": task_id' in runner
assert '"team_task_id": task_id' in source
- assert "TASK_MANAGER_DELEGATION_PATCH" in source
def test_task_manager_child_lead_yields_own_permit(self):
import inspect
@@ -243,19 +242,19 @@ class TestAgentActivities:
defn = getattr(execute_llm_step, "__temporal_activity_definition", None)
assert defn is not None
- assert defn.name == "agent.execute_llm_step.v1"
+ assert defn.name == "agent.execute_llm_step"
def test_persist_agent_turn_registered(self):
from services.temporal.agent_activities import persist_agent_turn
defn = getattr(persist_agent_turn, "__temporal_activity_definition")
- assert defn.name == "agent.persist_turn.v1"
+ assert defn.name == "agent.persist_turn"
def test_compact_agent_memory_registered(self):
from services.temporal.agent_activities import compact_agent_memory
defn = getattr(compact_agent_memory, "__temporal_activity_definition")
- assert defn.name == "agent.compact_memory.v1"
+ assert defn.name == "agent.compact_memory"
def test_collect_returns_all_agent_activities(self):
"""Each successive sprint added one F4.B agent activity:
@@ -268,35 +267,39 @@ class TestAgentActivities:
activities = collect_agent_activities()
names = sorted(getattr(a, "__temporal_activity_definition").name for a in activities)
assert names == [
- "agent.acquire_subagent_permit.v1",
- "agent.begin_delegation.v1",
- "agent.broadcast_progress.v1",
- "agent.compact_memory.v1",
- "agent.execute_llm_step.v1",
- "agent.finalize_team.v1",
- "agent.finish_delegation.v1",
- "agent.persist_turn.v1",
- "agent.prepare_payload.v1",
- "agent.queue_delegation.v1",
- "agent.refresh_tools.v1",
- "agent.register_task_execution.v1",
- "agent.release_subagent_permit.v1",
- "agent.skill.clear.v1",
- "agent.skill.invoke.v1",
- "agent.store_output.v1",
+ "agent.acquire_subagent_permit",
+ "agent.append_context",
+ "agent.begin_delegation",
+ "agent.broadcast_progress",
+ "agent.cancel_delegation",
+ "agent.compact_context",
+ "agent.compact_memory",
+ "agent.execute_llm_step",
+ "agent.finalize_team",
+ "agent.finish_delegation",
+ "agent.persist_turn",
+ "agent.prepare_context",
+ "agent.prepare_payload",
+ "agent.queue_delegation",
+ "agent.refresh_tools",
+ "agent.register_task_execution",
+ "agent.release_subagent_permit",
+ "agent.skill.clear",
+ "agent.skill.invoke",
+ "agent.store_output",
]
def test_prepare_payload_registered(self):
from services.temporal.agent_activities import prepare_agent_payload
defn = getattr(prepare_agent_payload, "__temporal_activity_definition")
- assert defn.name == "agent.prepare_payload.v1"
+ assert defn.name == "agent.prepare_payload"
def test_broadcast_progress_registered(self):
from services.temporal.agent_activities import broadcast_agent_progress
defn = getattr(broadcast_agent_progress, "__temporal_activity_definition")
- assert defn.name == "agent.broadcast_progress.v1"
+ assert defn.name == "agent.broadcast_progress"
async def test_temporal_tool_progress_preserves_visible_tool_name(self, monkeypatch):
"""A phase-only Temporal event must still update the agent card.
@@ -358,7 +361,7 @@ class TestAgentActivities:
from services.temporal.agent_workflow import AgentWorkflow
source = inspect.getsource(AgentWorkflow.run)
- failure = source.index("except Exception as e:", source.index('"agent.execute_llm_step.v1"'))
+ failure = source.index("except Exception as e:", source.index('"agent.execute_llm_step"'))
terminal_return = source.index('"error_type": "LLMStepError"', failure)
cleanup = source.index('activity_id="clear-active-skills-failed"', failure)
error_phase = source.index('phase="failed"', cleanup)
@@ -532,7 +535,7 @@ class TestAutoRebindTools:
defn = getattr(refresh_agent_tools, "__temporal_activity_definition", None)
assert defn is not None, "refresh_agent_tools missing @activity.defn"
- assert defn.name == "agent.refresh_tools.v1"
+ assert defn.name == "agent.refresh_tools"
def test_refresh_tools_in_collect(self):
"""Worker registration must include the new activity so
@@ -540,7 +543,7 @@ class TestAutoRebindTools:
from services.temporal.agent_activities import collect_agent_activities, refresh_agent_tools
names = {getattr(a, "__temporal_activity_definition").name for a in collect_agent_activities()}
- assert "agent.refresh_tools.v1" in names
+ assert "agent.refresh_tools" in names
assert refresh_agent_tools in collect_agent_activities()
async def test_refresh_tools_runs_without_nameerror(self, monkeypatch):
@@ -567,7 +570,7 @@ class TestAutoRebindTools:
from services.temporal.agent_workflow import AgentWorkflow
src = inspect.getsource(AgentWorkflow.run)
- assert '"agent.refresh_tools.v1"' in src, (
+ assert '"agent.refresh_tools"' in src, (
"AgentWorkflow tool dispatch must schedule agent.refresh_tools.v1 "
"when a tool result returns workflow_ops operations."
)
@@ -854,3 +857,179 @@ class TestNeedsCanvasDispatch:
"level so the workflow body can resolve plugin classes by "
"type string."
)
+
+
+class TestWorkerRegistrationParity:
+ """The three Worker constructions must expose the same surface.
+
+ They previously hand-maintained duplicate workflow lists, which is how
+ ``agent.cancel_delegation.v1`` ended up scheduled-but-unregistered and
+ how the V2 activity spread drifted between them.
+ """
+
+ def test_framework_workflow_list_is_single_sourced(self):
+ import inspect
+
+ from services.temporal import worker as worker_module
+
+ names = [cls.__name__ for cls in worker_module._framework_workflows()]
+ assert names == sorted(set(names), key=names.index), "duplicate workflow class"
+ assert "MachinaWorkflow" in names
+ assert "AgentWorkflow" in names
+
+ source = inspect.getsource(worker_module)
+ # Every construction goes through the helper; none re-lists classes.
+ assert source.count("workflows=_framework_workflows()") == 3
+ assert "workflows=[" not in source
+
+ def test_every_scheduled_agent_activity_is_registered(self):
+ """Guards the class of bug that left cancel_delegation unregistered."""
+ import inspect
+ import re
+
+ from services.temporal import agent_workflow as wf_module
+ from services.temporal.agent_activities import collect_agent_activities
+
+ registered = {
+ getattr(a, "__temporal_activity_definition").name
+ for a in collect_agent_activities()
+ }
+ scheduled = set(
+ re.findall(r'"(agent\.[a-z_.]+\.v\d)"', inspect.getsource(wf_module))
+ )
+ missing = scheduled - registered
+ assert not missing, f"scheduled but not registered on any worker: {sorted(missing)}"
+
+
+class TestCompactionPauseIsNotAnAnswer:
+ """A provider stop-to-compact must never surface as the agent's answer.
+
+ A compaction stop carries no tool calls, so `kind` is "final" and the
+ truncated content would be returned to the user verbatim.
+ """
+
+ def test_native_payload_requests_the_finish_reason(self):
+ import inspect
+
+ from services.temporal.agent_workflow import AgentWorkflow
+
+ source = inspect.getsource(AgentWorkflow.run)
+ # Without this the workflow cannot distinguish the two stop kinds.
+ assert '"include_finish_reason": True' in source
+
+ def test_workflow_continues_instead_of_finalising_on_compaction(self):
+ import inspect
+
+ from services.temporal.agent_workflow import AgentWorkflow
+
+ source = inspect.getsource(AgentWorkflow.run)
+ assert 'finish_reason == "compaction"' in source
+ # The guard must sit BEFORE the final-answer branch, or the answer
+ # is already returned by the time it runs.
+ assert source.index('finish_reason == "compaction"') < source.index('if kind == "final":')
+
+ def test_activity_only_emits_finish_reason_when_asked(self):
+ """Opt-in keeps the legacy payload shapes byte-identical."""
+ import inspect
+
+ from services.temporal.agent_activities import _execute_native_llm_step
+
+ source = inspect.getsource(_execute_native_llm_step)
+ assert 'payload.get("include_finish_reason")' in source
+
+
+class TestDelegatedChildrenInheritScope:
+ """A subagent must resolve its own context, not silently none.
+
+ The scope keys are injected into a ROOT node's context by
+ MachinaWorkflow. Delegated children are started by the agent workflow
+ itself, so without explicit inheritance they carry none of them and
+ every subagent turn goes unjournalled with no error.
+ """
+
+ def test_inherited_scope_forwards_only_present_keys(self):
+ from services.temporal.agent_workflow import _inherited_scope
+
+ got = _inherited_scope(
+ {"generation": 4, "graphVersion": 2, "user_id": "u1", "unrelated": "x"}
+ )
+ assert got == {"generation": 4, "graphVersion": 2, "user_id": "u1"}
+ # Absent keys must not materialise as None — `generation: None`
+ # would fail the int() coercion downstream.
+ assert _inherited_scope({}) == {}
+
+ def test_both_delegation_sites_inherit_scope(self):
+ import inspect
+
+ from services.temporal.agent_workflow import AgentWorkflow
+
+ source = inspect.getsource(AgentWorkflow.run)
+ assert source.count("**_inherited_scope(context)") == 2
+
+ def test_inherited_scope_never_overrides_explicit_child_keys(self):
+ import inspect
+
+ from services.temporal.agent_workflow import AgentWorkflow
+
+ source = inspect.getsource(AgentWorkflow.run)
+ for block in source.split("child_context = {")[1:]:
+ spread = block.index("**_inherited_scope(context)")
+ node_id = block.index('"node_id"')
+ assert spread < node_id, "spread must come first so explicit keys win"
+
+
+class TestAgentContinueAsNew:
+ """The agent loop carries a transcript and a growing tool list, so it
+ must roll over before Temporal's ~51,200-event hard terminate."""
+
+ def test_rollover_exists_and_is_bounded(self):
+ import inspect
+
+ from services.temporal.agent_workflow import AgentWorkflow
+
+ source = inspect.getsource(AgentWorkflow.run)
+ assert "workflow.continue_as_new(" in source
+ # Only references and counters cross the boundary. Carrying the
+ # transcript would defeat the rollover it is meant to enable.
+ carried = source[source.index("_RESUME_MARKER:") :][:600]
+ for forbidden in ('"messages"', '"tools"', '"assistant_message"'):
+ assert forbidden not in carried, f"{forbidden} must not cross a rollover"
+
+ def test_execution_id_survives_the_rollover(self):
+ """run_id changes on continue-as-new.
+
+ execution_id falls back to run_id[:8], so a resumed run would mint a
+ different id and break browser-session reuse and permit scoping.
+ """
+ import inspect
+
+ from services.temporal.agent_workflow import AgentWorkflow
+
+ source = inspect.getsource(AgentWorkflow.run)
+ assert 'resume.get("execution_id")' in source
+ assert source.index('resume.get("execution_id")') < source.index(
+ 'workflow.info().run_id[:8]'
+ ), "the carried id must take precedence over the run_id fallback"
+
+ def test_iteration_continues_rather_than_resetting(self):
+ """iteration is baked into delegation child ids and team_task_id;
+ resetting it to 0 collides with the pre-rollover run."""
+ import inspect
+
+ from services.temporal.agent_workflow import AgentWorkflow
+
+ source = inspect.getsource(AgentWorkflow.run)
+ assert "for iteration in range(iteration_offset, max_iterations)" in source
+ assert '"iteration": iteration + 1' in source
+
+ def test_rollover_refused_while_a_delegation_is_live(self):
+ """delegation_handles holds ChildWorkflowHandle objects and the
+ Task-Manager map holds asyncio.Tasks — neither is serializable."""
+ import inspect
+
+ from services.temporal.agent_workflow import AgentWorkflow
+
+ source = inspect.getsource(AgentWorkflow.run)
+ guard = "delegation_handles or task_manager_delegation_tasks"
+ assert guard in source
+ assert source.index(guard) < source.index("workflow.continue_as_new(")
diff --git a/server/tests/temporal/test_agent_workflow_replay.py b/server/tests/temporal/test_agent_workflow_replay.py
index 77677a2c..848bacfa 100644
--- a/server/tests/temporal/test_agent_workflow_replay.py
+++ b/server/tests/temporal/test_agent_workflow_replay.py
@@ -76,17 +76,17 @@ def _prepared_payload(*, engine: str) -> dict[str, Any]:
return payload
[email protected](name="agent.prepare_payload.v1")
[email protected](name="agent.prepare_payload")
async def _prepare_payload(context: dict[str, Any]) -> dict[str, Any]:
return _prepared_payload(engine=str(context["test_engine"]))
[email protected](name="agent.broadcast_progress.v1")
[email protected](name="agent.broadcast_progress")
async def _broadcast_progress(_payload: dict[str, Any]) -> dict[str, Any]:
return {"emitted": True}
[email protected](name="agent.execute_llm_step.v1")
[email protected](name="agent.execute_llm_step")
async def _execute_llm_step(payload: dict[str, Any]) -> dict[str, Any]:
if payload.get("llm_engine") == "native":
assert payload["message_wire_version"] == 2
@@ -129,12 +129,12 @@ async def _execute_llm_step(payload: dict[str, Any]) -> dict[str, Any]:
}
[email protected](name="agent.store_output.v1")
[email protected](name="agent.store_output")
async def _store_output(_payload: dict[str, Any]) -> dict[str, Any]:
return {"stored": True}
[email protected](name="agent.skill.clear.v1")
[email protected](name="agent.skill.clear")
async def _clear_skills(_payload: dict[str, Any]) -> dict[str, Any]:
return {"cleared": True}
@@ -211,13 +211,13 @@ async def _run_replay_gate() -> None:
assert legacy_result["result"]["response"] == "done"
expected_order = [
- "agent.prepare_payload.v1",
- "agent.broadcast_progress.v1",
- "agent.broadcast_progress.v1",
- "agent.execute_llm_step.v1",
- "agent.store_output.v1",
- "agent.skill.clear.v1",
- "agent.broadcast_progress.v1",
+ "agent.prepare_payload",
+ "agent.broadcast_progress",
+ "agent.broadcast_progress",
+ "agent.execute_llm_step",
+ "agent.store_output",
+ "agent.skill.clear",
+ "agent.broadcast_progress",
]
native_scheduled = _scheduled_activities(native_history)
emergency_scheduled = _scheduled_activities(emergency_history)
@@ -305,15 +305,12 @@ async def _run_replay_gate() -> None:
replay = await replayer.replay_workflow(captured)
assert replay.replay_failure is None
- # This fixture is frozen rather than generated from the workflow
- # implementation under test, protecting already-recorded histories
- # whose prepare result had no engine/version marker.
- frozen_legacy = _load_captured_history(
- "agent_legacy_pre_native_history.json.zlib.b64",
- "agent-legacy-pre-native-captured-v1",
- )
- replay = await replayer.replay_workflow(frozen_legacy)
- assert replay.replay_failure is None
+ # The frozen pre-native fixture was removed with the replay patches.
+ # It asserted that a history recorded BEFORE `agent-tool-call-identity-v2`
+ # still replayed; with the patch markers deleted that history is
+ # deliberately non-replayable, and Temporal history back-compat is
+ # explicitly out of scope. The generated-shape replays above still
+ # gate determinism for every history this code can now produce.
def test_all_cutover_history_shapes_execute_and_replay() -> None:
diff --git a/server/tests/temporal/test_agent_workflow_v2.py b/server/tests/temporal/test_agent_workflow_v2.py
new file mode 100644
index 00000000..ad607a81
--- /dev/null
+++ b/server/tests/temporal/test_agent_workflow_v2.py
@@ -0,0 +1,318 @@
+"""Focused contracts for Context-backed Temporal agent orchestration."""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, patch
+
+from pydantic import BaseModel
+import pytest
+
+
+def _ref(revision: int = 1) -> dict:
+ return {
+ "workflow_id": "workflow-1",
+ "context_node_id": "context-1",
+ "generation": 3,
+ "thread_id": "execution:run-1",
+ "epoch": 1,
+ "revision": revision,
+ }
+
+
+
+
+
+
+
+
+
+
+def _settings(*, enabled: bool = True):
+ return SimpleNamespace(
+ temporal_agent_workflow_enabled=enabled,
+ temporal_per_type_dispatch=True,
+ temporal_worker_pool_enabled=False,
+ )
+
+
+def test_new_generation_routes_to_v2_but_missing_metadata_stays_v1():
+ from services.temporal.workflow import MachinaWorkflow
+
+ instance = MachinaWorkflow()
+ with patch("core.config.Settings", side_effect=lambda: _settings()):
+ v2 = instance._resolve_dispatch(
+ "aiAgent",
+ graph_version=2,
+ generation=4,
+ context_v2_enabled=True,
+ )
+ missing_generation = instance._resolve_dispatch(
+ "aiAgent",
+ graph_version=2,
+ generation=0,
+ context_v2_enabled=True,
+ )
+ missing_version = instance._resolve_dispatch(
+ "aiAgent",
+ graph_version=0,
+ generation=4,
+ context_v2_enabled=True,
+ )
+
+ assert v2 == {
+ "kind": "child_workflow",
+ "name": "AgentWorkflow",
+ }
+ assert missing_generation["name"] == "AgentWorkflow"
+ assert missing_version["name"] == "AgentWorkflow"
+
+
+def test_delegated_thread_identity_does_not_inherit_parent_session():
+ from services.temporal.agent_activities import _thread_inputs
+
+ value = _thread_inputs(
+ {
+ "session_id": "parent-chat",
+ "team_task_id": "task-42",
+ "execution_id": "run-1",
+ }
+ )
+ assert value == {
+ "session_id": None,
+ "delegated_task_id": "task-42",
+ "execution_id": "run-1",
+ }
+
+
+def test_control_data_scope_is_not_treated_as_explicit_chat_session():
+ from services.temporal.agent_activities import _thread_inputs
+
+ value = _thread_inputs(
+ {
+ "session_id": "scope-1",
+ "data_scope_id": "scope-1",
+ "execution_id": "generation-1",
+ "context_execution_id": "firing-42",
+ }
+ )
+ assert value == {
+ "session_id": None,
+ "delegated_task_id": None,
+ "execution_id": "firing-42",
+ }
+
+
+def test_explicit_event_session_beats_execution_thread():
+ from services.temporal.agent_activities import _thread_inputs
+
+ value = _thread_inputs(
+ {
+ "session_id": "scope-1",
+ "data_scope_id": "scope-1",
+ "context_session_id": "chat-7",
+ "context_execution_id": "firing-42",
+ }
+ )
+ assert value == {
+ "session_id": "chat-7",
+ "delegated_task_id": None,
+ "execution_id": "firing-42",
+ }
+
+
+def test_prepare_and_rebind_reject_duplicate_canonical_tool_names():
+ from temporalio.exceptions import ApplicationError
+
+ from services.temporal.agent_activities import (
+ _validate_unique_tool_names,
+ )
+
+ tools = [
+ {
+ "name": "memory",
+ "tool_node_id": "memory-a",
+ "node_type": "simpleMemory",
+ },
+ {
+ "name": "memory",
+ "tool_node_id": "memory-b",
+ "node_type": "simpleMemory",
+ },
+ ]
+ with pytest.raises(ApplicationError) as exc_info:
+ _validate_unique_tool_names(tools, phase="hot rebind")
+
+ assert exc_info.value.type == "DuplicateAgentToolName"
+ assert "memory-a" in str(exc_info.value)
+ assert "memory-b" in str(exc_info.value)
+
+
+async def test_tool_input_preserves_supplied_only_semantics(monkeypatch):
+ import services.node_registry as registry
+ from services.temporal.agent_activities import _validate_tool_args
+
+ class MemoryInput(BaseModel):
+ operation: str
+ limit: int = 20
+
+ class MemoryNode:
+ @classmethod
+ def tool_input_model(cls):
+ return MemoryInput
+
+ monkeypatch.setattr(
+ registry,
+ "get_node_class",
+ lambda node_type: MemoryNode if node_type == "simpleMemory" else None,
+ )
+
+ validated, error = await _validate_tool_args(
+ {
+ "node_type": "simpleMemory",
+ "definition": {
+ "parameters": MemoryInput.model_json_schema(),
+ },
+ },
+ {
+ "args": {"operation": "list"},
+ },
+ )
+
+ assert error is None
+ assert validated == {"operation": "list"}
+
+
+async def test_plugin_tool_activity_loads_arguments_by_reference(monkeypatch):
+ import services.temporal.agent_activities as module
+
+ store = object()
+ monkeypatch.setattr(module, "_context_store", lambda: store)
+ monkeypatch.setattr(
+ module,
+ "_runtime_config",
+ AsyncMock(
+ return_value={
+ "protocol": "agent-context-v2",
+ "prepared": {
+ "tools": [
+ {
+ "name": "memory",
+ "tool_node_id": "memory-1",
+ "node_type": "simpleMemory",
+ "version": 2,
+ }
+ ]
+ },
+ }
+ ),
+ )
+ execute = AsyncMock(return_value={"result_ref": "sha256:result"})
+ monkeypatch.setattr(module, "_execute_tool_and_append", execute)
+ pending = {
+ "call_index": 1,
+ "call_id": "call-1",
+ "name": "memory",
+ "node_id": "memory-1",
+ "node_type": "simpleMemory",
+ "version": 2,
+ "task_queue": "memory-heavy",
+ "known": True,
+ }
+
+ result = await module.execute_tool_activity(
+ {
+ "protocol": "agent-context-tool-v2",
+ "context_ref": _ref(2),
+ "runtime_config_ref": "sha256:runtime",
+ "response_ref": "sha256:response",
+ "pending_tool": pending,
+ "operation_id": "tool-operation",
+ "iteration": 1,
+ },
+ expected_node_type="simpleMemory",
+ expected_version=2,
+ )
+
+ assert result == {"result_ref": "sha256:result"}
+ kwargs = execute.await_args.kwargs
+ assert kwargs["store"] is store
+ assert kwargs["response_ref"] == "sha256:response"
+ assert kwargs["pending"] == pending
+ assert "args" not in kwargs
+
+
+def _event(sequence, event_type, *, wire=None, payload_ref=None):
+ from models.agent_context import AgentContextEvent
+
+ return AgentContextEvent(
+ sequence=sequence,
+ event_type=event_type,
+ message_wire_v2=wire,
+ payload_ref=payload_ref,
+ operation_id=f"operation:{sequence}",
+ provider="openai",
+ previous_hash="0" * 64,
+ payload_hash=str(sequence) * 64,
+ )
+
+
+
+
+
+
+async def test_lifetime_usage_includes_compaction_calls():
+ from services.temporal.agent_activities import (
+ _context_ref,
+ _lifetime_usage,
+ )
+
+ class Store:
+ async def load_journal_page(self, *args, **kwargs):
+ return (
+ [
+ SimpleNamespace(
+ event_type="message.assistant",
+ payload_ref="sha256:assistant",
+ operation_id="temporal:run:iteration:1:assistant",
+ ),
+ SimpleNamespace(
+ event_type="context.compacted",
+ payload_ref="sha256:compaction",
+ operation_id="temporal:run:iteration:1:compact:event",
+ ),
+ ],
+ None,
+ )
+
+ async def get_blob(self, payload_ref):
+ return {
+ "usage": (
+ {
+ "input_tokens": 10,
+ "output_tokens": 2,
+ "total_tokens": 12,
+ }
+ if payload_ref == "sha256:assistant"
+ else {
+ "input_tokens": 4,
+ "output_tokens": 1,
+ "total_tokens": 5,
+ }
+ )
+ }
+
+ usage = await _lifetime_usage(
+ Store(),
+ _context_ref(_ref()),
+ "temporal:run",
+ )
+
+ assert usage == {
+ "input_tokens": 14,
+ "output_tokens": 3,
+ "total_tokens": 17,
+ }
diff --git a/server/tests/temporal/test_cooperative_pause.py b/server/tests/temporal/test_cooperative_pause.py
index 8ab7dd08..1f90ef49 100644
--- a/server/tests/temporal/test_cooperative_pause.py
+++ b/server/tests/temporal/test_cooperative_pause.py
@@ -78,88 +78,8 @@ async def test_controller_control_update_rejects_unknown_state_without_mutation(
assert controller.status()["revision"] == 0
-def test_pause_scheduling_changes_are_replay_patch_guarded():
- from nodes.scheduler.cron_scheduler import _workflow as cron_module
- from services.temporal import agent_workflow as agent_module
- from services.temporal import polling_trigger_workflow as polling_module
- from services.temporal import trigger_listener_workflow as trigger_module
- from services.temporal import workflow as machina_module
- from services.temporal import workflow_control_workflow as control_module
-
- expected = {
- machina_module.COOPERATIVE_PAUSE_SCHEDULING_PATCH:
- inspect.getsource(MachinaWorkflow.run),
- polling_module.COOPERATIVE_PAUSE_SCHEDULING_PATCH:
- inspect.getsource(PollingTriggerWorkflow.run),
- agent_module.COOPERATIVE_PAUSE_SCHEDULING_PATCH:
- inspect.getsource(AgentWorkflow.run),
- agent_module.DELEGATED_TASK_COOPERATIVE_PAUSE_SCHEDULING_PATCH:
- inspect.getsource(DelegatedTaskWorkflow.run),
- agent_module.DELEGATION_PERMIT_CLEANUP_PATCH:
- inspect.getsource(AgentWorkflow.run),
- agent_module.DELEGATION_ACQUIRE_CANCELLATION_PATCH:
- inspect.getsource(AgentWorkflow.run),
- agent_module.DELEGATED_TASK_CANCELLATION_TERMINAL_PATCH:
- inspect.getsource(DelegatedTaskWorkflow.run),
- control_module.COOPERATIVE_PAUSE_SCHEDULING_PATCH:
- inspect.getsource(WorkflowControlWorkflow.run),
- trigger_module.COOPERATIVE_PAUSE_SCHEDULING_PATCH:
- inspect.getsource(TriggerListenerWorkflow.run),
- cron_module.COOPERATIVE_PAUSE_SCHEDULING_PATCH:
- inspect.getsource(CronTriggerWorkflow.run),
- }
-
- assert set(expected) == {
- "machina-cooperative-pause-scheduling-v1",
- "polling-trigger-cooperative-pause-scheduling-v1",
- "agent-cooperative-pause-scheduling-v1",
- "delegated-task-cooperative-pause-scheduling-v1",
- "agent-delegation-permit-cleanup-v1",
- "agent-delegation-acquire-cancellation-v1",
- "delegated-task-cancellation-terminal-v1",
- "workflow-control-cooperative-pause-scheduling-v1",
- "trigger-cooperative-pause-scheduling-v1",
- "cron-cooperative-pause-scheduling-v1",
- }
- for patch_id, source in expected.items():
- assert "workflow.patched(" in source
- assert "COOPERATIVE_PAUSE_SCHEDULING_PATCH" in source
- assert patch_id.endswith("-v1")
-
-
-async def test_controller_preserves_legacy_admission_and_patch_gates_metadata(
- monkeypatch,
-):
- from services.temporal.trigger_listener_workflow import (
- TriggerListenerWorkflow,
- )
- spawn = AsyncMock()
- monkeypatch.setattr(TriggerListenerWorkflow, "_spawn_child_run", spawn)
- controller = WorkflowControlWorkflow()
- spec = {
- "listener_args": {
- "trigger_node_id": "push-1",
- "workflow_id": "workflow-1",
- }
- }
-
- controller._use_cooperative_pause_schedule_gate = False
- await controller._spawn_push_run({"id": "legacy"}, spec)
- assert (
- spawn.await_args.kwargs["admission_check"]
- == controller._wait_until_running
- )
- assert spawn.await_args.kwargs["search_attributes"] is None
- controller._use_cooperative_pause_schedule_gate = True
- await controller._spawn_push_run({"id": "new"}, spec)
- assert (
- spawn.await_args.kwargs["admission_check"]
- == controller._wait_until_running
- )
- assert spawn.await_args.kwargs["search_attributes"] is not None
async def _wait_for_predicate(predicate, blocked: asyncio.Event) -> None:
@@ -207,7 +127,7 @@ async def test_machina_rechecks_pause_between_ready_child_starts(monkeypatch):
monkeypatch.setattr(
MachinaWorkflow,
"_resolve_dispatch",
- lambda self, _node_type: {
+ lambda self, _node_type, **_kwargs: {
"kind": "child_workflow",
"name": "AgentWorkflow",
},
@@ -463,11 +383,11 @@ async def test_agent_rechecks_pause_before_tool_command_batch(monkeypatch):
async def execute_activity(name, *, args, **_kwargs):
nonlocal llm_step
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _agent_payload()
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_step += 1
if llm_step == 1:
instance._control_paused = True
@@ -490,9 +410,9 @@ async def test_agent_rechecks_pause_before_tool_command_batch(monkeypatch):
if name == "node.testTool.v1":
tool_started.set()
return {"success": True}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -544,13 +464,13 @@ async def test_agent_rechecks_pause_after_tool_phase_before_activity(monkeypatch
async def execute_activity(name, *, args, **_kwargs):
nonlocal llm_step
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _agent_payload()
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
if args[0]["phase"] == "executing_tool":
instance._control_paused = True
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_step += 1
if llm_step == 1:
return {
@@ -562,9 +482,9 @@ async def test_agent_rechecks_pause_after_tool_phase_before_activity(monkeypatch
if name == "node.testTool.v1":
tool_started.set()
return {"success": True}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -707,7 +627,7 @@ async def test_acquire_activity_compensates_cancelled_attempt(monkeypatch):
]
[email protected]("patch_enabled", [False, True])
[email protected]("patch_enabled", [True])
@pytest.mark.asyncio
async def test_delegated_acquire_cancellation_type_is_replay_patch_guarded(
monkeypatch,
@@ -739,9 +659,9 @@ async def test_delegated_acquire_cancellation_type_is_replay_patch_guarded(
acquire_kwargs: dict = {}
async def execute_activity(name, *, args, **kwargs):
- if name == "agent.register_task_execution.v1":
+ if name == "agent.register_task_execution":
return {}
- if name == "agent.acquire_subagent_permit.v1":
+ if name == "agent.acquire_subagent_permit":
acquire_kwargs.update(kwargs)
raise asyncio.CancelledError
raise AssertionError(f"Unexpected activity {name}")
@@ -767,7 +687,7 @@ async def test_delegated_acquire_cancellation_type_is_replay_patch_guarded(
assert "cancellation_type" not in acquire_kwargs
[email protected]("patch_enabled", [False, True])
[email protected]("patch_enabled", [True])
@pytest.mark.asyncio
async def test_agent_acquire_cancellation_type_is_replay_patch_guarded(
monkeypatch,
@@ -799,11 +719,11 @@ async def test_agent_acquire_cancellation_type_is_replay_patch_guarded(
acquire_kwargs: dict = {}
async def execute_activity(name, *, args, **kwargs):
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _delegation_agent_payload()
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
return {
"kind": "tool_calls",
"calls": [{
@@ -813,9 +733,9 @@ async def test_agent_acquire_cancellation_type_is_replay_patch_guarded(
}],
"usage": {},
}
- if name == "agent.queue_delegation.v1":
+ if name == "agent.queue_delegation":
return {}
- if name == "agent.acquire_subagent_permit.v1":
+ if name == "agent.acquire_subagent_permit":
acquire_kwargs.update(kwargs)
raise asyncio.CancelledError
raise AssertionError(f"Unexpected activity {name}")
@@ -840,7 +760,7 @@ async def test_agent_acquire_cancellation_type_is_replay_patch_guarded(
@pytest.mark.parametrize(
"failing_cleanup",
- [None, "agent.cancel_delegation.v1", "agent.release_subagent_permit.v1"],
+ [None, "agent.cancel_delegation", "agent.release_subagent_permit"],
)
@pytest.mark.asyncio
async def test_delegated_cancellation_persists_terminal_releases_and_reraises(
@@ -876,21 +796,21 @@ async def test_delegated_cancellation_persists_terminal_releases_and_reraises(
async def execute_activity(name, *, args, **_kwargs):
nonlocal register_count
calls.append((name, args[0]))
- if name == "agent.register_task_execution.v1":
+ if name == "agent.register_task_execution":
register_count += 1
if register_count == 2:
child_registered.set()
return {}
if name in {
- "agent.acquire_subagent_permit.v1",
- "agent.begin_delegation.v1",
+ "agent.acquire_subagent_permit",
+ "agent.begin_delegation",
}:
return {}
if name == failing_cleanup:
raise RuntimeError(f"{name} failed")
if name in {
- "agent.cancel_delegation.v1",
- "agent.release_subagent_permit.v1",
+ "agent.cancel_delegation",
+ "agent.release_subagent_permit",
}:
return {}
raise AssertionError(f"Unexpected activity {name}")
@@ -923,15 +843,15 @@ async def test_delegated_cancellation_persists_terminal_releases_and_reraises(
terminal = next(
payload
for name, payload in calls
- if name == "agent.cancel_delegation.v1"
+ if name == "agent.cancel_delegation"
)
assert terminal["reason"] == "Delegated task workflow cancelled"
assert terminal["terminal_event_id"] == "task-1:terminal"
# The normal finish path applies retry/requeue policy and must never run
# for a cancellation.
- assert not any(name == "agent.finish_delegation.v1" for name, _ in calls)
+ assert not any(name == "agent.finish_delegation" for name, _ in calls)
assert any(
- name == "agent.release_subagent_permit.v1"
+ name == "agent.release_subagent_permit"
for name, _payload in calls
)
@@ -964,11 +884,11 @@ async def test_agent_cancellation_releases_all_started_delegation_permits(
releases: list[tuple[str, str]] = []
async def execute_activity(name, *, args, **kwargs):
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _two_delegation_agent_payload()
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
return {
"kind": "tool_calls",
"calls": [
@@ -985,7 +905,7 @@ async def test_agent_cancellation_releases_all_started_delegation_permits(
],
"usage": {},
}
- if name == "agent.release_subagent_permit.v1":
+ if name == "agent.release_subagent_permit":
releases.append(
(kwargs["activity_id"], args[0]["permit_id"])
)
@@ -993,10 +913,10 @@ async def test_agent_cancellation_releases_all_started_delegation_permits(
raise RuntimeError("release failed")
return {"released": True}
if name in {
- "agent.queue_delegation.v1",
- "agent.acquire_subagent_permit.v1",
- "agent.begin_delegation.v1",
- "agent.register_task_execution.v1",
+ "agent.queue_delegation",
+ "agent.acquire_subagent_permit",
+ "agent.begin_delegation",
+ "agent.register_task_execution",
}:
return {}
raise AssertionError(f"Unexpected activity {name}")
@@ -1041,8 +961,8 @@ async def test_agent_cancellation_releases_all_started_delegation_permits(
@pytest.mark.parametrize(
("pause_activity", "blocked_command"),
[
- ("agent.acquire_subagent_permit.v1", "agent.begin_delegation.v1"),
- ("agent.begin_delegation.v1", "start_child_workflow"),
+ ("agent.acquire_subagent_permit", "agent.begin_delegation"),
+ ("agent.begin_delegation", "start_child_workflow"),
],
)
@pytest.mark.asyncio
@@ -1073,11 +993,11 @@ async def test_agent_rechecks_pause_between_delegation_commands(
async def execute_activity(name, *, args, **_kwargs):
nonlocal llm_step
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _delegation_agent_payload()
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_step += 1
if llm_step == 1:
return {
@@ -1093,20 +1013,20 @@ async def test_agent_rechecks_pause_between_delegation_commands(
}
return {"kind": "final", "content": "done", "usage": {}}
if name in {
- "agent.queue_delegation.v1",
- "agent.acquire_subagent_permit.v1",
- "agent.begin_delegation.v1",
- "agent.register_task_execution.v1",
- "agent.release_subagent_permit.v1",
- "agent.finish_delegation.v1",
+ "agent.queue_delegation",
+ "agent.acquire_subagent_permit",
+ "agent.begin_delegation",
+ "agent.register_task_execution",
+ "agent.release_subagent_permit",
+ "agent.finish_delegation",
}:
scheduled_commands.append(name)
if name == pause_activity:
instance._control_paused = True
return {}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -1143,13 +1063,13 @@ async def test_agent_rechecks_pause_between_delegation_commands(
assert child_started.is_set()
assert result["success"] is True
assert scheduled_commands == [
- "agent.queue_delegation.v1",
- "agent.acquire_subagent_permit.v1",
- "agent.begin_delegation.v1",
+ "agent.queue_delegation",
+ "agent.acquire_subagent_permit",
+ "agent.begin_delegation",
"start_child_workflow",
- "agent.register_task_execution.v1",
- "agent.release_subagent_permit.v1",
- "agent.finish_delegation.v1",
+ "agent.register_task_execution",
+ "agent.release_subagent_permit",
+ "agent.finish_delegation",
]
@@ -1195,11 +1115,11 @@ async def test_agent_rechecks_pause_before_task_manager_preflight_batch(monkeypa
preflight_started = asyncio.Event()
async def execute_activity(name, *, args, **kwargs):
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _mixed_preflight_payload()
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
return {
"kind": "tool_calls",
"calls": [
@@ -1220,7 +1140,7 @@ async def test_agent_rechecks_pause_before_task_manager_preflight_batch(monkeypa
"usage": {},
}
if (
- name == "agent.release_subagent_permit.v1"
+ name == "agent.release_subagent_permit"
and kwargs.get("activity_id") == "yield-own-permit-1"
):
instance._control_paused = True
@@ -1287,11 +1207,11 @@ async def test_agent_rechecks_pause_before_task_manager_child_start(monkeypatch)
async def execute_activity(name, *, args, **_kwargs):
nonlocal llm_step
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _mixed_preflight_payload()
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_step += 1
if llm_step == 1:
return {
@@ -1309,12 +1229,12 @@ async def test_agent_rechecks_pause_before_task_manager_child_start(monkeypatch)
"usage": {},
}
return {"kind": "final", "content": "done", "usage": {}}
- if name == "agent.queue_delegation.v1":
+ if name == "agent.queue_delegation":
instance._control_paused = True
return {"queued": True}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
diff --git a/server/tests/temporal/test_durability_hardening.py b/server/tests/temporal/test_durability_hardening.py
index 2a4610f6..5d09a49e 100644
--- a/server/tests/temporal/test_durability_hardening.py
+++ b/server/tests/temporal/test_durability_hardening.py
@@ -45,59 +45,6 @@ SERVER_DIR = Path(__file__).resolve().parents[2]
# ---------------------------------------------------------------------------
-def test_durability_changes_are_replay_patch_guarded():
- from nodes.scheduler.cron_scheduler import _workflow as cron_module
- from services.temporal import agent_workflow as agent_module
- from services.temporal import polling_trigger_workflow as polling_module
- from services.temporal import trigger_listener_workflow as trigger_module
- from services.temporal import workflow as machina_module
- from services.temporal import workflow_control_workflow as control_module
- from services.temporal.agent_workflow import AgentWorkflow, DelegatedTaskWorkflow
- from services.temporal.workflow import MachinaWorkflow
- from nodes.scheduler.cron_scheduler._workflow import CronTriggerWorkflow
-
- expected = {
- machina_module.UNBOUNDED_LIFETIMES_PATCH: inspect.getsource(MachinaWorkflow.run),
- machina_module.PAUSE_ON_FAILURE_PATCH: inspect.getsource(MachinaWorkflow.run),
- agent_module.UNBOUNDED_LIFETIMES_PATCH: inspect.getsource(AgentWorkflow.run),
- trigger_module.UNBOUNDED_CHILD_RUNS_PATCH: inspect.getsource(
- TriggerListenerWorkflow._spawn_child_run
- ),
- polling_module.UNBOUNDED_CHILD_RUNS_PATCH: inspect.getsource(
- PollingTriggerWorkflow._spawn_child_run
- ),
- cron_module.UNBOUNDED_CHILD_RUNS_PATCH: inspect.getsource(CronTriggerWorkflow.run),
- trigger_module.HISTORY_BOUNDED_CAN_PATCH: inspect.getsource(
- TriggerListenerWorkflow.run
- ),
- polling_module.HISTORY_BOUNDED_CAN_PATCH: inspect.getsource(
- PollingTriggerWorkflow.run
- ),
- control_module.CONTINUE_AS_NEW_PATCH: inspect.getsource(
- WorkflowControlWorkflow.run
- ),
- trigger_module.BOUNDED_STATUS_BROADCASTS_PATCH: inspect.getsource(
- TriggerListenerWorkflow._spawn_child_run
- ),
- }
-
- assert set(expected) == {
- "machina-unbounded-lifetimes-v1",
- "machina-pause-on-failure-v1",
- "agent-unbounded-lifetimes-v1",
- "trigger-unbounded-child-runs-v1",
- "polling-unbounded-child-runs-v1",
- "cron-unbounded-child-runs-v1",
- "trigger-history-bounded-can-v1",
- "polling-history-bounded-can-v1",
- "workflow-control-continue-as-new-v1",
- "trigger-bounded-status-broadcasts-v1",
- }
- # DelegatedTaskWorkflow shares agent_module's marker.
- assert "UNBOUNDED_LIFETIMES_PATCH" in inspect.getsource(DelegatedTaskWorkflow.run)
- for patch_id, source in expected.items():
- assert "workflow.patched(" in source
- assert patch_id.endswith("-v1")
# ---------------------------------------------------------------------------
@@ -120,20 +67,24 @@ def _listener_data() -> dict:
@pytest.mark.asyncio
[email protected]("patched_on", [True, False])
-async def test_listener_child_lifetime_caps_are_patch_gated(monkeypatch, patched_on):
+async def test_listener_child_runs_carry_no_lifetime_caps(monkeypatch):
+ """Child runs must start unbounded.
+
+ Temporal's execution/run timers keep ticking through a cooperative
+ pause, so any lifetime cap silently terminates a run that is paused
+ longer than the cap. Liveness is the heartbeat timeout's job.
+ """
from services.temporal import trigger_listener_workflow as trigger_module
temporal_workflow = trigger_module.workflow
monkeypatch.setattr(temporal_workflow, "logger", MagicMock())
- flags = {
- trigger_module.UNBOUNDED_CHILD_RUNS_PATCH: patched_on,
- trigger_module.BOUNDED_STATUS_BROADCASTS_PATCH: patched_on,
- }
+ # The spawn path refreshes the graph for uncontrolled deployments;
+ # ``found: False`` keeps the listener's own nodes/edges in play.
monkeypatch.setattr(
- temporal_workflow, "patched", lambda pid: flags.get(pid, False)
+ temporal_workflow,
+ "execute_activity",
+ AsyncMock(return_value={"found": False}),
)
- monkeypatch.setattr(temporal_workflow, "execute_activity", AsyncMock())
captured: dict = {}
@@ -147,12 +98,8 @@ async def test_listener_child_lifetime_caps_are_patch_gated(monkeypatch, patched
await listener._spawn_child_run({"id": "evt-1", "data": {}}, _listener_data())
assert captured["name"] == "MachinaWorkflow"
- if patched_on:
- assert "execution_timeout" not in captured
- assert "run_timeout" not in captured
- else:
- assert captured["execution_timeout"] == timedelta(hours=1)
- assert captured["run_timeout"] == timedelta(hours=1)
+ assert "execution_timeout" not in captured
+ assert "run_timeout" not in captured
@pytest.mark.asyncio
@@ -783,6 +730,7 @@ async def test_rearm_restores_paused_posture_from_snapshot(monkeypatch):
deploy_data = deploy.await_args.args[0]
assert deploy_data["workflow_id"] == "wf"
assert deploy_data["nodes"] == control.graph_snapshot["nodes"]
+ assert deploy_data["graphVersion"] == 0
# Generation-scoped persistence, same contract as handle_start_workflow.
assert deploy_data["session_id"] == "execution-1"
workflow_service.pause_deployment.assert_called_once_with("wf")
@@ -1254,7 +1202,7 @@ async def test_machina_failure_schedules_the_circuit_breaker_activity(monkeypatc
# Trigger-spawned run -> schedules the breaker.
await instance._pause_deployment_on_failure({"workflow_id": "wf"}, nodes, errors)
- assert captured["name"] == "workflow_control.pause_on_failure.v1"
+ assert captured["name"] == "workflow_control.pause_on_failure"
assert captured["payload"] == {
"workflow_id": "wf",
"reason": "credential expired",
diff --git a/server/tests/temporal/test_frozen_routing.py b/server/tests/temporal/test_frozen_routing.py
new file mode 100644
index 00000000..0a265b46
--- /dev/null
+++ b/server/tests/temporal/test_frozen_routing.py
@@ -0,0 +1,291 @@
+"""Replay-safe Temporal root routing and worker registration contracts."""
+
+from __future__ import annotations
+
+import inspect
+from datetime import timedelta
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+import nodes # noqa: F401 -- populate the node registry
+
+
+def _settings(
+ *,
+ agent: bool,
+ per_type: bool,
+ worker_pool: bool,
+) -> SimpleNamespace:
+ return SimpleNamespace(
+ temporal_agent_workflow_enabled=agent,
+ temporal_per_type_dispatch=per_type,
+ temporal_worker_pool_enabled=worker_pool,
+ )
+
+
+def test_routing_snapshot_captures_all_command_shaping_flags():
+ from services.temporal.executor import capture_temporal_routing_input
+ from services.temporal.workflow import TEMPORAL_ROUTING_INPUT_KEY
+
+ with patch(
+ "core.config.Settings",
+ side_effect=lambda: _settings(
+ agent=False,
+ per_type=True,
+ worker_pool=True,
+ ),
+ ):
+ value = capture_temporal_routing_input()
+
+ assert value == {
+ TEMPORAL_ROUTING_INPUT_KEY: {
+ "version": 1,
+ "agent_workflow_enabled": False,
+ "per_type_dispatch_enabled": True,
+ "worker_pool_enabled": True,
+ }
+ }
+
+
+def test_unknown_routing_input_version_uses_deterministic_safe_defaults():
+ from services.temporal.workflow import (
+ TEMPORAL_ROUTING_INPUT_KEY,
+ _frozen_routing_from_input,
+ )
+
+ value = _frozen_routing_from_input(
+ {
+ TEMPORAL_ROUTING_INPUT_KEY: {
+ "version": 999,
+ "agent_workflow_enabled": False,
+ "per_type_dispatch_enabled": False,
+ "worker_pool_enabled": True,
+ }
+ }
+ )
+
+ assert value == {
+ "version": 1,
+ "agent_workflow_enabled": True,
+ "per_type_dispatch_enabled": True,
+ "worker_pool_enabled": False,
+ }
+
+
+async def test_executor_places_frozen_routing_in_new_root_input():
+ from services.temporal.executor import TemporalExecutor
+ from services.temporal.workflow import TEMPORAL_ROUTING_INPUT_KEY
+
+ client = MagicMock()
+ client.execute_workflow = AsyncMock(
+ return_value={
+ "success": True,
+ "execution_trace": [],
+ "outputs": {},
+ }
+ )
+ executor = TemporalExecutor(client)
+
+ with patch(
+ "core.config.Settings",
+ side_effect=lambda: _settings(
+ agent=True,
+ per_type=False,
+ worker_pool=False,
+ ),
+ ):
+ result = await executor.execute_workflow(
+ workflow_id="wf-1",
+ nodes=[],
+ edges=[],
+ )
+
+ assert result["success"] is True
+ payload = client.execute_workflow.await_args.args[1]
+ assert payload["user_id"] == "owner"
+ assert payload[TEMPORAL_ROUTING_INPUT_KEY] == {
+ "version": 1,
+ "agent_workflow_enabled": True,
+ "per_type_dispatch_enabled": False,
+ "worker_pool_enabled": False,
+ }
+
+
+def test_frozen_dispatch_never_reads_mutable_settings():
+ from services.temporal.workflow import MachinaWorkflow
+
+ workflow_instance = MachinaWorkflow()
+ frozen = {
+ "version": 1,
+ "agent_workflow_enabled": False,
+ "per_type_dispatch_enabled": True,
+ "worker_pool_enabled": True,
+ }
+ with patch(
+ "core.config.Settings",
+ side_effect=AssertionError("workflow dispatch read live Settings"),
+ ):
+ dispatch = workflow_instance._resolve_dispatch(
+ "pythonExecutor",
+ routing_snapshot=frozen,
+ )
+
+ assert dispatch == {
+ "kind": "activity",
+ "name": "node.pythonExecutor.v1",
+ "queue": "code-exec",
+ }
+
+
+def test_frozen_agent_dispatch_selects_context_v2_protocol():
+ from services.temporal.workflow import MachinaWorkflow
+
+ dispatch = MachinaWorkflow()._resolve_dispatch(
+ "aiAgent",
+ graph_version=2,
+ generation=3,
+ context_v2_enabled=True,
+ routing_snapshot={
+ "version": 1,
+ "agent_workflow_enabled": True,
+ "per_type_dispatch_enabled": True,
+ "worker_pool_enabled": False,
+ },
+ )
+
+ assert dispatch == {
+ "kind": "child_workflow",
+ "name": "AgentWorkflow",
+ }
+
+
+
+
+async def test_create_worker_registers_plugin_and_pause_activities(
+ monkeypatch,
+):
+ from services.temporal import agent_activities
+ from services.temporal import plugin_activities
+ from services.temporal import worker as worker_module
+ from services.temporal.activities import (
+ pause_workflow_on_failure_activity,
+ )
+
+ async def plugin_activity(_payload):
+ return None
+
+ monkeypatch.setattr(
+ plugin_activities,
+ "collect_plugin_activities",
+ lambda: [plugin_activity],
+ )
+ monkeypatch.setattr(
+ agent_activities,
+ "collect_agent_activities",
+ lambda: [],
+ )
+ monkeypatch.setattr(
+ worker_module,
+ "_graceful_shutdown_timeout",
+ lambda: timedelta(seconds=1),
+ )
+
+ captured = {}
+
+ def fake_worker(_client, **kwargs):
+ captured.update(kwargs)
+ return MagicMock()
+
+ monkeypatch.setattr(worker_module, "Worker", fake_worker)
+ session = MagicMock()
+ await worker_module.create_worker(
+ MagicMock(),
+ session=session,
+ )
+
+ assert plugin_activity in captured["activities"]
+ assert pause_workflow_on_failure_activity in captured["activities"]
+
+
+async def test_standalone_worker_registers_plugin_and_pause_activities(
+ monkeypatch,
+):
+ from core import container as container_module
+ from services import model_registry
+ from services.temporal import agent_activities
+ from services.temporal import plugin_activities
+ from services.temporal import worker as worker_module
+ from services.temporal.activities import (
+ pause_workflow_on_failure_activity,
+ )
+
+ async def plugin_activity(_payload):
+ return None
+
+ monkeypatch.setattr(
+ plugin_activities,
+ "collect_plugin_activities",
+ lambda: [plugin_activity],
+ )
+ monkeypatch.setattr(
+ agent_activities,
+ "collect_agent_activities",
+ lambda: [],
+ )
+ monkeypatch.setattr(
+ worker_module,
+ "_graceful_shutdown_timeout",
+ lambda: timedelta(seconds=1),
+ )
+ monkeypatch.setattr(worker_module, "create_runtime", lambda: MagicMock())
+ monkeypatch.setattr(
+ worker_module.Client,
+ "connect",
+ AsyncMock(return_value=MagicMock()),
+ )
+
+ session = MagicMock(closed=False)
+ session.close = AsyncMock()
+ monkeypatch.setattr(
+ worker_module,
+ "create_shared_session",
+ AsyncMock(return_value=session),
+ )
+ registry = MagicMock()
+ monkeypatch.setattr(
+ model_registry,
+ "get_model_registry",
+ lambda: registry,
+ )
+ unifier = MagicMock()
+ unifier.aclose = AsyncMock()
+ monkeypatch.setattr(
+ container_module.container,
+ "chat_unifier",
+ lambda: unifier,
+ )
+
+ captured = {}
+
+ class _Worker:
+ def __init__(self, _client, **kwargs):
+ captured.update(kwargs)
+
+ async def run(self):
+ return None
+
+ monkeypatch.setattr(worker_module, "Worker", _Worker)
+
+ await worker_module.run_standalone_worker(
+ server_address="temporal.test:7233",
+ )
+
+ assert plugin_activity in captured["activities"]
+ assert pause_workflow_on_failure_activity in captured["activities"]
+ session.close.assert_awaited_once()
+ unifier.aclose.assert_awaited_once()
diff --git a/server/tests/temporal/test_machina_workflow_loop.py b/server/tests/temporal/test_machina_workflow_loop.py
index 0cea3ac3..03c56c08 100644
--- a/server/tests/temporal/test_machina_workflow_loop.py
+++ b/server/tests/temporal/test_machina_workflow_loop.py
@@ -87,7 +87,11 @@ class TestSkippedTriggerUnblocksDownstream:
monkeypatch.setattr(
MachinaWorkflow,
"_resolve_dispatch",
- lambda self, node_type: {"kind": "activity", "name": f"node.{node_type}.v1", "queue": None},
+ lambda self, node_type, **_kwargs: {
+ "kind": "activity",
+ "name": f"node.{node_type}.v1",
+ "queue": None,
+ },
)
wf = MachinaWorkflow()
diff --git a/server/tests/temporal/test_native_llm_cutover.py b/server/tests/temporal/test_native_llm_cutover.py
index 94c420b1..6a54eda2 100644
--- a/server/tests/temporal/test_native_llm_cutover.py
+++ b/server/tests/temporal/test_native_llm_cutover.py
@@ -167,11 +167,11 @@ class TestWorkflowBranchContract:
llm_calls: list[tuple[dict, dict]] = []
async def fake_execute_activity(name, *, args, **kwargs):
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _payload(native=True)
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_calls.append((args[0], kwargs))
return {
"kind": "final",
@@ -179,9 +179,9 @@ class TestWorkflowBranchContract:
"thinking": None,
"usage": {"input_tokens": 3, "output_tokens": 1},
}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -230,11 +230,11 @@ class TestWorkflowBranchContract:
async def fake_execute_activity(name, *, args, **_kwargs):
nonlocal llm_turn, compaction_calls
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return prepared
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_turn += 1
if llm_turn == 1:
return {
@@ -262,7 +262,7 @@ class TestWorkflowBranchContract:
"total_tokens": 6,
},
}
- if name == "agent.compact_memory.v1":
+ if name == "agent.compact_memory":
compaction_calls += 1
return {
"success": True,
@@ -275,9 +275,9 @@ class TestWorkflowBranchContract:
"total_tokens": 6,
},
}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -319,11 +319,11 @@ class TestWorkflowBranchContract:
async def fake_execute_activity(name, *, args, **_kwargs):
nonlocal llm_turn
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return prepared
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_turn += 1
if llm_turn == 1:
return {
@@ -352,7 +352,7 @@ class TestWorkflowBranchContract:
"total_tokens": 3,
},
}
- if name == "agent.compact_memory.v1":
+ if name == "agent.compact_memory":
compact_payloads.append(args[0])
return {
"success": True,
@@ -365,9 +365,9 @@ class TestWorkflowBranchContract:
"total_tokens": 500,
},
}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -416,11 +416,11 @@ class TestWorkflowBranchContract:
)
async def fake_execute_activity(name, *, args, **_kwargs):
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return prepared
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
return {
"kind": "tool_calls",
"calls": [
@@ -437,13 +437,13 @@ class TestWorkflowBranchContract:
"cache_read_tokens": 50,
},
}
- if name == "agent.compact_memory.v1":
+ if name == "agent.compact_memory":
raise AssertionError(
"markerless threshold must use input + output only"
)
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -468,15 +468,15 @@ class TestWorkflowBranchContract:
from services.temporal.agent_workflow import AgentWorkflow
async def fake_execute_activity(name, *, args, **_kwargs):
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _payload(native=False)
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
failure = RuntimeError("outer activity failure")
failure.cause = RuntimeError("legacy raw provider detail")
raise failure
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -506,11 +506,11 @@ class TestWorkflowBranchContract:
llm_calls: list[tuple[dict, dict]] = []
async def fake_execute_activity(name, *, args, **kwargs):
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _payload(native=False)
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_calls.append((args[0], kwargs))
return {
"kind": "final",
@@ -518,9 +518,9 @@ class TestWorkflowBranchContract:
"thinking": None,
"usage": {},
}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -569,12 +569,12 @@ class TestWorkflowBranchContract:
llm_calls: list[tuple[dict, dict]] = []
async def fake_execute_activity(name, *, args, **kwargs):
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
assert "api_key" not in prepared_payload
return prepared_payload
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_calls.append((args[0], kwargs))
return {
"kind": "final",
@@ -582,9 +582,9 @@ class TestWorkflowBranchContract:
"thinking": None,
"usage": {},
}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
diff --git a/server/tests/temporal/test_temporal_identity.py b/server/tests/temporal/test_temporal_identity.py
index 1b8a133e..e51a261a 100644
--- a/server/tests/temporal/test_temporal_identity.py
+++ b/server/tests/temporal/test_temporal_identity.py
@@ -66,31 +66,25 @@ def patched_workflow(monkeypatch):
class TestIdentityHelpers:
- def test_patch_ids_are_stable_protocol_constants(self):
- from services.temporal.agent_workflow import TOOL_CALL_IDENTITY_V2_PATCH
- from services.temporal.workflow import AGENT_CHILD_ID_V2_PATCH
-
- assert AGENT_CHILD_ID_V2_PATCH == "machina-agent-child-id-v2"
- assert TOOL_CALL_IDENTITY_V2_PATCH == "agent-tool-call-identity-v2"
def test_root_child_id_uses_run_and_canvas_node(self):
- from services.temporal.workflow import _agent_child_workflow_id_v2
+ from services.temporal.workflow import _agent_child_workflow_id
- assert _agent_child_workflow_id_v2("root-a", "agent-1") == "root-a-agent-agent-1"
- assert _agent_child_workflow_id_v2("root-a", "agent-1") != _agent_child_workflow_id_v2("root-a", "agent-2")
- assert _agent_child_workflow_id_v2("root-a", "agent-1") != _agent_child_workflow_id_v2("root-b", "agent-1")
+ assert _agent_child_workflow_id("root-a", "agent-1") == "root-a-agent-agent-1"
+ assert _agent_child_workflow_id("root-a", "agent-1") != _agent_child_workflow_id("root-a", "agent-2")
+ assert _agent_child_workflow_id("root-a", "agent-1") != _agent_child_workflow_id("root-b", "agent-1")
def test_same_tool_calls_have_distinct_command_ids(self):
from services.temporal.agent_workflow import (
- _delegation_child_id_v2,
- _refresh_tools_activity_id_v2,
- _tool_activity_id_v2,
+ _delegation_child_id,
+ _refresh_tools_activity_id,
+ _tool_activity_id,
)
- assert _tool_activity_id_v2("tool-1", 0, 0) == "tool-tool-1-1-1"
- assert _tool_activity_id_v2("tool-1", 0, 1) == "tool-tool-1-1-2"
- assert _delegation_child_id_v2("agent-run", "agent-2", 0, 1) == "agent-run-delegate-agent-2-1-2"
- assert _refresh_tools_activity_id_v2("builder-1", 2, 1) == "refresh-tools-builder-1-3-2"
+ assert _tool_activity_id("tool-1", 0, 0) == "tool-tool-1-1-1"
+ assert _tool_activity_id("tool-1", 0, 1) == "tool-tool-1-1-2"
+ assert _delegation_child_id("agent-run", "agent-2", 0, 1) == "agent-run-delegate-agent-2-1-2"
+ assert _refresh_tools_activity_id("builder-1", 2, 1) == "refresh-tools-builder-1-3-2"
def test_duplicate_visible_name_error_is_deterministic_and_actionable(self):
from services.temporal.agent_workflow import _duplicate_visible_tool_name_error
@@ -108,18 +102,6 @@ class TestIdentityHelpers:
assert "unique Tool Name" in message
assert _duplicate_visible_tool_name_error([_tool("todo-a"), _tool("search-1", name="search")]) is None
- def test_patch_gates_keep_legacy_command_shapes_in_source(self):
- from services.temporal.agent_workflow import AgentWorkflow
- from services.temporal.workflow import MachinaWorkflow
-
- root_source = inspect.getsource(MachinaWorkflow.run)
- assert 'workflow.patched(AGENT_CHILD_ID_V2_PATCH)' in root_source
- assert 'f"{workflow_slug}-{node_label_slug(node)}"' in root_source
-
- agent_source = inspect.getsource(AgentWorkflow.run)
- assert 'workflow.patched(TOOL_CALL_IDENTITY_V2_PATCH)' in agent_source
- assert 'f"tool-{tool_info[\'tool_node_id\']}-{iteration + 1}"' in agent_source
- assert 'f"refresh-tools-{tool_info[\'tool_node_id\']}-{iteration + 1}"' in agent_source
class TestRootChildIdentity:
@@ -128,9 +110,11 @@ class TestRootChildIdentity:
from services.temporal.workflow import MachinaWorkflow
child_ids: list[str] = []
+ child_contexts: list[dict] = []
- async def fake_start_child_workflow(_name, *, id, **_kwargs):
+ async def fake_start_child_workflow(_name, *, id, args, **_kwargs):
child_ids.append(id)
+ child_contexts.append(args[0])
handle = asyncio.get_running_loop().create_future()
handle.set_result({"success": True, "result": {"response": id}})
return handle
@@ -139,7 +123,11 @@ class TestRootChildIdentity:
monkeypatch.setattr(
MachinaWorkflow,
"_resolve_dispatch",
- lambda _self, _node_type: {"kind": "child_workflow", "name": "AgentWorkflow", "queue": None},
+ lambda _self, _node_type, **_kwargs: {
+ "kind": "child_workflow",
+ "name": "AgentWorkflow",
+ "queue": None,
+ },
)
result = await MachinaWorkflow().run(
@@ -152,50 +140,17 @@ class TestRootChildIdentity:
"workflow_id": "graph-1",
"workflow_slug": "Graph",
"execution_id": "root-run-1",
+ "user_id": "account-41",
}
)
assert result["success"] is True
assert child_ids == ["root-run-1-agent-agent-a", "root-run-1-agent-agent-b"]
+ assert [context["user_id"] for context in child_contexts] == [
+ "account-41",
+ "account-41",
+ ]
- @pytest.mark.asyncio
- async def test_pre_patch_history_keeps_legacy_label_child_id(self, monkeypatch, patched_workflow):
- from services.temporal.workflow import MachinaWorkflow
-
- monkeypatch.setattr(patched_workflow, "patched", lambda _patch_id: False)
- child_ids: list[str] = []
-
- async def fake_start_child_workflow(_name, *, id, **_kwargs):
- child_ids.append(id)
- handle = asyncio.get_running_loop().create_future()
- handle.set_result({"success": True, "result": {"response": id}})
- return handle
-
- monkeypatch.setattr(patched_workflow, "start_child_workflow", fake_start_child_workflow)
- monkeypatch.setattr(
- MachinaWorkflow,
- "_resolve_dispatch",
- lambda _self, _node_type: {
- "kind": "child_workflow",
- "name": "AgentWorkflow",
- "queue": None,
- },
- )
-
- result = await MachinaWorkflow().run(
- {
- "nodes": [
- {"id": "agent-a", "type": "aiAgent", "data": {"label": "Legacy Worker"}},
- ],
- "edges": [],
- "workflow_id": "graph-1",
- "workflow_slug": "Graph_One",
- "execution_id": "root-run-1",
- }
- )
-
- assert result["success"] is True
- assert child_ids == ["Graph_One-Legacy_Worker"]
class TestAgentCallIdentity:
@@ -209,11 +164,11 @@ class TestAgentCallIdentity:
async def fake_execute_activity(name, *, args, **kwargs):
nonlocal llm_steps
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _agent_payload([_tool("todo-1")])
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_steps += 1
if llm_steps == 1:
return {
@@ -228,9 +183,9 @@ class TestAgentCallIdentity:
if name == "node.writeTodos.v1":
tool_commands.append((kwargs["activity_id"], args[0]))
return {"success": True, "todos": []}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
@@ -249,50 +204,6 @@ class TestAgentCallIdentity:
assert all(payload["invoking_agent_node_id"] == "agent-1" for _command_id, payload in tool_commands)
assert all(payload["agent_iteration"] == 1 for _command_id, payload in tool_commands)
- @pytest.mark.asyncio
- async def test_pre_patch_history_keeps_legacy_activity_id_and_payload(self, monkeypatch, patched_workflow):
- import services.temporal.agent_workflow as agent_module
- from services.temporal.agent_workflow import AgentWorkflow
-
- monkeypatch.setattr(patched_workflow, "patched", lambda _patch_id: False)
- tool_commands: list[tuple[str, dict]] = []
- llm_steps = 0
-
- async def fake_execute_activity(name, *, args, **kwargs):
- nonlocal llm_steps
- if name == "agent.prepare_payload.v1":
- return _agent_payload([_tool("todo-1")])
- if name == "agent.broadcast_progress.v1":
- return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
- llm_steps += 1
- if llm_steps == 1:
- return {
- "kind": "tool_calls",
- "calls": [
- {"id": "legacy-call", "name": "write_todos", "args": {"todos": []}},
- ],
- "usage": {},
- }
- return {"kind": "final", "content": "done", "usage": {}}
- if name == "node.writeTodos.v1":
- tool_commands.append((kwargs["activity_id"], args[0]))
- return {"success": True, "todos": []}
- if name == "agent.store_output.v1":
- return {"stored": True}
- if name == "agent.skill.clear.v1":
- return {"cleared": True}
- raise AssertionError(f"Unexpected activity {name}")
-
- monkeypatch.setattr(patched_workflow, "execute_activity", fake_execute_activity)
- monkeypatch.setattr(agent_module, "get_node_class", lambda _node_type: SimpleNamespace(needs_canvas=False))
-
- result = await AgentWorkflow().run({"node_id": "agent-1", "execution_id": "root-run-1"})
-
- assert result["success"] is True
- assert tool_commands[0][0] == "tool-todo-1-1"
- assert "tool_call_index" not in tool_commands[0][1]
- assert "invoking_agent_node_id" not in tool_commands[0][1]
@pytest.mark.asyncio
async def test_duplicate_initial_names_fail_before_llm(self, monkeypatch, patched_workflow):
@@ -302,9 +213,9 @@ class TestAgentCallIdentity:
async def fake_execute_activity(name, *, args, **_kwargs):
activities.append((name, args[0]))
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _agent_payload([_tool("todo-a", label="First"), _tool("todo-b", label="Second")])
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
return {"emitted": True}
raise AssertionError(f"Duplicate names must fail before {name}")
@@ -320,8 +231,8 @@ class TestAgentCallIdentity:
"todo-b",
]
assert [name for name, _payload in activities] == [
- "agent.prepare_payload.v1",
- "agent.broadcast_progress.v1",
+ "agent.prepare_payload",
+ "agent.broadcast_progress",
]
error_phase = activities[-1][1]
assert error_phase["status"] == "error"
@@ -337,12 +248,12 @@ class TestAgentCallIdentity:
phases: list[dict] = []
async def fake_execute_activity(name, *, args, **kwargs):
- if name == "agent.prepare_payload.v1":
+ if name == "agent.prepare_payload":
return _agent_payload([_tool("todo-1", label="Original")])
- if name == "agent.broadcast_progress.v1":
+ if name == "agent.broadcast_progress":
phases.append(args[0])
return {"emitted": True}
- if name == "agent.execute_llm_step.v1":
+ if name == "agent.execute_llm_step":
llm_payloads.append(args[0])
if len(llm_payloads) == 1:
return {
@@ -353,12 +264,12 @@ class TestAgentCallIdentity:
return {"kind": "final", "content": "done", "usage": {}}
if name == "node.writeTodos.v1":
return {"success": True, "operations": [{"op": "add_node"}]}
- if name == "agent.refresh_tools.v1":
+ if name == "agent.refresh_tools":
refresh_commands.append((kwargs["activity_id"], args[0]))
return {"tools": [_tool("todo-2", label="Conflicting")]}
- if name == "agent.store_output.v1":
+ if name == "agent.store_output":
return {"stored": True}
- if name == "agent.skill.clear.v1":
+ if name == "agent.skill.clear":
return {"cleared": True}
raise AssertionError(f"Unexpected activity {name}")
diff --git a/server/tests/test_context_graph_migration.py b/server/tests/test_context_graph_migration.py
new file mode 100644
index 00000000..56f3f3ee
--- /dev/null
+++ b/server/tests/test_context_graph_migration.py
@@ -0,0 +1,574 @@
+from __future__ import annotations
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+from pydantic import BaseModel
+
+from services.workflow_migrations import normalize_workflow_graph
+
+
+def _node(node_id: str, node_type: str, x: int = 0):
+ return {
+ "id": node_id,
+ "type": node_type,
+ "position": {"x": x, "y": 20},
+ "data": {"label": node_id},
+ }
+
+
+def test_legacy_memory_becomes_isolated_context_and_shared_tool(monkeypatch):
+ monkeypatch.setattr(
+ "services.workflow_migrations._requires_context",
+ lambda node_type: node_type == "agent",
+ )
+ nodes = [
+ _node("memory", "simpleMemory"),
+ _node("first", "agent", 400),
+ _node("second", "agent", 800),
+ ]
+ edges = [
+ {
+ "source": "memory",
+ "target": "first",
+ "sourceHandle": "output-memory",
+ "targetHandle": "input-memory",
+ },
+ {
+ "source": "memory",
+ "target": "second",
+ "source_handle": "output-memory",
+ "target_handle": "input-memory",
+ },
+ ]
+ result = normalize_workflow_graph(
+ "42",
+ nodes,
+ edges,
+ {"memory": {"memory_content": "## Human\nhello"}},
+ )
+
+ contexts = [node for node in result.nodes if node["type"] == "context"]
+ assert [node["id"] for node in contexts] == ["42:context:1", "42:context:2"]
+ context_edges = [edge for edge in result.edges if edge.get("targetHandle") == "input-context"]
+ assert len(context_edges) == 2
+ assert len({edge["source"] for edge in context_edges}) == 2
+ tool_edges = [edge for edge in result.edges if edge.get("targetHandle") == "input-tools"]
+ assert len(tool_edges) == 2
+ assert {edge["source"] for edge in tool_edges} == {"42:simpleMemory:1"}
+ assert len(result.state_imports) == 2
+ assert all(item["event_type"] == "legacy_partial" for item in result.state_imports)
+ assert all("memory_content" not in node.get("data", {}) for node in contexts)
+
+
+def test_context_graph_normalization_is_idempotent(monkeypatch):
+ monkeypatch.setattr(
+ "services.workflow_migrations._requires_context",
+ lambda node_type: node_type == "agent",
+ )
+ first = normalize_workflow_graph(
+ "7",
+ [_node("agent", "agent")],
+ [],
+ )
+ second = normalize_workflow_graph(
+ "7",
+ first.nodes,
+ first.edges,
+ first.node_parameters,
+ )
+ assert second.nodes == first.nodes
+ assert second.edges == first.edges
+ assert second.aliases == {}
+
+
+def test_backend_repairs_required_edge_and_removes_deleted_agent_context(
+ monkeypatch,
+):
+ monkeypatch.setattr(
+ "services.workflow_migrations._requires_context",
+ lambda node_type: node_type == "agent",
+ )
+ nodes = [
+ {
+ **_node("agent", "agent"),
+ "data": {"label": "Agent"},
+ },
+ {
+ **_node("ctx", "context"),
+ "data": {
+ "label": "Context",
+ "systemManaged": True,
+ "agentNodeId": "agent",
+ },
+ },
+ ]
+ repaired = normalize_workflow_graph("3", nodes, [])
+ assert [edge for edge in repaired.edges if edge.get("targetHandle") == "input-context"]
+ context = next(node for node in repaired.nodes if node["type"] == "context")
+ assert context["data"]["agentNodeId"] == "3:agent:1"
+
+ deleted = normalize_workflow_graph(
+ "3",
+ [node for node in repaired.nodes if node["type"] != "agent"],
+ [],
+ )
+ assert not [node for node in deleted.nodes if node["type"] == "context"]
+
+
+def test_normalization_removes_duplicate_system_context_deterministically(
+ monkeypatch,
+):
+ monkeypatch.setattr(
+ "services.workflow_migrations._requires_context",
+ lambda node_type: node_type == "agent",
+ )
+ nodes = [
+ _node("agent", "agent"),
+ {
+ **_node("ctx-a", "context"),
+ "data": {
+ "label": "ctx-a",
+ "systemManaged": True,
+ "agentNodeId": "agent",
+ },
+ },
+ {
+ **_node("ctx-b", "context"),
+ "data": {
+ "label": "ctx-b",
+ "systemManaged": True,
+ "agentNodeId": "agent",
+ },
+ },
+ ]
+ edges = [
+ {
+ "source": "ctx-b",
+ "target": "agent",
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ }
+ ]
+
+ result = normalize_workflow_graph("5", nodes, edges)
+
+ contexts = [node for node in result.nodes if node["type"] == "context"]
+ assert len(contexts) == 1
+ assert contexts[0]["data"]["label"] == "ctx-b"
+ assert contexts[0]["data"]["agentNodeId"] == "5:agent:1"
+ assert any("Removed duplicate system Context" in warning for warning in result.warnings)
+
+
+def test_stale_system_context_owner_is_removed_instead_of_reassigned(
+ monkeypatch,
+):
+ monkeypatch.setattr(
+ "services.workflow_migrations._requires_context",
+ lambda node_type: node_type == "agent",
+ )
+ nodes = [
+ _node("agent", "agent"),
+ {
+ **_node("ctx", "context"),
+ "data": {
+ "label": "stale",
+ "systemManaged": True,
+ "agentNodeId": "deleted-agent",
+ },
+ },
+ ]
+ edges = [
+ {
+ "source": "ctx",
+ "target": "agent",
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ }
+ ]
+
+ result = normalize_workflow_graph("6", nodes, edges)
+
+ contexts = [node for node in result.nodes if node["type"] == "context"]
+ assert len(contexts) == 1
+ assert contexts[0]["data"]["label"] == "Context"
+ assert contexts[0]["data"]["agentNodeId"] == "6:agent:1"
+ assert any("Removed orphaned system Context 'ctx'" == warning for warning in result.warnings)
+
+
+def test_system_context_shared_edge_is_repaired_to_recorded_owner(
+ monkeypatch,
+):
+ monkeypatch.setattr(
+ "services.workflow_migrations._requires_context",
+ lambda node_type: node_type == "agent",
+ )
+ nodes = [
+ _node("a", "agent"),
+ _node("b", "agent"),
+ {
+ **_node("ctx", "context"),
+ "data": {
+ "label": "existing",
+ "systemManaged": True,
+ "agentNodeId": "a",
+ },
+ },
+ ]
+ edges = [
+ {
+ "source": "ctx",
+ "target": target,
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ }
+ for target in ("a", "b")
+ ]
+
+ result = normalize_workflow_graph("8", nodes, edges)
+
+ context_edges = [edge for edge in result.edges if edge.get("targetHandle") == "input-context"]
+ assert len(context_edges) == 2
+ assert len({edge["source"] for edge in context_edges}) == 2
+ existing = next(node for node in result.nodes if node["type"] == "context" and node["data"]["label"] == "existing")
+ assert existing["data"]["agentNodeId"] == "8:agent:1"
+ assert next(edge["target"] for edge in context_edges if edge["source"] == existing["id"]) == "8:agent:1"
+
+
+class _Params(BaseModel):
+ pass
+
+
+class _ContextCapable:
+ Params = _Params
+ credentials = ()
+ requires_context = True
+ ui_hints = {}
+
+
+class _ContextNode:
+ Params = _Params
+ credentials = ()
+ requires_context = False
+ ui_hints = {}
+
+
+async def test_validator_rejects_missing_multiple_and_shared_contexts(monkeypatch):
+ from services.workflow_validator import validate_workflow
+
+ monkeypatch.setattr(
+ "services.workflow_validator.get_node_class",
+ lambda node_type: {
+ "agent": _ContextCapable,
+ "context": _ContextNode,
+ }.get(node_type),
+ )
+ nodes = [
+ _node("a", "agent"),
+ _node("b", "agent"),
+ _node("c1", "context"),
+ _node("c2", "context"),
+ ]
+ edges = [
+ {
+ "source": "c1",
+ "target": "a",
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ },
+ {
+ "source": "c2",
+ "target": "a",
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ },
+ {
+ "source": "c1",
+ "target": "b",
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ },
+ ]
+ report = await validate_workflow(nodes, edges)
+ codes = {issue["code"] for issue in report["errors"]}
+ assert "MULTIPLE_CONTEXTS" in codes
+ assert "SHARED_CONTEXT" in codes
+
+ missing = await validate_workflow([_node("a", "agent")], [])
+ assert "MISSING_CONTEXT" in {issue["code"] for issue in missing["errors"]}
+
+
+async def test_save_rejects_ambiguous_shared_context_before_persistence(
+ monkeypatch,
+):
+ from services.workflow_storage import handlers
+
+ monkeypatch.setattr(
+ "services.workflow_migrations._requires_context",
+ lambda node_type: node_type == "agent",
+ )
+ monkeypatch.setattr(
+ "services.workflow_validator.get_node_class",
+ lambda node_type: {
+ "agent": _ContextCapable,
+ "context": _ContextNode,
+ }.get(node_type),
+ )
+ database = type("Database", (), {})()
+ database.allocate_workflow_id = AsyncMock(return_value="1")
+ database.get_workflow = AsyncMock(return_value=None)
+ database.list_workflow_slugs = AsyncMock(return_value=[])
+ database.get_node_parameters = AsyncMock(return_value={})
+ database.save_workflow = AsyncMock(return_value=True)
+ monkeypatch.setattr(handlers.container, "database", lambda: database)
+
+ result = await handlers.handle_save_workflow(
+ {
+ "workflow_id": "new",
+ "name": "Invalid Context",
+ "data": {
+ "nodes": [
+ _node("a", "agent"),
+ _node("b", "agent"),
+ {
+ **_node("ctx", "context"),
+ "data": {"label": "Context", "systemManaged": True},
+ },
+ ],
+ "edges": [
+ {
+ "source": "ctx",
+ "target": target,
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ }
+ for target in ("a", "b")
+ ],
+ },
+ },
+ websocket=None,
+ )
+
+ assert result["success"] is False
+ assert result["error"] == "invalid_context_topology"
+ assert "SHARED_CONTEXT" in {issue["code"] for issue in result["validation_errors"]}
+ database.save_workflow.assert_not_awaited()
+
+
+async def test_save_uses_authenticated_owner_and_ignores_client_owner(
+ monkeypatch,
+):
+ from services.workflow_storage import handlers
+
+ database = type("Database", (), {})()
+ database.allocate_workflow_id = AsyncMock(return_value="1")
+ database.get_workflow = AsyncMock(return_value=None)
+ database.list_workflow_slugs = AsyncMock(return_value=[])
+ database.get_node_parameters = AsyncMock(return_value={})
+ database.save_workflow = AsyncMock(return_value=True)
+ monkeypatch.setattr(handlers.container, "database", lambda: database)
+
+ result = await handlers.handle_save_workflow(
+ {
+ "workflow_id": "new",
+ "name": "Owned",
+ "data": {
+ "nodes": [],
+ "edges": [],
+ "owner_id": "client-forgery",
+ },
+ },
+ websocket=SimpleNamespace(state=SimpleNamespace(user_id="authenticated-user")),
+ )
+
+ assert result["success"] is True
+ assert result["data"]["owner_id"] == "authenticated-user"
+ assert database.save_workflow.await_args.kwargs["data"]["owner_id"] == ("authenticated-user")
+
+
+async def test_save_cannot_replace_backend_owned_context(
+ monkeypatch,
+):
+ from services.workflow_storage import handlers
+
+ monkeypatch.setattr(
+ "services.workflow_migrations._requires_context",
+ lambda node_type: node_type == "agent",
+ )
+ monkeypatch.setattr(
+ "services.workflow_validator.get_node_class",
+ lambda node_type: {
+ "agent": _ContextCapable,
+ "context": _ContextNode,
+ }.get(node_type),
+ )
+ existing = SimpleNamespace(
+ id="1",
+ name="Owned",
+ slug="owned",
+ description=None,
+ data={
+ "graphVersion": 2,
+ "owner_id": "owner",
+ "nodes": [
+ _node("1:agent:1", "agent"),
+ {
+ **_node("1:context:1", "context"),
+ "data": {
+ "label": "Context",
+ "systemManaged": True,
+ "agentNodeId": "1:agent:1",
+ },
+ },
+ ],
+ "edges": [
+ {
+ "source": "1:context:1",
+ "target": "1:agent:1",
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ }
+ ],
+ },
+ )
+ database = type("Database", (), {})()
+ database.get_workflow = AsyncMock(return_value=existing)
+ database.get_node_parameters = AsyncMock(return_value={})
+ database.save_workflow = AsyncMock(return_value=True)
+ monkeypatch.setattr(handlers.container, "database", lambda: database)
+
+ result = await handlers.handle_save_workflow(
+ {
+ "workflow_id": "1",
+ "name": "Owned",
+ "data": {
+ "nodes": [
+ _node("1:agent:1", "agent"),
+ {
+ **_node("1:context:99", "context"),
+ "data": {
+ "label": "Forged",
+ "systemManaged": True,
+ "agentNodeId": "1:agent:1",
+ },
+ },
+ ],
+ "edges": [
+ {
+ "source": "1:context:99",
+ "target": "1:agent:1",
+ "sourceHandle": "output-context",
+ "targetHandle": "input-context",
+ }
+ ],
+ "owner_id": "forged-owner",
+ },
+ },
+ websocket=None,
+ )
+
+ contexts = [
+ node
+ for node in result["data"]["nodes"]
+ if node["type"] == "context"
+ ]
+ assert [node["id"] for node in contexts] == ["1:context:1"]
+ assert contexts[0]["data"]["agentNodeId"] == "1:agent:1"
+ assert result["data"]["owner_id"] == "owner"
+ assert any(
+ "Ignored untrusted Context companion '1:context:99'"
+ == warning
+ for warning in result["migration_warnings"]
+ )
+
+
+def test_save_owner_resolution_preserves_existing_backend_owner():
+ from services.workflow_storage.handlers import _trusted_owner_id
+
+ assert (
+ _trusted_owner_id(
+ websocket=None,
+ existing=SimpleNamespace(data={"owner_id": "stored-owner"}),
+ )
+ == "stored-owner"
+ )
+
+
+async def test_workflow_delete_archives_context_before_graph(
+ monkeypatch,
+):
+ from services.workflow_storage import handlers
+
+ database = type("Database", (), {})()
+ database.get_workflow = AsyncMock(
+ return_value=SimpleNamespace(
+ data={
+ "nodes": [
+ _node("agent", "agent"),
+ _node("ctx", "context"),
+ ]
+ }
+ )
+ )
+ database.delete_workflow = AsyncMock(return_value=True)
+ store = SimpleNamespace(archive_context=AsyncMock(return_value=[]))
+ monkeypatch.setattr(handlers.container, "database", lambda: database)
+ monkeypatch.setattr(
+ "services.agent_context.AgentContextStore",
+ lambda _: store,
+ )
+
+ result = await handlers.handle_delete_workflow(
+ {"workflow_id": "12"},
+ websocket=None,
+ )
+
+ assert result == {
+ "success": True,
+ "workflow_id": "12",
+ "contexts_archived": 1,
+ "context_archives_pending": 0,
+ }
+ store.archive_context.assert_awaited_once_with(
+ workflow_id="12",
+ context_node_id="ctx",
+ generation=None,
+ operation_id="workflow-deleted:12:ctx",
+ )
+ database.delete_workflow.assert_awaited_once_with("12")
+
+
+async def test_rest_delete_uses_shared_context_lifecycle_path(
+ monkeypatch,
+):
+ from routers import database as database_router
+
+ shared_delete = AsyncMock(
+ return_value={
+ "success": True,
+ "workflow_id": "12",
+ "contexts_archived": 1,
+ "context_archives_pending": 0,
+ }
+ )
+ monkeypatch.setattr(
+ database_router,
+ "delete_workflow_with_context_archival",
+ shared_delete,
+ )
+ database = object()
+
+ result = await database_router.delete_workflow(
+ "12",
+ database=database,
+ )
+
+ assert result["success"] is True
+ shared_delete.assert_awaited_once_with(database, "12")
diff --git a/server/tests/test_deployment_canary_listener.py b/server/tests/test_deployment_canary_listener.py
index 2b9161d0..f9390b12 100644
--- a/server/tests/test_deployment_canary_listener.py
+++ b/server/tests/test_deployment_canary_listener.py
@@ -65,7 +65,13 @@ def fresh_canary_registry(monkeypatch):
# ---------------------------------------------------------------------------
-def _build_manager_with_state(workflow_id: str, nodes, edges, session_id="sess"):
+def _build_manager_with_state(
+ workflow_id: str,
+ nodes,
+ edges,
+ session_id="sess",
+ user_id="owner",
+):
"""Build a DeploymentManager populated with one in-flight deployment.
The canary methods read ``self._deployments[workflow_id]`` for nodes/edges
@@ -96,6 +102,7 @@ def _build_manager_with_state(workflow_id: str, nodes, edges, session_id="sess")
nodes=nodes,
edges=edges,
session_id=session_id,
+ user_id=user_id,
)
return mgr, broadcaster
@@ -233,11 +240,13 @@ class TestStartCanaryListener:
@pytest.mark.asyncio
async def test_starts_with_deterministic_id_and_use_existing_policy(self, monkeypatch):
from temporalio.common import WorkflowIDConflictPolicy
+ from types import SimpleNamespace
mgr, _ = _build_manager_with_state(
"wf-abc",
nodes=[_node("wh-1", "webhookTrigger"), _node("agent-1", "aiAgent")],
edges=[{"source": "wh-1", "target": "agent-1", "targetHandle": "input-main"}],
+ user_id="account-21",
)
recorded_start: List[Dict[str, Any]] = []
@@ -253,8 +262,18 @@ class TestStartCanaryListener:
wrapper.client = client
from core import container as container_mod
+ import core.config
monkeypatch.setattr(container_mod.container, "temporal_client", lambda: wrapper)
+ monkeypatch.setattr(
+ core.config,
+ "Settings",
+ lambda: SimpleNamespace(
+ temporal_agent_workflow_enabled=True,
+ temporal_per_type_dispatch=False,
+ temporal_worker_pool_enabled=False,
+ ),
+ )
monkeypatch.setattr(
container_mod.container,
"database",
@@ -277,6 +296,13 @@ class TestStartCanaryListener:
assert call["id"] == "wf-abc-webhookTrigger"
assert call["id_conflict_policy"] == WorkflowIDConflictPolicy.USE_EXISTING
assert call["task_queue"] == "machina-tasks"
+ assert call["args"][0]["user_id"] == "account-21"
+ assert call["args"][0]["_temporal_routing_v1"] == {
+ "version": 1,
+ "agent_workflow_enabled": True,
+ "per_type_dispatch_enabled": False,
+ "worker_pool_enabled": False,
+ }
@pytest.mark.asyncio
async def test_controlled_listener_is_registered_under_controller(self, monkeypatch):
diff --git a/server/tests/test_node_spec.py b/server/tests/test_node_spec.py
index d358146b..97ee5ec5 100644
--- a/server/tests/test_node_spec.py
+++ b/server/tests/test_node_spec.py
@@ -795,6 +795,9 @@ class TestUIHintsInNodeSpec:
spec = get_node_spec("masterSkill")
assert spec is not None
assert spec["displayName"] == "Master Skill"
+ assert {
+ handle.get("name") for handle in spec.get("handles", [])
+ } == {"output-tool"}
hints = spec.get("uiHints", {})
assert hints.get("isMasterSkillEditor") is True
assert hints.get("hideRunButton") is True
@@ -922,7 +925,11 @@ class TestNodeSpecContractInvariants:
"hasCodeEditor",
"isMasterSkillEditor",
"isMemoryPanel",
+ "isMemoryToolPanel",
+ "isContextPanel",
"isToolPanel",
+ "requiresContext",
+ "systemManaged",
# writeTodos: render the editable Current Todos manager in the
# middle section (in addition to isToolPanel).
"isTodoEditor",
@@ -1028,9 +1035,9 @@ class TestPluginContractInvariants:
assert h.get("kind") in {"input", "output"}, f"{t}.{h.get('name')}: kind={h.get('kind')!r} not input/output"
assert h.get("position") in self.VALID_POSITIONS, f"{t}.{h.get('name')}: position={h.get('position')!r} invalid"
- def test_agent_kind_has_skill_tool_memory_handles(self):
+ def test_agent_kind_has_skill_tool_context_handles(self):
# Contract: anything registered as an agent must accept skill,
- # tools, memory, and task inputs (the core n8n AIAgent handle
+ # tools, Context, and task inputs (the core AIAgent handle
# set). Keeps the AIAgentNode renderer honest.
for t in self._plugin_types():
spec = get_node_spec(t)
@@ -1042,7 +1049,7 @@ class TestPluginContractInvariants:
if "social" in (spec.get("group") or []):
continue
names = {h.get("name") for h in spec.get("handles") or []}
- for required in ("input-skill", "input-tools", "input-memory", "input-task"):
+ for required in ("input-skill", "input-tools", "input-context", "input-task"):
assert required in names, f"{t}: componentKind=agent missing handle {required!r}; got {sorted(names)}"
def test_trigger_kind_emits_output(self):
diff --git a/server/tests/test_plugin_contract.py b/server/tests/test_plugin_contract.py
index 62d5a08a..43bc763e 100644
--- a/server/tests/test_plugin_contract.py
+++ b/server/tests/test_plugin_contract.py
@@ -187,7 +187,12 @@ class TestToolSchemaGeneration:
# itself — the declaration points at a definition that no
# longer exists (nested BaseModel / Enum Params fields).
assert '"$ref"' not in json.dumps(params), f"{cls.__qualname__} tool schema contains a dangling $ref — " "nested refs must be inlined, not stripped"
- assert schema.get("name") == cls.type
+ expected_name = (
+ cls.tool_name
+ if getattr(cls, "tool_schema_locked", False)
+ else cls.type
+ )
+ assert schema.get("name") == expected_name
class TestToolSchemaFastPath:
@@ -209,6 +214,25 @@ class TestToolSchemaFastPath:
assert resolved is cls, f"{cls.__qualname__}: fast-path lookup by type '{cls.type}' " f"returned {resolved}, expected {cls}"
assert hasattr(resolved, "Params"), f"{cls.__qualname__} has no Params — fast-path would miss it"
+ def test_every_invokable_tool_emits_canonical_connection_handle(self):
+ from services.node_spec import get_node_spec
+
+ for cls in _all_plugin_classes():
+ is_invokable_tool = getattr(cls, "usable_as_tool", False) or (
+ getattr(cls, "component_kind", "") == "tool"
+ and getattr(cls, "ui_hints", {}).get("isMasterSkillEditor")
+ is not True
+ )
+ if not is_invokable_tool:
+ continue
+ handles = get_node_spec(cls.type).get("handles") or []
+ assert any(
+ handle.get("name") == "output-tool"
+ and handle.get("kind") == "output"
+ and handle.get("role") == "tools"
+ for handle in handles
+ ), f"{cls.type}: invokable tool is missing canonical output-tool"
+
class TestInterpretResultContract:
"""``BaseNode.interpret_result`` + ``ToolNode.interpret_result`` are
diff --git a/server/tests/test_polling_trigger_workflow.py b/server/tests/test_polling_trigger_workflow.py
index a48df1e1..2240c2ae 100644
--- a/server/tests/test_polling_trigger_workflow.py
+++ b/server/tests/test_polling_trigger_workflow.py
@@ -422,6 +422,13 @@ class TestPollingTriggerWorkflowBody:
"edges": [{"source": "gm-1", "target": "agent-1", "targetHandle": "input-main"}],
"session_id": "sess",
"seen_ids": [],
+ "_temporal_routing_v1": {
+ "version": 1,
+ "agent_workflow_enabled": True,
+ "per_type_dispatch_enabled": True,
+ "worker_pool_enabled": False,
+ },
+ "user_id": "user-9",
}
with pytest.raises(_StopLoop):
@@ -438,6 +445,12 @@ class TestPollingTriggerWorkflowBody:
# when listener_data omits them (this test does).
spawn_ids = [c["id"] for c in spawn_calls]
assert spawn_ids == ["wf-1-gm-1-c", "wf-1-gm-1-d"]
+ for call in spawn_calls:
+ child_payload = call["args"][0]
+ assert child_payload["_temporal_routing_v1"] == (
+ listener_data["_temporal_routing_v1"]
+ )
+ assert child_payload["user_id"] == "user-9"
@pytest.mark.asyncio
async def test_cycle_failure_does_not_break_loop(self, monkeypatch):
@@ -452,6 +465,11 @@ class TestPollingTriggerWorkflowBody:
# count them against the poll-cycle script.
if name == "broadcast_trigger_status_activity":
return None
+ # Same for the spawn-path graph refresh: it is infrastructure,
+ # not a poll cycle. ``found: False`` keeps the listener's own
+ # nodes/edges in play so the spawn behaves as before.
+ if name == "load_persisted_workflow_graph_activity":
+ return {"found": False}
call_count[0] += 1
if call_count[0] == 2: # second call fails
raise RuntimeError("transient API timeout")
diff --git a/server/tests/test_trigger_listener_workflow.py b/server/tests/test_trigger_listener_workflow.py
index 5e4dd9dc..bdcd388d 100644
--- a/server/tests/test_trigger_listener_workflow.py
+++ b/server/tests/test_trigger_listener_workflow.py
@@ -332,6 +332,13 @@ class TestSpawnChildRun:
"nodes": [_node("wh-1", "webhookTrigger"), _node("agent-1", "aiAgent")],
"edges": [_edge("wh-1", "agent-1")],
"session_id": "sess-1",
+ "_temporal_routing_v1": {
+ "version": 1,
+ "agent_workflow_enabled": True,
+ "per_type_dispatch_enabled": True,
+ "worker_pool_enabled": False,
+ },
+ "user_id": "user-7",
}
await wf._spawn_child_run(event, listener_data)
@@ -350,6 +357,10 @@ class TestSpawnChildRun:
assert wh["_pre_executed"] is True
assert wh["_trigger_output"]["path"] == "/hook"
assert wh["_trigger_output"]["_event_envelope"]["id"] == "evt-42"
+ assert child_args["_temporal_routing_v1"] == (
+ listener_data["_temporal_routing_v1"]
+ )
+ assert child_args["user_id"] == "user-7"
@pytest.mark.asyncio
async def test_spawn_uses_latest_saved_graph_tools(self, monkeypatch):
diff --git a/server/tests/test_workflow_sanitizer_v2.py b/server/tests/test_workflow_sanitizer_v2.py
new file mode 100644
index 00000000..9e58ec6d
--- /dev/null
+++ b/server/tests/test_workflow_sanitizer_v2.py
@@ -0,0 +1,198 @@
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from services.workflow_sanitizer import (
+ sanitize_runtime_payload,
+ sanitize_workflow_graph,
+)
+
+
+def test_server_sanitizer_removes_legacy_and_context_runtime_payloads():
+ graph = {
+ "graphVersion": 2,
+ "nodes": [
+ {
+ "id": "ctx",
+ "type": "context",
+ "data": {
+ "label": "Context",
+ "systemManaged": True,
+ "agentNodeId": "agent",
+ "contextJournal": [{"role": "user", "content": "secret"}],
+ "providerBindings": {"session": "uuid"},
+ },
+ },
+ {
+ "id": "memory",
+ "type": "simpleMemory",
+ "data": {
+ "label": "Memory",
+ "parameters": {
+ "reset_policy": "preserve",
+ "memory_content": "secret transcript",
+ "last_session_id": "uuid",
+ },
+ },
+ },
+ ],
+ "edges": [],
+ }
+
+ cleaned = sanitize_workflow_graph(graph)
+ context_data = cleaned["nodes"][0]["data"]
+ assert context_data == {
+ "label": "Context",
+ "systemManaged": True,
+ "agentNodeId": "agent",
+ }
+ assert cleaned["nodes"][1]["data"] == {"label": "Memory"}
+
+
+def test_sanitizer_preserves_unrelated_provider_and_file_references():
+ value = {
+ "provider": "openai",
+ "payload_ref": {"kind": "file", "path": "uploads/report.pdf"},
+ "nested": {"thought_signature": "secret", "title": "safe"},
+ }
+ assert sanitize_runtime_payload(value) == {
+ "provider": "openai",
+ "payload_ref": {"kind": "file", "path": "uploads/report.pdf"},
+ "nested": {"title": "safe"},
+ }
+
+
+def test_workflow_graph_uses_ui_schema_allowlists():
+ graph = {
+ "graphVersion": 2,
+ "owner_id": "authenticated-user",
+ "contextJournal": [{"content": "root secret"}],
+ "nodes": [
+ {
+ "id": "agent",
+ "type": "aiAgent",
+ "position": {"x": 10, "y": 20, "runtime": "drop"},
+ "selected": True,
+ "data": {
+ "label": "Agent",
+ "disabled": False,
+ "condition": {"field": "ok", "operator": "equals", "value": True},
+ "parameters": {"api_key": "secret", "model": "gpt"},
+ "activeReplay": {"messages": ["secret"]},
+ "output": {"content": "secret"},
+ },
+ }
+ ],
+ "edges": [
+ {
+ "id": "edge",
+ "source": "agent",
+ "target": "agent",
+ "sourceHandle": "output-main",
+ "targetHandle": "input-main",
+ "selected": True,
+ "data": {
+ "label": "when ready",
+ "condition": {
+ "field": "status",
+ "operator": "equals",
+ "value": "ready",
+ },
+ "providerBindings": {"session": "secret"},
+ },
+ }
+ ],
+ "runtime_data": {"journal": "secret"},
+ }
+
+ cleaned = sanitize_workflow_graph(graph)
+
+ assert set(cleaned) == {"graphVersion", "owner_id", "nodes", "edges"}
+ assert cleaned["owner_id"] == "authenticated-user"
+ assert cleaned["nodes"] == [
+ {
+ "id": "agent",
+ "type": "aiAgent",
+ "position": {"x": 10, "y": 20},
+ "data": {
+ "label": "Agent",
+ "disabled": False,
+ "condition": {
+ "field": "ok",
+ "operator": "equals",
+ "value": True,
+ },
+ },
+ }
+ ]
+ assert cleaned["edges"] == [
+ {
+ "id": "edge",
+ "source": "agent",
+ "target": "agent",
+ "sourceHandle": "output-main",
+ "targetHandle": "input-main",
+ "data": {
+ "label": "when ready",
+ "condition": {
+ "field": "status",
+ "operator": "equals",
+ "value": "ready",
+ },
+ },
+ }
+ ]
+
+
+async def test_export_parameter_read_is_authorized_and_server_redacted(
+ monkeypatch,
+):
+ from routers import websocket as websocket_router
+
+ database = SimpleNamespace(
+ get_workflow=AsyncMock(
+ return_value=SimpleNamespace(
+ data={
+ "owner_id": "user-1",
+ "nodes": [{"id": "memory", "type": "simpleMemory"}],
+ "edges": [],
+ }
+ )
+ ),
+ get_node_parameters=AsyncMock(
+ return_value={
+ "reset_policy": "preserve",
+ "memory_content": "private transcript",
+ "provider_binding": {"session": "secret"},
+ }
+ ),
+ )
+ monkeypatch.setattr(
+ websocket_router.container,
+ "database",
+ lambda: database,
+ )
+ socket = SimpleNamespace(state=SimpleNamespace(user_id="user-1"))
+ result = await websocket_router.handle_get_all_node_parameters(
+ {
+ "workflow_id": "workflow-1",
+ "node_ids": ["memory"],
+ "purpose": "export",
+ },
+ socket,
+ )
+ assert result["parameters"]["memory"]["parameters"] == {
+ "reset_policy": "preserve"
+ }
+
+ denied = await websocket_router.handle_get_all_node_parameters(
+ {
+ "workflow_id": "workflow-1",
+ "node_ids": ["memory"],
+ "purpose": "export",
+ },
+ SimpleNamespace(state=SimpleNamespace(user_id="user-2")),
+ )
+ assert denied == {"success": False, "error": "Workflow access denied"}