@dsh-cc/cli 0.5.0
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.
- package/LICENSE +201 -0
- package/README.md +58 -0
- package/bin/dsh-cc.js +155 -0
- package/bootstrap.mjs +317 -0
- package/package.json +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# @dsh-cc/cli
|
|
2
|
+
|
|
3
|
+
Optional `dsh-cc` bin. Canonical command is still `dsh --profile tui`.
|
|
4
|
+
|
|
5
|
+
First run bootstraps `$DSH_HOME/profiles/tui` with the three CC bundles.
|
|
6
|
+
The launcher is a thin flag translator: it derives the session-mode env the
|
|
7
|
+
TUI plugin consumes, then spawns `dsh --profile tui`. It never reads a
|
|
8
|
+
resume marker itself — marker reads and writes are owned by the TUI plugin.
|
|
9
|
+
|
|
10
|
+
`--worktree [name]` starts the session inside a git worktree at
|
|
11
|
+
`<repoRoot>/.claude/worktrees/<slug>` on branch `worktree-<slug>` (random
|
|
12
|
+
slug when no name is given). A newly created worktree starts a fresh
|
|
13
|
+
session (equivalent to `--new`). Re-invoking `--worktree <name>` when that
|
|
14
|
+
directory already exists reuses it and, because sessions are scoped to the
|
|
15
|
+
*project* (main git root; worktrees share it), falls back to the default
|
|
16
|
+
auto-resume — the TUI resumes the project's last session. `--resume` /
|
|
17
|
+
`--new` still override. The launcher marks the session via
|
|
18
|
+
`DSH_CC_WORKTREE`, and the TUI offers to keep or remove the worktree at
|
|
19
|
+
`/quit`. Requires a git repository with at least one commit.
|
|
20
|
+
|
|
21
|
+
## Resume environment contract
|
|
22
|
+
|
|
23
|
+
The launcher communicates the user's session intent to the TUI plugin (via
|
|
24
|
+
`cordis.patch.yml` `!!js` expressions) through three variables:
|
|
25
|
+
|
|
26
|
+
- `DSH_CC_RESUME_SESSION=<id>` — explicitly resume that session id
|
|
27
|
+
(`--resume <id>` / `--resume=<id>`).
|
|
28
|
+
- `DSH_CC_RESUME_SESSION=''` — an explicit fresh start (`--new`/`-n`, or a
|
|
29
|
+
freshly created worktree); the TUI must not read any marker.
|
|
30
|
+
- `DSH_CC_AUTO_RESUME='1'` — no explicit choice was made, so the TUI reads
|
|
31
|
+
its own project resume marker and resumes if one exists. Only ever set
|
|
32
|
+
when `DSH_CC_RESUME_SESSION` is left undefined.
|
|
33
|
+
- `DSH_CC_CONTINUE='1'` — the user passed `-c`/`--continue`; the TUI shows
|
|
34
|
+
a "no previous session to continue" notice when no marker exists
|
|
35
|
+
(previously this was a one-line launcher stderr hint; the marker-read
|
|
36
|
+
move into the TUI took that knowledge away from the launcher).
|
|
37
|
+
|
|
38
|
+
`DSH_CC_RESUME_SESSION`, `DSH_CC_AUTO_RESUME`, and `DSH_CC_CONTINUE` are
|
|
39
|
+
**launcher-owned**: the bin sanitizes them from the inherited environment at
|
|
40
|
+
entry (a parent dsh-cc TUI leaks them to a child launcher) and re-derives
|
|
41
|
+
them only from the current invocation's argv. Do not set them manually when
|
|
42
|
+
launching `dsh-cc`.
|
|
43
|
+
|
|
44
|
+
### Mixed-version degradation
|
|
45
|
+
|
|
46
|
+
Because the launcher npm package and the `tui` profile plugin version are
|
|
47
|
+
locked at install time, a mismatch only appears after upgrading the launcher
|
|
48
|
+
without reinstalling the profile:
|
|
49
|
+
|
|
50
|
+
- New launcher + old plugin: the plugin never sees `DSH_CC_AUTO_RESUME`, so
|
|
51
|
+
auto-resume is silently off — `/resume` still works for manual selection.
|
|
52
|
+
Fix by reinstalling the profile (`dsh-cc` re-runs bootstrap on a fresh
|
|
53
|
+
profile).
|
|
54
|
+
- Old launcher + new plugin: the old launcher reads the legacy marker and
|
|
55
|
+
the new plugin dual-writes it, so the common same-directory-restart path
|
|
56
|
+
keeps working.
|
|
57
|
+
|
|
58
|
+
This package does **not** ship a `dsh-tui` binary.
|
package/bin/dsh-cc.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Optional shortcut for `dsh --profile tui`. Canonical command remains
|
|
4
|
+
* `dsh --profile tui`. Do not ship a `dsh-tui` bin — that name belongs to
|
|
5
|
+
* the unrelated published dsh-TUI product.
|
|
6
|
+
*/
|
|
7
|
+
import { spawn, spawnSync } from 'node:child_process'
|
|
8
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
9
|
+
import { homedir } from 'node:os'
|
|
10
|
+
import { dirname, join } from 'node:path'
|
|
11
|
+
import { fileURLToPath } from 'node:url'
|
|
12
|
+
import { bootstrapCommand, dshUnavailableMessage, existingWorktreeDecision, interceptResume, parseWorktreeFlag, planWorktree, PROFILE, sanitizeInheritedEnv, slugRetryDecision, spawnEnv, worktreeAddArgv, worktreeEnv } from '../bootstrap.mjs'
|
|
13
|
+
|
|
14
|
+
const here = dirname(fileURLToPath(import.meta.url))
|
|
15
|
+
const ownVersion = JSON.parse(readFileSync(join(here, '..', 'package.json'), 'utf8')).version
|
|
16
|
+
|
|
17
|
+
if (process.argv.includes('--version') || process.argv.includes('-V')) {
|
|
18
|
+
console.log(ownVersion)
|
|
19
|
+
process.exit(0)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const home = process.env.DSH_HOME || join(homedir(), '.dsh')
|
|
23
|
+
const profileDir = join(home, 'profiles', PROFILE)
|
|
24
|
+
const add = bootstrapCommand(existsSync(join(profileDir, 'package.json')), ownVersion)
|
|
25
|
+
if (add !== undefined) {
|
|
26
|
+
console.error(`dsh-cc: initializing profile "${PROFILE}"…`)
|
|
27
|
+
const installed = spawnSync('dsh', add, { encoding: 'utf8', stdio: 'inherit' })
|
|
28
|
+
// A spawn error (e.g. dsh not on PATH) leaves status null — that is a
|
|
29
|
+
// missing-CLI problem, not an install failure.
|
|
30
|
+
if (installed.error) {
|
|
31
|
+
console.error(dshUnavailableMessage())
|
|
32
|
+
process.exit(1)
|
|
33
|
+
}
|
|
34
|
+
if (installed.status !== 0) {
|
|
35
|
+
console.error(`dsh-cc: plugin install failed. Retry:\n dsh ${add.join(' ')}`)
|
|
36
|
+
process.exit(installed.status ?? 1)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// A parent dsh-cc TUI process leaks DSH_CC_RESUME_SESSION / DSH_CC_AUTO_RESUME
|
|
41
|
+
// / DSH_CC_CONTINUE into a child launcher's environment. Strip them up front —
|
|
42
|
+
// before any flag is parsed — so the three are re-derived only from THIS
|
|
43
|
+
// invocation's argv (see sanitizeInheritedEnv). In particular an inherited
|
|
44
|
+
// DSH_CC_AUTO_RESUME=1 would otherwise defeat an explicit --new / --worktree.
|
|
45
|
+
const env0 = sanitizeInheritedEnv({ ...process.env })
|
|
46
|
+
|
|
47
|
+
// `--worktree [name]` is intercepted here (never forwarded to dsh): the
|
|
48
|
+
// launcher creates `<repoRoot>/.claude/worktrees/<slug>` itself and starts
|
|
49
|
+
// the session inside it, marking it via DSH_CC_WORKTREE so the TUI offers
|
|
50
|
+
// cleanup at /quit time.
|
|
51
|
+
const worktree = parseWorktreeFlag(process.argv.slice(2))
|
|
52
|
+
let spawnCwd
|
|
53
|
+
if (worktree.name !== undefined) {
|
|
54
|
+
// A fresh worktree starts a new session (--new equivalent): set at the end
|
|
55
|
+
// of this block. A REUSED worktree leaves DSH_CC_RESUME_SESSION undefined,
|
|
56
|
+
// so interceptResume sets DSH_CC_AUTO_RESUME=1 and the TUI resumes the
|
|
57
|
+
// project's last session.
|
|
58
|
+
const top = spawnSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8' })
|
|
59
|
+
if (top.error || top.status !== 0) {
|
|
60
|
+
console.error('dsh-cc: --worktree requires a git repository (run from inside a git working tree).')
|
|
61
|
+
process.exit(1)
|
|
62
|
+
}
|
|
63
|
+
const repoRoot = top.stdout.trim()
|
|
64
|
+
// Drop stale registrations left by crashed sessions before planning paths.
|
|
65
|
+
spawnSync('git', ['-C', repoRoot, 'worktree', 'prune'], { encoding: 'utf8' })
|
|
66
|
+
const head = spawnSync('git', ['-C', repoRoot, 'rev-parse', 'HEAD'], { encoding: 'utf8' })
|
|
67
|
+
if (head.error || head.status !== 0) {
|
|
68
|
+
console.error('dsh-cc: could not resolve HEAD; is this a repository without commits?')
|
|
69
|
+
process.exit(1)
|
|
70
|
+
}
|
|
71
|
+
const named = worktree.name !== null
|
|
72
|
+
let plan = null
|
|
73
|
+
let created = false
|
|
74
|
+
for (let attempt = 1; attempt <= 5; attempt += 1) {
|
|
75
|
+
let candidate
|
|
76
|
+
try {
|
|
77
|
+
candidate = planWorktree(repoRoot, worktree.name)
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error(`dsh-cc: ${error.message}`)
|
|
80
|
+
process.exit(1)
|
|
81
|
+
}
|
|
82
|
+
const pathExists = existsSync(candidate.worktreePath)
|
|
83
|
+
if (existingWorktreeDecision({ named, pathExists }) === 'reuse') {
|
|
84
|
+
plan = candidate
|
|
85
|
+
created = false
|
|
86
|
+
break
|
|
87
|
+
}
|
|
88
|
+
let failure = pathExists
|
|
89
|
+
? `path already exists: ${candidate.worktreePath}`
|
|
90
|
+
: null
|
|
91
|
+
if (failure === null) {
|
|
92
|
+
const add = spawnSync('git', ['-C', repoRoot, ...worktreeAddArgv(candidate)], { encoding: 'utf8' })
|
|
93
|
+
if (add.error || add.status !== 0) {
|
|
94
|
+
failure = (add.stderr ?? (add.error ? String(add.error) : '')).trim() || 'git worktree add failed'
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (failure === null) {
|
|
98
|
+
plan = candidate
|
|
99
|
+
created = true
|
|
100
|
+
break
|
|
101
|
+
}
|
|
102
|
+
if (slugRetryDecision({ named, attempt }) === 'fail') {
|
|
103
|
+
console.error(`dsh-cc: could not create worktree "${candidate.slug}": ${failure}`)
|
|
104
|
+
if (named) {
|
|
105
|
+
console.error('dsh-cc: pick another name, or remove the stale one: '
|
|
106
|
+
+ `git -C ${repoRoot} worktree remove --force ${candidate.worktreePath}`)
|
|
107
|
+
}
|
|
108
|
+
process.exit(1)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (plan === null) {
|
|
112
|
+
console.error('dsh-cc: could not allocate a worktree name after several attempts; try --worktree <name>.')
|
|
113
|
+
process.exit(1)
|
|
114
|
+
}
|
|
115
|
+
Object.assign(env0, worktreeEnv(plan, repoRoot, head.stdout.trim()))
|
|
116
|
+
spawnCwd = plan.worktreePath
|
|
117
|
+
const verb = created ? 'created' : 'reusing'
|
|
118
|
+
console.error(`dsh-cc: worktree "${plan.slug}" ${verb} at ${plan.worktreePath} (branch ${plan.branch})`)
|
|
119
|
+
// A freshly created isolation worktree starts a fresh session (equivalent
|
|
120
|
+
// to --new): the empty sentinel suppresses auto-resume. A reused worktree
|
|
121
|
+
// leaves the env undefined, so the TUI auto-resumes the project's last
|
|
122
|
+
// session (per the project model, main checkout and worktree share it).
|
|
123
|
+
// interceptResume still lets an explicit --resume on argv win.
|
|
124
|
+
if (created) env0.DSH_CC_RESUME_SESSION = ''
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const { env, args } = interceptResume(undefined, worktree.args, env0)
|
|
128
|
+
env.NODE_ENV ??= 'production'
|
|
129
|
+
// Stamp the profile so the TUI plugin can surface it as ctx.get('dshProfile').
|
|
130
|
+
env.DSH_CC_PROFILE = PROFILE
|
|
131
|
+
// Default NODE_COMPILE_CACHE so the child reuses compiled modules across
|
|
132
|
+
// boots (see spawnEnv); a user-set value always wins.
|
|
133
|
+
const spawnEnvironment = spawnEnv(env, home)
|
|
134
|
+
|
|
135
|
+
const child = spawn('dsh', ['--profile', PROFILE, ...args], {
|
|
136
|
+
env: spawnEnvironment,
|
|
137
|
+
stdio: 'inherit',
|
|
138
|
+
...(spawnCwd === undefined ? {} : { cwd: spawnCwd }),
|
|
139
|
+
})
|
|
140
|
+
// ENOENT (dsh missing from PATH) never reaches the exit handler — handle it
|
|
141
|
+
// here with the same guidance as the bootstrap-install path.
|
|
142
|
+
child.on('error', (error) => {
|
|
143
|
+
if (/** @type {NodeJS.ErrnoException} */ (error).code === 'ENOENT') {
|
|
144
|
+
console.error(dshUnavailableMessage())
|
|
145
|
+
process.exit(1)
|
|
146
|
+
}
|
|
147
|
+
throw error
|
|
148
|
+
})
|
|
149
|
+
child.on('exit', (code, signal) => {
|
|
150
|
+
if (signal) {
|
|
151
|
+
process.kill(process.pid, signal)
|
|
152
|
+
return
|
|
153
|
+
}
|
|
154
|
+
process.exit(code ?? 0)
|
|
155
|
+
})
|
package/bootstrap.mjs
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First-run helpers for the dsh-cc launcher. Pure so tests can drive the
|
|
3
|
+
* decision table without spawning dsh.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
|
|
8
|
+
export const PROFILE = 'tui'
|
|
9
|
+
export const BUNDLES = [
|
|
10
|
+
'@dsh-cc/bundle-permissions',
|
|
11
|
+
'@dsh-cc/bundle-shell',
|
|
12
|
+
'@dsh-cc/bundle-tui',
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Scan dsh-cc args for resume-mode flags and translate them into the env
|
|
17
|
+
* contract the TUI plugin consumes. Collection is order-independent: all
|
|
18
|
+
* flags are gathered during the scan, then applied once afterwards with
|
|
19
|
+
* fixed precedence `--resume <id>` / `--resume=<id>` > `--new` > `-c`.
|
|
20
|
+
*
|
|
21
|
+
* After precedence resolution the env is finished:
|
|
22
|
+
* - `DSH_CC_CONTINUE='1'` when `-c`/`--continue` was requested (the TUI
|
|
23
|
+
* shows a "no previous session to continue" notice when no marker exists).
|
|
24
|
+
* - `DSH_CC_AUTO_RESUME='1'` exactly when `DSH_CC_RESUME_SESSION` is left
|
|
25
|
+
* undefined — i.e. no explicit `--resume`/`--new` chose the session. The
|
|
26
|
+
* TUI then reads its own project resume marker. An explicit `--resume` or
|
|
27
|
+
* `--new` naturally suppresses AUTO_RESUME.
|
|
28
|
+
*
|
|
29
|
+
* Every resume-mode flag is stripped from the forwarded args; combined
|
|
30
|
+
* shorts (e.g. -cn) are not recognized — each flag must be its own token.
|
|
31
|
+
* The launcher never reads a marker itself: the TUI owns marker reads.
|
|
32
|
+
*
|
|
33
|
+
* @param {string | undefined} resumeFlag
|
|
34
|
+
* @param {string[]} rest
|
|
35
|
+
* @param {Record<string, string>} env
|
|
36
|
+
* @returns {{ env: Record<string, string>, args: string[], continueRequested: boolean }}
|
|
37
|
+
*/
|
|
38
|
+
export function interceptResume(resumeFlag, rest, env = {}) {
|
|
39
|
+
const nextEnv = { ...env }
|
|
40
|
+
const args = []
|
|
41
|
+
if (typeof resumeFlag === 'string' && resumeFlag.length > 0) {
|
|
42
|
+
nextEnv.DSH_CC_RESUME_SESSION = resumeFlag
|
|
43
|
+
}
|
|
44
|
+
let resumeId
|
|
45
|
+
let hasResumeId = false
|
|
46
|
+
let newSession = false
|
|
47
|
+
let continueRequested = false
|
|
48
|
+
for (let i = 0; i < rest.length; i += 1) {
|
|
49
|
+
const token = rest[i]
|
|
50
|
+
if (token === '--resume') {
|
|
51
|
+
const value = rest[i + 1]
|
|
52
|
+
if (value !== undefined && !value.startsWith('-')) {
|
|
53
|
+
resumeId = value
|
|
54
|
+
hasResumeId = true
|
|
55
|
+
i += 1
|
|
56
|
+
continue
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (token.startsWith('--resume=')) {
|
|
60
|
+
resumeId = token.slice('--resume='.length)
|
|
61
|
+
hasResumeId = true
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
if (token === '--new' || token === '-n') {
|
|
65
|
+
newSession = true
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
if (token === '--continue' || token === '-c') {
|
|
69
|
+
continueRequested = true
|
|
70
|
+
continue
|
|
71
|
+
}
|
|
72
|
+
args.push(token)
|
|
73
|
+
}
|
|
74
|
+
if (hasResumeId) {
|
|
75
|
+
nextEnv.DSH_CC_RESUME_SESSION = resumeId
|
|
76
|
+
} else if (newSession) {
|
|
77
|
+
// Empty string is the fresh-session sentinel (--new / freshly created
|
|
78
|
+
// worktree): the TUI starts fresh and must not read a marker.
|
|
79
|
+
nextEnv.DSH_CC_RESUME_SESSION = ''
|
|
80
|
+
}
|
|
81
|
+
if (continueRequested) nextEnv.DSH_CC_CONTINUE = '1'
|
|
82
|
+
if (nextEnv.DSH_CC_RESUME_SESSION === undefined) nextEnv.DSH_CC_AUTO_RESUME = '1'
|
|
83
|
+
return { env: nextEnv, args, continueRequested }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Strip the launcher-owned resume-session env vars that a parent dsh-cc TUI
|
|
88
|
+
* process leaks into a child launcher. The launcher must re-derive these
|
|
89
|
+
* from its own argv only (via {@link interceptResume}): an inherited
|
|
90
|
+
* `DSH_CC_AUTO_RESUME=1` would otherwise defeat the `--new`/`--worktree`
|
|
91
|
+
* gate (an explicit fresh start being ignored in favour of auto-resume), and
|
|
92
|
+
* an inherited `DSH_CC_RESUME_SESSION`/`DSH_CC_CONTINUE` would inject a
|
|
93
|
+
* session the user never asked this invocation to resume.
|
|
94
|
+
*
|
|
95
|
+
* Called once at the bin's entry, before any flag is parsed or the worktree
|
|
96
|
+
* block runs. Returns a NEW object — the input is never mutated.
|
|
97
|
+
*
|
|
98
|
+
* @param {Record<string, string | undefined>} env
|
|
99
|
+
* @returns {Record<string, string | undefined>}
|
|
100
|
+
*/
|
|
101
|
+
export function sanitizeInheritedEnv(env) {
|
|
102
|
+
const out = { ...env }
|
|
103
|
+
delete out.DSH_CC_RESUME_SESSION
|
|
104
|
+
delete out.DSH_CC_AUTO_RESUME
|
|
105
|
+
delete out.DSH_CC_CONTINUE
|
|
106
|
+
return out
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Guidance printed on every path where the `dsh` CLI cannot be spawned
|
|
111
|
+
* (not on PATH). Two lines: what happened, then how to fix it.
|
|
112
|
+
* @returns {string}
|
|
113
|
+
*/
|
|
114
|
+
export function dshUnavailableMessage() {
|
|
115
|
+
return 'dsh-cc: the `dsh` CLI is not on PATH.\n'
|
|
116
|
+
+ 'Install deepseek-harness first, e.g.: npm install -g @deepseek-ai/dsh'
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Environment for the final `dsh` spawn. Defaults `NODE_COMPILE_CACHE` to
|
|
121
|
+
* `<dshHome>/.cache/node-compile-cache` so the child's module-compile work is
|
|
122
|
+
* reused across boots (Node creates the dir itself — never mkdir here). A
|
|
123
|
+
* user-set `NODE_COMPILE_CACHE` always wins. Returns a NEW object — the
|
|
124
|
+
* input is never mutated.
|
|
125
|
+
*
|
|
126
|
+
* @param {Record<string, string | undefined>} env
|
|
127
|
+
* @param {string} dshHome
|
|
128
|
+
* @returns {Record<string, string | undefined>}
|
|
129
|
+
*/
|
|
130
|
+
export function spawnEnv(env, dshHome) {
|
|
131
|
+
const out = { ...env }
|
|
132
|
+
out.NODE_COMPILE_CACHE ??= join(dshHome, '.cache', 'node-compile-cache')
|
|
133
|
+
return out
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @param {boolean} profileExists
|
|
138
|
+
* @param {string} version
|
|
139
|
+
*/
|
|
140
|
+
export function bootstrapCommand(profileExists, version) {
|
|
141
|
+
if (profileExists) return undefined
|
|
142
|
+
return ['plugin', '--profile', PROFILE, 'add', ...BUNDLES.map(name => `${name}@${version}`)]
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// --- worktree support (--worktree) ------------------------------------------
|
|
146
|
+
// Slug, path, and branch rules mirror
|
|
147
|
+
// packages/workspace/tool-git-worktree/src/worktree.ts exactly — keep the two
|
|
148
|
+
// in sync. The tool package cannot be imported here: the launcher is plain
|
|
149
|
+
// dependency-free JS that runs before any build.
|
|
150
|
+
|
|
151
|
+
/** Env var carrying the launcher's worktree-session descriptor to the TUI. */
|
|
152
|
+
export const WORKTREE_ENV = 'DSH_CC_WORKTREE'
|
|
153
|
+
|
|
154
|
+
const MAX_SLUG_LENGTH = 64
|
|
155
|
+
const SEGMENT = /^[a-zA-Z0-9._-]+$/
|
|
156
|
+
const ADJECTIVES = ['swift', 'bright', 'calm', 'keen', 'bold']
|
|
157
|
+
const NOUNS = ['fox', 'owl', 'elm', 'oak', 'ray']
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Scan dsh-cc args for the `--worktree` flag and strip it. Name forms:
|
|
161
|
+
* `--worktree <name>`, `--worktree=<name>`, or bare `--worktree` (random
|
|
162
|
+
* name). A following token that starts with `-` is NOT taken as the name.
|
|
163
|
+
* @param {string[]} args
|
|
164
|
+
* @returns {{ name: string | null | undefined, args: string[] }}
|
|
165
|
+
* `name` is undefined when the flag is absent, null when present without a
|
|
166
|
+
* name, and the slug otherwise; `args` is the remainder to forward.
|
|
167
|
+
*/
|
|
168
|
+
export function parseWorktreeFlag(args) {
|
|
169
|
+
let name
|
|
170
|
+
const rest = []
|
|
171
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
172
|
+
const token = args[i]
|
|
173
|
+
if (token === '--worktree') {
|
|
174
|
+
const value = args[i + 1]
|
|
175
|
+
if (value !== undefined && !value.startsWith('-')) {
|
|
176
|
+
name = value
|
|
177
|
+
i += 1
|
|
178
|
+
} else {
|
|
179
|
+
name = null
|
|
180
|
+
}
|
|
181
|
+
continue
|
|
182
|
+
}
|
|
183
|
+
if (token.startsWith('--worktree=')) {
|
|
184
|
+
const value = token.slice('--worktree='.length)
|
|
185
|
+
name = value.length > 0 ? value : null
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
188
|
+
rest.push(token)
|
|
189
|
+
}
|
|
190
|
+
return { name, args: rest }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Validate a worktree slug. Identical rules and messages to the tool
|
|
195
|
+
* package's validateSlug.
|
|
196
|
+
* @param {string} slug
|
|
197
|
+
*/
|
|
198
|
+
export function validateWorktreeSlug(slug) {
|
|
199
|
+
if (slug.length > MAX_SLUG_LENGTH) {
|
|
200
|
+
throw new Error(
|
|
201
|
+
`invalid worktree name: must be ${MAX_SLUG_LENGTH} characters or fewer (got ${slug.length})`,
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
for (const segment of slug.split('/')) {
|
|
205
|
+
if (segment === '.' || segment === '..') {
|
|
206
|
+
throw new Error(
|
|
207
|
+
`invalid worktree name "${slug}": must not contain "." or ".." path segments`,
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
if (!SEGMENT.test(segment)) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`invalid worktree name "${slug}": each "/"-separated segment must be non-empty and contain only letters, digits, dots, underscores, and dashes`,
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* @param {string} slug - A validated worktree slug.
|
|
220
|
+
* @returns {string} the flattened single-directory name (`user/feature` → `user+feature`).
|
|
221
|
+
*/
|
|
222
|
+
export function flattenSlug(slug) {
|
|
223
|
+
return slug.replaceAll('/', '+')
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @param {string} slug - A validated worktree slug.
|
|
228
|
+
* @returns {string} the branch backing the worktree.
|
|
229
|
+
*/
|
|
230
|
+
export function worktreeBranch(slug) {
|
|
231
|
+
return `worktree-${flattenSlug(slug)}`
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* @param {string} repoRoot
|
|
236
|
+
* @param {string} slug - A validated worktree slug.
|
|
237
|
+
* @returns {string} the absolute on-disk worktree path.
|
|
238
|
+
*/
|
|
239
|
+
export function worktreePathFor(repoRoot, slug) {
|
|
240
|
+
return join(repoRoot, '.claude', 'worktrees', flattenSlug(slug))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Generate a random slug (`swift-fox-8f3a`), same word lists as the tool
|
|
245
|
+
* package. Injectable `rand` keeps tests deterministic.
|
|
246
|
+
* @param {() => number} [rand]
|
|
247
|
+
* @returns {string}
|
|
248
|
+
*/
|
|
249
|
+
export function randomWorktreeSlug(rand = Math.random) {
|
|
250
|
+
const adjective = ADJECTIVES[Math.floor(rand() * ADJECTIVES.length)]
|
|
251
|
+
const noun = NOUNS[Math.floor(rand() * NOUNS.length)]
|
|
252
|
+
const suffix = rand().toString(36).slice(2, 6)
|
|
253
|
+
return `${adjective}-${noun}-${suffix}`
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* @param {string} repoRoot
|
|
258
|
+
* @param {string | null} name - Requested slug, or null for a random one.
|
|
259
|
+
* @param {() => number} [rand]
|
|
260
|
+
* @returns {{ slug: string, worktreePath: string, branch: string }}
|
|
261
|
+
*/
|
|
262
|
+
export function planWorktree(repoRoot, name, rand) {
|
|
263
|
+
const slug = name === null || name === undefined ? randomWorktreeSlug(rand) : name
|
|
264
|
+
validateWorktreeSlug(slug)
|
|
265
|
+
return { slug, worktreePath: worktreePathFor(repoRoot, slug), branch: worktreeBranch(slug) }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* argv for `git worktree add -B <branch> <path> HEAD` (execFile form — no
|
|
270
|
+
* shell, so no quoting concerns). `-B` resets a stale orphan branch left by
|
|
271
|
+
* a removed worktree.
|
|
272
|
+
* @param {{ worktreePath: string, branch: string }} plan
|
|
273
|
+
* @returns {string[]}
|
|
274
|
+
*/
|
|
275
|
+
export function worktreeAddArgv(plan) {
|
|
276
|
+
return ['worktree', 'add', '-B', plan.branch, plan.worktreePath, 'HEAD']
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Env fragment handed to the spawned dsh process so the TUI can recognize
|
|
281
|
+
* this session as launcher-managed worktree session at /quit time.
|
|
282
|
+
* @param {{ worktreePath: string, branch: string }} plan
|
|
283
|
+
* @param {string} repoRoot
|
|
284
|
+
* @param {string} baseHead - The commit the worktree was based on.
|
|
285
|
+
* @returns {Record<string, string>}
|
|
286
|
+
*/
|
|
287
|
+
export function worktreeEnv(plan, repoRoot, baseHead) {
|
|
288
|
+
return {
|
|
289
|
+
[WORKTREE_ENV]: JSON.stringify({
|
|
290
|
+
repoRoot,
|
|
291
|
+
worktreePath: plan.worktreePath,
|
|
292
|
+
branch: plan.branch,
|
|
293
|
+
baseHead,
|
|
294
|
+
}),
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Collision policy for a *named* `--worktree <slug>` whose path already
|
|
300
|
+
* exists: reuse it (do not `git worktree add`). Random slugs still fail so
|
|
301
|
+
* they can retry a fresh name.
|
|
302
|
+
* @param {{ named: boolean, pathExists: boolean }} params
|
|
303
|
+
* @returns {'reuse' | 'create'}
|
|
304
|
+
*/
|
|
305
|
+
export function existingWorktreeDecision({ named, pathExists }) {
|
|
306
|
+
return named && pathExists ? 'reuse' : 'create'
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Collision policy for `git worktree add`: a random slug may retry with a
|
|
311
|
+
* fresh name; a user-named slug fails immediately with an actionable error.
|
|
312
|
+
* @param {{ named: boolean, attempt: number, maxAttempts?: number }} params
|
|
313
|
+
* @returns {'retry' | 'fail'}
|
|
314
|
+
*/
|
|
315
|
+
export function slugRetryDecision({ named, attempt, maxAttempts = 5 }) {
|
|
316
|
+
return !named && attempt < maxAttempts ? 'retry' : 'fail'
|
|
317
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dsh-cc/cli",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Optional dsh-cc shortcut: bootstraps the tui profile and runs dsh --profile tui",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"dsh-cc": "./bin/dsh-cc.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"bootstrap.mjs"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/dsh-cc/dsh-cc.git",
|
|
16
|
+
"directory": "packages/launcher/tui"
|
|
17
|
+
},
|
|
18
|
+
"license": "Apache-2.0",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
}
|
|
22
|
+
}
|