dsh-taskbridge 0.0.0-stage → 1.3.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.
- package/CHANGELOG.md +763 -0
- package/LICENSE +21 -0
- package/README.md +622 -3
- package/bin/dsh-delegate-codex-agent.js +115 -0
- package/bin/dsh-delegate-control.d.ts +90 -0
- package/bin/dsh-delegate-control.js +450 -0
- package/bin/dsh-delegate-mcp.d.ts +121 -0
- package/bin/dsh-delegate-mcp.js +273 -0
- package/codex/dsh-taskbridge.toml +247 -0
- package/cordis.patch.yml +6 -0
- package/docs/INTRODUCTION.md +15 -0
- package/docs/INTRODUCTION.zh.md +34 -0
- package/docs/auth-contract.md +138 -0
- package/docs/claude-code.md +31 -0
- package/docs/codex-integration.md +937 -0
- package/docs/contracts-stage-b.md +443 -0
- package/docs/conversation-placement.md +304 -0
- package/docs/plugin-market.md +30 -0
- package/docs/redaction-contract.md +127 -0
- package/docs/runtime-compatibility.md +223 -0
- package/docs/silent-wait.md +191 -0
- package/docs/taskbridge-upgrade.md +28 -0
- package/docs/taskdispatch-upgrade.md +25 -0
- package/docs/worker-permissions.md +347 -0
- package/docs/workspace-contract.md +538 -0
- package/icon.svg +1 -0
- package/lib/client.d.ts +118 -0
- package/lib/client.js +3232 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +138 -4
- package/src/auth/fence.js +361 -0
- package/src/auth/grants.js +789 -0
- package/src/client/api.js +2322 -0
- package/src/client/index.jsx +2333 -0
- package/src/client/styles.js +251 -0
- package/src/codex/installer.js +3299 -0
- package/src/codex/seam.js +429 -0
- package/src/codex/toml.js +784 -0
- package/src/config/defaults.js +164 -0
- package/src/config/redact.js +184 -0
- package/src/config/settings.js +629 -0
- package/src/config/store.js +186 -0
- package/src/config/validate.js +763 -0
- package/src/credentials/seam.js +202 -0
- package/src/http/execution-plane.js +456 -0
- package/src/http/respond.js +175 -0
- package/src/http/user-plane.js +1122 -0
- package/src/index.d.ts +332 -0
- package/src/index.js +657 -0
- package/src/jobs/activity.js +1008 -0
- package/src/jobs/engine.js +3614 -0
- package/src/jobs/history.js +1467 -0
- package/src/jobs/title.js +17 -0
- package/src/mcp/server.js +1821 -0
- package/src/mcp/wait.js +536 -0
- package/src/model/connectivity.js +371 -0
- package/src/model/discovery.js +582 -0
- package/src/model/native-catalog.js +68 -0
- package/src/model/openai-adapter.js +671 -0
- package/src/model/probe.js +264 -0
- package/src/model/providers.js +756 -0
- package/src/model/routing.js +321 -0
- package/src/review/preset.js +868 -0
- package/src/review/prompts.js +126 -0
- package/src/review/reviewer.js +933 -0
- package/src/runtime/agent-runner.js +1740 -0
- package/src/runtime/endpoint-discovery.js +118 -0
- package/src/runtime/permissions.js +1429 -0
- package/src/runtime/sandbox.js +358 -0
- package/src/runtime/session-placement.js +1121 -0
- package/src/workspace/audit.js +775 -0
- package/src/workspace/locks.js +167 -0
- package/src/workspace/preflight.js +424 -0
- package/src/workspace/worktrees.js +613 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
// The seam the user plane uses to reach the Codex integration.
|
|
2
|
+
//
|
|
3
|
+
// Why a seam and not a direct call: the user plane is exercised by HTTP tests that
|
|
4
|
+
// must not touch a real Codex home, and a deployment may legitimately have no
|
|
5
|
+
// Codex installed. The seam is the one place that decides *which* Codex home and
|
|
6
|
+
// *which* discovery file the plugin reports on, and it keeps that decision out of
|
|
7
|
+
// the route handler.
|
|
8
|
+
//
|
|
9
|
+
// Three properties are load bearing:
|
|
10
|
+
//
|
|
11
|
+
// * the Codex home is resolved per call, so an operator who changes `CODEX_HOME`
|
|
12
|
+
// and reloads DSH is reported the new directory without reinstalling;
|
|
13
|
+
// * the discovery path is the one *this* activation published (or would publish),
|
|
14
|
+
// so the MCP entry points Codex at the same DSH the plugin is running in —
|
|
15
|
+
// never at a repository checkout, never at a hard-coded default;
|
|
16
|
+
// * status only reads and run forwards an explicit operator action. The startup
|
|
17
|
+
// write is `refreshManagedIntegration`, which (a) refreshes a byte-exact shipped
|
|
18
|
+
// agent template and (b) upgrades an ALREADY INSTALLED managed
|
|
19
|
+
// `mcp_servers.dsh-delegate` entry that this build can prove is its own and that
|
|
20
|
+
// is merely stale — for example an entry written before `tool_timeout_sec`
|
|
21
|
+
// existed. It never creates a file or an entry, never adopts a same-name entry
|
|
22
|
+
// the plugin cannot prove it wrote, and never touches any other Codex setting.
|
|
23
|
+
//
|
|
24
|
+
// Which home the STARTUP check targets is a separate decision, made by
|
|
25
|
+
// {@link resolveStartupTarget}: an explicit `CODEX_HOME` wins, otherwise the target this
|
|
26
|
+
// plugin last installed into successfully (recorded in its own data directory), and only
|
|
27
|
+
// then the default. A default that is a link is refused with an explanation rather than
|
|
28
|
+
// guessed at, because walking it would write into a directory nobody named.
|
|
29
|
+
|
|
30
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
31
|
+
import { homedir } from 'node:os';
|
|
32
|
+
import { dirname, join } from 'node:path';
|
|
33
|
+
|
|
34
|
+
import {
|
|
35
|
+
inspectDirectory,
|
|
36
|
+
readCodexStatus,
|
|
37
|
+
refreshKnownAgentTemplate,
|
|
38
|
+
requireCodexHome,
|
|
39
|
+
resolveCodexHome,
|
|
40
|
+
runCodexAction,
|
|
41
|
+
} from './installer.js';
|
|
42
|
+
import { resolveEndpointDiscoveryPath } from '../runtime/endpoint-discovery.js';
|
|
43
|
+
import { writeFileAtomic } from '../config/store.js';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Where the plugin remembers the Codex home it installed into.
|
|
47
|
+
*
|
|
48
|
+
* It lives in the plugin's own data directory (which is the operator's `DSH_HOME`), is
|
|
49
|
+
* written only after a SUCCESSFUL explicit install, and holds one path. The automatic
|
|
50
|
+
* startup upgrade uses it so that a real, non-default Codex directory that was
|
|
51
|
+
* explicitly installed once keeps being upgraded — without the plugin ever guessing a
|
|
52
|
+
* location or walking a link to find one.
|
|
53
|
+
* @param {Record<string, string | undefined>} env - environment.
|
|
54
|
+
* @returns {string} absolute path of the installation record.
|
|
55
|
+
*/
|
|
56
|
+
export function installationRecordPath(env = process.env) {
|
|
57
|
+
const configured = typeof env.DSH_HOME === 'string' ? env.DSH_HOME.trim() : '';
|
|
58
|
+
const dshHome = configured === '' ? join(defaultHomeDirectory(), '.dsh') : configured;
|
|
59
|
+
return join(dshHome, 'data', 'dsh-delegate', 'codex-installation.json');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The home directory, resolved the same way the installer resolves it. */
|
|
63
|
+
function defaultHomeDirectory() {
|
|
64
|
+
return homedir();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The directory an unattended upgrade keeps its pre-write `config.toml` copy in.
|
|
69
|
+
*
|
|
70
|
+
* It is derived from the plugin's OWN data directory — the same root that holds the
|
|
71
|
+
* installation record — so the product never depends on a caller remembering to pass a
|
|
72
|
+
* destination, and never writes outside its data tree. The name matches the directory the
|
|
73
|
+
* activation path already uses (`<dataDir>/codex-config-backup`), so both callers share
|
|
74
|
+
* one convention. `DSH_HOME` decides that root; without one the documented `~/.dsh`
|
|
75
|
+
* fallback applies, exactly as for the record.
|
|
76
|
+
*
|
|
77
|
+
* The installer still treats a backup as a PRECONDITION: when this directory cannot be
|
|
78
|
+
* created or verified it refuses the upgrade instead of rewriting a config it could not
|
|
79
|
+
* preserve. Tests redirect `DSH_HOME` into their scratch tree, so no test ever reads or
|
|
80
|
+
* writes the operator's real data directory.
|
|
81
|
+
* @param {Record<string, string | undefined>} env - environment.
|
|
82
|
+
* @returns {string} absolute path of the default backup directory.
|
|
83
|
+
*/
|
|
84
|
+
export function defaultBackupDirectory(env = process.env) {
|
|
85
|
+
return join(dirname(installationRecordPath(env)), 'codex-config-backup');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Persist the Codex home an explicit install succeeded into.
|
|
90
|
+
*
|
|
91
|
+
* Only the installer's own action calls this, and only when that action reported no
|
|
92
|
+
* refusal and a non-null `changed` list; a failed install therefore never becomes the
|
|
93
|
+
* remembered target. A relative path is ignored (it would name nothing stable).
|
|
94
|
+
* @param {string} codexHome - absolute Codex home.
|
|
95
|
+
* @param {Record<string, string | undefined>} env - environment (for `DSH_HOME`).
|
|
96
|
+
* @returns {Promise<boolean>} whether the record was written.
|
|
97
|
+
*/
|
|
98
|
+
export async function rememberInstalledCodexHome(codexHome, env = process.env) {
|
|
99
|
+
if (typeof codexHome !== 'string' || codexHome.trim() === '') return false;
|
|
100
|
+
try {
|
|
101
|
+
const record = { schemaVersion: 1, codexHome, recordedAt: new Date().toISOString() };
|
|
102
|
+
await writeFileAtomic(installationRecordPath(env), `${JSON.stringify(record)}\n`);
|
|
103
|
+
return true;
|
|
104
|
+
} catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The Codex home this plugin last installed into successfully, or null.
|
|
111
|
+
*
|
|
112
|
+
* Reading the record is deliberately defensive: a missing, unreadable, malformed or
|
|
113
|
+
* relative record is "no trusted target", never a guess.
|
|
114
|
+
* @param {Record<string, string | undefined>} env - environment.
|
|
115
|
+
* @returns {string | null} absolute path, or null.
|
|
116
|
+
*/
|
|
117
|
+
export function readInstalledCodexHome(env = process.env) {
|
|
118
|
+
try {
|
|
119
|
+
const text = readFileSync(installationRecordPath(env), 'utf8');
|
|
120
|
+
const record = JSON.parse(text);
|
|
121
|
+
if (record === null || typeof record !== 'object' || Array.isArray(record)) return null;
|
|
122
|
+
if (record.schemaVersion !== 1) return null;
|
|
123
|
+
if (typeof record.codexHome !== 'string' || record.codexHome.trim() === '') return null;
|
|
124
|
+
return requireCodexHome(record.codexHome);
|
|
125
|
+
} catch {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Decide which Codex home the AUTOMATIC startup check may target.
|
|
132
|
+
*
|
|
133
|
+
* Precedence, and nothing else:
|
|
134
|
+
*
|
|
135
|
+
* 1. an explicit `CODEX_HOME` (or an explicit `codexHome` option) — the operator said
|
|
136
|
+
* which directory, so that directory is used;
|
|
137
|
+
* 2. the home this plugin recorded from a successful explicit install, when it still
|
|
138
|
+
* exists as a real directory;
|
|
139
|
+
* 3. the default home, but only when it exists and is NOT a link.
|
|
140
|
+
*
|
|
141
|
+
* A default that is a link (or whose kind cannot be verified) is not walked: the result
|
|
142
|
+
* says so, and the caller reports it instead of upgrading an unproven tree. This is the
|
|
143
|
+
* difference between "the operator configured a Codex home" and "a junction happens to
|
|
144
|
+
* point somewhere" — only the first is a target this plugin may write to on its own.
|
|
145
|
+
* @param {object} [options] - `env`, `codexHome` (explicit), `homeDirectory`.
|
|
146
|
+
* @returns {Promise<{ home: string | null, source: string, reason: string | null }>} target.
|
|
147
|
+
*/
|
|
148
|
+
export async function resolveStartupTarget(options = {}) {
|
|
149
|
+
const resolved = await resolveHomePreference({
|
|
150
|
+
env: options.env ?? process.env,
|
|
151
|
+
explicitHome: typeof options.codexHome === 'string' && options.codexHome.trim() !== '' ? options.codexHome : '',
|
|
152
|
+
homeDirectory: options.homeDirectory,
|
|
153
|
+
});
|
|
154
|
+
return {
|
|
155
|
+
home: resolved.home,
|
|
156
|
+
source: resolved.source,
|
|
157
|
+
reason: resolved.home === null ? 'no-codex-home'
|
|
158
|
+
: (resolved.blocked === true ? (resolved.source === 'default' ? 'default-is-link' : 'default-unverified') : null),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The ONE precedence this module implements, shared by the seam and this helper.
|
|
164
|
+
*
|
|
165
|
+
* Having a single implementation is the point: a second copy is how the page came to
|
|
166
|
+
* show one directory while the startup check upgraded another.
|
|
167
|
+
* @param {object} input - `env`, `explicitHome` (may be '').
|
|
168
|
+
* @returns {Promise<{ home: string | null, blocked: boolean, source: string }>} resolution.
|
|
169
|
+
*/
|
|
170
|
+
async function resolveHomePreference({ env, explicitHome = '', homeDirectory = undefined }) {
|
|
171
|
+
if (explicitHome !== '') return { home: requireCodexHome(explicitHome), blocked: false, source: 'explicit' };
|
|
172
|
+
const configured = typeof env.CODEX_HOME === 'string' ? env.CODEX_HOME.trim() : '';
|
|
173
|
+
if (configured !== '') return { home: requireCodexHome(configured), blocked: false, source: 'explicit' };
|
|
174
|
+
|
|
175
|
+
const remembered = readInstalledCodexHome(env);
|
|
176
|
+
if (remembered !== null) {
|
|
177
|
+
const kind = await inspectDirectory(remembered);
|
|
178
|
+
// The recorded directory is gone or is now a link: it is not a target any more, and
|
|
179
|
+
// the default below decides on its own evidence.
|
|
180
|
+
if (kind.kind === 'directory') return { home: remembered, blocked: false, source: 'recorded' };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const fallback = resolveCodexHome({ env, ...(homeDirectory === undefined ? {} : { homeDirectory }) });
|
|
184
|
+
const kind = await inspectDirectory(fallback);
|
|
185
|
+
// A default that does not exist is "no Codex installed here", not a target the plugin
|
|
186
|
+
// may create: the automatic check has nothing to upgrade.
|
|
187
|
+
if (kind.kind === 'missing') return { home: null, blocked: false, source: 'none' };
|
|
188
|
+
if (kind.kind === 'directory') return { home: fallback, blocked: false, source: 'default' };
|
|
189
|
+
// A link (or an unverifiable entry) at the default is reported, never walked.
|
|
190
|
+
return { home: fallback, blocked: true, source: 'default' };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Build the Codex seam for one plugin activation.
|
|
195
|
+
*
|
|
196
|
+
* @param {object} [options] - overrides; every one exists for a test or a host that
|
|
197
|
+
* resolves these differently, and none is part of the published plugin config.
|
|
198
|
+
* @param {Record<string, string | undefined>} [options.env] - environment to read
|
|
199
|
+
* (`CODEX_HOME`, `DSH_HOME`, `DSH_DELEGATE_DISCOVERY_FILE`).
|
|
200
|
+
* @param {string} [options.codexHome] - explicit Codex home, bypassing the env.
|
|
201
|
+
* @param {string} [options.discoveryFile] - explicit discovery file path.
|
|
202
|
+
* @param {string} [options.version] - version stamped into the managed agent file.
|
|
203
|
+
* @param {string} [options.backupDir] - destination for a pre-write copy during an
|
|
204
|
+
* unattended upgrade; defaults to {@link defaultBackupDirectory}.
|
|
205
|
+
* @returns {{ status: () => Promise<object>, run: (action: string) => Promise<object> }} seam.
|
|
206
|
+
*/
|
|
207
|
+
export function createCodexSeam(options = {}) {
|
|
208
|
+
const env = options.env ?? process.env;
|
|
209
|
+
const requestedHome = typeof options.codexHome === 'string' && options.codexHome.trim() !== ''
|
|
210
|
+
? options.codexHome
|
|
211
|
+
: null;
|
|
212
|
+
const requestedDiscovery = typeof options.discoveryFile === 'string' && options.discoveryFile.trim() !== ''
|
|
213
|
+
? options.discoveryFile
|
|
214
|
+
: null;
|
|
215
|
+
const version = typeof options.version === 'string' ? options.version : null;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Resolve the discovery file the MCP server will read.
|
|
219
|
+
*
|
|
220
|
+
* A relative `DSH_DELEGATE_DISCOVERY_FILE` is a deployment mistake; the installer
|
|
221
|
+
* refuses it, and so does this resolver — falling back to the default would write
|
|
222
|
+
* a Codex entry that silently points somewhere else.
|
|
223
|
+
* @returns {string | undefined} absolute path, or undefined to let the installer resolve the default.
|
|
224
|
+
*/
|
|
225
|
+
const discovery = () => {
|
|
226
|
+
if (requestedDiscovery !== null) return requestedDiscovery;
|
|
227
|
+
return resolveEndpointDiscoveryPath(env);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Build the argument set for one installer call from a resolved home.
|
|
232
|
+
*
|
|
233
|
+
* The home arrives already resolved (see {@link resolveHome}) so a relative
|
|
234
|
+
* `CODEX_HOME` is refused (`CODEX_HOME_INVALID`) instead of being resolved against the
|
|
235
|
+
* harness's working directory — which would report on, and write to, the wrong tree.
|
|
236
|
+
* @param {{ home: string | null }} resolution - resolved home.
|
|
237
|
+
* @returns {object} installer options.
|
|
238
|
+
*/
|
|
239
|
+
const baseFor = (resolution) => ({
|
|
240
|
+
codexHome: resolution.home,
|
|
241
|
+
env,
|
|
242
|
+
...(discovery() === undefined ? {} : { discoveryFile: discovery() }),
|
|
243
|
+
...(version === null ? {} : { version }),
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* The ONE resolution every seam method uses.
|
|
248
|
+
*
|
|
249
|
+
* Before this existed, the automatic check consulted the recorded directory while
|
|
250
|
+
* `status()` and the buttons consulted only `CODEX_HOME`/default — so removing
|
|
251
|
+
* `CODEX_HOME` made the page report (and the buttons write) a DIFFERENT tree than the
|
|
252
|
+
* one startup had just upgraded. Both now come from {@link resolveHomePreference}.
|
|
253
|
+
* @returns {Promise<{ home: string | null, blocked: boolean, source: string }>} resolution.
|
|
254
|
+
*/
|
|
255
|
+
const resolveHome = async () => await resolveHomePreference({ env, explicitHome: requestedHome ?? '' });
|
|
256
|
+
|
|
257
|
+
return {
|
|
258
|
+
/**
|
|
259
|
+
* The Codex home this seam reports on, as ONE resolution shared by status, the
|
|
260
|
+
* explicit actions and the automatic check (see `resolveHome`).
|
|
261
|
+
*
|
|
262
|
+
* `home` is `null` when no supported directory applies — the caller then refuses
|
|
263
|
+
* instead of falling back to the default, so the page and the buttons can never point
|
|
264
|
+
* at different trees.
|
|
265
|
+
* @returns {Promise<{ home: string | null, blocked: boolean, source: string }>} resolution.
|
|
266
|
+
*/
|
|
267
|
+
async resolveTarget() {
|
|
268
|
+
return await resolveHome();
|
|
269
|
+
},
|
|
270
|
+
/**
|
|
271
|
+
* Which home the AUTOMATIC startup check may target, and why.
|
|
272
|
+
*
|
|
273
|
+
* A thin view of {@link resolveTarget}: same precedence, plus a stable reason a
|
|
274
|
+
* caller can log or display.
|
|
275
|
+
* @returns {Promise<{ home: string | null, source: string, reason: string | null }>} target.
|
|
276
|
+
*/
|
|
277
|
+
async startupTarget() {
|
|
278
|
+
const resolved = await resolveHome();
|
|
279
|
+
const reason = resolved.home === null ? 'no-codex-home'
|
|
280
|
+
: (resolved.blocked === true ? (resolved.source === 'default' ? 'default-is-link' : 'default-unverified') : null);
|
|
281
|
+
return { home: resolved.home, source: resolved.source, reason };
|
|
282
|
+
},
|
|
283
|
+
/**
|
|
284
|
+
* Migrate a byte-exact, known prior agent template during plugin startup, and
|
|
285
|
+
* upgrade an already-installed managed MCP entry that is merely stale.
|
|
286
|
+
*
|
|
287
|
+
* This never creates the agent file, never CREATES the MCP entry, never adopts a
|
|
288
|
+
* same-name entry it cannot prove it wrote, and never edits another Codex setting.
|
|
289
|
+
* @param {object} [callOptions] - `startupTarget`: the target `startupTarget()`
|
|
290
|
+
* returned, so startup does not silently fall back to a different home.
|
|
291
|
+
* @returns {Promise<object>} migration outcome (`changed`, `refused`, `status`).
|
|
292
|
+
*/
|
|
293
|
+
async refreshKnownAgent(callOptions = {}) {
|
|
294
|
+
const resolved = await resolveTargetOptions(callOptions, baseFor, resolveHome);
|
|
295
|
+
if (resolved.refused !== undefined) return { changed: [], refused: [resolved.refused], status: null };
|
|
296
|
+
return await refreshKnownAgentTemplate(resolved.options);
|
|
297
|
+
},
|
|
298
|
+
/**
|
|
299
|
+
* The startup integration check: both halves, one call.
|
|
300
|
+
*
|
|
301
|
+
* It never runs without a resolved home: when there is none, or the caller passed a
|
|
302
|
+
* target with `home: null`, the answer is a stated refusal and NOTHING is read or
|
|
303
|
+
* written — never a silent fall back to the default directory.
|
|
304
|
+
* @param {object} [callOptions] - `startupTarget`, `backupDir`.
|
|
305
|
+
* @returns {Promise<object>} migration outcome with `changed` including `mcp` when
|
|
306
|
+
* a stale managed entry was upgraded in place.
|
|
307
|
+
*/
|
|
308
|
+
async refreshManagedIntegration(callOptions = {}) {
|
|
309
|
+
const resolved = await resolveTargetOptions(callOptions, baseFor, resolveHome);
|
|
310
|
+
if (resolved.refused !== undefined) return { changed: [], refused: [resolved.refused], status: null };
|
|
311
|
+
// Precedence: the call's explicit scratch destination, then the seam's configured
|
|
312
|
+
// one, then the plugin's own data directory. The last one is what makes a bare
|
|
313
|
+
// `refreshManagedIntegration()` safe: an unattended upgrade still has somewhere to
|
|
314
|
+
// put its pre-write copy, so the installer's backup precondition is met without
|
|
315
|
+
// the caller having to know about it.
|
|
316
|
+
const backupDir = typeof callOptions.backupDir === 'string' && callOptions.backupDir !== ''
|
|
317
|
+
? callOptions.backupDir
|
|
318
|
+
: (typeof options.backupDir === 'string' && options.backupDir !== ''
|
|
319
|
+
? options.backupDir
|
|
320
|
+
: defaultBackupDirectory(env));
|
|
321
|
+
return await refreshKnownAgentTemplate({
|
|
322
|
+
...resolved.options,
|
|
323
|
+
refreshMcp: true,
|
|
324
|
+
backupDir,
|
|
325
|
+
});
|
|
326
|
+
},
|
|
327
|
+
/**
|
|
328
|
+
* Read the Codex state. Never writes.
|
|
329
|
+
* @returns {Promise<object>} status document.
|
|
330
|
+
*/
|
|
331
|
+
async status() {
|
|
332
|
+
const resolved = await resolveTargetOptions({}, baseFor, resolveHome);
|
|
333
|
+
if (resolved.refused !== undefined) {
|
|
334
|
+
// No supported directory: report the refusal rather than the status of some
|
|
335
|
+
// other tree. The user plane turns a thrown installer error into a 4xx/5xx.
|
|
336
|
+
throw Object.assign(new Error(resolved.refused.message), { code: resolved.refused.code });
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
changed: [],
|
|
340
|
+
refused: [],
|
|
341
|
+
status: await readCodexStatus(resolved.options),
|
|
342
|
+
};
|
|
343
|
+
},
|
|
344
|
+
/**
|
|
345
|
+
* Perform one explicit operator action.
|
|
346
|
+
*
|
|
347
|
+
* A successful `install` is also the moment the plugin learns which directory is
|
|
348
|
+
* really its target — including when it wrote nothing because the installation was
|
|
349
|
+
* already exactly right (`changed: []` is a SUCCESS, not "nothing happened"). That
|
|
350
|
+
* home is recorded so later startup checks and the page use the same tree, and a
|
|
351
|
+
* non-default directory keeps working without the plugin ever scanning for one.
|
|
352
|
+
* @param {string} action - `install`, `repair` or `remove`.
|
|
353
|
+
* @returns {Promise<object>} outcome with `changed`, `refused` and `status`.
|
|
354
|
+
*/
|
|
355
|
+
async run(action) {
|
|
356
|
+
const resolved = await resolveTargetOptions({}, baseFor, resolveHome);
|
|
357
|
+
if (resolved.refused !== undefined) {
|
|
358
|
+
throw Object.assign(new Error(resolved.refused.message), { code: resolved.refused.code });
|
|
359
|
+
}
|
|
360
|
+
const outcome = await runCodexAction(action, resolved.options);
|
|
361
|
+
const home = outcome.status?.codexHome ?? resolved.options.codexHome;
|
|
362
|
+
// An install that was REFUSED records nothing; one that succeeded — changed or
|
|
363
|
+
// already correct and idempotent — records the directory it was performed on.
|
|
364
|
+
const installedCleanly = action === 'install' && outcome.refused.length === 0;
|
|
365
|
+
if (installedCleanly && typeof home === 'string' && home !== '') {
|
|
366
|
+
await rememberInstalledCodexHome(home, env);
|
|
367
|
+
}
|
|
368
|
+
return outcome;
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Resolve the installer options for one seam call.
|
|
375
|
+
*
|
|
376
|
+
* A caller-supplied `startupTarget` is honoured EXACTLY: with `home: null` the call is
|
|
377
|
+
* refused, because the caller just told us that directory is not usable and substituting
|
|
378
|
+
* the default would write to a tree nobody named. Without one, the seam's own unified
|
|
379
|
+
* resolution applies.
|
|
380
|
+
* @param {object} callOptions - `startupTarget`.
|
|
381
|
+
* @param {Function} baseFor - the seam's installer-option builder.
|
|
382
|
+
* @param {Function} resolveHome - the seam's unified home resolver.
|
|
383
|
+
* @returns {Promise<{ options: object, refused?: object }>} installer options, or a
|
|
384
|
+
* refusal-shaped record the caller returns verbatim.
|
|
385
|
+
*/
|
|
386
|
+
async function resolveTargetOptions(callOptions, baseFor, resolveHome) {
|
|
387
|
+
const supplied = callOptions?.startupTarget;
|
|
388
|
+
if (supplied !== undefined && supplied !== null) {
|
|
389
|
+
if (typeof supplied.home !== 'string' || supplied.home === '') {
|
|
390
|
+
return {
|
|
391
|
+
options: {},
|
|
392
|
+
refused: {
|
|
393
|
+
target: 'agent',
|
|
394
|
+
code: 'CODEX_HOME_INVALID',
|
|
395
|
+
message: 'the Codex startup target has no usable home, so the integration check was skipped instead of using the default',
|
|
396
|
+
reason: supplied.reason ?? 'no-codex-home',
|
|
397
|
+
},
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
const resolved = await resolveHome();
|
|
401
|
+
return { options: baseFor({ ...resolved, home: supplied.home, blocked: false }) };
|
|
402
|
+
}
|
|
403
|
+
const resolved = await resolveHome();
|
|
404
|
+
if (resolved.home === null) {
|
|
405
|
+
return {
|
|
406
|
+
options: {},
|
|
407
|
+
refused: {
|
|
408
|
+
target: 'agent',
|
|
409
|
+
code: 'CODEX_HOME_INVALID',
|
|
410
|
+
message: 'no usable Codex home was found, so the integration check was skipped instead of writing to the default',
|
|
411
|
+
reason: 'no-codex-home',
|
|
412
|
+
},
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
return { options: baseFor(resolved) };
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Resolve the seam a running plugin should expose.
|
|
420
|
+
*
|
|
421
|
+
* Returning `null` is a supported outcome: the user plane then answers with a 503
|
|
422
|
+
* rather than pretending the integration exists.
|
|
423
|
+
* @param {object} [options] - forwarded to {@link createCodexSeam}.
|
|
424
|
+
* @returns {{ status: Function, run: Function } | null} seam, or null when disabled.
|
|
425
|
+
*/
|
|
426
|
+
export function resolveCodexActionSeam(options = {}) {
|
|
427
|
+
if (options.disabled === true) return null;
|
|
428
|
+
return createCodexSeam(options);
|
|
429
|
+
}
|