← All courses ← Course home

Lesson 05 of 05

Ship a tiny workflow engine

One actor loads, runs one step, and saves. That is the durable workflow you can ship on iOS 27 or macOS 27.

Agent brief (llms.md)

The engine is one actor. It loads or creates a checkpoint, runs one step at a time, writes the file, and exposes status to SwiftUI. It is not a general workflow product. It is enough for an on-device agent that must not start over.

start or resumeLoad file
runNextOne step
persistAndPauseWrite and stop
@MainActor
final class WorkflowEngine {
    static let shared = WorkflowEngine()
    private let store: CheckpointStore
    private var session: LanguageModelSession?

    init(store: CheckpointStore = CheckpointStore.documents()) {
        self.store = store
    }

    func persistAndPause() async {
        guard let session, !session.isResponding else { return }
        if var checkpoint = try? await store.load() {
            checkpoint.transcriptData = try? encodeTranscript(session.transcript)
            checkpoint.updatedAt = .now
            try? await store.save(checkpoint)
        }
    }

    func resumeFromDisk() async throws -> WorkflowCheckpoint {
        try await resume(store: store)
    }
}

Checklist

  1. Write a Codable checkpoint with step, outputs, and optional transcript data.
  2. Write the file after every successful step, atomically, in the app container.
  3. On launch, create a new session and restore. Do not search memory for the old object.
  4. Skip steps that already have outputs.
  5. On scene leave, pause and save. Use preserveTranscript only if you will repair the transcript.
  6. Register a background processing task on iOS as a best-effort resume. Tell the user they may need to reopen the app.
  7. Redact before you persist. Keep side-effect steps behind a consensus gate.

Primary sources: Foundation Models, WWDC26 session 242, and Background Tasks.

Related courses: Make several agents agree first, Talk to tools with MCP, and Stop prompt attacks and leaks.

Short form. Save each step. Restore a new session. iOS may help in the background. It will not finish the job for you.

Key concepts

  • One actor runs one step at a time, then writes the file.
  • Persist and pause when the session is not responding.
  • iOS needs the Background Tasks capability and permitted identifiers.
  • Side-effect steps still need a consensus gate.

Takeaways

  • Save each step, restore a new session, and tell stakeholders the user may need to reopen the app.
  • Background Tasks may help on iOS. They will not finish the job for you.
  • Redact before persist.