Skip to content

Proposal: Let automation runs continue an existing session #572

Closed

Description

Tracked in #568. This proposal stands on its own and doesn't depend on the others tracked there.

Summary

Every automation run in AHP 1.0 creates a fresh session. Many valuable automations are long-lived instead: an agent that shepherds a pull request through CI failures, review comments, and merge, or a recurring "check on this" inside a conversation the user is already having. Hosts that route work into existing sessions today can't represent it over AHP.

This proposal lets a definition choose where each run's message goes: a new session (today's behavior), a specific existing chat, or the session the automation previously created for the same item, such as a pull request. Delivery reuses AHP's existing queued-message mechanics, and every message an automation delivers says which run delivered it.

Scenarios

  • "Check on this every hour." In the middle of a conversation, a user asks for an hourly check on a deployment. Each run should arrive in that conversation, which already has the context.
  • Pull request shepherd. One agent owns a pull request for its whole life. CI failures, review comments, and the final merge all go to the same session.
  • Issue to pull request. The session that picks up an issue opens a pull request and keeps owning both, so review feedback returns to the session that wrote the code.
  • Repository caretaker. One long-lived session receives every scheduled maintenance task for a repository.

AHP today

  • AutomationSessionTemplate is the "template from which the host creates a fresh session for each automation run," and AutomationDefinition.message is the "initial message sent to every newly created run session."
  • SessionMetadata.origin (AutomationSessionOrigin) records the single { automation, run } that created a session.
  • MessageOrigin carries only kind. MessageKind.Automation is described as "emitted automatically when an automation run starts a session."
  • Runs already link ordinary sessions (AutomationRunState.sessions, automationRun/sessionSet), and chats already hold input for later turns (ChatState.queuedMessages, chat/pendingMessageSet).

So a host can't say "this run went to that existing session," and a session that serves many runs can't say which turn belongs to which run.

Proposal

  1. Delivery target. AutomationDefinition gains an optional target:

    • newSession (the default, and today's behavior): create a session from definition.session for each run.
    • chat: deliver every run into one existing chat.
    • correlated: deliver runs that share a correlation value, such as the same pull request, into the session this automation previously created for that value, and create one from definition.session when there isn't one.
  2. The template applies only to sessions the run creates. When it delivers into an existing chat, the host doesn't apply definition.session: it doesn't change that session's provider, configuration, working directories, or customizations, and it doesn't install the automation's captured customizations there. A correlated session gets the full template, including captured customizations, when it's created. The message's own model and agent fields still apply where the chat's agent supports them.

  3. Correlation keys are host-defined and discovered. AutomationTriggerDefinition gains correlationKeys: the keys its events can carry (for example pullRequest, issue, or repository), each with a title. A correlated target lists key ids in priority order, and hosts validate them against discovery, just as they validate event ids.

  4. Resolving a correlated target. The host resolves the target for each run atomically:

    • Correlation values are opaque strings that the host derives from the run's input. They're scoped to the automation and the key.
    • The host uses the first listed key for which the input has a value. If none has a value, the run gets a new session with no correlation.
    • Runs with the same key and value resolve to the same session. When the first runs for a value arrive at the same time, the host creates only one session.
    • If the value's session is no longer usable (for example, it was removed or archived), the host creates a new session for the value, and later runs use the new one.
  5. Delivery uses existing chat mechanics. When the host delivers a run (it may first hold the run as pending, for example because of a host limit), it adds the run's message to the target chat as a queued pending message (chat/pendingMessageSet with kind queued). A run's message never steers an active turn. A busy chat starts it after the current turn, and an idle chat starts it right away, per the existing rule that hosts SHOULD consume a queued message immediately when the chat is idle. For correlated targets, the target chat is the session's defaultChat. Clients can reorder or withdraw a run's queued message like any other; hosts MAY reject other edits to it.

  6. The run records the work it owns. Before it queues the message, the host links the target session to the run with the existing automationRun/sessionSet, extending the existing rule that the host "records each session URI before sending its first message." The run also records each delivery: the chat, the id of the pending message it added, and, once the message is consumed, the id of the turn it started. Deliveries identify the run's own work in a session that may contain unrelated turns. Hosts can still link more sessions to a run for retries or parallel workers, as they can today.

  7. Run lifecycle when delivering into an existing chat.

    • The run is pending until its message is delivered and while it waits in the queue.
    • It becomes running when a turn starts from its message (chat/turnStarted with that pending message's id). As today, it stays running while that turn waits for input or confirmation.
    • With a single delivery, the run becomes completed, failed, or cancelled when that turn completes, errors, or is cancelled. A host that retries or adds workers ends the run when the last of the run's work ends, as it can today. If the run's turn ends with a resumable error, the host may resume the turn itself (for example, to retry a transient failure), or keep the run running while a person can still resume it. The run fails once the host decides not to resume, or once the turn can no longer be resumed.
    • If the run's queued message is removed without a turn starting from it (for example, a user withdraws it), the run is cancelled. Removal because a turn started from the message is consumption, not withdrawal.
    • Cancelling the run (automationRun/cancelRequested) removes its queued message, or cancels its turn if the turn has started. It never affects other work in the session.
    • If the target chat no longer exists, or the automation may no longer drive it, the run fails with an ErrorInfo.

    For the common single-delivery run, this matches how a run with a single fresh session already behaves: its outcome is the outcome of the turn its message opens.

  8. Attribution on messages. MessageOrigin gains optional references to the automation and run that delivered the message. Hosts set them on messages that automations deliver, whether into a fresh session or an existing one. They're optional, so older messages and hosts can omit them. SessionMetadata.origin keeps its meaning: what created the session. The description of MessageKind.Automation changes to "delivered by an automation run."

  9. Run correlation. For correlated targets, the run records the key id and value it was routed by, plus a display title, so run history can show "Run for pull request 123" and clients can find the session that handles a value.

  10. Manual override. runAutomation accepts an optional target that applies to that run only: a specific chat, a new session, or, for correlated automations, a specific correlation key and value ("run for pull request 123 in its session"). Clients learn values from earlier runs' correlation records. Retrying with the same requestId but a different target MUST be rejected.

  11. Authorization. An automation can deliver into a chat only if its principal may drive that chat. Hosts check this when the target is saved and again each time a run delivers.

  12. Capability. automations.targets lists the target kinds the host supports. Clients MUST NOT author other kinds.

Sketch

Names and shapes are illustrative. The implementation PR settles them.

interface AutomationDefinition {
  // ...existing fields
  /** Where each run's message is delivered. Omitted means a new session per run. */
  target?: AutomationTarget;
}

/** @nonexhaustive */
const enum AutomationTargetKind {
  NewSession = 'newSession',
  Chat = 'chat',
  Correlated = 'correlated',
}

type AutomationTarget =
  | { kind: AutomationTargetKind.NewSession }
  | { kind: AutomationTargetKind.Chat; chat: URI }
  /** Correlation key ids from trigger discovery, in priority order. */
  | { kind: AutomationTargetKind.Correlated; keys: string[] };

interface AutomationTriggerDefinition {
  // ...existing fields
  correlationKeys?: { id: string; title: string; description?: string }[];
}

interface AutomationRunState {
  // ...existing fields
  /** The correlation value this run was routed by. */
  correlation?: { key: string; value: string; title?: string };
  /** The run's own messages and turns. Upserted by id with a server-only Set action. */
  deliveries?: AutomationRunDelivery[];
}

interface AutomationRunDelivery {
  id: string;
  chat: URI;
  /** The queued PendingMessage the run added, when it went through the queue. */
  pendingMessageId?: string;
  /** The turn started from the run's message, once consumed. */
  turnId?: string;
}

interface MessageOrigin {
  kind: MessageKind;
  /** Set only when kind is 'automation': the delivering automation and run. */
  automation?: URI;
  run?: URI;
}

interface RunAutomationParams {
  // ...existing fields
  /** Overrides definition.target for this run only. */
  target?:
    | { kind: AutomationTargetKind.NewSession }
    | { kind: AutomationTargetKind.Chat; chat: URI }
    | { kind: AutomationTargetKind.Correlated; key: string; value: string };
}

interface AutomationCapabilities {
  // ...existing fields
  targets?: { kinds: AutomationTargetKind[] };
}

Why this shape

  • Runs already link ordinary sessions. Session and chat channels stay authoritative for transcripts, tools, and confirmations. This proposal only changes which session a run links and how its message gets there.
  • Queued messages already mean "wait for the active turn, then start." Reusing them makes delivery visible and controllable with existing UI. Users can see a run waiting in the queue and withdraw it.
  • Attribution belongs on the message. MessageOrigin.kind already says an automation produced a message. A session's origin can describe only one creator, so it can't describe the many runs that a long-lived session serves.
  • Deliveries make ownership explicit. A long-lived session mixes a run's turn with other turns. Recording the pending message and turn a run owns lets lifecycle, cancellation, and navigation target exactly that work, without redefining what it means for a run to link a session.
  • Correlation keys follow the event-trigger pattern. Their meaning is host-defined, their ids are stable, and they're discovered with listAutomationTriggerDefinitions, so clients can offer "one session per pull request" without knowing any particular event source.

Compatibility

  • Additive: an optional definition field, optional fields on trigger definitions, run state, and MessageOrigin, and capability-gated authoring. Adding optional fields to MessageOrigin is compatible on the Stable chat channel.
  • Clients that don't understand target still render everything correctly. Runs link sessions as they do today, and messages still carry kind: automation.
  • Patches replace only the fields they name, so a client that doesn't know target won't clear it when it edits other fields.

Out of scope

  • How ownership of a correlation value moves between sessions (for example, when an issue's session opens a pull request and takes ownership of it). This is host-defined, and it's observable through each run's linked sessions and correlation.
  • Routing different values to different chats within one shared session.
  • Agent-to-agent coordination.

Open questions

  1. Should a fixed target be a chat (proposed, because chats can move between sessions) or a session that resolves to its defaultChat?
  2. For correlated, should each value get its own session (proposed) or its own chat within one session?
  3. What makes a correlated session "no longer usable"? Should users be able to release a value so that the next run starts fresh?
  4. Does MessageOrigin need both automation and run, or is run enough now that runs record their deliveries?
  5. Should deliveries also be recorded for runs that create fresh sessions, so every run links directly to its first turn?

Related proposals (none required)

Activity

  1. added
    feature-request
    on Nov 6, 2026
  2. connor4312 commented on Nov 9, 2026

    @connor4312
    Member

    Why do you propose that the notion of 'correlation' and its keys be delivered to the client? Should those be equally well-served with just a toggle between hinting to the agent host whether an automation should use a new chat vs an existing chat and the notion of per-message origin details as described in #569 (comment)?

  3. benvillalobos commented on Nov 13, 2026

    @benvillalobos
    Member

    There's a want for this on the vscode team, but our use case is much simpler at the moment: Allow an automation reuse the same run (minimal metadata).

    We have a PR (cc Federico Brancasi (@federicobrancasi)) that attempts to accomplish this, adding a single boolean. I think we can expand this to a simpler shape that can scale into the more complex case mentioned in this proposal. Thoughts?

    interface AutomationDefinition {
      // ...existing fields
      /** Where runs go. Omitted means a new session per run. */
      target?: AutomationTarget;
    }
    
    /** Initially supported target kinds. */
    type AutomationTarget =
      | { kind: 'newSession' }
      /** Reuse the previous run's session if usable; otherwise create one. */
      | { kind: 'previousSession' };
    
    interface AutomationDefinitionPatch {
      // ...existing fields
      /** Omitted leaves unchanged; use newSession to reset. */
      target?: AutomationTarget;
    }
    
    interface AutomationCapabilities {
      // ...existing fields
      /** Clients may author only advertised target kinds. */
      targets?: {
        kinds: AutomationTarget['newSession', 'previousSession'][];
      };
    }
    

    Eventually we'd get:

    type AutomationTarget =
      | { kind: 'newSession' }
      | { kind: 'previousSession' }
      | { kind: 'chat'; chat: URI }
      | { kind: 'correlated'; keys: string[] };
    
  4. moved this to Under Discussion in AHP 1.2.0on Nov 13, 2026
  5. moved this from Under Discussion to In progress in AHP 1.2.0on Nov 13, 2026
  6. connor4312 commented on Nov 17, 2026

    @connor4312
    Member

    This is now implemented for general reuse signals, let me know if you have future needs around correlation.

  7. moved this from In progress to Done in AHP 1.2.0on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-request

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions