@deepseek-ai/dsh-api-terminal-controller 0.1.6-alpha.1 → 0.1.6-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ import type { SubprocessTerminalActivity } from '@deepseek-ai/dsh-subprocess';
2
+ import type { TerminalRetentionFrame } from './types.ts';
3
+ /** Validated Host timing policy for unattended terminal cleanup. */
4
+ export interface TerminalRetentionPolicy {
5
+ readonly unattendedTimeoutMs: number;
6
+ readonly activityPollIntervalMs: number;
7
+ readonly cleanupRetryMs: number;
8
+ }
9
+ /** Exactly one owner orders holds, observation, and retryable process cleanup. */
10
+ export declare class TerminalRetention {
11
+ private readonly policy;
12
+ private readonly inspect;
13
+ private readonly terminate;
14
+ private readonly failed;
15
+ private readonly lifetime;
16
+ private readonly holders;
17
+ private epoch;
18
+ private timer;
19
+ private observation;
20
+ private idle;
21
+ private closing;
22
+ private disposed;
23
+ private cleanup;
24
+ /**
25
+ * @param policy - deployment timing choices.
26
+ * @param inspect - fresh shell and owned-job observation.
27
+ * @param terminate - mark the identity closed, await process quiescence, and remove its owner record.
28
+ * @param failed - diagnostic sink for failed automatic cleanup.
29
+ */
30
+ constructor(policy: TerminalRetentionPolicy, inspect: () => Promise<SubprocessTerminalActivity>, terminate: () => Promise<void>, failed: (error: unknown) => void);
31
+ /**
32
+ * Hold one terminal for one physical Remote stream, independently of screen subscriptions.
33
+ * @param signal - transport generation lifetime.
34
+ * @returns acknowledgement followed by an open stream until cancellation or terminal closure.
35
+ */
36
+ retain(signal: AbortSignal): AsyncIterable<TerminalRetentionFrame>;
37
+ /** Invalidate outstanding idle observations before accepting input. */
38
+ invalidate(): void;
39
+ /**
40
+ * Start or join cleanup; failure keeps the identity closed and schedules one retry.
41
+ * @returns after owned process cleanup succeeds, or rejects with its failure.
42
+ */
43
+ close(): Promise<void>;
44
+ /**
45
+ * Stop timers and streams and await both observation and final cleanup.
46
+ * @returns after process quiescence; cleanup failure is reported to the disposing owner.
47
+ */
48
+ dispose(): Promise<void>;
49
+ private cancelTimer;
50
+ private schedule;
51
+ private observe;
52
+ }
53
+ //# sourceMappingURL=retention.d.ts.map
@@ -0,0 +1,153 @@
1
+ /** Window holds and conservative idle reclamation for one terminal owner. */
2
+ import { RemoteError } from '@deepseek-ai/dsh-typert-protocol';
3
+ /** Exactly one owner orders holds, observation, and retryable process cleanup. */
4
+ export class TerminalRetention {
5
+ policy;
6
+ inspect;
7
+ terminate;
8
+ failed;
9
+ lifetime = new AbortController();
10
+ holders = new Set();
11
+ epoch = 0;
12
+ timer;
13
+ observation;
14
+ idle;
15
+ closing = false;
16
+ disposed = false;
17
+ cleanup;
18
+ /**
19
+ * @param policy - deployment timing choices.
20
+ * @param inspect - fresh shell and owned-job observation.
21
+ * @param terminate - mark the identity closed, await process quiescence, and remove its owner record.
22
+ * @param failed - diagnostic sink for failed automatic cleanup.
23
+ */
24
+ constructor(policy, inspect, terminate, failed) {
25
+ this.policy = policy;
26
+ this.inspect = inspect;
27
+ this.terminate = terminate;
28
+ this.failed = failed;
29
+ this.schedule(0);
30
+ }
31
+ /**
32
+ * Hold one terminal for one physical Remote stream, independently of screen subscriptions.
33
+ * @param signal - transport generation lifetime.
34
+ * @returns acknowledgement followed by an open stream until cancellation or terminal closure.
35
+ */
36
+ async *retain(signal) {
37
+ signal.throwIfAborted();
38
+ if (this.closing || this.disposed)
39
+ throw new RemoteError('terminal/unavailable', 'Terminal is closing or unavailable', {});
40
+ const holder = {};
41
+ const ended = Promise.withResolvers();
42
+ const combined = AbortSignal.any([signal, this.lifetime.signal]);
43
+ const release = () => {
44
+ if (!this.holders.delete(holder))
45
+ return;
46
+ combined.removeEventListener('abort', release);
47
+ this.invalidate();
48
+ ended.resolve();
49
+ this.schedule(0);
50
+ };
51
+ this.holders.add(holder);
52
+ this.invalidate();
53
+ this.cancelTimer();
54
+ combined.addEventListener('abort', release, { once: true });
55
+ try {
56
+ yield { type: 'retained' };
57
+ await ended.promise;
58
+ }
59
+ finally {
60
+ release();
61
+ }
62
+ }
63
+ /** Invalidate outstanding idle observations before accepting input. */
64
+ invalidate() { this.epoch++; this.idle = undefined; }
65
+ /**
66
+ * Start or join cleanup; failure keeps the identity closed and schedules one retry.
67
+ * @returns after owned process cleanup succeeds, or rejects with its failure.
68
+ */
69
+ close() {
70
+ if (this.cleanup !== undefined)
71
+ return this.cleanup;
72
+ this.closing = true;
73
+ this.invalidate();
74
+ this.lifetime.abort(new Error('Terminal closed'));
75
+ this.cancelTimer();
76
+ this.cleanup = this.terminate().catch((error) => {
77
+ this.cleanup = undefined;
78
+ this.schedule(this.policy.cleanupRetryMs);
79
+ throw error;
80
+ });
81
+ return this.cleanup;
82
+ }
83
+ /**
84
+ * Stop timers and streams and await both observation and final cleanup.
85
+ * @returns after process quiescence; cleanup failure is reported to the disposing owner.
86
+ */
87
+ async dispose() {
88
+ this.disposed = true;
89
+ this.cancelTimer();
90
+ const observation = this.observation;
91
+ try {
92
+ await this.close();
93
+ }
94
+ finally {
95
+ await observation;
96
+ }
97
+ }
98
+ cancelTimer() { clearTimeout(this.timer); this.timer = undefined; }
99
+ schedule(delay) {
100
+ if (this.disposed || this.timer !== undefined)
101
+ return;
102
+ if (!this.closing && (this.holders.size > 0 || this.policy.unattendedTimeoutMs === 0))
103
+ return;
104
+ const due = performance.now() + delay;
105
+ this.timer = setTimeout(() => {
106
+ this.timer = undefined;
107
+ const remaining = due - performance.now();
108
+ if (remaining > 0) {
109
+ this.schedule(remaining);
110
+ return;
111
+ }
112
+ if (this.closing) {
113
+ void this.close().catch(this.failed);
114
+ return;
115
+ }
116
+ this.observe();
117
+ }, Math.min(delay, 2_147_483_647));
118
+ this.timer.unref();
119
+ }
120
+ observe() {
121
+ if (this.observation !== undefined)
122
+ return;
123
+ const epoch = this.epoch;
124
+ this.observation = (async () => {
125
+ let activity;
126
+ try {
127
+ activity = await this.inspect();
128
+ }
129
+ catch (_activityUnavailable) {
130
+ activity = { state: 'unknown', revision: 0 };
131
+ }
132
+ if (this.disposed || this.closing || this.holders.size > 0 || epoch !== this.epoch)
133
+ return;
134
+ const now = performance.now();
135
+ if (activity.state !== 'idle') {
136
+ this.idle = undefined;
137
+ return;
138
+ }
139
+ if (this.idle?.revision !== activity.revision || now - this.idle.observedAt > this.policy.activityPollIntervalMs * 2) {
140
+ this.idle = { since: now, observedAt: now, revision: activity.revision };
141
+ }
142
+ else
143
+ this.idle.observedAt = now;
144
+ if (now - this.idle.since >= this.policy.unattendedTimeoutMs)
145
+ await this.close();
146
+ })().catch(this.failed).finally(() => {
147
+ this.observation = undefined;
148
+ if (!this.closing)
149
+ this.schedule(this.policy.activityPollIntervalMs);
150
+ });
151
+ }
152
+ }
153
+ //# sourceMappingURL=retention.js.map
@@ -1,5 +1,6 @@
1
1
  import type { SubprocessTerminalHandle } from '@deepseek-ai/dsh-subprocess';
2
- import type { TerminalAttachmentId, TerminalFrame, WebTerminalInfo } from './types.ts';
2
+ import { type TerminalRetentionPolicy } from './retention.ts';
3
+ import type { TerminalAttachmentId, TerminalFrame, TerminalRetentionFrame, WebTerminalInfo } from './types.ts';
3
4
  /** Process lifetime is independent of follower and component lifetimes. */
4
5
  export declare class BrowserTerminal {
5
6
  private readonly handle;
@@ -12,6 +13,7 @@ export declare class BrowserTerminal {
12
13
  private operations;
13
14
  private readonly drained;
14
15
  private closing;
16
+ private retention;
15
17
  private controller;
16
18
  /**
17
19
  * @param handle - allocated terminal process range.
@@ -20,6 +22,20 @@ export declare class BrowserTerminal {
20
22
  * @param maxBufferedBytes - per-follower queue cap.
21
23
  */
22
24
  constructor(handle: SubprocessTerminalHandle, info: WebTerminalInfo, scrollback: number, maxBufferedBytes: number);
25
+ /**
26
+ * Start monitoring after this allocation is committed to its Session owner.
27
+ * @param policy - validated Host timing policy.
28
+ * @param closing - closes the id before any asynchronous termination.
29
+ * @param closed - removes the exact successfully terminated owner record.
30
+ * @param failed - diagnostic sink for background cleanup failure.
31
+ */
32
+ monitor(policy: TerminalRetentionPolicy, closing: () => void, closed: () => void, failed: (error: unknown) => void): void;
33
+ /**
34
+ * Retain this committed process independently of output attachment.
35
+ * @param signal - physical window stream lifetime.
36
+ * @returns its hold acknowledgement and lifetime.
37
+ */
38
+ retain(signal: AbortSignal): AsyncIterable<TerminalRetentionFrame>;
23
39
  /**
24
40
  * Attach with exclusive input control; an older attachment becomes read-only.
25
41
  * @param id - browser attachment identity.
@@ -52,6 +68,12 @@ export declare class BrowserTerminal {
52
68
  * @returns after process cleanup and final output drainage; failures remain retryable.
53
69
  */
54
70
  close(): Promise<void>;
71
+ /**
72
+ * Stop unattended cleanup scheduling and await final process cleanup.
73
+ * @returns after terminal and monitor quiescence.
74
+ */
75
+ dispose(): Promise<void>;
76
+ private closeProcess;
55
77
  private requireController;
56
78
  private broadcast;
57
79
  private enqueue;
@@ -1,15 +1,10 @@
1
1
  /** One PTY, a bounded terminal emulator and its detachable browser followers. */
2
- import { createRequire } from 'node:module';
3
2
  import { RemoteError } from '@deepseek-ai/dsh-typert-protocol';
3
+ import { createLazyRequire } from '@deepseek-ai/dsh-lazy-require';
4
4
  import { TerminalFollower } from "./stream.js";
5
- const { Terminal, SerializeAddon } = loadXterm();
6
- function loadXterm() {
7
- // The Preview's CommonJS wrapper owns its outer require binding; these literal calls also retain the CJS entries.
8
- const require = createRequire(import.meta.url);
9
- const { Terminal } = require('@xterm/headless');
10
- const { SerializeAddon } = require('@xterm/addon-serialize');
11
- return { Terminal, SerializeAddon };
12
- }
5
+ import { TerminalRetention } from "./retention.js";
6
+ const requireHeadless = createLazyRequire('@xterm/headless', import.meta.url);
7
+ const requireSerialize = createLazyRequire('@xterm/addon-serialize', import.meta.url);
13
8
  /** Process lifetime is independent of follower and component lifetimes. */
14
9
  export class BrowserTerminal {
15
10
  handle;
@@ -22,6 +17,7 @@ export class BrowserTerminal {
22
17
  operations = Promise.resolve();
23
18
  drained;
24
19
  closing;
20
+ retention;
25
21
  controller;
26
22
  /**
27
23
  * @param handle - allocated terminal process range.
@@ -33,11 +29,37 @@ export class BrowserTerminal {
33
29
  this.handle = handle;
34
30
  this.info = info;
35
31
  this.maxBufferedBytes = maxBufferedBytes;
32
+ const { Terminal } = requireHeadless();
33
+ const { SerializeAddon } = requireSerialize();
36
34
  this.screen = new Terminal({ cols: info.cols, rows: info.rows, scrollback, allowProposedApi: true });
37
35
  this.serializer = new SerializeAddon();
38
36
  this.screen.loadAddon(this.serializer);
39
37
  this.drained = this.consume();
40
38
  }
39
+ /**
40
+ * Start monitoring after this allocation is committed to its Session owner.
41
+ * @param policy - validated Host timing policy.
42
+ * @param closing - closes the id before any asynchronous termination.
43
+ * @param closed - removes the exact successfully terminated owner record.
44
+ * @param failed - diagnostic sink for background cleanup failure.
45
+ */
46
+ monitor(policy, closing, closed, failed) {
47
+ this.retention = new TerminalRetention(policy, () => this.handle.inspectActivity(), async () => {
48
+ closing();
49
+ await this.closeProcess();
50
+ closed();
51
+ }, failed);
52
+ }
53
+ /**
54
+ * Retain this committed process independently of output attachment.
55
+ * @param signal - physical window stream lifetime.
56
+ * @returns its hold acknowledgement and lifetime.
57
+ */
58
+ retain(signal) {
59
+ if (this.retention === undefined)
60
+ throw new Error('Terminal has not been committed');
61
+ return this.retention.retain(signal);
62
+ }
41
63
  /**
42
64
  * Attach with exclusive input control; an older attachment becomes read-only.
43
65
  * @param id - browser attachment identity.
@@ -78,6 +100,7 @@ export class BrowserTerminal {
78
100
  * @returns when the provider accepts the input.
79
101
  */
80
102
  write(id, data) {
103
+ this.retention?.invalidate();
81
104
  return this.enqueue(async () => { this.requireController(id); await this.handle.write(data); });
82
105
  }
83
106
  /**
@@ -109,6 +132,14 @@ export class BrowserTerminal {
109
132
  * @returns after process cleanup and final output drainage; failures remain retryable.
110
133
  */
111
134
  close() {
135
+ return this.retention?.close() ?? this.closeProcess();
136
+ }
137
+ /**
138
+ * Stop unattended cleanup scheduling and await final process cleanup.
139
+ * @returns after terminal and monitor quiescence.
140
+ */
141
+ dispose() { return this.retention?.dispose() ?? this.closeProcess(); }
142
+ closeProcess() {
112
143
  if (this.closing !== undefined)
113
144
  return this.closing;
114
145
  this.closing = (async () => {
@@ -2,6 +2,8 @@
2
2
  import type { Branded } from '@deepseek-ai/dsh-brand';
3
3
  declare module '@deepseek-ai/dsh-typert-protocol' {
4
4
  interface RemoteErrorDetailsMap {
5
+ /** The terminal identity is missing or has begun process cleanup. */
6
+ 'terminal/unavailable': Record<string, never>;
5
7
  /** Input or resize was refused without invalidating the output attachment. */
6
8
  'terminal/control-unavailable': {
7
9
  readonly reason: 'read-only' | 'not-running';
@@ -16,6 +18,10 @@ declare module '@deepseek-ai/dsh-typert-protocol' {
16
18
  export type WebTerminalId = Branded<'WebTerminalId'>;
17
19
  /** An attachment allowed to write and resize one terminal. */
18
20
  export type TerminalAttachmentId = Branded<'TerminalAttachmentId'>;
21
+ /** Acknowledges one physical window hold without taking screen or input control. */
22
+ export interface TerminalRetentionFrame {
23
+ readonly type: 'retained';
24
+ }
19
25
  /** An executable shell verified in the subprocess provider's execution environment. */
20
26
  export interface TerminalShell {
21
27
  readonly path: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@deepseek-ai/dsh-api-terminal-controller",
3
3
  "description": "Session-owned interactive terminals with shell discovery, screen recovery and typed Remote control",
4
- "version": "0.1.6-alpha.1",
4
+ "version": "0.1.6-alpha.2",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -53,28 +53,29 @@
53
53
  "@xterm/headless": "^6.0.0",
54
54
  "@xterm/addon-serialize": "^0.14.0",
55
55
  "zod": "^4.4.3",
56
- "@deepseek-ai/dsh-deque": "^0.1.6-alpha.1",
57
- "@deepseek-ai/dsh-typert-protocol": "^0.1.6-alpha.1",
56
+ "@deepseek-ai/dsh-deque": "^0.1.6-alpha.2",
57
+ "@deepseek-ai/dsh-lazy-require": "^0.1.6-alpha.2",
58
+ "@deepseek-ai/dsh-typert-protocol": "^0.1.6-alpha.2",
58
59
  "@deepseek-ai/schemastery": "^3.18.2"
59
60
  },
60
61
  "peerDependencies": {
61
- "@deepseek-ai/dsh-subprocess": "^0.1.6-alpha.1",
62
+ "@deepseek-ai/dsh-subprocess": "^0.1.6-alpha.2",
62
63
  "@deepseek-ai/cordis": "^4.0.2"
63
64
  },
64
65
  "devDependencies": {
65
66
  "@deepseek-ai/cordis": "^4.0.2",
66
- "@deepseek-ai/dsh-agent": "^0.1.6-alpha.1",
67
- "@deepseek-ai/dsh-api-gateway": "^0.1.6-alpha.1",
68
- "@deepseek-ai/dsh-brand": "^0.1.6-alpha.1",
69
- "@deepseek-ai/dsh-client-store": "^0.1.6-alpha.1",
70
- "@deepseek-ai/dsh-fs": "^0.1.6-alpha.1",
71
- "@deepseek-ai/dsh-sandbox": "^0.1.6-alpha.1",
72
- "@deepseek-ai/dsh-sandbox-policy": "^0.1.6-alpha.1",
73
- "@deepseek-ai/dsh-session": "^0.1.6-alpha.1",
74
- "@deepseek-ai/dsh-session-projection": "^0.1.6-alpha.1",
75
- "@deepseek-ai/dsh-subprocess-local": "^0.1.6-alpha.1",
76
- "@deepseek-ai/dsh-util-crypto": "^0.1.6-alpha.1",
77
- "@deepseek-ai/dsh-subprocess": "^0.1.6-alpha.1"
67
+ "@deepseek-ai/dsh-agent": "^0.1.6-alpha.2",
68
+ "@deepseek-ai/dsh-api-gateway": "^0.1.6-alpha.2",
69
+ "@deepseek-ai/dsh-brand": "^0.1.6-alpha.2",
70
+ "@deepseek-ai/dsh-client-store": "^0.1.6-alpha.2",
71
+ "@deepseek-ai/dsh-fs": "^0.1.6-alpha.2",
72
+ "@deepseek-ai/dsh-sandbox": "^0.1.6-alpha.2",
73
+ "@deepseek-ai/dsh-sandbox-policy": "^0.1.6-alpha.2",
74
+ "@deepseek-ai/dsh-session-projection": "^0.1.6-alpha.2",
75
+ "@deepseek-ai/dsh-subprocess": "^0.1.6-alpha.2",
76
+ "@deepseek-ai/dsh-subprocess-local": "^0.1.6-alpha.2",
77
+ "@deepseek-ai/dsh-util-crypto": "^0.1.6-alpha.2",
78
+ "@deepseek-ai/dsh-session": "^0.1.6-alpha.2"
78
79
  },
79
80
  "files": [
80
81
  "lib/index.js",