dsh-remote-vs 0.13.0 → 0.15.1

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.
Files changed (128) hide show
  1. package/README.en.md +455 -443
  2. package/README.md +408 -396
  3. package/lib/client.js +8062 -2651
  4. package/lib/index.js +1058 -288
  5. package/lib/p2-router-fs.js +1 -1
  6. package/lib/p2-router-refs.js +89 -10
  7. package/lib/p2-router-spill.js +73 -1
  8. package/lib/p2-router-subprocess.js +18 -6
  9. package/lib/spill-retention-BwKEWrl5.js +115 -0
  10. package/lib/{ssh-proc-arI5lm4W.js → ssh-proc-VKWn_LZC.js} +17 -0
  11. package/lib/types/archive.d.ts +1 -1
  12. package/lib/types/changes/recorder.d.ts +34 -2
  13. package/lib/types/client/CodeEditor.d.ts +12 -0
  14. package/lib/types/client/ConnectionStatus.d.ts +17 -0
  15. package/lib/types/client/DirectoryFlow.d.ts +2 -0
  16. package/lib/types/client/DoctorPanel.d.ts +25 -0
  17. package/lib/types/client/QuickOpen.d.ts +17 -0
  18. package/lib/types/client/RemoteExplorer.d.ts +192 -1
  19. package/lib/types/client/RemoteRoot.d.ts +17 -0
  20. package/lib/types/client/SearchView.d.ts +55 -0
  21. package/lib/types/client/TerminalView.d.ts +38 -0
  22. package/lib/types/client/WorkspaceWizard.d.ts +17 -0
  23. package/lib/types/client/api.d.ts +54 -4
  24. package/lib/types/client/changes/undo-affordance.d.ts +16 -0
  25. package/lib/types/client/changes/undo-context.d.ts +14 -18
  26. package/lib/types/client/changes/undo-target.d.ts +78 -0
  27. package/lib/types/client/connection.d.ts +111 -0
  28. package/lib/types/client/doctor.d.ts +122 -0
  29. package/lib/types/client/download.d.ts +35 -5
  30. package/lib/types/client/downloads.d.ts +18 -1
  31. package/lib/types/client/file-open-probe.d.ts +64 -0
  32. package/lib/types/client/i18n.d.ts +1 -1
  33. package/lib/types/client/quickopen-model.d.ts +332 -0
  34. package/lib/types/client/scm/DiffView.d.ts +34 -0
  35. package/lib/types/client/scm/actions.d.ts +18 -0
  36. package/lib/types/client/scm/diff-model.d.ts +17 -1
  37. package/lib/types/client/scm/git-cache.d.ts +10 -2
  38. package/lib/types/client/scm/read-queue.d.ts +2 -1
  39. package/lib/types/client/scm/stage-queue.d.ts +13 -0
  40. package/lib/types/client/scm/status-map.d.ts +3 -1
  41. package/lib/types/client/store.d.ts +114 -5
  42. package/lib/types/client/vsc/FindWidget.d.ts +7 -0
  43. package/lib/types/client/vsc/save-shortcut.d.ts +57 -0
  44. package/lib/types/deploy.d.ts +44 -2
  45. package/lib/types/git-format.d.ts +15 -0
  46. package/lib/types/http.d.ts +44 -0
  47. package/lib/types/p2/file-refs.d.ts +39 -2
  48. package/lib/types/p2/helper-rpc.d.ts +26 -0
  49. package/lib/types/p2/router-spill.d.ts +19 -0
  50. package/lib/types/p2/router-subprocess.d.ts +2 -0
  51. package/lib/types/p2/spill-retention.d.ts +74 -0
  52. package/lib/types/p2/worlds.d.ts +11 -0
  53. package/lib/types/pool.d.ts +73 -11
  54. package/lib/types/protocol.d.ts +63 -0
  55. package/lib/types/routes.d.ts +18 -0
  56. package/lib/types/sftp.d.ts +114 -8
  57. package/lib/types/store.d.ts +34 -0
  58. package/lib/types/timeouts.d.ts +14 -0
  59. package/lib/{worlds-C6Xha1mQ.js → worlds-Be1YE2Ot.js} +727 -78
  60. package/package.json +1 -1
  61. package/src/archive.ts +41 -9
  62. package/src/changes/recorder.ts +227 -31
  63. package/src/changes/status.ts +37 -1
  64. package/src/client/CodeEditor.tsx +84 -2
  65. package/src/client/ConnectionStatus.tsx +130 -0
  66. package/src/client/DirectoryFlow.tsx +40 -5
  67. package/src/client/DoctorPanel.tsx +202 -0
  68. package/src/client/DownloadDock.tsx +13 -25
  69. package/src/client/EditorPane.tsx +291 -7
  70. package/src/client/HostsView.tsx +17 -8
  71. package/src/client/QuickOpen.tsx +371 -0
  72. package/src/client/RemoteDirectoryPicker.tsx +54 -3
  73. package/src/client/RemoteExplorer.tsx +879 -160
  74. package/src/client/RemoteRoot.tsx +178 -1
  75. package/src/client/SearchView.tsx +180 -6
  76. package/src/client/SettingsHosts.tsx +1 -1
  77. package/src/client/TerminalView.tsx +174 -9
  78. package/src/client/WorkspaceWizard.tsx +110 -16
  79. package/src/client/WorkspacesView.tsx +22 -6
  80. package/src/client/api.ts +197 -11
  81. package/src/client/changes/ChangesDock.tsx +35 -8
  82. package/src/client/changes/undo-affordance.ts +33 -0
  83. package/src/client/changes/undo-context.ts +14 -18
  84. package/src/client/changes/undo-target.ts +139 -0
  85. package/src/client/connection.ts +264 -0
  86. package/src/client/doctor.ts +441 -0
  87. package/src/client/download.ts +148 -12
  88. package/src/client/downloads.ts +43 -2
  89. package/src/client/file-open-probe.ts +83 -0
  90. package/src/client/i18n.ts +244 -0
  91. package/src/client/index.tsx +5 -2
  92. package/src/client/quickopen-model.ts +778 -0
  93. package/src/client/remote-file-open.ts +21 -33
  94. package/src/client/scm/DiffView.tsx +116 -6
  95. package/src/client/scm/ScmPanel.tsx +19 -0
  96. package/src/client/scm/actions.ts +34 -2
  97. package/src/client/scm/diff-model.ts +33 -37
  98. package/src/client/scm/git-cache.ts +61 -4
  99. package/src/client/scm/read-queue.ts +33 -2
  100. package/src/client/scm/stage-queue.ts +72 -1
  101. package/src/client/scm/status-map.ts +22 -1
  102. package/src/client/store.tsx +704 -21
  103. package/src/client/vsc/FindWidget.tsx +20 -0
  104. package/src/client/vsc/kit.tsx +115 -6
  105. package/src/client/vsc/reveal-line.ts +33 -14
  106. package/src/client/vsc/save-shortcut.ts +131 -0
  107. package/src/client/vsc/vsc.css +419 -0
  108. package/src/deploy.ts +185 -33
  109. package/src/git-format.ts +136 -22
  110. package/src/git-ops.ts +14 -6
  111. package/src/http.ts +80 -12
  112. package/src/importers.ts +80 -21
  113. package/src/index.ts +21 -1
  114. package/src/p2/file-refs.ts +112 -10
  115. package/src/p2/helper-rpc.ts +73 -36
  116. package/src/p2/router-spill.ts +96 -1
  117. package/src/p2/router-subprocess.ts +18 -5
  118. package/src/p2/spill-retention.ts +134 -0
  119. package/src/p2/ssh-proc.ts +22 -0
  120. package/src/p2/worlds.ts +24 -0
  121. package/src/pool.ts +329 -47
  122. package/src/protocol.ts +66 -0
  123. package/src/routes.ts +251 -43
  124. package/src/sftp.ts +486 -26
  125. package/src/store.ts +207 -85
  126. package/src/terminal.ts +38 -2
  127. package/src/timeouts.ts +14 -0
  128. package/src/tools.ts +14 -5
@@ -3,12 +3,23 @@
3
3
  * spill backend (private realm) and per-host remote spill dirs. Unknown
4
4
  * sessions stay local (system callers); tombstoned sessions fail closed and
5
5
  * the caller keeps its inline result.
6
+ *
7
+ * Retention: the remote artifacts used to live forever (one file per oversized
8
+ * tool result, under `$HOME/.dsh-helpers/spills/session-<id>/`). They now get a
9
+ * TTL sweep once per host per process, plus a per-session check when the session
10
+ * is disposed — both best-effort, both outside the write's critical path (see
11
+ * `spill-retention.ts` for the commands and why they are safe to aim).
6
12
  */
7
13
  import { randomUUID } from 'node:crypto'
14
+ import type { Context } from '@deepseek-ai/cordis'
8
15
  import { SpillStore, SpillLocator } from '@deepseek-ai/dsh-spill'
9
16
  import type { SaveTextSpill, SpillRef } from '@deepseek-ai/dsh-spill'
10
17
  import type { SandboxExecutionPolicy, SandboxMode } from '@deepseek-ai/dsh-sandbox'
11
18
  import type { RemoteWorlds, WorldRoute } from './worlds.ts'
19
+ import { runBestEffort, sessionSpillDir, spillTtlDays } from './spill-retention.ts'
20
+
21
+ /** Ceiling on tracked sessions, so a long-lived Host cannot grow this map forever. */
22
+ const TRACKED_SESSIONS_CAP = 200
12
23
 
13
24
  /** Sanitize a suggested name to one safe segment (backend owns the rest). */
14
25
  function safeName(suggested: string): string {
@@ -16,9 +27,34 @@ function safeName(suggested: string): string {
16
27
  return `${base === '' ? 'spill' : base}-${randomUUID().slice(0, 8)}.txt`
17
28
  }
18
29
 
30
+ /** One remote spill directory this process wrote, kept for the dispose prune. */
31
+ interface SpillDirRecord {
32
+ alias: string
33
+ dir: string
34
+ }
35
+
19
36
  export default class TransparentSpillStore extends SpillStore {
20
37
  static inject = ['localSpillStore', 'remoteWorlds']
21
38
 
39
+ /** Hosts already TTL-swept by THIS process (one `find` per host, not per spill). */
40
+ private readonly sweptAliases = new Set<string>()
41
+ /** session id -> directories written for it, for the best-effort dispose prune. */
42
+ private readonly sessionDirs = new Map<string, SpillDirRecord[]>()
43
+
44
+ constructor(ctx: Context) {
45
+ super(ctx)
46
+ /**
47
+ * Session end is the one moment a spill directory is provably dead for the
48
+ * session that wrote it. The check still requires the DIRECTORY to be older
49
+ * than the TTL (`spillDirSweepCommand`), because `dsh-spill` states that a
50
+ * forked session inherits its parent's locators without copying them — a
51
+ * young directory may still be read by a live child.
52
+ */
53
+ ctx.on('session/disposed', (session) => {
54
+ this.pruneSession(String(session.id))
55
+ })
56
+ }
57
+
22
58
  private get local(): SpillStore {
23
59
  return (this.ctx as unknown as { localSpillStore: SpillStore }).localSpillStore
24
60
  }
@@ -43,12 +79,18 @@ export default class TransparentSpillStore extends SpillStore {
43
79
  // Remote backend: mirror local semantics (private dir, derived name).
44
80
  const fs = await this.remote.fsFor(route.alias)
45
81
  const home = await this.remote.homeFor(route.alias)
46
- const dir = `${home}/.dsh-helpers/spills/session-${input.owner.sessionId}`
82
+ // Retention is scheduled BEFORE the write and never blocks it (P2-N6: the
83
+ // first spill per host used to await a remote `find` with a 20 s budget).
84
+ // The per-host marker is set synchronously inside `sweepOnce`, so concurrent
85
+ // first spills still issue exactly one sweep; a slow/failed sweep only warns.
86
+ void this.sweepOnce(route.alias, home)
87
+ const dir = sessionSpillDir(home, input.owner.sessionId)
47
88
  await this.remote.mkdirRemote(route.alias, dir)
48
89
  const name = safeName(input.suggestedName ?? 'spill.txt')
49
90
  const fullPath = `${dir}/${name}`
50
91
  const target = await fs.resolve(fullPath, {})
51
92
  await fs.writeText(target, input.content, { kind: 'createIfAbsent' }, undefined, this.policyFor(dir))
93
+ this.remember(String(input.owner.sessionId), route.alias, dir)
52
94
  const bytes = Buffer.byteLength(input.content, 'utf8')
53
95
  return {
54
96
  locator: SpillLocator(`remote:${route.alias}:${fullPath}`),
@@ -72,4 +114,57 @@ export default class TransparentSpillStore extends SpillStore {
72
114
  }
73
115
  return { mode, workspaceRoot: dir }
74
116
  }
117
+
118
+ /**
119
+ * Sweep one host's spill root once per process.
120
+ *
121
+ * The marker is set BEFORE awaiting so two concurrent first spills on the same
122
+ * host cannot both issue the sweep; a failed sweep still leaves the host marked
123
+ * (retention is a nicety, retrying it on every spill would be a tax).
124
+ */
125
+ private async sweepOnce(alias: string, home: string): Promise<void> {
126
+ if (this.sweptAliases.has(alias)) return
127
+ this.sweptAliases.add(alias)
128
+ await runBestEffort(
129
+ `ttl sweep on ${alias}`,
130
+ () => this.remote.sweepSpillRoot(alias, home, spillTtlDays()),
131
+ message => this.warn(message),
132
+ )
133
+ }
134
+
135
+ /** Remember one written directory for the dispose prune (bounded, oldest first). */
136
+ private remember(sessionId: string, alias: string, dir: string): void {
137
+ const existing = this.sessionDirs.get(sessionId)
138
+ if (existing === undefined) {
139
+ if (this.sessionDirs.size >= TRACKED_SESSIONS_CAP) {
140
+ const oldest = this.sessionDirs.keys().next().value
141
+ if (oldest !== undefined) this.sessionDirs.delete(oldest)
142
+ }
143
+ this.sessionDirs.set(sessionId, [{ alias, dir }])
144
+ return
145
+ }
146
+ if (!existing.some(record => record.alias === alias && record.dir === dir)) existing.push({ alias, dir })
147
+ }
148
+
149
+ /** Best-effort TTL check for the directories one session wrote. Never throws. */
150
+ private pruneSession(sessionId: string): void {
151
+ const records = this.sessionDirs.get(sessionId)
152
+ if (records === undefined) return
153
+ this.sessionDirs.delete(sessionId)
154
+ for (const record of records) {
155
+ void runBestEffort(
156
+ `session prune of ${record.dir}`,
157
+ () => this.remote.sweepSpillDir(record.alias, record.dir, spillTtlDays()),
158
+ message => this.warn(message),
159
+ )
160
+ }
161
+ }
162
+
163
+ private warn(message: string): void {
164
+ try {
165
+ this.ctx.logger.warn(`[remote-vs] ${message}`)
166
+ } catch {
167
+ // Logging must never break a spill.
168
+ }
169
+ }
75
170
  }
@@ -15,11 +15,24 @@ import type {
15
15
  import type { RemoteWorlds, WorldRoute } from './worlds.ts'
16
16
  import { remoteCommand } from './ssh-proc.ts'
17
17
 
18
- /** True when argv selects PowerShell (remote worlds are POSIX-only). */
19
- function isPwshArgv(argv: readonly string[]): boolean {
18
+ /**
19
+ * Windows shells: remote worlds are POSIX-only, so ALL of these fail closed.
20
+ *
21
+ * Recognising only `pwsh` was fail-open: `powershell.exe` / `cmd.exe` / `.ps1`
22
+ * fell through to the bare-name rewrite and were then looked up on the remote
23
+ * PATH. Usually that produced a misleading "not found"; with a same-named binary
24
+ * on the host it would have run something else entirely. Exported so the guard
25
+ * can be asserted directly, without a host.
26
+ */
27
+ const WINDOWS_SHELL = /^(?:pwsh|pwsh-preview|powershell|powershell_ise|cmd)(?:\.exe)?$/i
28
+ /** Windows script extensions: argv[0] naming one cannot run without a shell. */
29
+ const WINDOWS_SCRIPT = /\.(?:ps1|bat|cmd)$/i
30
+
31
+ /** True when argv selects a Windows shell (unsupported in remote worlds). */
32
+ export function isWindowsShellArgv(argv: readonly string[]): boolean {
20
33
  if (argv.length === 0) return false
21
34
  const base = (argv[0] as string).replace(/\\/g, '/').split('/').pop() ?? ''
22
- return /^pwsh(\.exe)?$/i.test(base)
35
+ return WINDOWS_SHELL.test(base) || WINDOWS_SCRIPT.test(base)
23
36
  }
24
37
 
25
38
  export default class TransparentSubprocessRuntime extends SubprocessRuntime {
@@ -63,9 +76,9 @@ export default class TransparentSubprocessRuntime extends SubprocessRuntime {
63
76
  spawn(spec: SubprocessSpawnSpec): SubprocessHandle {
64
77
  const route = this.effective(this.remote.routeDir(spec.cwd), `spawn ${spec.argv[0] ?? ''} @ ${spec.cwd}`)
65
78
  if (route.kind === 'local') return this.local.spawn(spec)
66
- if (isPwshArgv(spec.argv)) {
79
+ if (isWindowsShellArgv(spec.argv)) {
67
80
  throw new SubprocessExecutableNotFoundError(
68
- `remote worlds are POSIX-only: '${spec.argv[0]}' cannot run on ${route.alias}; use the bash tool (Remote preset) instead`,
81
+ `remote worlds are POSIX-only: '${spec.argv[0]}' is a Windows shell and cannot run on ${route.alias}; use the bash tool (Remote preset) instead`,
69
82
  )
70
83
  }
71
84
  const runtime = this.remote.procFor(route.alias)
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Remote spill retention: the policy constant and the pure command builders.
3
+ *
4
+ * Why this module exists: the spill backend writes model-facing artifacts into
5
+ * the remote `$HOME` under a per-session directory (`router-spill.ts`), and the
6
+ * spill service definition owns NO retention policy ({@link SpillStore} only
7
+ * declares `saveText`). Nothing removed those files, so a long-lived host grew
8
+ * one artifact per oversized tool result forever — including the content of
9
+ * command output, which is exactly the material that should not live on a build
10
+ * machine indefinitely.
11
+ *
12
+ * The policy is deliberately lazy: a TTL sweep per host (one `find` on the first
13
+ * spill), plus a per-session directory check when a session is disposed. Two
14
+ * rules shape the commands below:
15
+ *
16
+ * 1. **Deletion must never block a write.** Everything is best-effort (see
17
+ * {@link runBestEffort}); a failed cleanup is logged and forgotten.
18
+ * 2. **Deletion must be impossible to aim at anything else.** Every path is an
19
+ * absolute POSIX path under `<home>/.dsh-helpers/spills`, every shell word is
20
+ * single-quoted, the sweep is capped at depth 1 and to `session-*`, and the
21
+ * session-directory check only matches the directory itself (`-maxdepth 0`).
22
+ *
23
+ * Kept free of cordis/pool on purpose, so `scripts/check-spill-retention.mjs`
24
+ * drives the real builders without a host.
25
+ */
26
+
27
+ /** Subdirectory of the remote `$HOME` that holds every spilled artifact. */
28
+ export const SPILLS_SUBDIR = '.dsh-helpers/spills'
29
+
30
+ /** Default lifetime of a remote spill directory, in whole days. */
31
+ export const SPILL_TTL_DAYS_DEFAULT = 14
32
+
33
+ /** TTL bounds: below a day a sweep would race live sessions; above a quarter a "temporary" artifact is not. */
34
+ const TTL_MIN_DAYS = 1
35
+ const TTL_MAX_DAYS = 90
36
+
37
+ /** The TTL in days: `DSH_REMOTE_VS_SPILL_TTL_DAYS` when sane, else the default. */
38
+ export function spillTtlDays(): number {
39
+ const raw = Number(process.env.DSH_REMOTE_VS_SPILL_TTL_DAYS)
40
+ if (!Number.isFinite(raw) || raw < TTL_MIN_DAYS) return SPILL_TTL_DAYS_DEFAULT
41
+ return Math.min(Math.floor(raw), TTL_MAX_DAYS)
42
+ }
43
+
44
+ /** A TTL clamped to the supported range (also used by direct callers/tests). */
45
+ export function clampTtlDays(value: number): number {
46
+ if (!Number.isFinite(value)) return SPILL_TTL_DAYS_DEFAULT
47
+ return Math.min(Math.max(Math.floor(value), TTL_MIN_DAYS), TTL_MAX_DAYS)
48
+ }
49
+
50
+ /** POSIX single-quote one shell word. Throws on NUL, which no shell can carry. */
51
+ export function shq(word: string): string {
52
+ if (word.includes('\0')) throw new Error('NUL byte in shell word')
53
+ return `'${word.replace(/'/g, `'\\''`)}'`
54
+ }
55
+
56
+ /** Refuse a path that is not remote-absolute: retention must never guess a directory. */
57
+ function remoteAbsolute(path: string): string {
58
+ if (!path.startsWith('/')) throw new Error(`refusing non-absolute remote path: ${path}`)
59
+ return path.length > 1 ? path.replace(/\/+$/, '') : path
60
+ }
61
+
62
+ /** The spills root inside one remote home. */
63
+ export function spillRoot(home: string): string {
64
+ return `${remoteAbsolute(home)}/${SPILLS_SUBDIR}`
65
+ }
66
+
67
+ /**
68
+ * The per-session spill directory.
69
+ *
70
+ * Mirrors `router-spill.ts`'s previous inline spelling so existing sessions keep
71
+ * finding their files, and rejects anything that could escape the root: a session
72
+ * id comes from the Host, but a directory name derived from it still has to be
73
+ * one safe path segment.
74
+ */
75
+ export function sessionSpillDir(home: string, sessionId: string | number): string {
76
+ const id = String(sessionId)
77
+ if (id === '' || !/^[A-Za-z0-9._-]+$/.test(id) || id === '.' || id === '..') {
78
+ throw new Error(`unsafe spill session id: ${id}`)
79
+ }
80
+ return `${spillRoot(home)}/session-${id}`
81
+ }
82
+
83
+ /**
84
+ * One sweep command for a whole host: every `session-*` directory under the
85
+ * spills root older than `ttlDays`.
86
+ *
87
+ * `-mindepth 1 -maxdepth 1` keeps the walk inside the root's own children (no
88
+ * recursion, so a stray directory elsewhere can never match), `-name 'session-*'`
89
+ * pins the namespace, and `|| true` makes "the root does not exist yet" (the
90
+ * normal first-run case) a success: retention is not allowed to look like
91
+ * failure to its caller.
92
+ */
93
+ export function spillSweepCommand(home: string, ttlDays: number): string {
94
+ const ttl = clampTtlDays(ttlDays)
95
+ return `find ${shq(spillRoot(home))} -mindepth 1 -maxdepth 1 -type d -name 'session-*' -mtime +${ttl} -exec rm -rf -- {} + 2>/dev/null || true`
96
+ }
97
+
98
+ /**
99
+ * One command for a single session's spill directory: remove it only when the
100
+ * directory itself is older than the TTL.
101
+ *
102
+ * The mtime gate is what makes this safe for forked sessions: `dsh-spill` states
103
+ * that a fork inherits the parent's locators without copying them, so a young
104
+ * directory may still be referenced by a live child. `-maxdepth 0` matches the
105
+ * directory alone — never its contents.
106
+ */
107
+ export function spillDirSweepCommand(dir: string, ttlDays: number): string {
108
+ const ttl = clampTtlDays(ttlDays)
109
+ return `find ${shq(remoteAbsolute(dir))} -maxdepth 0 -type d -mtime +${ttl} -exec rm -rf -- {} + 2>/dev/null || true`
110
+ }
111
+
112
+ /**
113
+ * Run one retention step without ever failing the caller.
114
+ *
115
+ * A write path that waits on a cleanup is a write path that can be broken by a
116
+ * cleanup; the caller only gets a warning sink. Returns whether the step ran.
117
+ */
118
+ export async function runBestEffort(
119
+ what: string,
120
+ run: () => Promise<void>,
121
+ warn?: (message: string) => void,
122
+ ): Promise<boolean> {
123
+ try {
124
+ await run()
125
+ return true
126
+ } catch (error) {
127
+ try {
128
+ warn?.(`spill retention (${what}) failed: ${error instanceof Error ? error.message : String(error)}`)
129
+ } catch {
130
+ // Logging must never break the write either.
131
+ }
132
+ return false
133
+ }
134
+ }
@@ -91,6 +91,26 @@ function sweepSpills(now: number): void {
91
91
  }
92
92
  }
93
93
 
94
+ /**
95
+ * Whether this process has already swept the Host spill dir.
96
+ *
97
+ * The sweep used to hang off `spillCreate` alone, so a Host that stopped
98
+ * producing over-cap output kept its old files forever. Arming it from the first
99
+ * remote spawn makes it independent of whether THIS run ever spills.
100
+ */
101
+ let hostSweepArmed = false
102
+
103
+ /** Arm the one-per-process Host spill sweep (cheap readdir; best effort). */
104
+ function armHostSpillSweep(now: number): void {
105
+ if (hostSweepArmed) return
106
+ hostSweepArmed = true
107
+ try {
108
+ sweepSpills(now)
109
+ } catch {
110
+ // Retention must never break a spawn.
111
+ }
112
+ }
113
+
94
114
  /**
95
115
  * One collected stream: tail window + offset readers + optional full-stream
96
116
  * spill file (Host disk). Mirrors the stock local runtime's contract
@@ -337,6 +357,8 @@ export class SshProcRuntime {
337
357
  if (spec.signal?.aborted === true) {
338
358
  throw new Error('spawn aborted before start')
339
359
  }
360
+ // First remote process of this Host process: sweep the Host spill dir once.
361
+ armHostSpillSweep(Date.now())
340
362
  const useSetsid = this.setsid ?? true
341
363
  return new SshProcHandle(this.pool, this.alias, spec, useSetsid, () => {
342
364
  this.setsid = false
package/src/p2/worlds.ts CHANGED
@@ -23,6 +23,7 @@ import { aliasRoot } from '../paths.ts'
23
23
  import type { RemoteHostEntry, RemoteWorkspaceBinding } from '../protocol.ts'
24
24
  import { RpcConnection } from './helper-rpc.ts'
25
25
  import { SshProcRuntime } from './ssh-proc.ts'
26
+ import { spillDirSweepCommand, spillSweepCommand } from './spill-retention.ts'
26
27
  import { legacyWorkspaceTitle, workspaceTitle } from '../workspace-title.ts'
27
28
 
28
29
  export type RoutingMode = 'off' | 'shadow' | 'enforce'
@@ -446,6 +447,29 @@ export class RemoteWorlds {
446
447
  }
447
448
  }
448
449
 
450
+ /**
451
+ * TTL-sweep one host's spill root (retention, best effort).
452
+ *
453
+ * Deliberately narrow: the command is built by `spill-retention.ts` (absolute
454
+ * path under that host's `$HOME`, `session-*` children only, depth 1), so this
455
+ * method cannot be talked into deleting an arbitrary directory. Callers treat a
456
+ * rejection as a warning, never as an operation failure.
457
+ */
458
+ async sweepSpillRoot(alias: string, home: string, ttlDays: number): Promise<void> {
459
+ const result = await this.deps.pool.exec(alias, spillSweepCommand(home, ttlDays), { timeoutMs: 20000 })
460
+ if (!result.success) {
461
+ throw new Error(`remote spill sweep failed: ${result.stderr || result.error || `exit ${result.exitCode}`}`)
462
+ }
463
+ }
464
+
465
+ /** Remove one session's spill directory when it is older than the TTL (best effort). */
466
+ async sweepSpillDir(alias: string, dir: string, ttlDays: number): Promise<void> {
467
+ const result = await this.deps.pool.exec(alias, spillDirSweepCommand(dir, ttlDays), { timeoutMs: 20000 })
468
+ if (!result.success) {
469
+ throw new Error(`remote spill dir sweep failed: ${result.stderr || result.error || `exit ${result.exitCode}`}`)
470
+ }
471
+ }
472
+
449
473
  /** Drop one host's RPC + fs view + proc runtime (call after host update/delete). */
450
474
  async dropHost(alias: string): Promise<void> {
451
475
  this.fsReady.delete(alias)