Skip to content

feat(automations): add foundation — types, storage, scheduler, leader election - #323745

Merged
Ben Villalobos (benvillalobos) merged 10 commits into
mainfrom
bv/automations-1-foundation
Jun 30, 2026
Merged

Ben Villalobos (benvillalobos) merged 10 commits into
mainfrom
bv/automations-1-foundation

Conversation

@benvillalobos

@benvillalobos Ben Villalobos (benvillalobos) commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

Automations PR 1 (Foundation)

Context

This is the first PR in a 4-part stack that adds Automations to VS Code, scheduled agentic tasks that run Copilot prompts against a workspace on a recurring cadence (manual, hourly, daily, weekly). Local-only for now.

This PR lays the infrastructure: types, persistent storage, a multi-window-safe scheduler with leader election, and the runner contract.

Gated behind chat.automations.enabled (machine-scoped).

Architecture

classDiagram
    class IAutomation {
        +id: string
        +name: string
        +prompt: string
        +schedule: IAutomationSchedule
        +enabled: boolean
        +folderUri: URI
        +nextRunAt?: Date
    }

    class AutomationService {
        +automations: IObservable~IAutomation[]~
        +runs: IObservable~IAutomationRun[]~
        +create() / update() / delete()
        +recordRunStart() / updateRun()
        -storage: IStorageService
    }

    class AutomationScheduler {
        -tick every 60s (leader only)
        +dispatchDue(trigger)
        +markStaleRunsFailed()
        -runOneWithTimeout()
    }

    class AutomationLeaderElection {
        +isLeader: IObservable~boolean~
        -heartbeat every 30s
        -stale threshold: 90s
        -TOCTOU nonce guard
    }

    class IAutomationRunner {
        +runOnce(automation, trigger, windowId, token)
    }

    AutomationScheduler --> AutomationLeaderElection : checks leadership
    AutomationScheduler --> AutomationService : reads due automations
    AutomationScheduler --> IAutomationRunner : dispatches runs
    AutomationService --> IStorageService : persists (APPLICATION scope)
Loading

Scheduler Flow

sequenceDiagram
    participant S as Scheduler (60s tick)
    participant L as LeaderElection
    participant AS as AutomationService
    participant R as Runner

    S->>L: isLeader?
    alt not leader
        S-->>S: skip tick
    else leader
        S->>AS: get enabled automations where nextRunAt ≤ now
        loop each due automation
            S->>AS: advanceNextRunAt(id) [preemptive bump]
            S->>R: runOnce(automation, trigger, token)
            R->>AS: recordRunStart → updateRun(running)
            R-->>R: send prompt via sessions layer
            R->>AS: updateRun(completed/failed)
        end
    end
Loading

Includes tests for schedule math (DST edge cases, all cadences).

Design Decisions

  • Multiple VS Code instances will elect a "leader" to be the one dispatching automated runs
  • On crash, recovery on leadership marks stale runs as failed and fires catch-up for overdue automations.
  • Default timeout of 30 minutes per run

…, leader election

Introduces the Automations subsystem core:
- IAutomation types and IAutomationSchedule (interval-based: manual/hourly/daily/weekly)
- DST-safe computeNextRunAt scheduling logic
- IAutomationService with persistent JSON ledger (StorageScope.APPLICATION)
- Optimistic concurrency via revision counter for cross-window safety
- Leader election to ensure single scheduler instance across windows
- AutomationScheduler: periodic tick loop checking due automations
- Feature gate: chat.automations.enabled setting mirrored to context key
- Unit tests for scheduling logic

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 30, 2026 16:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces the foundational infrastructure for Chat Automations (scheduled agentic tasks) across the workbench and sessions layers, including domain types, schedule computation, persistence, scheduling with leader election, telemetry hooks, and unit tests. It wires the feature behind chat.automations.enabled and sets up the core scheduler/leader election mechanics for multi-window safety.

Changes:

  • Added Automations domain model + schedule math (computeNextRunAt) with DST-aware local wall-clock semantics and tests.
  • Added service contracts (workbench) and sessions-layer implementation for persistent storage + run ledger, including optimistic revisioning.
  • Added sessions-layer scheduler + leader election and registered contributions/settings, plus unit tests for scheduler/election/service.
Show a summary per file
File Description
src/vs/workbench/contrib/chat/test/common/automations/schedule.test.ts Adds unit tests for schedule computation including DST edge cases.
src/vs/workbench/contrib/chat/common/automations/schedule.ts Implements DST-aware next-run computation and localized day-of-week labels.
src/vs/workbench/contrib/chat/common/automations/automationTelemetry.ts Defines GDPR-classified telemetry events for Automations lifecycle and runs.
src/vs/workbench/contrib/chat/common/automations/automationSessionTypes.ts Adds workbench-side bridge interface for enumerating session types per folder.
src/vs/workbench/contrib/chat/common/automations/automationService.ts Introduces the Automations service contract (CRUD + run ledger operations).
src/vs/workbench/contrib/chat/common/automations/automationsEnabled.ts Defines settings/constants + context key for feature gating and run timeout default.
src/vs/workbench/contrib/chat/common/automations/automationRunner.ts Adds runner contract (IAutomationRunner) for executing an automation run.
src/vs/workbench/contrib/chat/common/automations/automationDialogService.ts Adds workbench-to-sessions dialog service bridge interface for future UI.
src/vs/workbench/contrib/chat/common/automations/automation.ts Adds core domain types: automation, schedule, run status/trigger.
src/vs/sessions/sessions.common.main.ts Registers the Automations contribution in the sessions entrypoint.
src/vs/sessions/contrib/automations/test/browser/automationService.test.ts Adds unit tests for the sessions-layer AutomationService persistence/ledger semantics.
src/vs/sessions/contrib/automations/test/browser/automationScheduler.test.ts Adds scheduler-core tests (leadership, catch-up, timeout, toggle behavior).
src/vs/sessions/contrib/automations/test/browser/automationLeaderElection.test.ts Adds leader election tests (staleness, disposal, TOCTOU readback, error paths).
src/vs/sessions/contrib/automations/browser/automationService.ts Implements persistent storage-backed AutomationService with observables and revisioning.
src/vs/sessions/contrib/automations/browser/automationScheduler.ts Implements scheduler core with leader gating, crash recovery, and per-run timeout handling.
src/vs/sessions/contrib/automations/browser/automations.contribution.ts Registers DI singletons, settings, context-key mirroring, and a toggle command contribution.
src/vs/sessions/contrib/automations/browser/automationLeaderElection.ts Implements storage-backed multi-window leader election with heartbeat + TOCTOU nonce guard.

Review details

  • Files reviewed: 17/17 changed files
  • Comments generated: 6
  • Review effort level: Low

- Add _unsupportedSchema guard to recordRunStart and updateRun
- Localize CRASH_RECOVERY_REASON string via nls.localize()
- Move imports above registerSingleton call in contribution file
- Fix test comment referencing wrong helper name (buildLocalDate -> localDate)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 17/17 changed files
  • Comments generated: 5
  • Review effort level: Low

- Add _unsupportedSchema guard to markStaleRunsFailed and advanceNextRunAt
- Replace boolean telemetry flags with actual enum values (permissionLevel, isolationMode)
- Remove hasProviderId, hasSessionTypeId, hasModelId, hasMode, hasPermissionLevel flags
- Add scope: ConfigurationScope.MACHINE to runTimeoutMinutes setting
AutomationSchedulerCore (and its leader election heartbeat/storage
writes) are now only constructed when the feature setting is enabled.
Uses MutableDisposable for clean teardown on runtime toggle.

Addresses CCR feedback: avoid 30s heartbeat timer + storage writes
when the feature is disabled.
Wrap advanceNextRunAt + runOneWithTimeout in try/catch so a failure
in one automation does not abort dispatch of remaining due automations.
Adds vs/sessions/contrib/automations to the translation resources
so nls.localize calls pass the eslint translation-remind rule.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 17/17 changed files
  • Comments generated: 5
  • Review effort level: Low

- Normalize permissionLevel/isolationMode in createAutomation and
  mergeAutomation: reject unknown values to keep persisted data and
  GDPR-classified telemetry low-cardinality.
- Wrap persist() storage read and write in try/catch so storage
  failures degrade gracefully instead of breaking the scheduler.
…al dispatch

- Move scheduler registration from AfterRestored to Eventually —
  background concern that doesn't need to block workbench restore.
- Remove re-read-before-write in persist() — refreshFromStorage via
  onDidChangeValue already handles cross-window sync. Eliminates
  redundant JSON.parse on every mutation and the misleading optimistic
  concurrency check that overwrote anyway.
- Document that _pendingRuns serializes dispatch intentionally.
Replace all em dashes with periods or semicolons. Remove TOCTOU
acronym. Replace multiplication sign with plain x.
@benvillalobos
Ben Villalobos (benvillalobos) merged commit 041ae8b into main Jun 30, 2026
29 checks passed
@benvillalobos
Ben Villalobos (benvillalobos) deleted the bv/automations-1-foundation branch June 30, 2026 23:38
@vs-code-engineering vs-code-engineering Bot added this to the 1.128.0 milestone Jun 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants