Member Junction
    Preparing search index...

    Durable MJ: Tasks-backed implementation of IA2ATaskStore (WS-4.5b).

    Combines the in-process L1 cache and SSE subscriber notifications of TaskStore with durable persistence through MJTaskEntity ('MJ: Tasks'). Enables horizontal scaling (multiple A2A server replicas behind a load balancer) and zero-data-loss rolling restarts: any replica can serve GET /a2a/tasks/:taskId, continue a multi-turn task on POST /a2a/tasks/send, or cancel a task on POST /a2a/tasks/:taskId/cancel.

    Only A2A rows. MJ: Tasks also holds task-graph steps, conversation tasks and people's to-dos, and a caller who owns one of those knows its ID. The store reads and writes a row only when it is filed under the A2A_TASK_TYPE_NAME type, carries the A2A_TASK_PROTOCOL payloads and belongs to the caller; any other row is "not found". Without that, tasks/send on a task-graph step's ID overwrote its payloads (its envelope, failure policy and continuation) and status, and could complete a pending Human step.

    Concurrent writers. Two replicas can save the same task at once (two sends, or a cancel and the owning replica's completion). Each save re-reads the row and merges (MergeStoredA2ATask), checks __mj_UpdatedAt has not moved before writing, and reads the row back afterwards; a row that moved, or that came back without what this save wrote, is merged and written again. BaseEntity.Save() has no conditional update, so this narrows the lost-update window to the gap between the check and the write rather than closing it; the read-back makes the losing writer repair it.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    _abortControllers: UUIDMap<AbortController> = ...
    _listeners: UUIDMap<Set<(task: Task) => void>> = ...
    _owners: UUIDMap<string> = ...

    The ID of the user each task was created for. Kept beside the task rather than on it, because the task is the wire shape returned to clients.

    _tasks: UUIDMap<Task> = ...

    Accessors

    • get PersistenceEnabled(): boolean

      Whether durable MJ: Tasks persistence is enabled on this store instance.

      Returns boolean

    • get ShutdownName(): string

      Optional human-readable identifier surfaced in shutdown logs and used for de-duplication when Register is called repeatedly with the same instance but no name. Falls back to the constructor name.

      Returns string

    Methods

    • Cancel an in-flight task, aborting its registered AbortController (if any) and notifying any active SSE subscribers immediately.

      Parameters

      • taskId: string
      • reason: string = 'Task cancelled via A2A'

      Returns boolean

    • Cancels a task owned by contextUser across both local in-flight execution and durable MJ: Tasks storage. In durable mode, the replica running the task polls stored status (default 5 s, WatchCancellation) and aborts its local work when it observes Cancelled, or stops on its next save via MergeStoredA2ATask. Returns false when the task is not the caller's, or the cancellation could not be saved.

      Parameters

      Returns Promise<boolean>

    • The task with this ID when it belongs to userId, otherwise undefined. A task that belongs to someone else is indistinguishable from one that does not exist, so a caller cannot learn another user's task IDs, nor read, continue or cancel their tasks.

      Parameters

      • id: string
      • userId: string

      Returns Task

    • Loads an A2A Task for contextUser, consulting MJ: Tasks when persistence is enabled so cross-replica updates/cancellations are observed immediately, and falling back to the local L1 cache when persistence is disabled or the row does not exist yet. A row that is not the caller's A2A task reads as missing.

      Parameters

      Returns Promise<Task>

    • Register an AbortController for an in-flight task so CancelTask can propagate cancellation down to AgentOperations / AgentRunner.

      Parameters

      • taskId: string
      • controller: AbortController = ...

      Returns AbortController

    • Persists an A2A Task into both the local L1 cache and the MJ: Tasks entity. Returns false when the task could not be written — no authenticated owner, someone else's task, a row that is not an A2A task, a failed save, or a row too contended to settle — so the caller can report it rather than carry on with a task that exists only in this process's memory.

      Parameters

      Returns Promise<boolean>

    • Stores a task.

      Parameters

      • id: string
      • task: Task
      • OptionalownerUserId: string

        The user the task was created for. Only that user can retrieve it through GetForUser; a task stored without an owner is retrievable by no one. Omit it when replacing a stored task, to keep its owner.

      Returns this

    • Begin the periodic sweep. Idempotent; calling twice is safe. Uses unref() so the sweep timer doesn't hold the process alive on its own.

      Returns void

    • Subscribe to event-driven updates for a specific task ID (replaces 500ms polling). Returns an unsubscribe function.

      Parameters

      • taskId: string
      • listener: (task: Task) => void

      Returns () => void

    • In durable mode, subscribes to task updates across replicas by combining in-process events with periodic polling of the stored row in MJ: Tasks (S10).

      Polling stops when the task reaches a terminal status, or when its stored row has not changed for staleAfterMs (a task orphaned by a replica that died stays in_progress forever); onStale is then called so the caller can end its stream and say why. A task this replica is still running (it holds a live abort controller for it) is never stale.

      Parameters

      • taskId: string
      • contextUser: UserInfo
      • provider: IMetadataProvider
      • listener: (task: Task) => void
      • intervalMs: number = 5000
      • OptionalonStale: () => void
      • staleAfterMs: number = RESUBSCRIBE_STALE_AFTER_MS

      Returns () => void

    • Drop terminal-state tasks that haven't been updated in retentionMs. Returns the count evicted (useful for tests and diagnostics).

      Parameters

      • now: number = ...

      Returns number

    • In durable mode, periodically checks the stored status of an in-flight task while this replica is running it. When stored status is observed as 'cancelled' (e.g. cancelled on another replica), aborts local work via the registered AbortController (S10).

      Parameters

      Returns () => void

      A cleanup function to stop polling when the task finishes locally.