From f9ccc62472c0ce9b997770c3dd4471bad44c9902 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 20 Apr 2026 14:35:21 +0000 Subject: [PATCH] sync temporal-developer skill v0.3.2 from source repo --- .../skills/temporal-developer/SKILL.md | 29 +- .../references/core/ai-patterns.md | 12 +- .../references/core/determinism.md | 14 +- .../references/core/dev-management.md | 1 - .../references/core/error-reference.md | 16 +- .../references/core/gotchas.md | 28 +- .../references/core/patterns.md | 44 +- .../references/core/troubleshooting.md | 14 +- .../references/core/versioning.md | 4 + .../references/dotnet/advanced-features.md | 203 +++++++ .../references/dotnet/data-handling.md | 217 ++++++++ .../dotnet/determinism-protection.md | 51 ++ .../references/dotnet/determinism.md | 56 ++ .../references/dotnet/dotnet.md | 202 +++++++ .../references/dotnet/error-handling.md | 157 ++++++ .../references/dotnet/gotchas.md | 262 +++++++++ .../references/dotnet/observability.md | 108 ++++ .../references/dotnet/patterns.md | 495 ++++++++++++++++++ .../references/dotnet/testing.md | 177 +++++++ .../references/dotnet/versioning.md | 307 +++++++++++ .../references/go/advanced-features.md | 2 + .../references/go/data-handling.md | 2 + .../references/go/determinism-protection.md | 5 + .../references/go/determinism.md | 4 +- .../temporal-developer/references/go/go.md | 12 + .../references/go/gotchas.md | 1 + .../references/go/observability.md | 48 +- .../references/go/patterns.md | 3 + .../references/go/versioning.md | 6 + .../references/java/advanced-features.md | 1 + .../references/java/determinism-protection.md | 4 +- .../references/java/determinism.md | 4 +- .../references/java/error-handling.md | 5 + .../references/java/gotchas.md | 2 + .../references/java/java.md | 16 +- .../references/java/observability.md | 1 + .../references/java/patterns.md | 2 + .../references/java/versioning.md | 1 + .../references/python/advanced-features.md | 3 +- .../references/python/data-handling.md | 2 + .../python/determinism-protection.md | 6 +- .../references/python/determinism.md | 5 +- .../references/python/error-handling.md | 5 +- .../references/python/gotchas.md | 2 + .../references/python/observability.md | 3 +- .../references/python/patterns.md | 3 + .../references/python/python.md | 10 +- .../references/python/sync-vs-async.md | 4 + .../references/python/testing.md | 1 - .../references/python/versioning.md | 29 +- .../typescript/advanced-features.md | 4 + .../references/typescript/data-handling.md | 1 + .../typescript/determinism-protection.md | 1 - .../references/typescript/determinism.md | 4 +- .../references/typescript/gotchas.md | 1 + .../references/typescript/patterns.md | 3 + .../references/typescript/typescript.md | 14 +- .../references/typescript/versioning.md | 3 + 58 files changed, 2544 insertions(+), 76 deletions(-) create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/advanced-features.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/data-handling.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/determinism-protection.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/determinism.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/dotnet.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/error-handling.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/gotchas.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/observability.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/patterns.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/testing.md create mode 100644 plugins/temporal-developer/skills/temporal-developer/references/dotnet/versioning.md diff --git a/plugins/temporal-developer/skills/temporal-developer/SKILL.md b/plugins/temporal-developer/skills/temporal-developer/SKILL.md index b0aae86..9f12f6c 100644 --- a/plugins/temporal-developer/skills/temporal-developer/SKILL.md +++ b/plugins/temporal-developer/skills/temporal-developer/SKILL.md @@ -1,14 +1,14 @@ --- name: temporal-developer -description: Develop, debug, and manage Temporal applications across Python, TypeScript, Go, and Java. Use when the user is building workflows, activities, or workers with a Temporal SDK, debugging issues like non-determinism errors, stuck workflows, or activity retries, using Temporal CLI, Temporal Server, or Temporal Cloud, or working with durable execution concepts like signals, queries, heartbeats, versioning, continue-as-new, child workflows, or saga patterns. -version: 0.2.0 +description: Develop, debug, and manage Temporal applications across Python, TypeScript, Go, Java and .NET. Use when the user is building workflows, activities, or workers with a Temporal SDK, debugging issues like non-determinism errors, stuck workflows, or activity retries, using Temporal CLI, Temporal Server, or Temporal Cloud, or working with durable execution concepts like signals, queries, heartbeats, versioning, continue-as-new, child workflows, or saga patterns. +version: 0.3.2 --- # Skill: temporal-developer ## Overview -Temporal is a durable execution platform that makes workflows survive failures automatically. This skill provides guidance for building Temporal applications in Python, TypeScript, Go, and Java. +Temporal is a durable execution platform that makes workflows survive failures automatically. This skill provides guidance for building Temporal applications in Python, TypeScript, Go, Java and .NET. ## Core Architecture @@ -77,34 +77,35 @@ Once you've downloaded the file, extract the downloaded archive and add the temp ### Read All Relevant References 1. First, read the getting started guide for the language you are working in: - - Python -> read `references/python/python.md` - - TypeScript -> read `references/typescript/typescript.md` - - Java -> read `references/java/java.md` - - Go -> read `references/go/go.md` + - Python -> read `references/python/python.md` + - TypeScript -> read `references/typescript/typescript.md` + - Go -> read `references/go/go.md` + - Java -> read `references/java/java.md` + - .NET (C#) -> read `references/dotnet/dotnet.md` 2. Second, read appropriate `core` and language-specific references for the task at hand. - ## Primary References + - **`references/core/determinism.md`** - Why determinism matters, replay mechanics, basic concepts of activities - + Language-specific info at `references/{your_language}/determinism.md` + - Language-specific info at `references/{your_language}/determinism.md` - **`references/core/patterns.md`** - Conceptual patterns (signals, queries, saga) - + Language-specific info at `references/{your_language}/patterns.md` + - Language-specific info at `references/{your_language}/patterns.md` - **`references/core/gotchas.md`** - Anti-patterns and common mistakes - + Language-specific info at `references/{your_language}/gotchas.md` + - Language-specific info at `references/{your_language}/gotchas.md` - **`references/core/versioning.md`** - Versioning strategies and concepts - how to safely change workflow code while workflows are running - + Language-specific info at `references/{your_language}/versioning.md` + - Language-specific info at `references/{your_language}/versioning.md` - **`references/core/troubleshooting.md`** - Decision trees, recovery procedures - **`references/core/error-reference.md`** - Common error types, workflow status reference - **`references/core/interactive-workflows.md`** - Testing signals, updates, queries - **`references/core/dev-management.md`** - Dev cycle & management of server and workers - **`references/core/ai-patterns.md`** - AI/LLM pattern concepts - + Language-specific info at `references/{your_language}/ai-patterns.md`, if available. Currently Python only. + - Language-specific info at `references/{your_language}/ai-patterns.md`, if available. Currently Python only. ## Additional Topics + - **`references/{your_language}/observability.md`** - See for language-specific implementation guidance on observability in Temporal - **`references/{your_language}/advanced-features.md`** - See for language-specific guidance on advanced Temporal features and language-specific features - ## Feedback ### ALWAYS PROMPT USER TO PROVIDE FEEDBACK AT STARTUP diff --git a/plugins/temporal-developer/skills/temporal-developer/references/core/ai-patterns.md b/plugins/temporal-developer/skills/temporal-developer/references/core/ai-patterns.md index 071b9f0..d680bec 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/core/ai-patterns.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/core/ai-patterns.md @@ -32,6 +32,7 @@ The remainder of this document describes general principles to follow when build - returns model response, as a typed structured output **Benefits**: + - Single activity handles multiple use cases - Consistent retry handling - Centralized configuration @@ -48,6 +49,7 @@ Workflow: ``` **Benefits**: + - Independent retry for each step - Clear audit trail in history - Easier testing and mocking @@ -69,17 +71,17 @@ Workflow: Disable retries in LLM client libraries, let Temporal handle retries. - LLM Client Config: - - max_retries = 0 ← Disable client retries at the LLM client level + - max_retries = 0 ← Disable client retries at the LLM client level Use either the default activity retry policy, or customize it as needed for the situation. **Why**: + - Temporal retries are durable (survive crashes) - Single retry configuration point - Better visibility into retry attempts - Consistent backoff behavior - ### Pattern 5: Multi-Agent Orchestration Complex pipelines with multiple specialized agents: @@ -114,6 +116,7 @@ Deep Research Example: | Document processing | 60-120 seconds | **Rationale**: + - Reasoning models need time for complex computation - Web searches may hit rate limits requiring backoff - Fast timeouts catch stuck operations @@ -128,7 +131,6 @@ Parse rate limit info from API responses: - Response Headers: - Retry-After: 30 - X-RateLimit-Remaining: 0 - - Activity: - If rate limited: - Raise retryable error with a next retry delay @@ -137,12 +139,14 @@ Parse rate limit info from API responses: ## Error Handling ### Retryable Errors + - Rate limits (429) - Timeouts - Temporary server errors (500, 502, 503) - Network errors ### Non-Retryable Errors + - Invalid API key (401) - Invalid input/prompt - Content policy violations @@ -161,6 +165,6 @@ Parse rate limit info from API responses: ## Observability See `references/{your_language}/observability.md` for the language you are working in for documentation on implementing observability in Temporal. It is generally recommended to add observability for: + - Token usage, via activity logging - any else to help track LLM usage and debug agentic flows, within moderation. - diff --git a/plugins/temporal-developer/skills/temporal-developer/references/core/determinism.md b/plugins/temporal-developer/skills/temporal-developer/references/core/determinism.md index 16f04db..004f879 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/core/determinism.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/core/determinism.md @@ -50,22 +50,27 @@ Result: Commands don't match history → NondeterminismError ## Sources of Non-Determinism ### Time-Based Operations + - `datetime.now()`, `time.time()`, `Date.now()` - Different value on each execution ### Random Values + - `random.random()`, `Math.random()`, `uuid.uuid4()` - Different value on each execution ### External State + - Reading files, environment variables, databases, networking / HTTP calls - State may change between executions ### Non-Deterministic Iteration + - Map/dict iteration order (in some languages) - Set iteration order ### Threading/Concurrency + - Race conditions produce different outcomes - Non-deterministic ordering @@ -76,18 +81,21 @@ In Temporal, activities are the primary mechanism for making non-deterministic c For a few simple cases, like timestamps, random values, UUIDs, etc. the Temporal SDK in your language may provide durable variants that are simple to use. See `references/{your_language}/determinism.md` for the language you are working in for more info. ## SDK Protection Mechanisms + Each Temporal SDK language provides a different level of protection against non-determinism: -- Python: The Python SDK runs workflows in a sandbox that intercepts and aborts non-deterministic calls early at runtime. +- Python: The Python SDK runs workflows in a sandbox that intercepts and aborts non-deterministic calls early at runtime. - TypeScript: The TypeScript SDK runs workflows in an isolated V8 sandbox, intercepting many common sources of non-determinism and replacing them automatically with deterministic variants. - Java: The Java SDK has no sandbox. Determinism is enforced by developer conventions — the SDK provides `Workflow.*` APIs as safe alternatives (e.g., `Workflow.sleep()` instead of `Thread.sleep()`), and non-determinism is only detected at replay time via `NonDeterministicException`. A static analysis tool (`temporal-workflowcheck`, beta) can catch violations at build time. Cooperative threading under a global lock eliminates the need for synchronization. - Go: The Go SDK has no runtime sandbox. Therefore, non-determinism bugs will never be immediately appararent, and are usually only observable during replay. The optional `workflowcheck` static analysis tool can be used to check for many sources of non-determinism at compile time. +- .NET: The .NET SDK has no sandbox. It uses a custom TaskScheduler and a runtime EventListener to detect invalid task scheduling. Developers must use `Workflow.*` safe alternatives (e.g., Workflow.DelayAsync instead of Task.Delay) and avoid non-deterministic .NET Task APIs. Regardless of which SDK you are using, it is your responsibility to ensure that workflow code does not contain sources of non-determinism. Use SDK-specific tools as well as replay tests for doing so. ## Detecting Non-Determinism ### During Execution + - `NondeterminismError` raised when Commands don't match Events - Workflow becomes blocked until code is fixed @@ -98,13 +106,17 @@ Replay tests verify that workflows follow identical code paths when re-run, by a ## Recovery from Non-Determinism ### Accidental Change + If you accidentally introduced non-determinism: + 1. Revert code to match what's in history 2. Restart worker 3. Workflow auto-recovers ### Intentional Change + If you need to change workflow logic: + 1. Use the **Patching API** to support both old and new code paths 2. Or terminate old workflows and start new ones with updated code diff --git a/plugins/temporal-developer/skills/temporal-developer/references/core/dev-management.md b/plugins/temporal-developer/skills/temporal-developer/references/core/dev-management.md index 01faed0..45385d3 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/core/dev-management.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/core/dev-management.md @@ -20,7 +20,6 @@ When you need a new worker, you should start it in the background (and preferrab **Best practice**: As far as local development goes, run only ONE worker instance with the latest code. Don't keep stale workers (running old code) around. - ### Cleanup **Always kill workers when done.** Don't leave workers running. diff --git a/plugins/temporal-developer/skills/temporal-developer/references/core/error-reference.md b/plugins/temporal-developer/skills/temporal-developer/references/core/error-reference.md index 74570ae..29a40b7 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/core/error-reference.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/core/error-reference.md @@ -6,14 +6,14 @@ | **Deadlock** | TMPRL1101 | `WorkflowTaskFailed` in history, worker logs | Workflow blocked too long (deadlock detected) | Remove blocking operations from workflow code (no I/O, no sleep, no threading locks). Use Temporal primitives instead. | https://github.com/temporalio/rules/blob/main/rules/TMPRL1101.md | | **Unfinished handlers** | TMPRL1102 | `WorkflowTaskFailed` in history | Workflow completed while update/signal handlers still running | Ensure all handlers complete before workflow finishes. Use `workflow.wait_condition()` to wait for handler completion. | https://github.com/temporalio/rules/blob/main/rules/TMPRL1102.md | | **Payload overflow** | TMPRL1103 | `WorkflowTaskFailed` or `ActivityTaskFailed` in history | Payload size limit exceeded (default 2MB) | Reduce payload size. Use external storage (S3, database) for large data and pass references instead. | https://github.com/temporalio/rules/blob/main/rules/TMPRL1103.md | -| **Workflow code bug** | | `WorkflowTaskFailed` in history | Bug in workflow logic | Fix code → Restart worker → Workflow auto-resumes | | -| **Missing workflow** | | Worker logs | Workflow not registered | Add to worker.py → Restart worker | | -| **Missing activity** | | Worker logs | Activity not registered | Add to worker.py → Restart worker | | -| **Activity bug** | | `ActivityTaskFailed` in history | Bug in activity code | Fix code → Restart worker → Auto-retries | | -| **Activity retries** | | `ActivityTaskFailed` (count >2) | Repeated failures | Fix code → Restart worker → Auto-retries | | -| **Sandbox violation** | | Worker logs | Bad imports in workflow | Fix workflow.py imports → Restart worker | | -| **Task queue mismatch** | | Workflow never starts | Different queues in starter/worker | Align task queue names | | -| **Timeout** | | Status = TIMED_OUT | Operation too slow | Increase timeout config | | +| **Workflow code bug** | | `WorkflowTaskFailed` in history | Bug in workflow logic | Fix code → Restart worker → Workflow auto-resumes | | +| **Missing workflow** | | Worker logs | Workflow not registered | Add to worker.py → Restart worker | | +| **Missing activity** | | Worker logs | Activity not registered | Add to worker.py → Restart worker | | +| **Activity bug** | | `ActivityTaskFailed` in history | Bug in activity code | Fix code → Restart worker → Auto-retries | | +| **Activity retries** | | `ActivityTaskFailed` (count >2) | Repeated failures | Fix code → Restart worker → Auto-retries | | +| **Sandbox violation** | | Worker logs | Bad imports in workflow | Fix workflow.py imports → Restart worker | | +| **Task queue mismatch** | | Workflow never starts | Different queues in starter/worker | Align task queue names | | +| **Timeout** | | Status = TIMED_OUT | Operation too slow | Increase timeout config | | ## Workflow Status Reference diff --git a/plugins/temporal-developer/skills/temporal-developer/references/core/gotchas.md b/plugins/temporal-developer/skills/temporal-developer/references/core/gotchas.md index 55b6ddb..677362f 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/core/gotchas.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/core/gotchas.md @@ -9,6 +9,7 @@ This document provides a general overview of conceptual-level gotchas in Tempora **The Problem**: Activities may execute more than once due to retries or Worker failures. If an activity calls an external service without an idempotency key, you may charge a customer twice, send duplicate emails, or create duplicate records. **Symptoms**: + - Duplicate side effects (double charges, duplicate notifications) - Data inconsistencies after retries @@ -21,6 +22,7 @@ This document provides a general overview of conceptual-level gotchas in Tempora **The Problem**: Code in workflow functions runs on first execution AND on every replay. Any side effect (logging, notifications, metrics, etc.) will happen multiple times and non-deterministic code (IO, current time, random numbers, threading, etc.) won't replay correctly. **Symptoms**: + - Non-determinism errors - Sandbox violations, depending on SDK language - Duplicate log entries @@ -28,11 +30,12 @@ This document provides a general overview of conceptual-level gotchas in Tempora - Inflated metrics **The Fix**: + - Use Temporal replay-aware managed side effects for common, non-business logic cases: - - Temporal workflow logging - - Temporal date time - - Temporal UUID generation - - Temporal random number generation + - Temporal workflow logging + - Temporal date time + - Temporal UUID generation + - Temporal random number generation - Put all other side effects in Activities See `references/core/determinism.md` for more info. @@ -42,10 +45,12 @@ See `references/core/determinism.md` for more info. **The Problem**: If Worker A runs part of a workflow with code v1, then Worker B (with code v2) picks it up, replay may produce different Commands. **Symptoms**: + - Non-determinism errors after deploying new code - Errors mentioning "command mismatch" or "unexpected command" **The Fix**: + - Use Worker Versioning for production deployments - Use patching APIs - During development: kill old workers before starting new ones @@ -60,6 +65,7 @@ See `references/core/versioning.md` for more info. **The Problem**: Using aggressive activity retry policies that give up too easily. **Symptoms**: + - Workflows failing on transient errors - Unnecessary workflow failures during brief outages @@ -72,6 +78,7 @@ See `references/core/versioning.md` for more info. **The Problem**: Queries and update validators are read-only. Modifying state causes non-determinism on replay, and must strictly be avoided. **Symptoms**: + - State inconsistencies after workflow replay - Non-determinism errors @@ -82,6 +89,7 @@ See `references/core/versioning.md` for more info. **The Problem**: Queries and update validators must return immediately. They cannot await activities, child workflows, timers, or conditions. **Symptoms**: + - Query / update validators timeouts - Deadlocks @@ -110,6 +118,7 @@ See language-specific gotchas for details. **The Problem**: Not testing what happens when things go wrong. **Questions to answer**: + - What happens when an Activity exhausts all retries? - What happens when a workflow is cancelled mid-execution? - What happens during a Worker restart? @@ -121,6 +130,7 @@ See language-specific gotchas for details. **The Problem**: Changing workflow code without verifying existing workflows can still replay. **Symptoms**: + - Non-determinism errors after deployment - Stuck workflows that can't make progress @@ -133,6 +143,7 @@ See language-specific gotchas for details. **The Problem**: Catching errors without proper handling hides failures. **Symptoms**: + - Silent failures - Workflows completing "successfully" despite errors - Difficult debugging @@ -144,10 +155,12 @@ See language-specific gotchas for details. **The Problem**: Marking transient errors as non-retryable, or permanent errors as retryable. **Symptoms**: + - Workflows failing on temporary network issues (if marked non-retryable) - Infinite retries on invalid input (if marked retryable) **The Fix**: + - **Retryable**: Network errors, timeouts, rate limits, temporary unavailability - **Non-retryable**: Invalid input, authentication failures, business rule violations, resource not found @@ -158,6 +171,7 @@ See language-specific gotchas for details. **The Problem**: When a workflow is cancelled, cleanup code after the cancellation point doesn't run unless explicitly protected. **Symptoms**: + - Resources not released after cancellation - Incomplete compensation/rollback - Leaked state @@ -169,10 +183,12 @@ See language-specific gotchas for details. **The Problem**: Activities must opt in to receive cancellation. Without proper handling, a cancelled activity continues running to completion, wasting resources. **Requirements for activity cancellation**: + 1. **Heartbeating** - Cancellation is delivered via heartbeat. Activities that don't heartbeat won't know they've been cancelled. 2. **Checking for cancellation** - Activity must explicitly check for cancellation or await a cancellation signal. **Symptoms**: + - Cancelled activities running to completion - Wasted compute on work that will be discarded - Delayed workflow cancellation @@ -184,11 +200,13 @@ See language-specific gotchas for details. **The Problem**: Temporal has built-in limits on payload sizes. Exceeding them causes workflows to fail. **Limits**: + - Max 2MB per individual payload - Max 4MB per gRPC message -- Max 50MB for complete workflow history (aim for <10MB in practice) +- Max 50MB for complete workflow history (aim for < 10MB in practice) **Symptoms**: + - Payload too large errors - gRPC message size exceeded errors - Workflow history growing unboundedly diff --git a/plugins/temporal-developer/skills/temporal-developer/references/core/patterns.md b/plugins/temporal-developer/skills/temporal-developer/references/core/patterns.md index 2ab5b72..7e7c7a3 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/core/patterns.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/core/patterns.md @@ -2,8 +2,9 @@ ## Overview -Common patterns for building robust Temporal workflows. +Common patterns for building robust Temporal workflows. See the language-specific references for the language you are working in: + - `references/{language}/{language}.md` for the root level documentation for that language - `references/{language}/patterns.md` for language-specific example code of the patterns in this file. @@ -12,18 +13,21 @@ See the language-specific references for the language you are working in: **Purpose**: Send data to a running workflow asynchronously (fire-and-forget). **When to Use**: + - Human approval workflows - Adding items to a workflow's queue - Notifying workflow of external events - Live configuration updates **Characteristics**: + - Asynchronous - sender doesn't wait for response - Can mutate workflow state - Durable - signals are persisted in history - Can be sent before workflow starts (signal-with-start) **Example Flow**: + ``` Client Workflow │ │ @@ -41,12 +45,14 @@ you want the external process to Heartbeat or receive Cancellation. If this may **Purpose**: Read workflow state synchronously without modifying it. **When to Use**: + - Building dashboards showing workflow progress - Health checks and monitoring - Debugging workflow state - Exposing current status to external systems **Characteristics**: + - Synchronous - caller waits for response - Read-only - must not modify state - Not recorded in history @@ -54,6 +60,7 @@ you want the external process to Heartbeat or receive Cancellation. If this may - Can run even on completed workflows **Example Flow**: + ``` Client Workflow │ │ @@ -67,12 +74,14 @@ Client Workflow **Purpose**: Modify workflow state and receive a response synchronously. **When to Use**: + - Operations that need confirmation (add item, return count) - Validation before accepting changes - Replace signal+query combinations - Request-response patterns within workflow **Characteristics**: + - Synchronous - caller waits for completion - Can mutate state AND return values - Supports validators to reject invalid updates before they even get persisted into history @@ -80,6 +89,7 @@ Client Workflow - Recorded in history **Example Flow**: + ``` Client Workflow │ │ @@ -91,34 +101,39 @@ Client Workflow ## Child Workflows **When to Use**: + - Prevent history from growing too large - Isolate failure domains (child can fail without failing parent) - Different retry policies for different parts **Characteristics**: + - Own history (doesn't bloat parent) - Independent lifecycle options (ParentClosePolicy) - Can be cancelled independently - Results returned to parent **Parent Close Policies**: + - `TERMINATE` - Child terminated when parent closes (default) - `ABANDON` - Child continues running independently - `REQUEST_CANCEL` - Cancellation requested but not forced -**Note:** Do not need to use child workflows simply for breaking complex logic down into smaller pieces. Standard programming abstractions within a workflow can already be used for that. +**Note:** Do not need to use child workflows simply for breaking complex logic down into smaller pieces. Standard programming abstractions within a workflow can already be used for that. ## Continue-as-New **Purpose**: Prevent unbounded history growth by "restarting" with fresh history. **When to Use**: + - Long-running workflows (entity workflows, subscriptions) - Workflows with many iterations - When history approaches 10,000+ events - Periodic cleanup of accumulated state **How It Works**: + ``` Workflow (history: 10,000 events) │ @@ -136,12 +151,14 @@ New Workflow Execution (history: 0 events) **Purpose**: Distributed transactions with compensation for failures. **When to Use**: + - Multi-step operations that span services - Operations requiring rollback on failure - Financial transactions, order processing - Booking systems with multiple reservations **How It Works**: + ``` Step 1: Reserve inventory └─ Compensation: Release inventory @@ -158,6 +175,7 @@ On failure at step 3: ``` **Implementation Pattern**: + 1. Track compensation actions as you complete each step 2. On failure, execute compensations in reverse order 3. Handle compensation failures gracefully (log, alert, manual intervention) @@ -167,12 +185,14 @@ On failure at step 3: **Purpose**: Run multiple independent operations concurrently. **When to Use**: + - Processing multiple items that don't depend on each other - Calling multiple APIs simultaneously - Fan-out/fan-in patterns - Reducing total workflow duration **Patterns**: + - `Promise` / `asyncio` - Use traditional concurrency helpers (e.g. wait for all, wait for first, etc) - Partial failure handling - Continue with successful results @@ -181,12 +201,14 @@ On failure at step 3: **Purpose**: Model long-lived entities as workflows that handle events. **When to Use**: + - Subscription management - User sessions - Shopping carts - Any stateful entity receiving events over time **How It Works**: + ``` Entity Workflow (user-123) │ @@ -207,12 +229,14 @@ Entity Workflow (user-123) **Purpose**: Durable delays that survive worker restarts. **Use Cases**: + - Scheduled reminders - Timeout handling - Delayed actions - Polling with intervals **Characteristics**: + - Timers are durable (persisted in history) - Can be cancelled @@ -256,12 +280,13 @@ To ensure that polling_activity is restarted in a timely manner, we make sure th Define an Activity which fails (raises an exception) exactly when polling is not completed. The polling loop is accomplished via activity retries, by setting the following Retry options: + - backoff_coefficient: to 1 - initial_interval: to the polling interval (e.g. 60 seconds) This will enable the Activity to be retried exactly on the set interval. -**Advantage:** Individual Activity retries are not recorded in Workflow History, so this approach can poll for a very long time without affecting the history size. +**Advantage:** Individual Activity retries are not recorded in Workflow History, so this approach can poll for a very long time without affecting the history size. ## Idempotency Patterns @@ -285,6 +310,7 @@ Activity: charge_payment(order_id, amount) ``` **Good idempotency key sources**: + - Workflow ID (unique per workflow execution) - Business identifier (order ID, transaction ID) - Workflow ID + activity name + attempt number @@ -337,13 +363,15 @@ This ensures that on replay, already-completed steps are skipped. **Purpose**: Handle data that exceeds Temporal's payload limits without polluting workflow history. **Limits** (see `references/core/gotchas.md` for details): + - Max 2MB per individual payload - Max 4MB per gRPC message -- Max 50MB for workflow history (aim for <10MB) +- Max 50MB for workflow history (aim for < 10MB) **Key Principle**: Large data should never flow through workflow history. Activities read and write large data directly, passing only small references through the workflow. **Wrong Approach**: + ``` Workflow │ @@ -357,6 +385,7 @@ Workflow This defeats the purpose—large data enters workflow history multiple times. **Correct Approach**: + ``` Workflow │ @@ -369,6 +398,7 @@ Workflow The workflow only handles references (small strings). The activity does all large data operations internally. **Implementation Pattern**: + 1. Accept a reference (URL, S3 key, database ID) as activity input 2. Download/fetch the large data inside the activity 3. Process the data inside the activity @@ -376,6 +406,7 @@ The workflow only handles references (small strings). The activity does all larg 5. Return only a reference to the result **Other Strategies**: + - **Compression**: Use a PayloadCodec to compress data automatically - **Chunking**: Split large collections across multiple activities, each handling a subset @@ -384,11 +415,13 @@ The workflow only handles references (small strings). The activity does all larg **Purpose**: Enable cancellation delivery and progress tracking for long-running activities. **Why Heartbeat**: + 1. **Support activity cancellation** - Cancellations are delivered to activities via heartbeat. Activities that don't heartbeat won't know they've been cancelled. 2. **Resume progress after failure** - Heartbeat details persist across retries, allowing activities to resume where they left off. 3. **Detect stuck activities** - If an activity stops heartbeating, Temporal can time it out and retry. **How Cancellation Works**: + ``` Workflow requests activity cancellation │ @@ -411,17 +444,20 @@ Activity calls heartbeat() **Purpose**: Reduce latency for short, lightweight operations by skipping the task queue. ONLY use these when necessary for performance. Do NOT use these by default, as they are not durable and distributed. **When to Use**: + - Short operations completing in milliseconds/seconds - High-frequency calls where task queue overhead is significant - Low-latency requirements where you can't afford task queue round-trip **Characteristics**: + - Executes on the same worker that runs the workflow - No task queue round-trip (lower latency) - Still recorded in history - Should complete quickly (default timeout is short) **Trade-offs**: + - Less visibility in Temporal UI (no separate task) - Must complete on the same worker - Not suitable for long-running operations diff --git a/plugins/temporal-developer/skills/temporal-developer/references/core/troubleshooting.md b/plugins/temporal-developer/skills/temporal-developer/references/core/troubleshooting.md index 952d4e2..1df80f9 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/core/troubleshooting.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/core/troubleshooting.md @@ -59,19 +59,15 @@ Workflow stuck in RUNNING? 1. **No worker running** - See references/core/dev-management.md - 2. **Worker on wrong task queue** - Check: Worker logs for task queue name - Fix: Start worker with matching task queue - 3. **Worker has stale code** - Check: Worker startup time vs code changes - Fix: Restart worker with updated code - 4. **Workflow waiting for signal** - Check: Workflow history for pending signals - Fix: Send expected signal or check signal sender - 5. **Activity stuck/timing out** - Check: Activity retry attempts in history - Fix: Investigate activity failure, increase timeout @@ -107,6 +103,7 @@ NondeterminismError? ### Common Causes 1. **Changed call order** + ``` # Before # After (BREAKS) await activity_a await activity_b @@ -114,28 +111,33 @@ NondeterminismError? ``` 2. **Changed call name** + ``` # Before # After (BREAKS) await process_order(...) await handle_order(...) ``` 3. **Added/removed call** + - Adding new activity mid-workflow - Removing activity that was previously called 4. **Using non-deterministic code** + - `datetime.now()` in workflow (use `workflow.now()`) - `random.random()` in workflow (use `workflow.random()`) ### Recovery **Accidental Change:** + 1. Identify the change 2. Revert code to match history 3. Restart worker 4. Workflow automatically recovers **Intentional Change:** + 1. Use patching API for gradual migration 2. Or terminate old workflows, start new ones @@ -163,11 +165,9 @@ Workflow status = FAILED? 1. **Unhandled exception in workflow** - Check error message and stack trace - Fix bug in workflow code - 2. **Activity exhausted retries** - All retry attempts failed - Check activity logs for root cause - 3. **Non-retryable error thrown** - Error marked as non-retryable - Intentional failure, check business logic @@ -236,11 +236,9 @@ Activity retrying repeatedly? 1. **Bug in activity code** - Fix the bug - Consider marking certain errors as non-retryable - 2. **External service down** - Retries are working as intended - Monitor service recovery - 3. **Invalid input** - Validate inputs before activity - Return non-retryable error for bad input diff --git a/plugins/temporal-developer/skills/temporal-developer/references/core/versioning.md b/plugins/temporal-developer/skills/temporal-developer/references/core/versioning.md index 226bb83..3081dcb 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/core/versioning.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/core/versioning.md @@ -40,14 +40,17 @@ else: ### Three-Phase Lifecycle **Phase 1: Patch In** + - Add both old and new code paths - New workflows take new path, old workflows take old path **Phase 2: Deprecate** + - After all old workflows complete, remove old code - Keep deprecation marker for history compatibility **Phase 3: Remove** + - After all deprecated workflows complete - Remove patch entirely, only new code remains @@ -116,6 +119,7 @@ Worker v2.0 (Build ID: def456) **Build ID**: Specific code version (e.g., git commit hash) **Versioning Behaviors**: + - `PINNED` - Workflows stay on original worker version - `AUTO_UPGRADE` - Workflows can move to newer versions diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/advanced-features.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/advanced-features.md new file mode 100644 index 0000000..fd0f81e --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/advanced-features.md @@ -0,0 +1,203 @@ +# .NET SDK Advanced Features + +## Schedules + +Create recurring workflow executions. + +```csharp +using Temporalio.Client.Schedules; + +var scheduleId = "daily-report"; +await client.CreateScheduleAsync( + scheduleId, + new Schedule( + Action: ScheduleActionStartWorkflow.Create( + (DailyReportWorkflow wf) => wf.RunAsync(), + new(id: "daily-report", taskQueue: "reports")), + Spec: new ScheduleSpec + { + Intervals = new List + { + new(Every: TimeSpan.FromDays(1)), + }, + })); + +// Manage schedules +var handle = client.GetScheduleHandle(scheduleId); +await handle.PauseAsync("Maintenance window"); +await handle.UnpauseAsync(); +await handle.TriggerAsync(); // Run immediately +await handle.DeleteAsync(); +``` + +## Async Activity Completion + +For activities that complete asynchronously (e.g., human tasks, external callbacks). +If you configure a `HeartbeatTimeout` on this activity, the external completer is responsible for sending heartbeats via the async handle. +If you do NOT set a `HeartbeatTimeout`, no heartbeats are required. + +**Note:** If the external system that completes the asynchronous action can reliably be trusted to do the task and Signal back with the result, and it doesn't need to Heartbeat or receive Cancellation, then consider using **signals** instead. + +```csharp +using Temporalio.Activities; +using Temporalio.Client; + +[Activity] +public async Task RequestApprovalAsync(string requestId) +{ + var taskToken = ActivityExecutionContext.Current.Info.TaskToken; + + // Store task token for later completion (e.g., in database) + await StoreTaskTokenAsync(requestId, taskToken); + + // Mark this activity as waiting for external completion + throw new CompleteAsyncException(); +} + +// Later, complete the activity from another process +public async Task CompleteApprovalAsync(string requestId, bool approved) +{ + var client = await TemporalClient.ConnectAsync(new("localhost:7233")); + // Retrieve the task token from external storage (e.g., database) + var taskToken = await GetTaskTokenAsync(requestId); + + var handle = client.GetAsyncActivityHandle(taskToken); + + // Optional: if a HeartbeatTimeout was set, you can periodically: + // await handle.HeartbeatAsync(progressDetails); + + if (approved) + await handle.CompleteAsync("approved"); + else + // You can also fail or report cancellation via the handle + await handle.FailAsync(new ApplicationFailureException("Rejected")); +} +``` + +## Worker Tuning + +Configure worker performance settings. + +```csharp +var worker = new TemporalWorker( + client, + new TemporalWorkerOptions("my-task-queue") + { + // Workflow task concurrency + MaxConcurrentWorkflowTasks = 100, + // Activity task concurrency + MaxConcurrentActivities = 100, + // Graceful shutdown timeout + GracefulShutdownTimeout = TimeSpan.FromSeconds(30), + } + .AddWorkflow() + .AddAllActivities(new MyActivities())); +``` + +## Workflow Init Attribute + +Use `[WorkflowInit]` on a constructor to run initialization code when a workflow is first created. + +**Purpose:** Execute some setup code before signal/update happens or run is invoked. + +```csharp +[Workflow] +public class MyWorkflow +{ + private readonly string _initialValue; + private readonly List _items = new(); + + [WorkflowInit] + public MyWorkflow(string initialValue) + { + _initialValue = initialValue; + } + + [WorkflowRun] + public async Task RunAsync(string initialValue) + { + // _initialValue and _items are already initialized + return _initialValue; + } +} +``` + +Constructor and `[WorkflowRun]` method must have the same parameters with the same types. You cannot make blocking calls (activities, sleeps, etc.) from the constructor. + +## Workflow Failure Exception Types + +Control which exceptions cause workflow failures vs workflow task retries. + +**Default behavior:** Only `ApplicationFailureException` fails a workflow. All other exceptions retry the workflow task forever (treated as bugs to fix with a code deployment). + +**Tip for testing:** Set `WorkflowFailureExceptionTypes` to include `Exception` so any unhandled exception fails the workflow immediately rather than retrying the workflow task forever. This surfaces bugs faster. + +### Worker-Level Configuration + +```csharp +var worker = new TemporalWorker( + client, + new TemporalWorkerOptions("my-task-queue") + { + // These exception types will fail the workflow execution (not just the task) + WorkflowFailureExceptionTypes = new[] { typeof(ArgumentException), typeof(InvalidOperationException) }, + } + .AddWorkflow() + .AddAllActivities(new MyActivities())); +``` + +## Dependency Injection + +The .NET SDK supports dependency injection via the `Temporalio.Extensions.Hosting` package, which integrates with .NET's generic host. + +### Worker as Generic Host + +```csharp +using Temporalio.Extensions.Hosting; + +public class Program +{ + public static async Task Main(string[] args) + { + var host = Host.CreateDefaultBuilder(args) + .ConfigureServices(ctx => + ctx. + AddScoped(). + AddHostedTemporalWorker( + clientTargetHost: "localhost:7233", + clientNamespace: "default", + taskQueue: "my-task-queue"). + AddScopedActivities(). + AddWorkflow()) + .Build(); + await host.RunAsync(); + } +} +``` + +### Activity Dependency Injection + +As shown in the host setup above, activities can be registered with `AddScopedActivities()`, `AddSingletonActivities()`, or `AddTransientActivities()`. Activities registered this way are created via DI, allowing constructor injection: + +```csharp +public class MyActivities +{ + private readonly ILogger _logger; + private readonly IOrderRepository _repository; + + public MyActivities(ILogger logger, IOrderRepository repository) + { + _logger = logger; + _repository = repository; + } + + [Activity] + public async Task GetOrderAsync(string orderId) + { + _logger.LogInformation("Fetching order {OrderId}", orderId); + return await _repository.GetAsync(orderId); + } +} +``` + +**Note:** Dependency injection is NOT available in workflows — workflows must be self-contained for determinism. diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/data-handling.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/data-handling.md new file mode 100644 index 0000000..8d0bb23 --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/data-handling.md @@ -0,0 +1,217 @@ +# .NET SDK Data Handling + +## Overview + +The .NET SDK uses data converters to serialize/deserialize workflow inputs, outputs, and activity parameters. + +## Default Data Converter + +The default converter handles: + +- `null` +- `byte[]` (as binary) +- `Google.Protobuf.IMessage` instances +- Anything that `System.Text.Json` supports +- `IRawValue` as unconverted raw payloads + +## Custom Data Converter + +Customize serialization by extending `DefaultPayloadConverter`. For example, to use camelCase property naming: + +```csharp +using System.Text.Json; +using Temporalio.Client; +using Temporalio.Converters; + +public class CamelCasePayloadConverter : DefaultPayloadConverter +{ + public CamelCasePayloadConverter() + : base(new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }) + { + } +} + +var client = await TemporalClient.ConnectAsync(new() +{ + TargetHost = "localhost:7233", + Namespace = "my-namespace", + DataConverter = DataConverter.Default with + { + PayloadConverter = new CamelCasePayloadConverter(), + }, +}); +``` + +## Protobuf Support + +The default data converter includes built-in support for Protocol Buffer messages via `Google.Protobuf.IMessage`. Protobuf messages are automatically serialized using proto3 JSON. + +```csharp +// Any Google.Protobuf.IMessage is automatically handled +[Workflow] +public class MyWorkflow +{ + [WorkflowRun] + public async Task RunAsync(MyProtoRequest request) + { + // Protobuf messages are serialized/deserialized automatically + return await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.ProcessAsync(request), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + } +} +``` + +## Payload Encryption + +Encrypt sensitive workflow data using a custom `IPayloadCodec`: + +```csharp +using Temporalio.Converters; +using Google.Protobuf; + +public class EncryptionCodec : IPayloadCodec +{ + public Task> EncodeAsync( + IReadOnlyCollection payloads) => + Task.FromResult>(payloads.Select(p => + new Payload + { + Metadata = { ["encoding"] = "binary/encrypted" }, + Data = ByteString.CopyFrom(Encrypt(p.ToByteArray())), + }).ToList()); + + public Task> DecodeAsync( + IReadOnlyCollection payloads) => + Task.FromResult>(payloads.Select(p => + { + if (p.Metadata.GetValueOrDefault("encoding") != "binary/encrypted") + return p; + return Payload.Parser.ParseFrom(Decrypt(p.Data.ToByteArray())); + }).ToList()); + + private byte[] Encrypt(byte[] data) => /* your encryption logic */; + private byte[] Decrypt(byte[] data) => /* your decryption logic */; +} + +// Apply encryption codec +var client = await TemporalClient.ConnectAsync(new("localhost:7233") +{ + DataConverter = DataConverter.Default with + { + PayloadCodec = new EncryptionCodec(), + }, +}); +``` + +## Search Attributes + +Custom searchable fields for workflow visibility. These can be set at workflow start: + +```csharp +using Temporalio.Common; + +var handle = await client.StartWorkflowAsync( + (OrderWorkflow wf) => wf.RunAsync(order), + new(id: $"order-{order.Id}", taskQueue: "orders") + { + TypedSearchAttributes = new SearchAttributeCollection.Builder() + .Set(SearchAttributeKey.CreateKeyword("OrderId"), order.Id) + .Set(SearchAttributeKey.CreateKeyword("OrderStatus"), "pending") + .Set(SearchAttributeKey.CreateFloat("OrderTotal"), order.Total) + .Build(), + }); +``` + +Or upserted during workflow execution: + +```csharp +[Workflow] +public class OrderWorkflow +{ + [WorkflowRun] + public async Task RunAsync(Order order) + { + // ... process order ... + + // Update search attribute + Workflow.UpsertTypedSearchAttributes( + SearchAttributeKey.CreateKeyword("OrderStatus").ValueSet("completed")); + return "done"; + } +} +``` + +### Querying Workflows by Search Attributes + +```csharp +await foreach (var wf in client.ListWorkflowsAsync( + "OrderStatus = \"processing\" OR OrderStatus = \"pending\"")) +{ + Console.WriteLine($"Workflow {wf.Id} is still processing"); +} +``` + +## Workflow Memo + +Store arbitrary metadata with workflows (not searchable). + +```csharp +await client.ExecuteWorkflowAsync( + (OrderWorkflow wf) => wf.RunAsync(order), + new(id: $"order-{order.Id}", taskQueue: "orders") + { + Memo = new Dictionary + { + ["customer_name"] = order.CustomerName, + ["notes"] = "Priority customer", + }, + }); +``` + +```csharp +// Read memo from workflow +[Workflow] +public class OrderWorkflow +{ + [WorkflowRun] + public async Task RunAsync(Order order) + { + var notes = Workflow.Memo["notes"]; + // ... + } +} +``` + +## Deterministic APIs for Values + +Use these APIs within workflows for deterministic random values and UUIDs: + +```csharp +[Workflow] +public class MyWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + // Deterministic GUID (same on replay) + var uniqueId = Workflow.NewGuid(); + + // Deterministic random (same on replay) + var value = Workflow.Random.Next(1, 100); + + // Deterministic current time + var now = Workflow.UtcNow; + + return uniqueId.ToString(); + } +} +``` + +## Best Practices + +1. Use records or classes with `System.Text.Json` support for input/output +2. Keep payloads small — see `references/core/gotchas.md` for limits +3. Encrypt sensitive data with `IPayloadCodec` +4. Use `Workflow.NewGuid()` and `Workflow.Random` for deterministic values +5. Use camelCase converter if interoperating with other SDKs diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/determinism-protection.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/determinism-protection.md new file mode 100644 index 0000000..8c7f331 --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/determinism-protection.md @@ -0,0 +1,51 @@ +# .NET Determinism Protection + +## Overview + +The .NET SDK has no runtime sandbox. Determinism is enforced by **developer convention** and **runtime task detection**. Unlike the Python and TypeScript SDKs, the .NET SDK will not intercept or replace non-deterministic calls at compile time or import time. The SDK does provide a runtime `EventListener` that detects some invalid task scheduling, but catching all non-deterministic code requires following the rules below and testing, in particular replay tests (see `references/dotnet/testing.md`). + +## Runtime Task Detection + +By default, the .NET SDK enables an `EventListener` that monitors task events. When workflow code accidentally starts a task on the wrong scheduler (e.g., via `Task.Run`), an `InvalidWorkflowOperationException` is thrown. This causes the workflow task to fail, which will continuously retry until the code is fixed. + +```csharp +// This will be detected at runtime and fail the workflow task +[Workflow] +public class BadWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + // BAD: Task.Run uses TaskScheduler.Default + await Task.Run(() => DoSomething()); + } +} +``` + +## .NET Task Determinism Rules + +Many .NET `Task` APIs implicitly use `TaskScheduler.Default`, which breaks determinism. Here are the key rules: + +**Do NOT use:** + +- `Task.Run` — uses default scheduler. Use `Workflow.RunTaskAsync`. +- `Task.ConfigureAwait(false)` — leaves current context. Use `ConfigureAwait(true)` or omit. +- `Task.Delay` / `Task.Wait` / timeout-based `CancellationTokenSource` — uses system timers. Use `Workflow.DelayAsync` / `Workflow.WaitConditionAsync`. +- `Task.WhenAny` — use `Workflow.WhenAnyAsync`. +- `Task.WhenAll` — use `Workflow.WhenAllAsync` (technically safe currently, but wrapper is recommended). +- `CancellationTokenSource.CancelAsync` — use `CancellationTokenSource.Cancel`. +- `System.Threading.Semaphore` / `SemaphoreSlim` / `Mutex` — use `Temporalio.Workflows.Semaphore` / `Mutex`. + +**Be wary of:** + +- Third-party libraries that implicitly use `TaskScheduler.Default` +- `Dataflow` blocks and similar concurrency libraries with hidden default scheduler usage + +## Best Practices + +1. **Always use `Workflow.*` alternatives** for Task operations in workflows +2. **Don't disable the `EventListener`** — it's on by default and catches mistakes at runtime +3. **Separate workflow and activity code** into different files/projects for clarity +4. **Use `SortedDictionary`** or sort collections before iterating — `Dictionary` iteration order is not guaranteed +5. **Test with replay** to catch non-determinism early +6. **Review third-party library usage** in workflow code for hidden default scheduler usage diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/determinism.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/determinism.md new file mode 100644 index 0000000..c1dbf56 --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/determinism.md @@ -0,0 +1,56 @@ +# .NET SDK Determinism + +## Overview + +The .NET SDK has NO runtime sandbox (unlike Python/TypeScript). Workflows must be deterministic for replay, and determinism is enforced by developer convention and runtime task detection via an `EventListener` (see `references/dotnet/determinism-protection.md`). + +## Why Determinism Matters: History Replay + +Temporal provides durable execution through **History Replay**. When a Worker restores workflow state, it re-executes workflow code from the beginning. This requires the code to be **deterministic**. See `references/core/determinism.md` for a deep explanation. + +## Forbidden Operations in Workflows + +The following are forbidden inside workflow code but are appropriate to use in activities. + +```csharp +// DO NOT do these in workflows: +await Task.Run(() => { }); // Uses default scheduler +await Task.Delay(TimeSpan.FromSeconds(1)); // System timer +var now = DateTime.UtcNow; // System clock +var r = new Random().Next(); // Non-deterministic +var id = Guid.NewGuid(); // Non-deterministic +File.ReadAllText("file.txt"); // I/O +await httpClient.GetAsync("..."); // Network I/O +``` + +Most non-determinism and side effects should be wrapped in Activities. + +## Safe Builtin Alternatives + +| Forbidden | Safe Alternative | +|-----------|------------------| +| `DateTime.Now` / `DateTime.UtcNow` | `Workflow.UtcNow` | +| `Random` | `Workflow.Random` | +| `Guid.NewGuid()` | `Workflow.NewGuid()` | +| `Task.Delay` | `Workflow.DelayAsync` | +| `Thread.Sleep` | `Workflow.DelayAsync` | +| `Task.Run` | `Workflow.RunTaskAsync` | +| `Task.WhenAll` | `Workflow.WhenAllAsync` | +| `Task.WhenAny` | `Workflow.WhenAnyAsync` | +| `System.Threading.Mutex` | `Temporalio.Workflows.Mutex` | +| `System.Threading.Semaphore` | `Temporalio.Workflows.Semaphore` | +| `CancellationTokenSource.CancelAsync` | `CancellationTokenSource.Cancel` | + +## Testing Replay Compatibility + +Use `WorkflowReplayer` to verify your code changes are compatible with existing histories. See the Workflow Replay Testing section of `references/dotnet/testing.md`. + +## Best Practices + +1. Always use `Workflow.*` APIs instead of standard .NET equivalents (see table above) +2. Never use `ConfigureAwait(false)` in workflows +3. Use `SortedDictionary` or sort before iterating collections +4. Move all I/O operations (network, filesystem, database) into activities +5. Use `Workflow.Logger` instead of `Console.WriteLine` for replay-safe logging +6. Keep workflow code focused on orchestration; delegate non-deterministic work to activities +7. Test with replay after making changes to workflow definitions diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/dotnet.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/dotnet.md new file mode 100644 index 0000000..437fcbb --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/dotnet.md @@ -0,0 +1,202 @@ +# Temporal .NET SDK Reference + +## Overview + +The Temporal .NET SDK provides a high-performance, type-safe approach to building durable workflows using C# and .NET. Workflows use attributes (`[Workflow]`, `[WorkflowRun]`) and lambda expressions for type-safe invocations. Supports .NET Framework 4.6.2+ and .NET Core 3.1+ (including .NET 5+). + +**CRITICAL**: The .NET SDK has **no sandbox**. Developers must be careful to avoid non-deterministic code in workflows. See the Determinism Rules section below and `references/dotnet/determinism.md`. + +## Understanding Replay + +Temporal workflows are durable through history replay. For details on how this works, see `references/core/determinism.md`. + +## Quick Start + +**Add Dependency:** Install the Temporal SDK NuGet package: + +```bash +dotnet add package Temporalio +``` + +**Activities.cs** - Activity definitions (separate file for clarity): + +```csharp +using Temporalio.Activities; + +public class MyActivities +{ + [Activity] + public string Greet(string name) + { + return $"Hello, {name}!"; + } +} +``` + +**GreetingWorkflow.workflow.cs** - Workflow definition: + +```csharp +using Temporalio.Workflows; + +[Workflow] +public class GreetingWorkflow +{ + [WorkflowRun] + public async Task RunAsync(string name) + { + return await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.Greet(name), + new() { StartToCloseTimeout = TimeSpan.FromSeconds(30) }); + } +} +``` + +**Worker (Program.cs)** - Worker setup: + +```csharp +using Temporalio.Client; +using Temporalio.Worker; + +var client = await TemporalClient.ConnectAsync(new("localhost:7233")); + +using var worker = new TemporalWorker( + client, + new TemporalWorkerOptions("my-task-queue") + .AddWorkflow() + .AddAllActivities(new MyActivities())); + +await worker.ExecuteAsync(); +``` + +**Start the dev server:** Start `temporal server start-dev` in the background. + +**Start the worker:** Run `dotnet run` in the worker project. + +**Starter (Program.cs)** - Start a workflow execution: + +```csharp +using Temporalio.Client; + +var client = await TemporalClient.ConnectAsync(new("localhost:7233")); + +var result = await client.ExecuteWorkflowAsync( + (GreetingWorkflow wf) => wf.RunAsync("my name"), + new(id: $"greeting-{Guid.NewGuid()}", taskQueue: "my-task-queue")); + +Console.WriteLine($"Result: {result}"); +``` + +**Run the workflow:** Run `dotnet run` in the starter project. Should output: `Result: Hello, my name!`. + +## Key Concepts + +### Workflow Definition + +- Use `[Workflow]` attribute on class +- Put any state initialization logic in the constructor of your workflow class to guarantee that it happens before signals/updates arrive. If your state initialization logic requires the workflow parameters, then add the `[WorkflowInit]` attribute and parameters to your constructor. +- Use `[WorkflowRun]` on the async entry point method +- Must return `Task` or `Task` +- Use `[WorkflowSignal]`, `[WorkflowQuery]`, `[WorkflowUpdate]` for handlers + +### Activity Definition + +- Use `[Activity]` attribute on methods +- Can be sync or async +- Instance methods support dependency injection +- Static methods are also supported + +### Worker Setup + +- Connect client, create `TemporalWorker` with workflows and activities +- Use `AddWorkflow()` and `AddAllActivities(instance)` or `AddActivity(method)` + +### Determinism + +**Workflow code must be deterministic!** The .NET SDK has no sandbox. See the Determinism Rules section below and `references/core/determinism.md` and `references/dotnet/determinism.md`. + +## File Organization Best Practice + +**Keep Workflow definitions in separate files from Activity definitions.** While not as critical as Python (no sandbox reloading), separation improves clarity and testability. Use the `.workflow.cs` extension for workflow files so the `.editorconfig` overrides (see below) apply only to workflow code. + +``` +MyTemporalApp/ +├── Workflows/ +│ └── GreetingWorkflow.workflow.cs # Only Workflow classes +├── Activities/ +│ └── TranslateActivities.cs # Only Activity classes +├── Models/ +│ └── OrderInput.cs # Shared data models +├── Worker/ +│ └── Program.cs # Worker setup +└── Starter/ + └── Program.cs # Client code to start workflows +``` + +## Workflow .editorconfig + +Workflow code violates some standard .NET analyzer rules. The recommended approach is to use the `.workflow.cs` file extension for workflow files and scope the overrides to that extension: + +```ini +# Configuration specific for Temporal workflows +[*.workflow.cs] + +# We use getters for queries, they cannot be properties +dotnet_diagnostic.CA1024.severity = none + +# Don't force workflows to have static methods +dotnet_diagnostic.CA1822.severity = none + +# Do not need ConfigureAwait for workflows +dotnet_diagnostic.CA2007.severity = none + +# Do not need task scheduler for workflows +dotnet_diagnostic.CA2008.severity = none + +# Workflow randomness is intentionally deterministic +dotnet_diagnostic.CA5394.severity = none + +# Allow async methods to not have await in them +dotnet_diagnostic.CS1998.severity = none + +# Don't force workflows to call async methods +dotnet_diagnostic.VSTHRD103.severity = none + +# Don't avoid, but rather encourage things using TaskScheduler.Current in workflows +dotnet_diagnostic.VSTHRD105.severity = none +``` + +## Determinism Rules + +The .NET SDK has **no sandbox** like Python or TypeScript. Developers must avoid non-deterministic operations manually. Many standard .NET `Task` APIs use `TaskScheduler.Default` implicitly, which breaks determinism. + +See `references/dotnet/determinism.md` for the full list of forbidden operations, safe alternatives, and best practices. See `references/dotnet/determinism-protection.md` for details on the runtime detection mechanism. + +## Common Pitfalls + +1. **Using `Task.Run` in workflows** — Uses default scheduler, breaks determinism. Use `Workflow.RunTaskAsync`. +2. **Using `Task.Delay` in workflows** — Uses system timer. Use `Workflow.DelayAsync`. +3. **`ConfigureAwait(false)` in workflows** — Leaves the deterministic scheduler. Never use in workflows. +4. **Non-`ApplicationFailureException` in workflows** — Other exceptions retry the workflow task forever instead of failing the workflow. +5. **Dictionary iteration in workflows** — `Dictionary` has no guaranteed order. Use `SortedDictionary`. +6. **Forgetting to heartbeat** — Long-running activities need `ActivityExecutionContext.Current.Heartbeat()` calls. +7. **Using `CancellationTokenSource.CancelAsync`** — Use `CancellationTokenSource.Cancel` instead. +8. **Logging with `Console.WriteLine` in workflows** — Use `Workflow.Logger` for replay-safe logging. + +## Writing Tests + +See `references/dotnet/testing.md` for info on writing tests. + +## Additional Resources + +### Reference Files + +- **`references/dotnet/patterns.md`** — Signals, queries, child workflows, saga pattern, etc. +- **`references/dotnet/determinism.md`** — Essentials of determinism in .NET +- **`references/dotnet/gotchas.md`** — .NET-specific mistakes and anti-patterns +- **`references/dotnet/error-handling.md`** — ApplicationFailureException, retry policies, non-retryable errors +- **`references/dotnet/observability.md`** — Logging, metrics, tracing +- **`references/dotnet/testing.md`** — WorkflowEnvironment, time-skipping, activity mocking +- **`references/dotnet/advanced-features.md`** — Schedules, worker tuning, dependency injection +- **`references/dotnet/data-handling.md`** — Data converters, payload encryption, etc. +- **`references/dotnet/versioning.md`** — Patching API, workflow type versioning, Worker Versioning +- **`references/dotnet/determinism-protection.md`** — Runtime task detection, .NET Task determinism rules diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/error-handling.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/error-handling.md new file mode 100644 index 0000000..f441620 --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/error-handling.md @@ -0,0 +1,157 @@ +# .NET SDK Error Handling + +## Overview + +The .NET SDK uses `ApplicationFailureException` for application-specific errors and provides comprehensive retry policy configuration. Generally, the following information about errors and retryability applies across activities, child workflows and Nexus operations. + +## Application Failures + +```csharp +using Temporalio.Activities; +using Temporalio.Exceptions; + +[Activity] +public async Task ValidateOrderAsync(Order order) +{ + if (!order.IsValid()) + { + throw new ApplicationFailureException( + "Invalid order", + errorType: "ValidationError"); + } +} +``` + +## Non-Retryable Errors + +```csharp +using Temporalio.Activities; +using Temporalio.Exceptions; + +[Activity] +public async Task ChargeCardAsync(ChargeCardInput input) +{ + if (!IsValidCard(input.CardNumber)) + { + throw new ApplicationFailureException( + "Permanent failure - invalid credit card", + errorType: "PaymentError", + nonRetryable: true); // Will not retry activity + } + return await ProcessPaymentAsync(input.CardNumber, input.Amount); +} +``` + +## Handling Activity Errors in Workflows + +```csharp +using Temporalio.Workflows; +using Temporalio.Exceptions; + +[Workflow] +public class MyWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + try + { + return await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.RiskyActivityAsync(), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + } + catch (ActivityFailureException ex) when (!TemporalException.IsCanceledException(ex)) + { + Workflow.Logger.LogError(ex, "Activity failed"); + throw new ApplicationFailureException( + "Workflow failed due to activity error"); + } + } +} +``` + +## Retry Configuration + +```csharp +using Temporalio.Common; +using Temporalio.Workflows; + +[Workflow] +public class MyWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + return await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.MyActivityAsync(), + new() + { + StartToCloseTimeout = TimeSpan.FromMinutes(10), + RetryPolicy = new() + { + MaximumInterval = TimeSpan.FromMinutes(1), + MaximumAttempts = 5, + NonRetryableErrorTypes = new[] { "ValidationError", "PaymentError" }, + }, + }); + } +} +``` + +Only set options such as MaximumInterval, MaximumAttempts etc. if you have a domain-specific reason to. +If not, prefer to leave them at their defaults. + +## Timeout Configuration + +```csharp +[Workflow] +public class MyWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + return await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.MyActivityAsync(), + new() + { + StartToCloseTimeout = TimeSpan.FromMinutes(5), // Single attempt + ScheduleToCloseTimeout = TimeSpan.FromMinutes(30), // Including retries + HeartbeatTimeout = TimeSpan.FromMinutes(2), // Between heartbeats + }); + } +} +``` + +## Workflow Failure + +**Critical .NET behavior:** Only `ApplicationFailureException` will fail a workflow. All other exceptions (including standard .NET exceptions like `NullReferenceException`, `KeyNotFoundException`, etc.) will **retry the workflow task** indefinitely. This is by design — those are treated as bugs to be fixed with a code deployment, not reasons for the workflow to fail. + +```csharp +[Workflow] +public class MyWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + if (someCondition) + { + throw new ApplicationFailureException( + "Cannot process order", + errorType: "BusinessError"); + } + return "success"; + } +} +``` + +**Note:** Do not use `nonRetryable:` with `ApplicationFailureException` inside a workflow (as opposed to an activity). + +## Best Practices + +1. Use specific error types for different failure modes +2. Mark permanent failures as non-retryable in activities +3. Configure appropriate retry policies +4. Log errors before re-raising +5. Use `ActivityFailureException` to catch activity failures in workflows +6. Design code to be idempotent for safe retries (see more at `references/core/patterns.md`) +7. Only throw `ApplicationFailureException` from workflows to fail them — other exceptions will retry the workflow task diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/gotchas.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/gotchas.md new file mode 100644 index 0000000..9b5806c --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/gotchas.md @@ -0,0 +1,262 @@ +# .NET Gotchas + +.NET-specific mistakes and anti-patterns. See also [Common Gotchas](references/core/gotchas.md) for language-agnostic concepts. + +## .NET Task Determinism + +The biggest .NET gotcha. Many `Task` APIs implicitly use `TaskScheduler.Default`, which breaks determinism. The SDK detects some of these at runtime via an `EventListener`, but not all. + +### Task.Run + +```csharp +// BAD: Uses TaskScheduler.Default +await Task.Run(() => DoSomething()); + +// GOOD: Uses current (deterministic) scheduler +await Workflow.RunTaskAsync(() => DoSomething()); +``` + +### Task.Delay / Thread.Sleep + +```csharp +// BAD: Uses system timer +await Task.Delay(TimeSpan.FromMinutes(5)); + +// GOOD: Creates durable timer in event history +await Workflow.DelayAsync(TimeSpan.FromMinutes(5)); +``` + +### ConfigureAwait(false) + +```csharp +// BAD: Leaves the deterministic context +var result = await SomeCallAsync().ConfigureAwait(false); + +// GOOD: Stays on deterministic scheduler (or just omit ConfigureAwait) +var result = await SomeCallAsync().ConfigureAwait(true); +var result = await SomeCallAsync(); // Also fine +``` + +### Task.WhenAll / Task.WhenAny + +```csharp +// BAD: Potential non-determinism +await Task.WhenAll(task1, task2); +await Task.WhenAny(task1, task2); + +// GOOD: Deterministic wrappers +await Workflow.WhenAllAsync(task1, task2); +await Workflow.WhenAnyAsync(task1, task2); +``` + +### Threading Primitives + +```csharp +// BAD: System threading primitives +var mutex = new System.Threading.Mutex(); +var semaphore = new SemaphoreSlim(1); + +// GOOD: Temporal workflow-safe alternatives +var mutex = new Temporalio.Workflows.Mutex(); +var semaphore = new Temporalio.Workflows.Semaphore(1); +``` + +See `references/dotnet/determinism-protection.md` for the complete list. + +## Wrong Retry Classification + +**Example:** Transient network errors should be retried. Authentication errors should not be. +See `references/dotnet/error-handling.md` to understand how to classify errors. + +## Heartbeating + +### Forgetting to Heartbeat Long Activities + +```csharp +// BAD: No heartbeat, can't detect stuck activities +[Activity] +public async Task ProcessLargeFileAsync(string path) +{ + foreach (var chunk in ReadChunks(path)) + await ProcessAsync(chunk); // Takes hours, no heartbeat + +// GOOD: Regular heartbeats with progress +[Activity] +public async Task ProcessLargeFileAsync(string path) +{ + var chunks = ReadChunks(path); + for (var i = 0; i < chunks.Count; i++) + { + ActivityExecutionContext.Current.Heartbeat($"Processing chunk {i}"); + await ProcessAsync(chunks[i]); + } +} +``` + +### Heartbeat Timeout Too Short + +```csharp +// BAD: Heartbeat timeout shorter than processing time +await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.ProcessChunkAsync(), + new() + { + StartToCloseTimeout = TimeSpan.FromMinutes(30), + HeartbeatTimeout = TimeSpan.FromSeconds(10), // Too short! + }); + +// GOOD: Heartbeat timeout allows for processing variance +await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.ProcessChunkAsync(), + new() + { + StartToCloseTimeout = TimeSpan.FromMinutes(30), + HeartbeatTimeout = TimeSpan.FromMinutes(2), + }); +``` + +Set heartbeat timeout as high as acceptable for your use case — each heartbeat counts as an action. + +## Cancellation + +### Not Handling Workflow Cancellation + +```csharp +// BAD: Cleanup doesn't run on cancellation +[Workflow] +public class BadWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.AcquireResourceAsync(), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.DoWorkAsync(), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.ReleaseResourceAsync(), // Never runs if cancelled! + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + } +} + +// GOOD: Use try/finally for cleanup +[Workflow] +public class GoodWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.AcquireResourceAsync(), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + try + { + await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.DoWorkAsync(), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + } + finally + { + await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.ReleaseResourceAsync(), + new() + { + StartToCloseTimeout = TimeSpan.FromMinutes(5), + CancellationToken = CancellationToken.None, + }); + } + } +} +``` + +### Not Handling Activity Cancellation + +Activities must **opt in** to receive cancellation. This requires: + +1. **Heartbeating** — Cancellation is delivered via heartbeat +2. **Checking the cancellation token** — Token is triggered when heartbeat detects cancellation + +```csharp +// BAD: Activity ignores cancellation +[Activity] +public async Task LongActivityAsync() +{ + await DoExpensiveWorkAsync(); // Runs to completion even if cancelled +} + +// GOOD: Heartbeat, check cancellation, and handle cleanup +[Activity] +public async Task LongActivityAsync() +{ + try + { + foreach (var item in items) + { + ActivityExecutionContext.Current.Heartbeat(); + ActivityExecutionContext.Current.CancellationToken.ThrowIfCancellationRequested(); + await ProcessAsync(item); + } + } + catch (OperationCanceledException) + { + await CleanupAsync(); + throw; + } +} +``` + +## Testing + +### Not Testing Failures + +It is important to make sure workflows work as expected under failure paths in addition to happy paths. Please see `references/dotnet/testing.md` for more info. + +### Not Testing Replay + +Replay tests help you test that you do not have hidden sources of non-determinism bugs in your workflow code. Please see `references/dotnet/testing.md` for more info. + +## Timers and Sleep + +### Using Task.Delay + +```csharp +// BAD: Task.Delay uses system timer, not deterministic during replay +[Workflow] +public class BadWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + await Task.Delay(TimeSpan.FromMinutes(1)); // SDK will detect and fail the task + } +} + +// GOOD: Use Workflow.DelayAsync for deterministic timers +[Workflow] +public class GoodWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + await Workflow.DelayAsync(TimeSpan.FromMinutes(1)); // Deterministic + } +} +``` + +**Why this matters:** `Task.Delay` uses the system clock, which differs between original execution and replay. `Workflow.DelayAsync` creates a durable timer in the event history, ensuring consistent behavior during replay. + +## Dictionary Iteration Order + +```csharp +// BAD: Dictionary iteration order is not guaranteed +var dict = new Dictionary { ["b"] = 2, ["a"] = 1 }; +foreach (var kvp in dict) // Order may differ between executions! + await ProcessAsync(kvp.Key, kvp.Value); + +// GOOD: Use SortedDictionary or sort before iterating +var dict = new SortedDictionary { ["b"] = 2, ["a"] = 1 }; +foreach (var kvp in dict) // Always iterates in key order + await ProcessAsync(kvp.Key, kvp.Value); +``` diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/observability.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/observability.md new file mode 100644 index 0000000..6919207 --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/observability.md @@ -0,0 +1,108 @@ +# .NET SDK Observability + +## Overview + +The .NET SDK provides observability through logging, metrics, and tracing using standard .NET patterns. + +## Logging + +### Workflow Logging (Replay-Safe) + +Use `Workflow.Logger` for replay-safe logging that avoids duplicate messages: + +```csharp +[Workflow] +public class MyWorkflow +{ + [WorkflowRun] + public async Task RunAsync(string name) + { + Workflow.Logger.LogInformation("Workflow started for {Name}", name); + + var result = await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.MyActivityAsync(), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + + Workflow.Logger.LogInformation("Activity completed with {Result}", result); + return result; + } +} +``` + +The workflow logger automatically: + +- Suppresses duplicate logs during replay +- Includes workflow context (workflow ID, run ID, etc.) + +### Activity Logging + +Use `ActivityExecutionContext.Current.Logger` for context-aware activity logging: + +```csharp +[Activity] +public async Task ProcessOrderAsync(string orderId) +{ + var logger = ActivityExecutionContext.Current.Logger; + logger.LogInformation("Processing order {OrderId}", orderId); + + // Perform work... + + logger.LogInformation("Order processed successfully"); + return "completed"; +} +``` + +### Customizing Logger Configuration + +```csharp +using Microsoft.Extensions.Logging; + +var client = await TemporalClient.ConnectAsync(new("localhost:7233") +{ + LoggerFactory = LoggerFactory.Create(builder => + builder + .AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] ") + .SetMinimumLevel(LogLevel.Information)), +}); +``` + +## Metrics + +### Enabling SDK Metrics + +Metrics are configured on `TemporalRuntime`. Create the runtime globally before any client/worker and set a Prometheus endpoint or custom metric meter. + +```csharp +using Temporalio.Client; +using Temporalio.Runtime; + +// Create runtime with Prometheus endpoint +var runtime = new TemporalRuntime(new() +{ + Telemetry = new() { Metrics = new() { Prometheus = new("0.0.0.0:9000") } }, +}); + +// Use this runtime for all clients +var client = await TemporalClient.ConnectAsync( + new("localhost:7233") { Runtime = runtime }); +``` + +Alternatively, use `Temporalio.Extensions.DiagnosticSource` to bridge metrics to a .NET `System.Diagnostics.Metrics.Meter` for integration with OpenTelemetry or other .NET metrics pipelines. + +### Key SDK Metrics + +- `temporal_request` — Client requests to server +- `temporal_workflow_task_execution_latency` — Workflow task processing time +- `temporal_activity_execution_latency` — Activity execution time +- `temporal_workflow_task_replay_latency` — Replay duration + +## Search Attributes (Visibility) + +See the Search Attributes section of `references/dotnet/data-handling.md` + +## Best Practices + +1. Use `Workflow.Logger` in workflows, `ActivityExecutionContext.Current.Logger` in activities +2. Don't use `Console.WriteLine` in workflows — it will produce duplicate output on replay +3. Configure metrics for production monitoring +4. Use Search Attributes for business-level visibility diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/patterns.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/patterns.md new file mode 100644 index 0000000..586fab0 --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/patterns.md @@ -0,0 +1,495 @@ +# .NET SDK Patterns + +## Signals + +```csharp +[Workflow] +public class OrderWorkflow +{ + private bool _approved; + private readonly List _items = new(); + + [WorkflowSignal] + public async Task ApproveAsync() + { + _approved = true; + } + + [WorkflowSignal] + public async Task AddItemAsync(string item) + { + _items.Add(item); + } + + [WorkflowRun] + public async Task RunAsync() + { + await Workflow.WaitConditionAsync(() => _approved); + return $"Processed {_items.Count} items"; + } +} +``` + +## Dynamic Signal Handlers + +For handling signals with names not known at compile time. Use cases for this pattern are rare — most workflows should use statically defined signal handlers. + +```csharp +[Workflow] +public class DynamicSignalWorkflow +{ + private readonly Dictionary> _signals = new(); + + [WorkflowSignal(Dynamic = true)] + public async Task HandleSignalAsync(string signalName, IRawValue[] args) + { + if (!_signals.ContainsKey(signalName)) + _signals[signalName] = new List(); + var value = Workflow.PayloadConverter.ToValue(args.Single()); + _signals[signalName].Add(value); + } + + [WorkflowRun] + public async Task>> RunAsync() + { + await Workflow.WaitConditionAsync(() => _signals.ContainsKey("done")); + return _signals; + } +} +``` + +## Queries + +**Important:** Queries must NOT modify workflow state or have side effects. + +```csharp +[Workflow] +public class StatusWorkflow +{ + private string _status = "pending"; + private int _progress; + + [WorkflowQuery] + public string GetStatus() => _status; + + [WorkflowQuery] + public int Progress => _progress; + + [WorkflowRun] + public async Task RunAsync() + { + _status = "running"; + for (var i = 0; i < 100; i++) + { + _progress = i; + await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.ProcessItem(i), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(1) }); + } + _status = "completed"; + return "done"; + } +} +``` + +## Dynamic Query Handlers + +For handling queries with names not known at compile time. Use cases for this pattern are rare — most workflows should use statically defined query handlers. + +```csharp +[Workflow] +public class DynamicQueryWorkflow +{ + private readonly SortedDictionary _state = new() + { + ["status"] = "running", + ["progress"] = "0", + }; + + [WorkflowQuery(Dynamic = true)] + public string HandleQuery(string queryName, IRawValue[] args) + { + return _state.GetValueOrDefault(queryName, "unknown"); + } + + [WorkflowRun] + public async Task RunAsync() { /* ... */ } +} +``` + +## Updates + +```csharp +[Workflow] +public class OrderWorkflow +{ + private readonly List _items = new(); + + [WorkflowUpdate] + public async Task AddItemAsync(string item) + { + _items.Add(item); + return _items.Count; + } + + [WorkflowUpdateValidator(nameof(AddItemAsync))] + public void ValidateAddItem(string item) + { + if (string.IsNullOrEmpty(item)) + throw new ArgumentException("Item cannot be empty"); + if (_items.Count >= 100) + throw new InvalidOperationException("Order is full"); + } + + [WorkflowRun] + public async Task RunAsync() + { + await Workflow.WaitConditionAsync(() => _items.Count > 0); + return $"Order with {_items.Count} items"; + } +} +``` + +**Important:** Validators must NOT mutate workflow state or do anything blocking (no activities, sleeps, or other commands). They are read-only, similar to query handlers. Throw an exception to reject the update; return void to accept. + +## Child Workflows + +```csharp +[Workflow] +public class ParentWorkflow +{ + [WorkflowRun] + public async Task> RunAsync(List orders) + { + var results = new List(); + foreach (var order in orders) + { + var result = await Workflow.ExecuteChildWorkflowAsync( + (ProcessOrderWorkflow wf) => wf.RunAsync(order), + new() + { + Id = $"order-{order.Id}", + // Control what happens to child when parent completes + // Terminate (default), Abandon, RequestCancel + ParentClosePolicy = ParentClosePolicy.Abandon, + }); + results.Add(result); + } + return results; + } +} +``` + +## Handles to External Workflows + +```csharp +[Workflow] +public class CoordinatorWorkflow +{ + [WorkflowRun] + public async Task RunAsync(string targetWorkflowId) + { + var handle = Workflow.GetExternalWorkflowHandle(targetWorkflowId); + + // Signal the external workflow + await handle.SignalAsync(wf => wf.DataReadyAsync(new DataPayload())); + + // Or cancel it + await handle.CancelAsync(); + } +} +``` + +## Parallel Execution + +```csharp +[Workflow] +public class ParallelWorkflow +{ + [WorkflowRun] + public async Task RunAsync(string[] items) + { + var tasks = items.Select(item => + Workflow.ExecuteActivityAsync( + (MyActivities a) => a.ProcessItem(item), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) })); + + return await Workflow.WhenAllAsync(tasks); + } +} +``` + +## Deterministic Task Alternatives + +.NET `Task` APIs often use `TaskScheduler.Default` implicitly. Use Temporal's deterministic alternatives: + +```csharp +// Instead of Task.WhenAll: +await Workflow.WhenAllAsync(task1, task2, task3); + +// Instead of Task.WhenAny: +await Workflow.WhenAnyAsync(task1, task2); + +// Instead of Task.Run: +await Workflow.RunTaskAsync(() => SomeWork()); + +// Instead of Task.Delay: +await Workflow.DelayAsync(TimeSpan.FromMinutes(5)); + +// Instead of System.Threading.Mutex: +var mutex = new Temporalio.Workflows.Mutex(); +await mutex.WaitOneAsync(); +try { /* critical section */ } +finally { mutex.ReleaseMutex(); } + +// Instead of System.Threading.Semaphore: +var semaphore = new Temporalio.Workflows.Semaphore(3); +await semaphore.WaitAsync(); +try { /* limited concurrency section */ } +finally { semaphore.Release(); } +``` + +## Continue-as-New + +```csharp +[Workflow] +public class LongRunningWorkflow +{ + [WorkflowRun] + public async Task RunAsync(WorkflowState state) + { + while (true) + { + state = await ProcessNextBatch(state); + + if (state.IsComplete) + return "done"; + + if (Workflow.ContinueAsNewSuggested) + throw Workflow.CreateContinueAsNewException( + (LongRunningWorkflow wf) => wf.RunAsync(state)); + } + } +} +``` + +## Saga Pattern (Compensations) + +**Important:** Compensation activities should be idempotent — they may be retried (as with ALL activities). + +```csharp +[Workflow] +public class OrderSagaWorkflow +{ + [WorkflowRun] + public async Task RunAsync(Order order) + { + var compensations = new List>(); + + try + { + // IMPORTANT: Save compensation BEFORE calling the activity. + // If activity fails after completing but before returning, + // compensation must still be registered. + compensations.Add(() => Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.ReleaseInventoryIfReservedAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) })); + await Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.ReserveInventoryAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + + compensations.Add(() => Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.RefundPaymentIfChargedAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) })); + await Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.ChargePaymentAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + + await Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.ShipOrderAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + + return "Order completed"; + } + catch (Exception ex) + { + Workflow.Logger.LogError(ex, "Order failed, running compensations"); + compensations.Reverse(); + foreach (var compensate in compensations) + { + try { await compensate(); } + catch (Exception compErr) + { + Workflow.Logger.LogError(compErr, "Compensation failed"); + } + } + throw; + } + } +} +``` + +## Cancellation Handling (CancellationToken) + +.NET uses standard `CancellationToken` for workflow cancellation. + +```csharp +[Workflow] +public class CancellableWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + try + { + await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.LongRunningAsync(), + new() { StartToCloseTimeout = TimeSpan.FromHours(1) }); + return "completed"; + } + catch (Exception e) when (TemporalException.IsCanceledException(e)) + { + // The "when" clause above is because we only want to apply the logic to cancellation, but + // this kind of cleanup could be done on any/all exceptions too. + Workflow.Logger.LogError(e, "Cancellation occurred, performing cleanup"); + + // Call cleanup activity. If this throws, it will swallow the original exception which we + // are ok with here. This could be changed to just log a failure and let the original + // cancellation continue. + // The default token on Workflow.CancellationToken is now marked + // cancelled, so we pass a different one. We use CancellationToken.None here because the + // cleanup activity itself doesn't need to be cancellable; if it did (e.g. you want to + // cancel cleanup from a timeout or another signal), create a new detached + // CancellationTokenSource and pass its Token instead. + await Workflow.ExecuteActivityAsync( + (MyActivities a) => a.MyCancellationCleanupActivity(), + new() + { + ScheduleToCloseTimeout = TimeSpan.FromMinutes(5), + CancellationToken = CancellationToken.None, + }); + + // Rethrow the cancellation + throw; + } + } +} +``` + +## Wait Condition with Timeout + +```csharp +[Workflow] +public class ApprovalWorkflow +{ + private bool _approved; + + [WorkflowSignal] + public async Task ApproveAsync() => _approved = true; + + [WorkflowRun] + public async Task RunAsync() + { + // Wait for approval with 24-hour timeout + var gotApproval = await Workflow.WaitConditionAsync( + () => _approved, + TimeSpan.FromHours(24)); + + return gotApproval ? "approved" : "auto-rejected due to timeout"; + } +} +``` + +## Waiting for All Handlers to Finish + +Signal and update handlers should generally be non-async (avoid running activities from them). Otherwise, the workflow may complete before handlers finish their execution. However, making handlers non-async sometimes requires workarounds that add complexity. + +When async handlers are necessary, use `WaitConditionAsync(AllHandlersFinished)` at the end of your workflow (or before continue-as-new) to prevent completion until all pending handlers complete. + +```csharp +[Workflow] +public class HandlerAwareWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + // ... main workflow logic ... + + // Before exiting, wait for all handlers to finish + await Workflow.WaitConditionAsync(() => Workflow.AllHandlersFinished); + return "done"; + } +} +``` + +## Activity Heartbeat Details + +### WHY: + +- **Support activity cancellation** — Cancellations are delivered via heartbeat; activities that don't heartbeat won't know they've been cancelled +- **Resume progress after worker failure** — Heartbeat details persist across retries + +### WHEN: + +- **Cancellable activities** — Any activity that should respond to cancellation +- **Long-running activities** — Track progress for resumability +- **Checkpointing** — Save progress periodically + +```csharp +[Activity] +public async Task ProcessLargeFileAsync(string filePath) +{ + var info = ActivityExecutionContext.Current.Info; + // Get heartbeat details from previous attempt (if any) + var startLine = info.HeartbeatDetails.Count > 0 + ? await info.HeartbeatDetailAtAsync(0) + : 0; + + var lines = await File.ReadAllLinesAsync(filePath); + for (var i = startLine; i < lines.Length; i++) + { + await ProcessLineAsync(lines[i]); + + // Heartbeat with progress + // If cancelled, CancellationToken will be triggered + ActivityExecutionContext.Current.Heartbeat(i + 1); + ActivityExecutionContext.Current.CancellationToken.ThrowIfCancellationRequested(); + } + + return "completed"; +} +``` + +## Timers + +```csharp +[Workflow] +public class TimerWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + await Workflow.DelayAsync(TimeSpan.FromHours(1)); + return "Timer fired"; + } +} +``` + +## Local Activities + +**Purpose**: Reduce latency for short, lightweight operations by skipping the task queue. ONLY use these when necessary for performance. Do NOT use these by default, as they are not durable and distributed. + +```csharp +[Workflow] +public class LocalActivityWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + var result = await Workflow.ExecuteLocalActivityAsync( + (MyActivities a) => a.QuickLookup("key"), + new() { StartToCloseTimeout = TimeSpan.FromSeconds(5) }); + return result; + } +} +``` diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/testing.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/testing.md new file mode 100644 index 0000000..d60805a --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/testing.md @@ -0,0 +1,177 @@ +# .NET SDK Testing + +## Overview + +You test Temporal .NET Workflows using the `Temporalio.Testing` namespace plus a normal .NET test framework. The .NET SDK is compatible with any testing framework; most samples use xUnit. The SDK provides `WorkflowEnvironment` for testing workflows in a local environment and `ActivityEnvironment` for isolated activity testing. + +## Test Environment Setup + +The core pattern is: + +1. Start a `WorkflowEnvironment` (`WorkflowEnvironment.StartLocalAsync()`). +2. Create a `TemporalWorker` in that environment with your Workflow and Activities registered. +3. Use the environment's client to execute the Workflow, using a fresh GUID for the task queue name and workflow ID. +4. Assert on the result or status. + +```csharp +using Temporalio.Testing; +using Temporalio.Worker; + +[Fact] +public async Task TestWorkflow() +{ + await using var env = await WorkflowEnvironment.StartLocalAsync(); + + using var worker = new TemporalWorker( + env.Client, + new TemporalWorkerOptions($"task-queue-{Guid.NewGuid()}") + .AddWorkflow() + .AddAllActivities(new MyActivities())); + + await worker.ExecuteAsync(async () => + { + var result = await env.Client.ExecuteWorkflowAsync( + (MyWorkflow wf) => wf.RunAsync("input"), + new(id: $"wf-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!)); + Assert.Equal("expected", result); + }); +} +``` + +Conveniently, the local `env` can be shared among tests, e.g. via a fixture class. + +If your workflows / tests involve long durations (such as using Temporal timers / sleeps), then you can use the time-skipping environment, via `WorkflowEnvironment.StartTimeSkippingAsync()`. Only use time-skipping if you must. It is not thread safe and cannot be shared among tests. + +## Activity Mocking + +The .NET SDK provides a straightforward way to mock Activities. Create a mock function with the `[Activity]` attribute and specify the name of the original Activity you want to mock: + +```csharp +[Fact] +public async Task TestWithMockActivity() +{ + await using var env = await WorkflowEnvironment.StartLocalAsync(); + + [Activity("MyActivity")] + static Task MockMyActivity(string input) => + Task.FromResult($"mocked: {input}"); + + using var worker = new TemporalWorker( + env.Client, + new TemporalWorkerOptions($"task-queue-{Guid.NewGuid()}") + .AddWorkflow() + .AddActivity(MockMyActivity)); + + await worker.ExecuteAsync(async () => + { + var result = await env.Client.ExecuteWorkflowAsync( + (MyWorkflow wf) => wf.RunAsync("test"), + new(id: $"wf-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!)); + Assert.Equal("mocked: test", result); + }); +} +``` + +**Note:** If the original activity method name ends with `Async` and returns a `Task`, the default activity name has `Async` trimmed off. For example, `MyActivityAsync` has default name `MyActivity`. + +## Testing Signals and Queries + +```csharp +[Fact] +public async Task TestSignalsAndQueries() +{ + await using var env = await WorkflowEnvironment.StartLocalAsync(); + + using var worker = new TemporalWorker(/* ... */); + + await worker.ExecuteAsync(async () => + { + var handle = await env.Client.StartWorkflowAsync( + (MyWorkflow wf) => wf.RunAsync(), + new(id: $"wf-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!)); + + // Send signal + await handle.SignalAsync(wf => wf.MySignalAsync("data")); + + // Query state + var status = await handle.QueryAsync(wf => wf.GetStatus()); + Assert.Equal("expected", status); + + // Wait for completion + var result = await handle.GetResultAsync(); + }); +} +``` + +## Testing Failure Cases + +```csharp +[Fact] +public async Task TestActivityFailureHandling() +{ + await using var env = await WorkflowEnvironment.StartLocalAsync(); + + [Activity("RiskyActivity")] + static Task MockFailingActivity() => + throw new ApplicationFailureException("Simulated failure", nonRetryable: true); + + using var worker = new TemporalWorker(/* ... with mock activity */); + + await worker.ExecuteAsync(async () => + { + var ex = await Assert.ThrowsAsync(() => + env.Client.ExecuteWorkflowAsync( + (MyWorkflow wf) => wf.RunAsync(), + new(id: $"wf-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!))); + }); +} +``` + +## Replay Testing + +```csharp +using Temporalio.Worker; + +[Fact] +public async Task TestReplay() +{ + var historyJson = await File.ReadAllTextAsync("example-history.json"); + var replayer = new WorkflowReplayer( + new WorkflowReplayerOptions() + .AddWorkflow()); + + await replayer.ReplayWorkflowAsync( + WorkflowHistory.FromJson("my-workflow-id", historyJson)); +} +``` + +## Activity Testing + +```csharp +using Temporalio.Testing; + +[Fact] +public async Task TestActivity() +{ + var env = new ActivityEnvironment(); + var activities = new MyActivities(); + var result = await env.RunAsync(() => activities.MyActivity("arg1")); + Assert.Equal("expected", result); +} +``` + +The `ActivityEnvironment` provides: + +- `Info` — Activity info, defaulted to basic values +- `CancellationTokenSource` — Token source for issuing cancellation +- `Heartbeater` — Callback invoked each heartbeat +- `Logger` — Activity logger + +## Best Practices + +1. Use the `WorkflowEnvironment.StartLocalAsync` environment for most testing +2. Use time-skipping environment for workflows with durable timers / durable sleeps +3. Mock external dependencies in activities +4. Test replay compatibility, especially when changing workflow code +5. Test signal/query handlers explicitly +6. Use unique workflow IDs and task queues per test to avoid conflicts — `Guid.NewGuid()` is easiest diff --git a/plugins/temporal-developer/skills/temporal-developer/references/dotnet/versioning.md b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/versioning.md new file mode 100644 index 0000000..6371926 --- /dev/null +++ b/plugins/temporal-developer/skills/temporal-developer/references/dotnet/versioning.md @@ -0,0 +1,307 @@ +# .NET SDK Versioning + +For conceptual overview and guidance on choosing an approach, see `references/core/versioning.md`. + +## Patching API + +### The Patched() Method + +The `Workflow.Patched()` method checks whether a Workflow should run new or old code: + +```csharp +[Workflow] +public class ShippingWorkflow +{ + [WorkflowRun] + public async Task RunAsync() + { + if (Workflow.Patched("send-email-instead-of-fax")) + { + // New code path + await Workflow.ExecuteActivityAsync( + (ShippingActivities a) => a.SendEmailAsync(), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + } + else + { + // Old code path (for replay of existing workflows) + await Workflow.ExecuteActivityAsync( + (ShippingActivities a) => a.SendFaxAsync(), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + } + } +} +``` + +**How it works:** + +- For new executions: `Patched()` returns `true` and records a marker in the Workflow history +- For replay with the marker: `Patched()` returns `true` (history includes this patch) +- For replay without the marker: `Patched()` returns `false` (history predates this patch) + +### Three-Step Patching Process + +**Warning:** Failing to follow this process correctly will result in non-determinism errors for in-flight workflows. + +**Step 1: Patch in New Code** + +```csharp +[Workflow] +public class OrderWorkflow +{ + [WorkflowRun] + public async Task RunAsync(Order order) + { + if (Workflow.Patched("add-fraud-check")) + { + await Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.CheckFraudAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(2) }); + } + + return await Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.ProcessPaymentAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + } +} +``` + +**Step 2: Deprecate the Patch** + +Once all pre-patch Workflow Executions have completed: + +```csharp +[Workflow] +public class OrderWorkflow +{ + [WorkflowRun] + public async Task RunAsync(Order order) + { + Workflow.DeprecatePatch("add-fraud-check"); + + await Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.CheckFraudAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(2) }); + + return await Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.ProcessPaymentAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + } +} +``` + +**Step 3: Remove the Patch** + +After all workflows with the deprecated patch marker have completed, remove the `DeprecatePatch()` call entirely: + +```csharp +[Workflow] +public class OrderWorkflow +{ + [WorkflowRun] + public async Task RunAsync(Order order) + { + await Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.CheckFraudAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(2) }); + + return await Workflow.ExecuteActivityAsync( + (OrderActivities a) => a.ProcessPaymentAsync(order), + new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); + } +} +``` + +### Query Filters for Finding Workflows by Version + +Use List Filters to find workflows with specific patch versions: + +```bash +# Find running workflows with a specific patch +temporal workflow list --query \ + 'WorkflowType = "OrderWorkflow" AND ExecutionStatus = "Running" AND TemporalChangeVersion = "add-fraud-check"' + +# Find running workflows without any patch (pre-patch versions) +temporal workflow list --query \ + 'WorkflowType = "OrderWorkflow" AND ExecutionStatus = "Running" AND TemporalChangeVersion IS NULL' +``` + +## Workflow Type Versioning + +For incompatible changes, create a new Workflow Type instead of using patches: + +```csharp +[Workflow("PizzaWorkflow")] +public class PizzaWorkflow +{ + [WorkflowRun] + public async Task RunAsync(PizzaOrder order) + { + return await ProcessOrderV1Async(order); + } +} + +[Workflow("PizzaWorkflowV2")] +public class PizzaWorkflowV2 +{ + [WorkflowRun] + public async Task RunAsync(PizzaOrder order) + { + return await ProcessOrderV2Async(order); + } +} +``` + +Register both with the Worker: + +```csharp +var worker = new TemporalWorker( + client, + new TemporalWorkerOptions("pizza-task-queue") + .AddWorkflow() + .AddWorkflow() + .AddAllActivities(new PizzaActivities())); +``` + +Update client code to start new workflows with the new type: + +```csharp +// Old workflows continue on PizzaWorkflow +// New workflows use PizzaWorkflowV2 +var handle = await client.StartWorkflowAsync( + (PizzaWorkflowV2 wf) => wf.RunAsync(order), + new(id: $"pizza-{order.Id}", taskQueue: "pizza-task-queue")); +``` + +Check for open executions before removing the old type: + +```bash +temporal workflow list --query 'WorkflowType = "PizzaWorkflow" AND ExecutionStatus = "Running"' +``` + +## Worker Versioning + +Worker Versioning manages versions at the deployment level, allowing multiple Worker versions to run simultaneously. + +### Key Concepts + +**Worker Deployment**: A logical service grouping similar Workers together (e.g., "loan-processor"). All versions of your code live under this umbrella. + +**Worker Deployment Version**: A specific snapshot of your code identified by a deployment name and Build ID (e.g., "loan-processor:v1.0" or "loan-processor:abc123"). + +### Configuring Workers for Versioning + +```csharp +using Temporalio.Worker; + +var worker = new TemporalWorker( + client, + new TemporalWorkerOptions("my-task-queue") + { + DeploymentOptions = new WorkerDeploymentOptions( + DeploymentName: "my-service", + BuildId: Environment.GetEnvironmentVariable("BUILD_ID") ?? "dev"), + UseWorkerVersioning = true, + } + .AddWorkflow() + .AddAllActivities(new MyActivities())); +``` + +**Configuration parameters:** + +- `UseWorkerVersioning`: Enables Worker Versioning +- `DeploymentOptions`: Identifies the Worker Deployment Version (deployment name + build ID) +- Build ID: Typically a git commit hash, version number, or timestamp + +### PINNED vs AUTO_UPGRADE Behaviors + +**PINNED Behavior** + +Workflows stay locked to their original Worker version: + +```csharp +[Workflow(VersioningBehavior = VersioningBehavior.Pinned)] +public class StableWorkflow { /* ... */ } +``` + +**When to use PINNED:** + +- Short-running workflows (minutes to hours) +- Consistency is critical (e.g., financial transactions) +- You want to eliminate version compatibility complexity +- Building new applications and want simplest development experience + +**AUTO_UPGRADE Behavior** + +Workflows can move to newer versions: + +```csharp +[Workflow(VersioningBehavior = VersioningBehavior.AutoUpgrade)] +public class UpgradableWorkflow { /* ... */ } +``` + +**When to use AUTO_UPGRADE:** + +- Long-running workflows (weeks or months) +- Workflows need to benefit from bug fixes during execution +- Migrating from traditional rolling deployments +- You are already using patching APIs for version transitions + +**Important:** AUTO_UPGRADE workflows still need patching to handle version transitions safely since they can move between Worker versions. + +### Worker Configuration with Default Behavior + +```csharp +var worker = new TemporalWorker( + client, + new TemporalWorkerOptions("my-task-queue") + { + DeploymentOptions = new WorkerDeploymentOptions( + DeploymentName: "order-service", + BuildId: Environment.GetEnvironmentVariable("BUILD_ID") ?? "dev") + { + DefaultVersioningBehavior = VersioningBehavior.Pinned, + }, + UseWorkerVersioning = true, + } + .AddWorkflow() + .AddAllActivities(new OrderActivities())); +``` + +### Deployment Strategies + +**Blue-Green Deployments** + +Maintain two environments and switch traffic between them: + +1. Deploy new code to idle environment +2. Run tests and validation +3. Switch traffic to new environment +4. Keep old environment for instant rollback + +**Rainbow Deployments** + +Multiple versions run simultaneously: + +- New workflows use latest version +- Existing workflows complete on their original version +- Add new versions alongside existing ones +- Gradually sunset old versions as workflows complete + +### Querying Workflows by Worker Version + +```bash +# Find workflows on a specific Worker version +temporal workflow list --query \ + 'TemporalWorkerDeploymentVersion = "my-service:v1.0.0" AND ExecutionStatus = "Running"' +``` + +## Best Practices + +1. **Check for open executions** before removing old code paths +2. **Use descriptive patch IDs** that explain the change (e.g., "add-fraud-check" not "patch-1") +3. **Deploy patches incrementally**: patch, deprecate, remove +4. **Use PINNED for short workflows** to simplify version management +5. **Use AUTO_UPGRADE with patching** for long-running workflows that need updates +6. **Generate Build IDs from code** (git hash) to ensure changes produce new versions +7. **Avoid rolling deployments** for high-availability services with long-running workflows diff --git a/plugins/temporal-developer/skills/temporal-developer/references/go/advanced-features.md b/plugins/temporal-developer/skills/temporal-developer/references/go/advanced-features.md index 55e4e57..b64ce94 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/go/advanced-features.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/go/advanced-features.md @@ -174,12 +174,14 @@ func FileProcessingWorkflow(ctx workflow.Context, file FileParam) error { ``` Key points: + - `workflow.ErrSessionFailed` is returned if the worker hosting the session dies - `CompleteSession` releases resources -- always call it (use `defer`) - Use case: file processing (download, process, upload on same host), GPU workloads, or any pipeline needing local state - `MaxConcurrentSessionExecutionSize` on `worker.Options` limits how many sessions a single worker can handle **Limitations:** + - Sessions do not survive worker process restarts — if the worker dies, the session fails and activities must be retried from the workflow level - There is no server-side support for sessions — the Go SDK implements them entirely client-side using internal task queue routing - Session concurrency limiting is per-process, not per-host — only one worker process per host if you rely on this diff --git a/plugins/temporal-developer/skills/temporal-developer/references/go/data-handling.md b/plugins/temporal-developer/skills/temporal-developer/references/go/data-handling.md index e887e7b..18ccf57 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/go/data-handling.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/go/data-handling.md @@ -125,11 +125,13 @@ dataConverter := converter.NewCompositeDataConverter( ## Protobuf Support Binary protobuf: + ```go converter.NewProtoPayloadConverter() ``` JSON protobuf: + ```go converter.NewProtoJSONPayloadConverter() ``` diff --git a/plugins/temporal-developer/skills/temporal-developer/references/go/determinism-protection.md b/plugins/temporal-developer/skills/temporal-developer/references/go/determinism-protection.md index b37d94a..2cdd829 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/go/determinism-protection.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/go/determinism-protection.md @@ -29,6 +29,7 @@ workflowcheck -show-pos ./... ### What It Detects **Non-deterministic functions/variables:** + - `time.Now` -- obtaining current time - `time.Sleep` -- sleeping - `crypto/rand.Reader` -- crypto random reader @@ -36,6 +37,7 @@ workflowcheck -show-pos ./... - `os.Stdin`, `os.Stdout`, `os.Stderr` -- standard I/O streams **Non-deterministic Go constructs:** + - Starting a goroutine (`go func()`) - Sending to a channel - Receiving from a channel @@ -45,6 +47,7 @@ workflowcheck -show-pos ./... ### Limitations `workflowcheck` cannot catch everything. It does **not** detect: + - Global variable mutation - Non-determinism via reflection - Runtime-conditional non-determinism @@ -72,6 +75,7 @@ workflowcheck -config workflowcheck.config.yaml ./... ## Determinism Rules **You must:** + - Use `workflow.Go(ctx, func(ctx workflow.Context) { ... })` instead of `go` - Use `workflow.NewChannel(ctx)` instead of `chan` - Use `workflow.NewSelector(ctx)` instead of `select` @@ -81,6 +85,7 @@ workflowcheck -config workflowcheck.config.yaml ./... - Sort map keys before iterating, or use `workflow.SideEffect` / an activity **You must not:** + - Start native goroutines - Use native channels or `select` - Call `time.Now()` or `time.Sleep()` diff --git a/plugins/temporal-developer/skills/temporal-developer/references/go/determinism.md b/plugins/temporal-developer/skills/temporal-developer/references/go/determinism.md index 0cff905..c8b52b9 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/go/determinism.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/go/determinism.md @@ -8,9 +8,9 @@ The Go SDK has NO runtime sandbox (unlike Python/TypeScript). Workflows must be Temporal provides durable execution through **History Replay**. When a Worker restores workflow state, it re-executes workflow code from the beginning. This requires the code to be **deterministic**. See `references/core/determinism.md` for a deep explanation. -## Forbidden Operations +## Forbidden Operations in Workflows -Do not use any of the following in workflow code: +Do not use any of the following in workflow code (they are appropriate to use in activities): - **Native goroutines** (`go func()`) -- use `workflow.Go()` instead - **Native channels** (`chan`, send, receive, `range` over channel) -- use `workflow.Channel` instead diff --git a/plugins/temporal-developer/skills/temporal-developer/references/go/go.md b/plugins/temporal-developer/skills/temporal-developer/references/go/go.md index 827d35c..546e1b1 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/go/go.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/go/go.md @@ -7,11 +7,13 @@ The Temporal Go SDK (`go.temporal.io/sdk`) provides a strongly-typed, idiomatic ## Quick Start **Add Dependency:** In your Go module, add the Temporal SDK: + ```bash go get go.temporal.io/sdk ``` **workflows/greeting.go** - Workflow definition: + ```go package workflows @@ -37,6 +39,7 @@ func GreetingWorkflow(ctx workflow.Context, name string) (string, error) { ``` **activities/greet.go** - Activity definition: + ```go package activities @@ -53,6 +56,7 @@ func (a *Activities) Greet(ctx context.Context, name string) (string, error) { ``` **worker/main.go** - Worker setup: + ```go package main @@ -90,6 +94,7 @@ func main() { **Start the worker:** Run `go run worker/main.go` in the background. **starter/main.go** - Start a workflow execution: + ```go package main @@ -136,6 +141,7 @@ func main() { ## Key Concepts ### Workflow Definition + - Exported function with `workflow.Context` as the first parameter - Returns `(ResultType, error)` or just `error` - Signature: `func MyWorkflow(ctx workflow.Context, input MyInput) (MyOutput, error)` @@ -143,12 +149,14 @@ func main() { - Register with `w.RegisterWorkflow(MyWorkflow)` ### Activity Definition + - Regular function or struct methods with `context.Context` as the first parameter - Struct methods are preferred for dependency injection - Signature: `func (a *Activities) MyActivity(ctx context.Context, input string) (string, error)` - Register struct with `w.RegisterActivity(&Activities{})` (registers all exported methods) ### Worker Setup + - Create client with `client.Dial(client.Options{})` - Create worker with `worker.New(c, "task-queue", worker.Options{})` - Register workflows and activities @@ -159,6 +167,7 @@ func main() { **Workflow code must be deterministic!** The Go SDK has no sandbox -- determinism is enforced by convention and tooling. Use Temporal replacements instead of native Go constructs: + - `workflow.Go()` instead of `go` (goroutines) - `workflow.Channel` instead of `chan` - `workflow.Selector` instead of `select` @@ -167,6 +176,7 @@ Use Temporal replacements instead of native Go constructs: - `workflow.GetLogger()` instead of `log` / `fmt.Println` for replay-safe logging Use the **`workflowcheck`** static analysis tool to catch non-deterministic code: + ```bash go install go.temporal.io/sdk/contrib/tools/workflowcheck@latest workflowcheck ./... @@ -191,6 +201,7 @@ myapp/ ``` **Activities as struct methods for dependency injection:** + ```go // activities/greet.go type Activities struct { @@ -230,6 +241,7 @@ See `references/go/testing.md` for info on writing tests. ## Additional Resources ### Reference Files + - **`references/go/patterns.md`** - Signals, queries, child workflows, saga pattern, etc. - **`references/go/determinism.md`** - Determinism rules, workflowcheck tool, safe alternatives - **`references/go/gotchas.md`** - Go-specific mistakes and anti-patterns diff --git a/plugins/temporal-developer/skills/temporal-developer/references/go/gotchas.md b/plugins/temporal-developer/skills/temporal-developer/references/go/gotchas.md index 4b7ddf3..6ba46ff 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/go/gotchas.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/go/gotchas.md @@ -206,6 +206,7 @@ func GoodWorkflow(ctx workflow.Context) error { ### Not Handling Activity Cancellation Activities must **opt in** to receive cancellation. This requires: + 1. **Heartbeating** - Cancellation is delivered via heartbeat 2. **Checking ctx.Done()** - Detect when cancellation arrives diff --git a/plugins/temporal-developer/skills/temporal-developer/references/go/observability.md b/plugins/temporal-developer/skills/temporal-developer/references/go/observability.md index ba55140..a7867b3 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/go/observability.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/go/observability.md @@ -28,6 +28,7 @@ func MyWorkflow(ctx workflow.Context, input string) (string, error) { ``` The workflow logger automatically: + - Suppresses duplicate logs during replay - Includes workflow context (workflow ID, run ID, etc.) @@ -45,6 +46,7 @@ func MyActivity(ctx context.Context, input string) (string, error) { ``` Activity logger includes: + - Activity ID, type, and task queue - Workflow ID and run ID - Attempt number (for retries) @@ -60,43 +62,68 @@ logger.Info("Processing order") // includes orderId and customerId ## Customizing the Logger -Set a custom logger via `client.Options{Logger: myLogger}`. Implement the `log.Logger` interface (Debug, Info, Warn, Error methods). +The SDK ships a single built-in **`slog` adapter** (`log.NewStructuredLogger`) and considers `slog` (go 1.21+) the universal bridge to other logging libraries. + +### The `log.Logger` Interface + +```go +// go.temporal.io/sdk/log +type Logger interface { + Debug(msg string, keyvals ...interface{}) + Info(msg string, keyvals ...interface{}) + Warn(msg string, keyvals ...interface{}) + Error(msg string, keyvals ...interface{}) +} +``` + +Optional companion interfaces: `WithLogger` (adds `.With()`) and `WithSkipCallers` (fixes caller frames). -### Using slog (Go 1.21+) +### Using slog (Recommended) ```go import ( "log/slog" "os" - tlog "go.temporal.io/sdk/log" + "go.temporal.io/sdk/log" ) slogHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}) -logger := tlog.NewStructuredLogger(slog.New(slogHandler)) +logger := log.NewStructuredLogger(slog.New(slogHandler)) c, err := client.Dial(client.Options{ Logger: logger, }) ``` -### Using Third-Party Loggers (Logrus, Zap, etc.) +### Using slog as a Bridge to Third-Party Loggers -Use the [logur](https://github.com/logur/logur) adapter package: +Any third-party logger that can back an `slog.Handler` works with `log.NewStructuredLogger` — this includes zap, zerolog, logrus, and most modern Go logging libraries. The pattern is: create an `slog.Handler` from your logger, then wrap it with `log.NewStructuredLogger`. + +**Example with Zap:** ```go import ( - "github.com/sirupsen/logrus" - logrusadapter "logur.dev/adapter/logrus" - "logur.dev/logur" + "log/slog" + + "go.uber.org/zap" + "go.uber.org/zap/exp/zapslog" + "go.temporal.io/sdk/log" ) -logger := logur.LoggerToKV(logrusadapter.New(logrus.New())) +zapLogger, _ := zap.NewProduction() +handler := zapslog.NewHandler(zapLogger.Core()) +logger := log.NewStructuredLogger(slog.New(handler)) + c, err := client.Dial(client.Options{ Logger: logger, }) ``` +### Direct Adapter (Alternative) + +If you cannot use the slog bridge, you can implement the `log.Logger` interface directly. The Temporal samples repo has a ~60-line [zap adapter](https://github.com/temporalio/samples-go/blob/main/zapadapter/zap_adapter.go) that implements `Logger`, `WithLogger`, and `WithSkipCallers` and can be copied into your project. + ## Metrics Use the Tally library (`go.temporal.io/sdk/contrib/tally`) with Prometheus: @@ -134,6 +161,7 @@ c, err := client.Dial(client.Options{ ``` Key SDK metrics: + - `temporal_workflow_task_execution_latency` -- Workflow task processing time - `temporal_activity_execution_latency` -- Activity execution time - `temporal_workflow_task_replay_latency` -- Replay duration diff --git a/plugins/temporal-developer/skills/temporal-developer/references/go/patterns.md b/plugins/temporal-developer/skills/temporal-developer/references/go/patterns.md index 732083f..298cca4 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/go/patterns.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/go/patterns.md @@ -284,6 +284,7 @@ func ApprovalWorkflow(ctx workflow.Context) (string, error) { ``` Key points: + - `AddReceive(channel, callback)` -- fires when a channel has a message (must consume with `c.Receive`) - `AddFuture(future, callback)` -- fires when a future resolves (once per Selector) - `AddDefault(callback)` -- fires immediately if nothing else is ready @@ -457,10 +458,12 @@ func MyWorkflow(ctx workflow.Context) (string, error) { ## Activity Heartbeat Details ### WHY: + - **Support activity cancellation** -- Cancellations are delivered via heartbeat; activities that don't heartbeat won't know they've been cancelled - **Resume progress after worker failure** -- Heartbeat details persist across retries ### WHEN: + - **Cancellable activities** -- Any activity that should respond to cancellation - **Long-running activities** -- Track progress for resumability - **Checkpointing** -- Save progress periodically diff --git a/plugins/temporal-developer/skills/temporal-developer/references/go/versioning.md b/plugins/temporal-developer/skills/temporal-developer/references/go/versioning.md index b6b6c27..c8f7280 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/go/versioning.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/go/versioning.md @@ -45,6 +45,7 @@ err = workflow.ExecuteActivity(ctx, ActivityC, data).Get(ctx, &result1) ``` Keep the `GetVersion` call even with a single branch. This ensures: + 1. If an older execution replays on this code, it fails fast instead of proceeding incorrectly 2. If you need further changes, you just bump `maxSupported` @@ -139,6 +140,7 @@ w := worker.New(c, "my-task-queue", worker.Options{ ``` **Configuration fields:** + - `UseVersioning`: enables Worker Versioning - `Version`: identifies the Worker Deployment Version (deployment name + build ID) - `DefaultVersioningBehavior`: `VersioningBehaviorPinned` or `VersioningBehaviorAutoUpgrade` @@ -151,6 +153,7 @@ w := worker.New(c, "my-task-queue", worker.Options{ Workflows stay locked to their original Worker version. **When to use PINNED:** + - Short-running workflows (minutes to hours) - Consistency is critical (e.g., financial transactions) - You want to eliminate version compatibility complexity @@ -161,6 +164,7 @@ Workflows stay locked to their original Worker version. Workflows can move to newer versions. **When to use AUTO_UPGRADE:** + - Long-running workflows (weeks or months) - Workflows need to benefit from bug fixes during execution - Migrating from traditional rolling deployments @@ -189,6 +193,7 @@ w := worker.New(c, "orders-task-queue", worker.Options{ **Blue-Green Deployments** Maintain two environments and switch traffic between them: + 1. Deploy new code to idle environment 2. Run tests and validation 3. Switch traffic to new environment @@ -197,6 +202,7 @@ Maintain two environments and switch traffic between them: **Rainbow Deployments** Multiple versions run simultaneously: + - New workflows use latest version - Existing workflows complete on their original version - Add new versions alongside existing ones diff --git a/plugins/temporal-developer/skills/temporal-developer/references/java/advanced-features.md b/plugins/temporal-developer/skills/temporal-developer/references/java/advanced-features.md index e897bb1..e736da2 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/java/advanced-features.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/java/advanced-features.md @@ -77,6 +77,7 @@ public void completeApproval(String requestId, boolean approved) { ActivityCompletionClient completionClient = client.newActivityCompletionClient(); + // Retrieve the task token from external storage (e.g., database) byte[] taskToken = getTaskToken(requestId); if (approved) { diff --git a/plugins/temporal-developer/skills/temporal-developer/references/java/determinism-protection.md b/plugins/temporal-developer/skills/temporal-developer/references/java/determinism-protection.md index 78c4446..1894644 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/java/determinism-protection.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/java/determinism-protection.md @@ -4,7 +4,9 @@ The Java SDK has **no sandbox** (only Python and TypeScript have sandboxing). Java relies on developer conventions and runtime replay detection to enforce determinism. A static analysis tool (`temporal-workflowcheck`) is available in beta. -## Forbidden Operations +## Forbidden Operations in Workflows + +The following are forbidden inside workflow code but are appropriate to use in activities. ```java // BAD: Non-deterministic operations in workflow code diff --git a/plugins/temporal-developer/skills/temporal-developer/references/java/determinism.md b/plugins/temporal-developer/skills/temporal-developer/references/java/determinism.md index 1981d00..29f25d5 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/java/determinism.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/java/determinism.md @@ -14,7 +14,9 @@ Java workflow code runs in a cooperative threading model where only one workflow `temporal-workflowcheck` (static analysis, beta) and `WorkflowReplayer` (replay testing) can help uncover some violations, but they are not exhaustive — careful code review and adherence to the rules below remain essential. -## Forbidden Operations +## Forbidden Operations in Workflows + +The following are forbidden inside workflow code but are appropriate to use in activities. - `Thread.sleep()` — blocks the real thread, bypasses Temporal timers - `new Thread()` or thread pools — breaks the cooperative threading model diff --git a/plugins/temporal-developer/skills/temporal-developer/references/java/error-handling.md b/plugins/temporal-developer/skills/temporal-developer/references/java/error-handling.md index 97d4cea..753d69a 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/java/error-handling.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/java/error-handling.md @@ -77,6 +77,7 @@ Activity failures are always wrapped in `ActivityFailure`. The original exceptio ```java import io.temporal.failure.ActivityFailure; import io.temporal.failure.ApplicationFailure; +import io.temporal.failure.CanceledFailure; import io.temporal.failure.TimeoutFailure; import io.temporal.workflow.Workflow; @@ -86,6 +87,10 @@ public class MyWorkflowImpl implements MyWorkflow { try { return activities.riskyOperation(); } catch (ActivityFailure af) { + // Let cancellation propagate so the workflow is canceled, not failed + if (af.getCause() instanceof CanceledFailure) { + throw af; + } if (af.getCause() instanceof ApplicationFailure) { ApplicationFailure appFailure = (ApplicationFailure) af.getCause(); String type = appFailure.getType(); diff --git a/plugins/temporal-developer/skills/temporal-developer/references/java/gotchas.md b/plugins/temporal-developer/skills/temporal-developer/references/java/gotchas.md index 567fb64..4943f0d 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/java/gotchas.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/java/gotchas.md @@ -7,6 +7,7 @@ Java-specific mistakes and anti-patterns. See also [Common Gotchas](../core/gotc **Critical: The Java SDK has NO sandbox.** Unlike Python (which uses a sandbox) or TypeScript (which uses V8 isolation), the Java SDK relies entirely on developer conventions. Non-deterministic calls silently succeed during initial execution but cause `NonDeterministicException` on replay. Forbidden in workflow code — use the Temporal `Workflow.*` equivalents instead: + - `Thread.sleep` → `Workflow.sleep` - `UUID.randomUUID` → `Workflow.randomUUID` - `Math.random` → `Workflow.newRandom` @@ -103,6 +104,7 @@ public class GoodWorkflow implements MyWorkflow { ### Not Handling Activity Cancellation Activities must **opt in** to receive cancellation. This requires: + 1. **Heartbeating** - Cancellation is delivered via heartbeat 2. **Catching CanceledFailure** - Thrown when heartbeat detects cancellation diff --git a/plugins/temporal-developer/skills/temporal-developer/references/java/java.md b/plugins/temporal-developer/skills/temporal-developer/references/java/java.md index e18d723..b260424 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/java/java.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/java/java.md @@ -9,11 +9,13 @@ The Temporal Java SDK (`io.temporal:temporal-sdk`) uses an interface + implement **Add Dependencies:** Gradle: + ```groovy implementation 'io.temporal:temporal-sdk:1.+' ``` Maven: + ```xml io.temporal @@ -23,6 +25,7 @@ Maven: ``` **GreetActivities.java** - Activity interface: + ```java package greetingapp; @@ -38,6 +41,7 @@ public interface GreetActivities { ``` **GreetActivitiesImpl.java** - Activity implementation: + ```java package greetingapp; @@ -51,6 +55,7 @@ public class GreetActivitiesImpl implements GreetActivities { ``` **GreetingWorkflow.java** - Workflow interface: + ```java package greetingapp; @@ -66,6 +71,7 @@ public interface GreetingWorkflow { ``` **GreetingWorkflowImpl.java** - Workflow implementation: + ```java package greetingapp; @@ -91,6 +97,7 @@ public class GreetingWorkflowImpl implements GreetingWorkflow { ``` **GreetingWorker.java** - Worker setup: + ```java package greetingapp; @@ -127,6 +134,7 @@ public class GreetingWorker { **Start the worker:** Run `GreetingWorker.main()` (e.g., `./gradlew run` or `mvn compile exec:java -Dexec.mainClass="greetingapp.GreetingWorker"`). **Starter.java** - Start a workflow execution: + ```java package greetingapp; @@ -161,6 +169,7 @@ public class Starter { ## Key Concepts ### Workflow Definition + - Annotate interface with `@WorkflowInterface` - Put any state initialization logic in the workflow constructor to guarantee that it happens before signals/updates arrive. If your state initialization logic requires the workflow parameters, then add the `@WorkflowInit` decorator and parameters to your constructor. - Annotate entry point method with `@WorkflowMethod` (exactly one per interface) @@ -170,12 +179,14 @@ public class Starter { - Implementation class implements the interface ### Activity Definition + - Annotate interface with `@ActivityInterface` - Optionally annotate methods with `@ActivityMethod` (for custom names) - Implementation class can throw any exception - Call from workflow via `Workflow.newActivityStub()` ### Worker Setup + - `WorkflowServiceStubs` -- gRPC connection to Temporal Server - `WorkflowClient` -- client used by worker to communicate with server - `WorkerFactory` -- creates Worker instances @@ -201,6 +212,7 @@ greetingapp/ The Java SDK has **no sandbox**. The developer is fully responsible for writing deterministic workflow code. All non-deterministic operations must happen in Activities. **Do not use in workflow code:** + - `Thread` / `new Thread()` -- use `Workflow.newTimer()` or `Async.function()` - `synchronized` / `Lock` -- workflow code is single-threaded - `UUID.randomUUID()` -- use `Workflow.randomUUID()` @@ -210,7 +222,8 @@ The Java SDK has **no sandbox**. The developer is fully responsible for writing - `Thread.sleep()` -- use `Workflow.sleep()` - Mutable static fields -- workflow instances must not share state -**Use Workflow.* APIs instead:** +**Use `Workflow.*` APIs instead:** + - `Workflow.sleep()` for timers - `Workflow.currentTimeMillis()` for current time - `Workflow.randomUUID()` for UUIDs @@ -238,6 +251,7 @@ See `references/java/testing.md` for info on writing tests. ## Additional Resources ### Reference Files + - **`references/java/patterns.md`** - Signals, queries, child workflows, saga pattern, etc. - **`references/java/determinism.md`** - Determinism rules and safe alternatives for Java - **`references/java/gotchas.md`** - Java-specific mistakes and anti-patterns diff --git a/plugins/temporal-developer/skills/temporal-developer/references/java/observability.md b/plugins/temporal-developer/skills/temporal-developer/references/java/observability.md index d7d9528..338fcb7 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/java/observability.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/java/observability.md @@ -31,6 +31,7 @@ public class OrderWorkflowImpl implements OrderWorkflow { ``` The workflow logger automatically: + - Suppresses duplicate logs during replay - Includes workflow context (workflow ID, run ID, etc.) - Uses SLF4J under the hood diff --git a/plugins/temporal-developer/skills/temporal-developer/references/java/patterns.md b/plugins/temporal-developer/skills/temporal-developer/references/java/patterns.md index ed2fb37..e6428a9 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/java/patterns.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/java/patterns.md @@ -423,10 +423,12 @@ public class MyWorkflowImpl implements MyWorkflow { ## Activity Heartbeat Details ### WHY: + - **Support activity cancellation** — Cancellations are delivered via heartbeat; activities that don't heartbeat won't know they've been cancelled - **Resume progress after worker failure** — Heartbeat details persist across retries ### WHEN: + - **Cancellable activities** — Any activity that should respond to cancellation - **Long-running activities** — Track progress for resumability - **Checkpointing** — Save progress periodically diff --git a/plugins/temporal-developer/skills/temporal-developer/references/java/versioning.md b/plugins/temporal-developer/skills/temporal-developer/references/java/versioning.md index d1a9205..0e520f2 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/java/versioning.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/java/versioning.md @@ -38,6 +38,7 @@ public class ShippingWorkflowImpl implements ShippingWorkflow { ``` **How it works:** + - For new executions: returns `maxSupported` and records a marker in history - For replay with the marker: returns the recorded version - For replay without the marker: returns `DEFAULT_VERSION` (-1) diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/advanced-features.md b/plugins/temporal-developer/skills/temporal-developer/references/python/advanced-features.md index e0d3297..3d86e9f 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/advanced-features.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/advanced-features.md @@ -62,6 +62,7 @@ async def request_approval(request_id: str) -> None: # Later, complete the activity from another process async def complete_approval(request_id: str, approved: bool): client = await Client.connect("localhost:7233", namespace="default") + # Retrieve the task token from external storage (e.g., database) task_token = await get_task_token(request_id) handle = client.get_async_activity_handle(task_token=task_token) @@ -85,6 +86,7 @@ The Python SDK runs workflows in a sandbox to help you ensure determinism. You c **The Python SDK is NOT compatible with gevent.** Gevent's monkey patching modifies Python's asyncio event loop in ways that break the SDK's deterministic execution model. If your application uses gevent: + - You cannot run Temporal workers in the same process - Consider running workers in a separate process without gevent - Use a message queue or HTTP API to communicate between gevent and Temporal processes @@ -163,4 +165,3 @@ worker = Worker( workflow_failure_exception_types=[ValueError, CustomBusinessError], ) ``` - diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/data-handling.md b/plugins/temporal-developer/skills/temporal-developer/references/python/data-handling.md index 662101e..65f4a99 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/data-handling.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/data-handling.md @@ -7,6 +7,7 @@ The Python SDK uses data converters to serialize/deserialize workflow inputs, ou ## Default Data Converter The default converter handles: + - `None` - `bytes` (as binary) - Protobuf messages @@ -59,6 +60,7 @@ client = await Client.connect( ## Custom Data Conversion Usually the easiest way to do this is via implementing an EncodingPayloadConverter and CompositePayloadConverter. See: + - https://raw.githubusercontent.com/temporalio/samples-python/refs/heads/main/custom_converter/shared.py - https://raw.githubusercontent.com/temporalio/samples-python/refs/heads/main/custom_converter/starter.py diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/determinism-protection.md b/plugins/temporal-developer/skills/temporal-developer/references/python/determinism-protection.md index 1376ced..2eba418 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/determinism-protection.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/determinism-protection.md @@ -7,14 +7,15 @@ The Python SDK runs workflows in a sandbox that provides automatic protection ag ## How the Sandbox Works The sandbox: + - Isolates global state via `exec` compilation - Restricts non-deterministic library calls via proxy objects - Passes through standard library with restrictions - Reloads workflow files on each execution -## Forbidden Operations +## Forbidden Operations in Workflows -These operations will fail in the sandbox: +These operations are forbidden inside workflow code (appropriate in activities) and will fail in the sandbox: - **Direct I/O**: Network calls, file reads/writes - **Threading**: `threading` module operations @@ -35,6 +36,7 @@ with workflow.unsafe.imports_passed_through(): ``` **When to use pass-through:** + - Data classes and models (Pydantic, dataclasses) - Serialization libraries - Type definitions diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/determinism.md b/plugins/temporal-developer/skills/temporal-developer/references/python/determinism.md index e925f7c..2be8f75 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/determinism.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/determinism.md @@ -8,7 +8,9 @@ The Python SDK runs workflows in a sandbox that provides automatic protection ag Temporal provides durable execution through **History Replay**. When a Worker needs to restore workflow state (after a crash, cache eviction, or to continue after a long timer), it re-executes the workflow code from the beginning, which requires the workflow code to be **deterministic**. -## Forbidden Operations +## Forbidden Operations in Workflows + +The following are forbidden inside workflow code but are appropriate to use in activities. - Direct I/O (network, filesystem) - Threading operations @@ -34,6 +36,7 @@ Use the `Replayer` class to verify your code changes are compatible with existin ## Sandbox Behavior The sandbox: + - Isolates global state via `exec` compilation - Restricts non-deterministic library calls via proxy objects - Passes through standard library with restrictions diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/error-handling.md b/plugins/temporal-developer/skills/temporal-developer/references/python/error-handling.md index 19460cb..ed9e69d 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/error-handling.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/error-handling.md @@ -47,7 +47,7 @@ async def charge_card(input: ChargeCardInput) -> str: ```python from datetime import timedelta from temporalio import workflow -from temporalio.exceptions import ActivityError, ApplicationError +from temporalio.exceptions import ActivityError, ApplicationError, is_cancelled_exception @workflow.defn class MyWorkflow: @@ -59,6 +59,9 @@ class MyWorkflow: start_to_close_timeout=timedelta(minutes=5), ) except ActivityError as e: + # Let cancellation propagate so the workflow is canceled, not failed + if is_cancelled_exception(e): + raise workflow.logger.error(f"Activity failed: {e}") # Handle or re-raise raise ApplicationError("Workflow failed due to activity error") diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/gotchas.md b/plugins/temporal-developer/skills/temporal-developer/references/python/gotchas.md index 95ebe8a..a32b045 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/gotchas.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/gotchas.md @@ -211,10 +211,12 @@ class GoodWorkflow: ### Not Handling Activity Cancellation Activities must **opt in** to receive cancellation. This requires: + 1. **Heartbeating** - Cancellation is delivered via heartbeat 2. **Catching the cancellation exception** - Exception is raised when heartbeat detects cancellation **Cancellation exceptions:** + - Async activities: `asyncio.CancelledError` - Sync threaded activities: `temporalio.exceptions.CancelledError` diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/observability.md b/plugins/temporal-developer/skills/temporal-developer/references/python/observability.md index 26296c3..0130d89 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/observability.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/observability.md @@ -27,6 +27,7 @@ class MyWorkflow: ``` The workflow logger automatically: + - Suppresses duplicate logs during replay - Includes workflow context (workflow ID, run ID, etc.) @@ -46,6 +47,7 @@ async def process_order(order_id: str) -> str: ``` Activity logger includes: + - Activity ID, type, and task queue - Workflow ID and run ID - Attempt number (for retries) @@ -92,7 +94,6 @@ Runtime.set_default(runtime, error_if_already_set=True) - `temporal_activity_execution_latency` - Activity execution time - `temporal_workflow_task_replay_latency` - Replay duration - ## Search Attributes (Visibility) See the Search Attributes section of `references/python/data-handling.md` diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/patterns.md b/plugins/temporal-developer/skills/temporal-developer/references/python/patterns.md index 6843985..ae70757 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/patterns.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/patterns.md @@ -321,14 +321,17 @@ class MyWorkflow: ## Activity Heartbeat Details ### WHY: + - **Support activity cancellation** - Cancellations are delivered via heartbeat; activities that don't heartbeat won't know they've been cancelled - **Resume progress after worker failure** - Heartbeat details persist across retries **Cancellation exceptions:** + - Async activities: `asyncio.CancelledError` - Sync threaded activities: `temporalio.exceptions.CancelledError` ### WHEN: + - **Cancellable activities** - Any activity that should respond to cancellation - **Long-running activities** - Track progress for resumability - **Checkpointing** - Save progress periodically diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/python.md b/plugins/temporal-developer/skills/temporal-developer/references/python/python.md index 2c56843..bc0a0f3 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/python.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/python.md @@ -9,6 +9,7 @@ The Temporal Python SDK (`temporalio`) provides a fully async, type-safe approac **Add Dependency on Temporal:** In the package management system of the Python project you are working on, add a dependency on `temporalio`. **activities/greet.py** - Activity definitions (separate file for performance): + ```python from temporalio import activity @@ -18,6 +19,7 @@ def greet(name: str) -> str: ``` **workflows/greeting.py** - Workflow definition (import activities through sandbox): + ```python from datetime import timedelta from temporalio import workflow @@ -35,6 +37,7 @@ class GreetingWorkflow: ``` **worker.py** - Worker setup (imports activity and workflow, runs indefinitely and processes tasks): + ```python import asyncio import concurrent.futures @@ -70,6 +73,7 @@ if __name__ == "__main__": **Start the worker:** Start `python worker.py` in the background (appropriately adjust command for your project, like `uv run python worker.py`) **starter.py** - Start a workflow execution: + ```python import asyncio from temporalio.client import Client @@ -93,10 +97,10 @@ if __name__ == "__main__": **Run the workflow:** Run `python starter.py` (or uv run, etc.). Should output: `Result: Hello, my-name!`. - ## Key Concepts ### Workflow Definition + - Use `@workflow.defn` decorator on class - Put any state initialization logic in the `__init__` of your workflow class to guarantee that it happens before signals/updates arrive. If your state initialization logic requires the workflow parameters, then add the `@workflow.init` decorator and parameters to your `__init__`. - Use `@workflow.run` on the entry point method @@ -104,6 +108,7 @@ if __name__ == "__main__": - Use `@workflow.signal`, `@workflow.query`, `@workflow.update` for handlers ### Activity Definition + - Use `@activity.defn` decorator - Can be sync or async functions - **Default to sync activities** - safer and easier to debug @@ -113,6 +118,7 @@ if __name__ == "__main__": See `sync-vs-async.md` for detailed guidance on choosing between sync and async. ### Worker Setup + - Connect client, create Worker with workflows and activities - Run the worker - Activities can specify custom executor @@ -136,6 +142,7 @@ my_temporal_app/ ``` **In the Workflow file, import Activities through the sandbox:** + ```python # workflows/greeting.py from temporalio import workflow @@ -162,6 +169,7 @@ See `references/python/testing.md` for info on writing tests. ## Additional Resources ### Reference Files + - **`references/python/patterns.md`** - Signals, queries, child workflows, saga pattern, etc. - **`references/python/determinism.md`** - Sandbox behavior, safe alternatives, pass-through pattern, history replay - **`references/python/gotchas.md`** - Python-specific mistakes and anti-patterns diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/sync-vs-async.md b/plugins/temporal-developer/skills/temporal-developer/references/python/sync-vs-async.md index 7875582..247b0e5 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/sync-vs-async.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/sync-vs-async.md @@ -19,6 +19,7 @@ Activities should be synchronous by default. Use async only when certain the cod The Python async event loop runs in a single thread. When any task runs, no other tasks can execute until an `await` is reached. If code makes a blocking call (file I/O, synchronous HTTP, etc.), the entire event loop freezes. **Consequences of blocking the event loop:** + - Worker cannot communicate with Temporal Server - Workflow progress blocks across the worker - Potential deadlocks and unpredictable behavior @@ -73,6 +74,7 @@ async def my_async_activity(name: str) -> str: | `httpx` | Both | Yes (use async mode) | **Example: Wrong way (blocks event loop)** + ```python @activity.defn async def bad_activity(url: str) -> str: @@ -82,6 +84,7 @@ async def bad_activity(url: str) -> str: ``` **Example: Correct way (async-safe)** + ```python @activity.defn async def good_activity(url: str) -> str: @@ -150,6 +153,7 @@ For CPU-bound work and multi-core usage: ### Separate Workers for Workflows vs Activities Some teams deploy: + - Workflow-only workers (CPU-bound, need deadlock detection) - Activity-only workers (I/O-bound, may need more parallelism) diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/testing.md b/plugins/temporal-developer/skills/temporal-developer/references/python/testing.md index e4a7823..71a47b1 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/testing.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/testing.md @@ -140,7 +140,6 @@ async def test_replay(): ) ``` - ## Activity Testing ```python diff --git a/plugins/temporal-developer/skills/temporal-developer/references/python/versioning.md b/plugins/temporal-developer/skills/temporal-developer/references/python/versioning.md index abd4445..c1ad39a 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/python/versioning.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/python/versioning.md @@ -30,6 +30,7 @@ class ShippingWorkflow: ``` **How it works:** + - For new executions: `patched()` returns `True` and records a marker in the Workflow history - For replay with the marker: `patched()` returns `True` (history includes this patch) - For replay without the marker: `patched()` returns `False` (history predates this patch) @@ -213,6 +214,7 @@ worker = Worker( ``` **Configuration parameters:** + - `use_worker_versioning`: Enables Worker Versioning - `version`: Identifies the Worker Deployment Version (deployment name + build ID) - Build ID: Typically a git commit hash, version number, or timestamp @@ -224,13 +226,13 @@ worker = Worker( Workflows stay locked to their original Worker version: ```python -from temporalio.workflow import VersioningBehavior +from temporalio import workflow +from temporalio.common import VersioningBehavior -@workflow.defn +@workflow.defn(versioning_behavior=VersioningBehavior.PINNED) class StableWorkflow: @workflow.run async def run(self) -> str: - # This workflow will always run on its assigned version return await workflow.execute_activity( process_order, start_to_close_timeout=timedelta(minutes=5), @@ -238,6 +240,7 @@ class StableWorkflow: ``` **When to use PINNED:** + - Short-running workflows (minutes to hours) - Consistency is critical (e.g., financial transactions) - You want to eliminate version compatibility complexity @@ -247,7 +250,22 @@ class StableWorkflow: Workflows can move to newer versions: +```python +from temporalio import workflow +from temporalio.common import VersioningBehavior + +@workflow.defn(versioning_behavior=VersioningBehavior.AUTO_UPGRADE) +class UpgradableWorkflow: + @workflow.run + async def run(self) -> str: + return await workflow.execute_activity( + process_order, + start_to_close_timeout=timedelta(minutes=5), + ) +``` + **When to use AUTO_UPGRADE:** + - Long-running workflows (weeks or months) - Workflows need to benefit from bug fixes during execution - Migrating from traditional rolling deployments @@ -258,7 +276,6 @@ Workflows can move to newer versions: ### Worker Configuration with Default Behavior ```python -# For short-running workflows, prefer PINNED worker = Worker( client, task_queue="orders-task-queue", @@ -270,7 +287,7 @@ worker = Worker( build_id=os.environ["BUILD_ID"], ), use_worker_versioning=True, - # default_versioning_behavior=VersioningBehavior.PINNED, + default_versioning_behavior=VersioningBehavior.PINNED, ), ) ``` @@ -280,6 +297,7 @@ worker = Worker( **Blue-Green Deployments** Maintain two environments and switch traffic between them: + 1. Deploy new code to idle environment 2. Run tests and validation 3. Switch traffic to new environment @@ -288,6 +306,7 @@ Maintain two environments and switch traffic between them: **Rainbow Deployments** Multiple versions run simultaneously: + - New workflows use latest version - Existing workflows complete on their original version - Add new versions alongside existing ones diff --git a/plugins/temporal-developer/skills/temporal-developer/references/typescript/advanced-features.md b/plugins/temporal-developer/skills/temporal-developer/references/typescript/advanced-features.md index 17b7e61..ed9817d 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/typescript/advanced-features.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/typescript/advanced-features.md @@ -39,6 +39,7 @@ await handle.delete(); Complete an activity asynchronously from outside the activity function. Useful when the activity needs to wait for an external event. **In the activity - return the task token:** + ```typescript import { CompleteAsyncError, activityInfo } from '@temporalio/activity'; @@ -50,6 +51,7 @@ export async function doSomethingAsync(): Promise { ``` **External completion (from another process, machine, etc.):** + ```typescript import { Client } from '@temporalio/client'; @@ -61,6 +63,7 @@ async function doSomeWork(taskToken: Uint8Array): Promise { ``` **When to use:** + - Waiting for human approval - Waiting for external webhook callback - Long-polling external systems @@ -93,6 +96,7 @@ const worker = await Worker.create({ ``` **Key settings:** + - `maxConcurrentWorkflowTaskExecutions`: Max workflows running simultaneously (default: 40) - `maxConcurrentActivityTaskExecutions`: Max activities running simultaneously (default: 100) - `shutdownGraceTime`: Time to wait for in-progress work before forced shutdown diff --git a/plugins/temporal-developer/skills/temporal-developer/references/typescript/data-handling.md b/plugins/temporal-developer/skills/temporal-developer/references/typescript/data-handling.md index bfd4925..c8be6f8 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/typescript/data-handling.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/typescript/data-handling.md @@ -7,6 +7,7 @@ The TypeScript SDK uses data converters to serialize/deserialize workflow inputs ## Default Data Converter The default converter handles: + - `undefined` and `null` - `Uint8Array` (as binary) - JSON-serializable types diff --git a/plugins/temporal-developer/skills/temporal-developer/references/typescript/determinism-protection.md b/plugins/temporal-developer/skills/temporal-developer/references/typescript/determinism-protection.md index 54303ba..81c513a 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/typescript/determinism-protection.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/typescript/determinism-protection.md @@ -29,7 +29,6 @@ const worker = await Worker.create({ Use this with *extreme caution*. - ## Function Replacement Functions like `Math.random()`, `Date`, and `setTimeout()` are replaced by deterministic versions. diff --git a/plugins/temporal-developer/skills/temporal-developer/references/typescript/determinism.md b/plugins/temporal-developer/skills/temporal-developer/references/typescript/determinism.md index 47f8948..dfd3464 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/typescript/determinism.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/typescript/determinism.md @@ -28,7 +28,9 @@ The Temporal workflow sandbox will use the same random seed when replaying a wor See `references/typescript/determinism-protection.md` for more information about the sandbox. -## Forbidden Operations +## Forbidden Operations in Workflows + +The following are forbidden inside workflow code but are appropriate to use in activities. ```typescript // DO NOT do these in workflows: diff --git a/plugins/temporal-developer/skills/temporal-developer/references/typescript/gotchas.md b/plugins/temporal-developer/skills/temporal-developer/references/typescript/gotchas.md index d234f74..61763b3 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/typescript/gotchas.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/typescript/gotchas.md @@ -145,6 +145,7 @@ export async function workflowWithCleanup(): Promise { ### Not Handling Activity Cancellation Activities must **opt in** to receive cancellation. This requires: + 1. **Heartbeating** - Cancellation is delivered via heartbeat 2. **Checking for cancellation** - Either await `Context.current().cancelled` or use `cancellationSignal()` diff --git a/plugins/temporal-developer/skills/temporal-developer/references/typescript/patterns.md b/plugins/temporal-developer/skills/temporal-developer/references/typescript/patterns.md index 3d59e23..6dc2b32 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/typescript/patterns.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/typescript/patterns.md @@ -289,6 +289,7 @@ export async function scopedWorkflow(): Promise { **WHY**: Triggers provide a one-shot promise that resolves when a signal is received. Cleaner than condition() for single-value signals. **WHEN to use**: + - Waiting for a single response (approval, completion notification) - Converting signal-based events into awaitable promises @@ -351,10 +352,12 @@ export async function handlerAwareWorkflow(): Promise { ## Activity Heartbeat Details ### WHY: + - **Support activity cancellation** - Cancellations are delivered via heartbeat; activities that don't heartbeat won't know they've been cancelled - **Resume progress after worker failure** - Heartbeat details persist across retries ### WHEN: + - **Cancellable activities** - Any activity that should respond to cancellation - **Long-running activities** - Track progress for resumability - **Checkpointing** - Save progress periodically diff --git a/plugins/temporal-developer/skills/temporal-developer/references/typescript/typescript.md b/plugins/temporal-developer/skills/temporal-developer/references/typescript/typescript.md index 9918ee7..9e125cb 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/typescript/typescript.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/typescript/typescript.md @@ -13,13 +13,15 @@ Temporal workflows are durable through history replay. For details on how this w ## Quick Start **Add Dependencies:** Install the Temporal SDK packages (use the package manager appropriate for your project): + ```bash npm install @temporalio/client @temporalio/worker @temporalio/workflow @temporalio/activity ``` -Note: if you are working in production, it is strongly advised to use ~ version constraints, i.e. `npm install ... --save-prefix='~'` if using NPM. +Note: if you are working in production, it is strongly advised to use ~ version constraints, i.e. `npm install ... --save-prefix='~'` if using NPM. **activities.ts** - Activity definitions (separate file to distinguish workflow vs activity code): + ```typescript export async function greet(name: string): Promise { return `Hello, ${name}!`; @@ -27,6 +29,7 @@ export async function greet(name: string): Promise { ``` **workflows.ts** - Workflow definition (use type-only imports for activities): + ```typescript import { proxyActivities } from '@temporalio/workflow'; import type * as activities from './activities'; @@ -41,6 +44,7 @@ export async function greetingWorkflow(name: string): Promise { ``` **worker.ts** - Worker setup (imports activities and workflows, runs indefinitely): + ```typescript import { Worker } from '@temporalio/worker'; import * as activities from './activities'; @@ -62,6 +66,7 @@ run().catch(console.error); **Start the worker:** Run `npx ts-node worker.ts` in the background. **client.ts** - Start a workflow execution: + ```typescript import { Client } from '@temporalio/client'; import { greetingWorkflow } from './workflows'; @@ -87,16 +92,19 @@ run().catch(console.error); ## Key Concepts ### Workflow Definition + - Async functions exported from workflow file - Use `proxyActivities()` with type-only imports - Use `defineSignal()`, `defineQuery()`, `defineUpdate()`, `setHandler()` for handlers ### Activity Definition + - Regular async functions - Can perform I/O, network calls, etc. - Use `heartbeat()` for long operations ### Worker Setup + - Use `Worker.create()` with `workflowsPath` (dev) or `workflowBundle` (production) - see `references/typescript/gotchas.md` - Import activities directly (not via proxy) @@ -115,6 +123,7 @@ my_temporal_app/ ``` **In the Workflow file, use type-only imports for activities:** + ```typescript // workflows/greeting.ts import { proxyActivities } from '@temporalio/workflow'; @@ -130,11 +139,13 @@ const { translate } = proxyActivities({ The TypeScript SDK runs workflows in an isolated V8 sandbox. **Automatic replacements:** + - `Math.random()` → deterministic seeded PRNG - `Date.now()` → workflow start time - `setTimeout` → deterministic timer **Safe to use:** + - `sleep()` from `@temporalio/workflow` - `condition()` for waiting - Standard JavaScript operations @@ -160,6 +171,7 @@ See `references/typescript/testing.md` for info on writing tests. ## Additional Resources ### Reference Files + - **`references/typescript/patterns.md`** - Signals, queries, child workflows, saga pattern, etc. - **`references/typescript/determinism.md`** - Essentials of determinism in TypeScript - **`references/typescript/gotchas.md`** - TypeScript-specific mistakes and anti-patterns diff --git a/plugins/temporal-developer/skills/temporal-developer/references/typescript/versioning.md b/plugins/temporal-developer/skills/temporal-developer/references/typescript/versioning.md index a9f57a2..b4b8e19 100644 --- a/plugins/temporal-developer/skills/temporal-developer/references/typescript/versioning.md +++ b/plugins/temporal-developer/skills/temporal-developer/references/typescript/versioning.md @@ -25,6 +25,7 @@ export async function myWorkflow(): Promise { ``` **How it works:** + - If the Workflow is running for the first time, `patched()` returns `true` and inserts a marker into the Event History - During replay, if the history contains a marker with the same `patchId`, `patched()` returns `true` - During replay, if no matching marker exists, `patched()` returns `false` @@ -175,6 +176,7 @@ const worker = await Worker.create({ ``` **Configuration options:** + - `useWorkerVersioning`: Enables Worker Versioning - `version.deploymentName`: Logical name for your service (consistent across versions) - `version.buildId`: Unique identifier for this build @@ -195,6 +197,7 @@ const worker = await Worker.create({ ### When to Use Worker Versioning Worker Versioning is best suited for: + - **Short-running Workflows**: Old Workers only need to run briefly during deployment transitions - **Frequent deployments**: Eliminates the need for code-level patching on every change - **Blue-green deployments**: Run old and new versions simultaneously with traffic control