feat(workspace): stop a Session's running work before archiving it (#4765)

* feat(workspace): stop a Session's running work before archiving it

Archiving was a pure visibility write, so a Session archived mid-turn kept
running out of sight. The Workspace registry now owns archive admission
through two Host events: `workspace/session-activity` reports what still
runs and refuses the plain archive as `workspace/session-active`;
`workspace/session-stop`, requested with `stopActivity`, stops that work the
way the user's own stop actions do before the archive write. The API Session
Controller reports and stops turns, subagent descendants, and owned jobs and
rejects any model step for an archived Session; the Schedule plugin reports
and deletes active reminders. The sidebar turns the refusal into a
stop-and-archive confirmation listing the Host's answer.

* test(workspace): pin the widened archive signature and prove child liveness by heartbeat

The Client inspect catalog spec pins archiveSession's new options
parameter, and the archive e2e reads the Bash child's liveness from a
heartbeat file instead of a pid: the Linux sandbox runs the tool in its own
PID namespace, so the pid it reports is not comparable from the test.

* test(workspace): cover the archive-admission providers and dialog to the per-file gate

Drop the provider's unused disposer and the dialog's unreachable confirm
guard, read reminders without a catch that no composition can reach, and
add the cases the coverage lane found missing: a stop without a jobs
registry, a looping lineage, a descendant whose projections reject, the
dialog's Escape guard and non-Error reason, and a family merged by another
provider.

* fix(workspace): write the archive before stopping, gate subagent lineage, and delete reminders under the schedule barriers

Review round on #4765. The registry now writes the archive set before it
dispatches `workspace/session-stop`, so the durable set already gates
every wake the stops induce; the controller's `agent/pre-step` gate also
blocks subagent descendants of an archived Session and contains each
cancel or kill on its own. The Schedule plugin's stop listener runs as a
management delete in the agent's transaction queue between the same two
persistence barriers the `schedule_delete` tool uses. The confirm dialog
selects singular or plural English lines and keys list rows by index.
Docs: archive-admission section with type-equiv blocks on the workspace
subsystem page, write-first order and residual windows in the READMEs
and the Agent Note, and the missing zh reminders sentence.

* test(web): scope the question composer's New Session wait to the Sessions tree

The wide sidebar's New Session button carries the same visible label as
a fresh session row, so the unscoped `getByText` either settled on the
button before the row existed or hit a strict-mode violation once the
row rendered first, which is how the snapshots lane failed on
ece68c2ab5. Waiting inside the Sessions tree names the row alone.

* fix(schedule): answer archive admission from the owned runtime's fold, not the projection registry

The `sessionProjections.snapshot` API is the Client projection view and
is not a Host read path (review on #4765). `ScheduleRuntime.activeRecords()`
folds the live log the way every drive does, and the archive-admission
listeners ask the owned runtime for that fold: a session without a live
agent or without an owned runtime has nothing armed to fire, so it reports
nothing and has nothing to stop. Covers the runtime-less agent, a fold that
turns unreadable at the persistence barrier, and the stopping/faulted
runtime answering nothing.

* refactor(subagent): report and stop subagent descendants from the Subagent runtime

The `subagent` family of archive admission moves out of the API Session
Controller into `dsh-subagent`, which records the lineage at delegation
and owns the descriptor (review on #4765): `SubagentRuntime` answers
`workspace/session-activity` with the running descendants, labelled from
each child's descriptor through a live Session observation, and
`workspace/session-stop` by cancelling each of them with the parent
cause. The controller keeps turns, owned jobs, and the lineage step gate,
and composes its provider through `ctx.plugin(SessionActivityProvider)`
with declared injections instead of a bare installer call. `dsh-workspace`
becomes an optional type-only peer of `dsh-subagent`.

* refactor(jobs): answer archive admission for owned jobs from the job registry seam

The `job` family moves out of the API Session Controller into `dsh-jobs`
(review on #4765): the seam's constructor installs the
`workspace/session-activity` and `workspace/session-stop` listeners for
every implementation through the abstract `list` and `kill` alone, so the
controller no longer knows the jobs vocabulary and keeps only the turn
family and the archived-lineage step gate. `dsh-workspace` becomes an
optional type-only peer of `dsh-jobs`.

* refactor(workspace): leave SessionActivityKindMap empty and let each provider merge its family

The workspace package no longer names any activity family (review on
#4765): `SessionActivityKindMap` is empty, and each provider merges its own
key from a module both its faces import — `turn` in the API Session
Controller's types, `job` in the job registry seam's view, `subagent` in
the Subagent runtime's control types, `schedule` in the Schedule plugin's
types. The confirm dialog imports those modules for its cases and keeps
the generic line for any other key; the registry and controller suites
merge test-owned families instead of borrowing the providers'.

* refactor(agent): report and stop the running turn from the Agent registry

The `turn` family moves out of the API Session Controller into `dsh-agent`
(review on #4765): the registry's constructor installs the
`workspace/session-activity` and `workspace/session-stop` listeners for
every Agent it publishes, answering with its own lookup and cancelling the
turn with the user cause, and merges the `turn` key from its client-safe
types. The controller keeps only the archived-lineage step gate, renamed
`ArchivedSessionGate`. `dsh-workspace` becomes an optional type-only peer
of `dsh-agent`; the workspace package depends on none of the providers,
so the edge stays acyclic.

Since #3127 a foreground Bash command is a registered job from its start,
so the archive dialog lists it beside the turn; the e2e and its ARIA golden
expect that row, and the capture keeps the running job by design.
This commit is contained in:
Yichen Jiang authored and GitHub committed 2026-09-21 11:46:13 +00:00
1 parent f65c15a5cf
commit cbae324bfa
109 files changed
+2763 -300

No files matched your search

@@ -15,9 +15,12 @@ export { ClientWorkspaceModel } from './model.ts'
export type {
WorkspaceFollowSink, WorkspaceListPhase, WorkspaceRemote, WorkspaceSnapshot,
} from './model.ts'
export { WorkspaceController, WorkspaceCreateError } from './service.ts'
export { WorkspaceArchiveError, WorkspaceController, WorkspaceCreateError } from './service.ts'
export type { IWorkspaces, WorkspaceSource } from './service.ts'
export type { WorkspaceId, WorkspaceInitializeDefaultRequest, WorkspaceView } from '../types.ts'
export type {
SessionActivity, SessionActivityItem, SessionActivityKind, SessionActivityKindMap, WorkspaceId,
WorkspaceInitializeDefaultRequest, WorkspaceView,
} from '../types.ts'
type WorkspaceBaselineFrame = Extract<WorkspaceFollowFrame, { type: 'baseline' }>
@@ -189,13 +189,18 @@ export class ClientWorkspaceModel implements WorkspaceFollowSink {
* Archive one Session and install the returned complete archive set.
* A reply superseded by a later archive request or a pushed set installs nothing.
* @param sessionId - Session to archive.
* @param options - Whether the Host stops the Session's running work instead of refusing.
* @returns generated Remote result.
*/
async archiveSession(
sessionId: WorkspaceArchiveSessionRequest['sessionId'],
options: Pick<WorkspaceArchiveSessionRequest, 'stopActivity'> = {},
): Promise<RemoteResult<WorkspaceArchiveValue>> {
const requestSeq = ++this.archiveRequestSeq
const result = await this.remote.archiveSession({ sessionId })
const result = await this.remote.archiveSession({
sessionId,
...(options.stopActivity === true ? { stopActivity: true } : {}),
})
if (result.ok && requestSeq === this.archiveRequestSeq) {
this.installArchived(result.value.archivedSessionIds)
// The Host drops an archived session's pin in the same durable write;
@@ -17,6 +17,20 @@ export class WorkspaceCreateError extends Error {
}
}
/**
* Archive failed on the Host. `rpcError.code` distinguishes the active-session
* refusal (`workspace/session-active`, whose details name what still runs)
* from a missing session or a carrier fault.
*/
export class WorkspaceArchiveError extends Error {
override readonly name = 'WorkspaceArchiveError'
/** @param rpcError - Host business or folded carrier failure. */
constructor(readonly rpcError: RemoteFailure) {
super(`workspace session archive failed: ${rpcError.code}: ${rpcError.message}`)
}
}
/** Bare observable source for the Workspace Controller snapshot. */
export interface WorkspaceSource {
/** Read the identity-stable current snapshot. */
@@ -67,8 +81,11 @@ export interface IWorkspaces {
/**
* Archive a Session from Workspace grouping surfaces.
* @param sessionId - Session to archive.
* @param options - `stopActivity` asks the Host to stop the Session's running work instead of refusing.
* @throws {WorkspaceArchiveError} when the Host refuses; without `stopActivity` a Session with
* running work fails as `workspace/session-active`, its details naming what runs.
*/
archiveSession(sessionId: SessionId): Promise<void>
archiveSession(sessionId: SessionId, options?: { readonly stopActivity?: boolean }): Promise<void>
/**
* Unarchive a Session from the archived Session list.
* @param sessionId - Session to unarchive.
@@ -139,9 +156,9 @@ export class WorkspaceController extends Service implements IWorkspaces {
if (!result.ok) throw commandError('reorder', result.error)
}
async archiveSession(sessionId: SessionId): Promise<void> {
const result = await this.model.archiveSession(sessionId)
if (!result.ok) throw commandError('session archive', result.error)
async archiveSession(sessionId: SessionId, options: { readonly stopActivity?: boolean } = {}): Promise<void> {
const result = await this.model.archiveSession(sessionId, options)
if (!result.ok) throw new WorkspaceArchiveError(result.error)
}
async unarchiveSession(sessionId: SessionId): Promise<void> {
@@ -3,6 +3,7 @@
import type { Context } from '@deepseek-ai/cordis'
import type { Workspace } from '@deepseek-ai/dsh-workspace'
import {
WorkspaceActiveSessionError,
WorkspaceArchivedSessionPinError,
WorkspaceId,
WorkspaceMoveInvalidError,
@@ -151,16 +152,32 @@ export class WorkspaceCommands {
}
/**
* Add one known Session to the registry-global archive set.
* @param request - Session identity to archive.
* Add one known Session to the registry-global archive set. Without
* `stopActivity` a Session with running work is refused as
* `workspace/session-active` with the activity the registry's providers
* reported; with it, the providers stop that work first.
* @param request - Session identity to archive and whether to stop its work.
* @returns the complete resulting archive set.
*/
async archiveSession(request: WorkspaceArchiveSessionRequest): Promise<WorkspaceArchiveValue> {
try {
await this.ctx.workspaceRegistry.archiveSession(request.sessionId)
await this.ctx.workspaceRegistry.archiveSession(
request.sessionId,
request.stopActivity === true ? { stopActivity: true } : {},
)
} catch (error) {
if (!(error instanceof WorkspaceUnknownSessionError)) throw error
throw new RemoteError('session/not-found', error.message, { sessionId: request.sessionId }, { cause: error })
if (error instanceof WorkspaceUnknownSessionError) {
throw new RemoteError('session/not-found', error.message, { sessionId: request.sessionId }, { cause: error })
}
if (error instanceof WorkspaceActiveSessionError) {
throw new RemoteError(
'workspace/session-active',
error.message,
{ sessionId: request.sessionId, activity: error.activity },
{ cause: error },
)
}
throw error
}
return { archivedSessionIds: [...this.ctx.workspaceRegistry.archivedSessionIds] }
}
+21 -1
View File
@@ -6,9 +6,12 @@
*/
import type { SessionId } from '@deepseek-ai/dsh-session/types'
import type { WorkspaceId } from '@deepseek-ai/dsh-workspace/types'
import type { SessionActivity, WorkspaceId } from '@deepseek-ai/dsh-workspace/types'
export type { WorkspaceId } from '@deepseek-ai/dsh-workspace/types'
export type {
SessionActivity, SessionActivityItem, SessionActivityKind, SessionActivityKindMap,
} from '@deepseek-ai/dsh-workspace/types'
export type { DirectoryEntry, DirectoryListing } from '@deepseek-ai/dsh-host-directory-picker/types'
/** One durable Workspace projected for browser consumers. */
@@ -32,6 +35,15 @@ declare module '@deepseek-ai/dsh-typert-protocol' {
'workspace/invalid-path': { readonly path: string }
/** Another Workspace already uses the requested name. */
'workspace/name-conflict': { readonly name: string }
/**
* The Session still has running work — its own turn, a subagent, a
* background job, or an active schedule — so archiving was refused
* without a write; `activity` names what must stop first.
*/
'workspace/session-active': {
readonly sessionId: SessionId
readonly activity: readonly SessionActivity[]
}
/** The Session or its anchor is not in the Workspace's manual order. */
'workspace/move-invalid': {
readonly workspaceId: WorkspaceId
@@ -110,6 +122,14 @@ export interface WorkspaceInsertSessionBeforeRequest {
/** Session requested for archival from Workspace grouping surfaces. */
export interface WorkspaceArchiveSessionRequest {
readonly sessionId: SessionId
/**
* Stop the Session's running work — its turn, subagent descendants, owned
* background jobs, and active schedules — instead of refusing the archive
* as `workspace/session-active`. The stops are requested before the
* archive write and are not awaited; the response arrives once the archive
* set is durable.
*/
readonly stopActivity?: boolean
}
/** Session requested for restoration from the archived Session list. */