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
- Write a Codable checkpoint with step, outputs, and optional transcript data.
- Write the file after every successful step, atomically, in the app container.
- On launch, create a new session and restore. Do not search memory for the old object.
- Skip steps that already have outputs.
- On scene leave, pause and save. Use
preserveTranscriptonly if you will repair the transcript. - Register a background processing task on iOS as a best-effort resume. Tell the user they may need to reopen the app.
- 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.