@hyperwindmill/caretaker-cli 0.19.0 → 0.20.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/dist/cli/web/scheduler/locks.d.ts +6 -0
- package/dist/cli/web/scheduler/locks.d.ts.map +1 -1
- package/dist/cli/web/scheduler/locks.js +6 -0
- package/dist/cli/web/scheduler/task_strategy.d.ts.map +1 -1
- package/dist/cli/web/scheduler/task_strategy.js +82 -3
- package/dist/cli/web/server.d.ts.map +1 -1
- package/dist/cli/web/server.js +111 -6
- package/dist/harness/tools/builtin/task_tools.d.ts.map +1 -1
- package/dist/harness/tools/builtin/task_tools.js +48 -6
- package/dist/lib/repo_url.d.ts +12 -0
- package/dist/lib/repo_url.d.ts.map +1 -0
- package/dist/lib/repo_url.js +36 -0
- package/dist/lib/task_git.d.ts +53 -1
- package/dist/lib/task_git.d.ts.map +1 -1
- package/dist/lib/task_git.js +197 -5
- package/dist/store/json.d.ts +1 -0
- package/dist/store/json.d.ts.map +1 -1
- package/dist/store/json.js +9 -0
- package/dist/webview/standalone.js +1185 -1047
- package/dist/webview/standalone.js.map +4 -4
- package/package.json +3 -3
package/dist/lib/task_git.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { execFile, exec as execCb } from 'node:child_process';
|
|
2
2
|
import { promisify } from 'node:util';
|
|
3
|
-
import { rm } from 'node:fs/promises';
|
|
4
|
-
import { join, relative, dirname } from 'node:path';
|
|
3
|
+
import { rm, mkdir, readdir, rename } from 'node:fs/promises';
|
|
4
|
+
import { join, relative, dirname, basename } from 'node:path';
|
|
5
5
|
import { dataDir } from '../store/db.js';
|
|
6
6
|
import { commandEnv } from '../harness/tools/builtin/shell-env.js';
|
|
7
7
|
import { execInContainer } from './docker.js';
|
|
8
|
+
import { decrypt, isEncrypted } from './encryption.js';
|
|
8
9
|
const exec = promisify(execFile);
|
|
9
10
|
const execShell = promisify(execCb);
|
|
10
|
-
async function git(cwd, args) {
|
|
11
|
+
async function git(cwd, args, extraEnv, timeout) {
|
|
11
12
|
const { stdout } = await exec('git', args, {
|
|
12
13
|
cwd,
|
|
13
|
-
env: commandEnv(),
|
|
14
|
+
env: { ...commandEnv(), ...extraEnv },
|
|
14
15
|
maxBuffer: 32 * 1024 * 1024,
|
|
16
|
+
timeout,
|
|
15
17
|
});
|
|
16
18
|
return stdout.trim();
|
|
17
19
|
}
|
|
@@ -23,6 +25,192 @@ export async function isGitRepo(dir) {
|
|
|
23
25
|
return false;
|
|
24
26
|
}
|
|
25
27
|
}
|
|
28
|
+
// Inline credential helper: git shells this out and reads username/password
|
|
29
|
+
// from stdout. The token travels only in the child env (CARETAKER_GIT_TOKEN)
|
|
30
|
+
// — never on argv (visible in `ps`), never in .git/config.
|
|
31
|
+
const CRED_HELPER = '!f(){ echo username=x-access-token; echo password=$CARETAKER_GIT_TOKEN; }; f';
|
|
32
|
+
export function gitAuthArgs(hasToken) {
|
|
33
|
+
if (!hasToken)
|
|
34
|
+
return [];
|
|
35
|
+
// First -c clears any configured helpers so ours is the only one consulted —
|
|
36
|
+
// a system helper answering first with stale credentials would shadow the token.
|
|
37
|
+
return ['-c', 'credential.helper=', '-c', `credential.helper=${CRED_HELPER}`];
|
|
38
|
+
}
|
|
39
|
+
export function gitAuthEnv(token) {
|
|
40
|
+
// GIT_TERMINAL_PROMPT=0: the scheduler must never hang on an interactive
|
|
41
|
+
// username/password prompt — fail fast with a real error instead.
|
|
42
|
+
const env = { GIT_TERMINAL_PROMPT: '0' };
|
|
43
|
+
if (token)
|
|
44
|
+
env.CARETAKER_GIT_TOKEN = isEncrypted(token) ? decrypt(token) : token;
|
|
45
|
+
return env;
|
|
46
|
+
}
|
|
47
|
+
// Network ops only: a hung TCP connection would hold syncingProjects and the
|
|
48
|
+
// task lock until the OS timeout. Local git commands stay unbounded on purpose
|
|
49
|
+
// (a worktree add on a huge repo is slow but legitimate).
|
|
50
|
+
const NET_TIMEOUT_MS = 120_000;
|
|
51
|
+
/** Network git ops (clone/fetch/pull/push): auth + a readable error carrying
|
|
52
|
+
* git's stderr, which becomes blockedReason / UI copy downstream. */
|
|
53
|
+
async function netGit(cwd, args, token) {
|
|
54
|
+
try {
|
|
55
|
+
return await git(cwd, [...gitAuthArgs(!!token), ...args], gitAuthEnv(token), NET_TIMEOUT_MS);
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
const e = err;
|
|
59
|
+
const detail = e.killed
|
|
60
|
+
? `timed out after ${NET_TIMEOUT_MS / 1000}s`
|
|
61
|
+
: (e.stderr || e.message || String(err)).toString().trim();
|
|
62
|
+
throw new Error(`git ${args[0]} failed: ${detail}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Last line of defence before a remote URL reaches git. Form and API
|
|
67
|
+
* validation are UX; this is the enforcement point, because a URL git accepts
|
|
68
|
+
* gets written into `.git/config` and passed on argv. Anything that put a
|
|
69
|
+
* credential-bearing URL into the config by another route — a hand-edited
|
|
70
|
+
* `caretaker.json`, a surface whose save path lacks the check, a restored
|
|
71
|
+
* backup — is stopped here rather than leaking the secret.
|
|
72
|
+
*/
|
|
73
|
+
function assertCleanRemoteUrl(url) {
|
|
74
|
+
let parsed;
|
|
75
|
+
try {
|
|
76
|
+
parsed = new URL(url);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return; // No scheme: a filesystem path, which cannot carry credentials.
|
|
80
|
+
}
|
|
81
|
+
if (parsed.username || parsed.password) {
|
|
82
|
+
throw new Error('Refusing to use a repository URL with embedded credentials: git would store it in .git/config and expose it in the process list. Remove the credentials from the URL and use the access token field.');
|
|
83
|
+
}
|
|
84
|
+
// Deliberately NOT enforcing https here — that transport policy belongs to
|
|
85
|
+
// config validation (lib/repo_url.ts). A filesystem path is a legitimate
|
|
86
|
+
// remote for a local mirror and for the tests.
|
|
87
|
+
}
|
|
88
|
+
export async function pushBranch(worktreePath, branch, repo) {
|
|
89
|
+
assertCleanRemoteUrl(repo.url);
|
|
90
|
+
// Push to the configured URL, not "origin": correct even when workingDir is a
|
|
91
|
+
// pre-existing local repo whose origin points elsewhere.
|
|
92
|
+
await netGit(worktreePath, ['push', repo.url, `${branch}:${branch}`], repo.token);
|
|
93
|
+
}
|
|
94
|
+
export function projectWorkingDir(project) {
|
|
95
|
+
const dir = (project.workingDir || '').trim();
|
|
96
|
+
if (dir)
|
|
97
|
+
return dir;
|
|
98
|
+
// Remote-backed with no explicit dir: managed clone under the data dir.
|
|
99
|
+
// Resolved at call time (dataDir() follows CARETAKER_HOME), never persisted.
|
|
100
|
+
if ((project.repositoryUrl || '').trim())
|
|
101
|
+
return join(dataDir(), 'repos', String(project.id));
|
|
102
|
+
return '';
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The clone directory caretaker owns for this project, or null when the repo
|
|
106
|
+
* lives in a user-chosen `workingDir` (or the project isn't remote-backed).
|
|
107
|
+
* The delete path uses this: anything non-null is caretaker's to remove,
|
|
108
|
+
* anything null must never be touched.
|
|
109
|
+
*/
|
|
110
|
+
export function managedRepoDir(project) {
|
|
111
|
+
if ((project.workingDir || '').trim())
|
|
112
|
+
return null;
|
|
113
|
+
return projectWorkingDir(project) || null;
|
|
114
|
+
}
|
|
115
|
+
export async function projectRepoStatus(project) {
|
|
116
|
+
const dest = projectWorkingDir(project);
|
|
117
|
+
if (!dest)
|
|
118
|
+
return { state: 'absent' };
|
|
119
|
+
let entries;
|
|
120
|
+
try {
|
|
121
|
+
entries = await readdir(dest);
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return { state: 'absent' };
|
|
125
|
+
}
|
|
126
|
+
if (entries.length === 0)
|
|
127
|
+
return { state: 'absent' };
|
|
128
|
+
if (!(await isGitRepo(dest)))
|
|
129
|
+
return { state: 'broken' };
|
|
130
|
+
try {
|
|
131
|
+
const branch = await git(dest, ['rev-parse', '--abbrev-ref', 'HEAD']);
|
|
132
|
+
const commit = await git(dest, ['rev-parse', '--short', 'HEAD']);
|
|
133
|
+
return { state: 'cloned', branch, commit };
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return { state: 'cloned' };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Clone-or-pull a remote-backed project's repository. Progress is yielded for
|
|
141
|
+
* the ndjson endpoint; the heartbeat just drains it. Throws on failure —
|
|
142
|
+
* callers turn that into a blocked task / in-band error line.
|
|
143
|
+
*/
|
|
144
|
+
export async function* syncProjectRepo(project) {
|
|
145
|
+
const url = (project.repositoryUrl || '').trim();
|
|
146
|
+
if (!url)
|
|
147
|
+
throw new Error('Project has no repository URL');
|
|
148
|
+
assertCleanRemoteUrl(url);
|
|
149
|
+
const dest = projectWorkingDir(project);
|
|
150
|
+
const token = project.repositoryToken;
|
|
151
|
+
let status = await projectRepoStatus(project);
|
|
152
|
+
if (status.state === 'broken') {
|
|
153
|
+
// Auto-wipe only the managed default path. A user-chosen workingDir that
|
|
154
|
+
// isn't a repo could be anything (Documents, home…) — never delete it.
|
|
155
|
+
if ((project.workingDir || '').trim()) {
|
|
156
|
+
throw new Error(`Directory ${dest} exists but is not a git repository. ` +
|
|
157
|
+
`Refusing to overwrite a user-chosen directory — point the project at an empty or valid path.`);
|
|
158
|
+
}
|
|
159
|
+
yield { step: 'clean', message: `Removing broken checkout at ${dest}` };
|
|
160
|
+
await rm(dest, { recursive: true, force: true });
|
|
161
|
+
status = { state: 'absent' };
|
|
162
|
+
}
|
|
163
|
+
if (status.state === 'absent') {
|
|
164
|
+
const parent = dirname(dest);
|
|
165
|
+
await mkdir(parent, { recursive: true });
|
|
166
|
+
// Sweep temp dirs left by crashed clones (`<name>.cloning-<pid>`).
|
|
167
|
+
for (const entry of await readdir(parent)) {
|
|
168
|
+
if (entry.startsWith(`${basename(dest)}.cloning-`)) {
|
|
169
|
+
yield { step: 'clean', message: `Removing stale temp clone ${entry}` };
|
|
170
|
+
await rm(join(parent, entry), { recursive: true, force: true });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const tmp = `${dest}.cloning-${process.pid}`;
|
|
174
|
+
yield { step: 'clone', message: `Cloning ${url}` };
|
|
175
|
+
await netGit(parent, ['clone', url, tmp], token);
|
|
176
|
+
// Atomic hand-over: the destination only ever appears as a complete clone.
|
|
177
|
+
await rm(dest, { recursive: true, force: true }); // absent = missing or empty dir
|
|
178
|
+
await rename(tmp, dest);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
// Realign `origin` to the configured URL before touching the network:
|
|
182
|
+
// fetch/pull talk to whatever origin is, and the credential helper answers
|
|
183
|
+
// for ANY host it's asked about (no host matching). An inherited or
|
|
184
|
+
// pre-existing clone pointing elsewhere would be offered the project's
|
|
185
|
+
// token — confused deputy. `url` already passed assertCleanRemoteUrl.
|
|
186
|
+
const currentOrigin = await git(dest, ['remote', 'get-url', 'origin']).catch(() => null);
|
|
187
|
+
if (currentOrigin !== url) {
|
|
188
|
+
await git(dest, ['remote', currentOrigin === null ? 'add' : 'set-url', 'origin', url]);
|
|
189
|
+
yield { step: 'clean', message: `Realigned origin to ${url}` };
|
|
190
|
+
}
|
|
191
|
+
yield { step: 'pull', message: 'Fetching and fast-forwarding' };
|
|
192
|
+
await netGit(dest, ['fetch', 'origin'], token);
|
|
193
|
+
try {
|
|
194
|
+
await netGit(dest, ['pull', '--ff-only'], token);
|
|
195
|
+
}
|
|
196
|
+
catch (e) {
|
|
197
|
+
// Diverged (upstream force-push, stray local commit, realigned origin):
|
|
198
|
+
// ff-only then fails identically on every retry, so without a self-heal
|
|
199
|
+
// the project is stuck until someone deletes the dir by hand. Only
|
|
200
|
+
// caretaker writes to the MANAGED clone (tasks work in worktrees), so
|
|
201
|
+
// there is no user work to lose. A user-chosen workingDir is never reset
|
|
202
|
+
// — same rule as the `broken` wipe above.
|
|
203
|
+
if ((project.workingDir || '').trim())
|
|
204
|
+
throw e;
|
|
205
|
+
const upstream = await git(dest, ['rev-parse', '--abbrev-ref', '@{upstream}']).catch(() => null);
|
|
206
|
+
if (!upstream)
|
|
207
|
+
throw e;
|
|
208
|
+
yield { step: 'clean', message: `Diverged from ${upstream} — resetting the managed clone` };
|
|
209
|
+
await git(dest, ['reset', '--hard', upstream]);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
yield { step: 'done', message: `Repository ready at ${dest}`, status: await projectRepoStatus(project) };
|
|
213
|
+
}
|
|
26
214
|
function slug(title) {
|
|
27
215
|
return (title
|
|
28
216
|
.toLowerCase()
|
|
@@ -150,7 +338,11 @@ export async function finalizeDone(worktreePath) {
|
|
|
150
338
|
await git(mainRepo, ['worktree', 'prune']).catch(() => { });
|
|
151
339
|
}
|
|
152
340
|
}
|
|
153
|
-
export async function discardWorktree(worktreePath, title) {
|
|
341
|
+
export async function discardWorktree(worktreePath, title, push) {
|
|
154
342
|
await commitWip(worktreePath, title);
|
|
343
|
+
// Push BEFORE removal: a failed push aborts the discard so unpushed work
|
|
344
|
+
// never loses its worktree. Callers surface the error to the user/agent.
|
|
345
|
+
if (push)
|
|
346
|
+
await pushBranch(worktreePath, push.branch, push);
|
|
155
347
|
await finalizeDone(worktreePath);
|
|
156
348
|
}
|
package/dist/store/json.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AgentConfig, CaretakerConfig, McpServersFile, PluginsFile } from '../types.js';
|
|
2
|
+
export { validateRepositoryUrl } from '../lib/repo_url.js';
|
|
2
3
|
export declare function dataDir(): string;
|
|
3
4
|
export declare function configPath(): string;
|
|
4
5
|
export declare function agentsPath(): string;
|
package/dist/store/json.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/store/json.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/store/json.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAM7F,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAI3D,wBAAgB,OAAO,IAAI,MAAM,CAEhC;AACD,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AACD,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AACD,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AACD,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAED,eAAO,MAAM,aAAa,EAAE,eAG3B,CAAC;AAqDF,wBAAsB,UAAU,IAAI,OAAO,CAAC,eAAe,CAAC,CAE3D;AAED,wBAAsB,UAAU,CAAC,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAsBlE;AAED,wBAAsB,UAAU,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC,CAEzD;AAED,wBAAsB,UAAU,CAAC,CAAC,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAEhE;AAED,wBAAsB,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC,CASxD;AAED,wBAAsB,WAAW,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAElE;AAMD,wBAAsB,cAAc,IAAI,OAAO,CAAC,cAAc,CAAC,CAO9D;AAED,wBAAsB,cAAc,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAExE;AAID;;;GAGG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAO5E"}
|
package/dist/store/json.js
CHANGED
|
@@ -3,6 +3,10 @@ import { join } from 'node:path';
|
|
|
3
3
|
import { mkdir, readFile, writeFile, chmod, rename } from 'node:fs/promises';
|
|
4
4
|
import { existsSync } from 'node:fs';
|
|
5
5
|
import { encrypt, isEncrypted } from '../lib/encryption.js';
|
|
6
|
+
// Re-exported through the package's `./store` entry point so every surface that
|
|
7
|
+
// persists config (web server, VSCode extension) enforces the same rule on
|
|
8
|
+
// `projects[].repositoryUrl` before calling saveConfig.
|
|
9
|
+
export { validateRepositoryUrl } from '../lib/repo_url.js';
|
|
6
10
|
// Path accessors are resolved at call time, not import time, so test runs
|
|
7
11
|
// that share a process can switch CARETAKER_HOME between suites.
|
|
8
12
|
export function dataDir() {
|
|
@@ -88,6 +92,11 @@ export async function saveConfig(c) {
|
|
|
88
92
|
}
|
|
89
93
|
}
|
|
90
94
|
}
|
|
95
|
+
for (const project of c.projects || []) {
|
|
96
|
+
if (project.repositoryToken && !isEncrypted(project.repositoryToken)) {
|
|
97
|
+
project.repositoryToken = encrypt(project.repositoryToken);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
91
100
|
if (c.voice?.apiKey && !isEncrypted(c.voice.apiKey)) {
|
|
92
101
|
c.voice.apiKey = encrypt(c.voice.apiKey);
|
|
93
102
|
}
|