passport-bridge 0.8.2 → 0.10.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/README.md +100 -3
- package/dist/accessClient.d.ts +128 -0
- package/dist/accessClient.js +436 -0
- package/dist/activeProfile.d.ts +21 -0
- package/dist/boundedFile.d.ts +2 -0
- package/dist/boundedMcpFetch.d.ts +6 -0
- package/dist/boundedResponse.d.ts +4 -0
- package/dist/bridge.d.ts +98 -0
- package/dist/bridge.js +49 -42
- package/dist/bridgeConfig.d.ts +1 -0
- package/dist/bridgePoller.d.ts +34 -0
- package/dist/bridgePoller.js +123 -0
- package/dist/bridgeShutdown.d.ts +20 -0
- package/dist/bridgeShutdown.js +5 -1
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +4 -0
- package/dist/cliAccess.d.ts +3 -0
- package/dist/cliAccess.js +119 -0
- package/dist/cliArgs.d.ts +16 -0
- package/dist/cliArgs.js +14 -3
- package/dist/cliClient.d.ts +62 -0
- package/dist/cliConnect.d.ts +22 -0
- package/dist/cliDeadline.d.ts +9 -0
- package/dist/cliDescribe.d.ts +8 -0
- package/dist/cliLogin.d.ts +3 -0
- package/dist/cliOffline.d.ts +6 -0
- package/dist/cliOutput.d.ts +7 -0
- package/dist/cliOutput.js +4 -0
- package/dist/cliPagination.d.ts +15 -0
- package/dist/cliProtocol.d.ts +4 -0
- package/dist/cliReady.d.ts +9 -0
- package/dist/cliResult.d.ts +7 -0
- package/dist/cliSession.d.ts +20 -0
- package/dist/cliSetup.d.ts +10 -0
- package/dist/cliSetup.js +157 -0
- package/dist/cliSupport.d.ts +102 -0
- package/dist/completion.d.ts +2 -0
- package/dist/completion.js +20 -2
- package/dist/credentialStorage.d.ts +12 -0
- package/dist/credentials.d.ts +64 -0
- package/dist/discover.d.ts +52 -0
- package/dist/doctor.d.ts +34 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/legacyCliSecurity.d.ts +10 -0
- package/dist/managedSkills.d.ts +7 -0
- package/dist/managedSkills.js +268 -0
- package/dist/managedSkillsContract.generated.d.ts +82 -0
- package/dist/managedSkillsContract.generated.js +32 -0
- package/dist/managedSkillsFiles.d.ts +23 -0
- package/dist/managedSkillsFiles.js +215 -0
- package/dist/managedSkillsIpc.d.ts +5 -0
- package/dist/managedSkillsIpc.js +44 -0
- package/dist/managedSkillsValidation.d.ts +14 -0
- package/dist/managedSkillsValidation.js +118 -0
- package/dist/passport.d.ts +7 -0
- package/dist/passport.js +4 -0
- package/dist/privateStableFile.d.ts +13 -0
- package/dist/recoveryCleanup.d.ts +5 -0
- package/dist/recoveryIdentity.d.ts +22 -0
- package/dist/tokenWorkspace.d.ts +3 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +13 -2
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { ManagedSkillFiles } from "./managedSkillsFiles.js";
|
|
4
|
+
import { digest, MANAGED_SKILL_FILE_LIMIT, MANAGED_SKILL_JOURNAL_LIMIT, MANAGED_SKILL_RECORD_LIMIT, object, skillContents, skillName, validatePackage, validateRequest, validateSource } from "./managedSkillsValidation.js";
|
|
5
|
+
class Conflict extends Error {
|
|
6
|
+
}
|
|
7
|
+
const same = (left, right) => JSON.stringify(left) === JSON.stringify(right);
|
|
8
|
+
const serialize = (value) => `${JSON.stringify(value)}\n`;
|
|
9
|
+
function paths(files, source) {
|
|
10
|
+
const name = skillName(source);
|
|
11
|
+
const vendor = source.client === "codex" ? ".agents" : ".claude";
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
skill: join(files.home, vendor, "skills", name, "SKILL.md"),
|
|
15
|
+
record: join(files.registry, `${name}.json`),
|
|
16
|
+
journal: join(files.registry, `${name}.pending.json`)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function artifactData(value, source) {
|
|
20
|
+
if (value === null)
|
|
21
|
+
return null;
|
|
22
|
+
const row = object(value);
|
|
23
|
+
const artifact = validatePackage(row.package);
|
|
24
|
+
const contents = skillContents(skillName(source), artifact);
|
|
25
|
+
if (row.contents !== contents || Buffer.byteLength(contents) > MANAGED_SKILL_FILE_LIMIT) {
|
|
26
|
+
throw new Conflict("Managed workflow ownership could not be verified.");
|
|
27
|
+
}
|
|
28
|
+
return { package: artifact, contents };
|
|
29
|
+
}
|
|
30
|
+
function recordData(value, source) {
|
|
31
|
+
if (value === null)
|
|
32
|
+
return null;
|
|
33
|
+
const row = object(value);
|
|
34
|
+
if (row.version !== 1 || !same(validateSource(row.source), source)) {
|
|
35
|
+
throw new Conflict("This workflow belongs to another Passport setup.");
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
version: 1,
|
|
39
|
+
source,
|
|
40
|
+
current: artifactData(row.current, source),
|
|
41
|
+
previous: artifactData(row.previous, source)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function inspect(files, source, desired) {
|
|
45
|
+
const target = paths(files, source);
|
|
46
|
+
const recordRaw = files.read(target.record, MANAGED_SKILL_RECORD_LIMIT);
|
|
47
|
+
const record = recordRaw === null ? null : recordData(JSON.parse(recordRaw), source);
|
|
48
|
+
const journalRaw = files.read(target.journal, MANAGED_SKILL_JOURNAL_LIMIT);
|
|
49
|
+
const content = files.read(target.skill, MANAGED_SKILL_FILE_LIMIT);
|
|
50
|
+
const directory = files.existsDirectory(dirname(target.skill));
|
|
51
|
+
const clean = content === (record?.current?.contents ?? null);
|
|
52
|
+
const sameRevision = record?.current?.package.revisionId === desired?.revisionId;
|
|
53
|
+
const changedRevision = desired && sameRevision && !same(record?.current?.package, desired);
|
|
54
|
+
let state = "missing";
|
|
55
|
+
if (journalRaw !== null)
|
|
56
|
+
state = "recovery_needed";
|
|
57
|
+
else if ((!record && (content !== null || directory)) || !clean || changedRevision)
|
|
58
|
+
state = "modified";
|
|
59
|
+
else if (record?.current)
|
|
60
|
+
state = desired && !sameRevision ? "update_available" : "installed";
|
|
61
|
+
return {
|
|
62
|
+
record,
|
|
63
|
+
recordRaw,
|
|
64
|
+
journalRaw,
|
|
65
|
+
content,
|
|
66
|
+
status: {
|
|
67
|
+
source,
|
|
68
|
+
skillName: target.name,
|
|
69
|
+
state,
|
|
70
|
+
installedRevision: record?.current?.package.revisionId ?? null,
|
|
71
|
+
installedVersion: record?.current?.package.revisionVersion ?? null,
|
|
72
|
+
hasRollback: Boolean(record?.previous),
|
|
73
|
+
reviewToken: digest(JSON.stringify({ source, recordRaw, journalRaw, content, directory, desired: desired ?? null }))
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function readJournal(raw, source) {
|
|
78
|
+
const value = object(JSON.parse(raw));
|
|
79
|
+
if (value.version !== 1 || !same(validateSource(value.source), source))
|
|
80
|
+
throw new Conflict("Invalid workflow recovery record.");
|
|
81
|
+
const before = recordData(value.before, source);
|
|
82
|
+
const after = recordData(value.after, source);
|
|
83
|
+
if (!after)
|
|
84
|
+
throw new Conflict("Invalid workflow recovery record.");
|
|
85
|
+
return { version: 1, source, before, after };
|
|
86
|
+
}
|
|
87
|
+
/** The journal precedes the skill file. Recovery accepts only one of the exact
|
|
88
|
+
* two reviewed states, preserving any edit made outside this transaction. */
|
|
89
|
+
function completeTransaction(files, journal, direction, checkpoint) {
|
|
90
|
+
const target = paths(files, journal.source);
|
|
91
|
+
const currentRecord = files.read(target.record, MANAGED_SKILL_RECORD_LIMIT);
|
|
92
|
+
const beforeRaw = journal.before ? serialize(journal.before) : null;
|
|
93
|
+
const afterRaw = serialize(journal.after);
|
|
94
|
+
if (currentRecord !== beforeRaw && currentRecord !== afterRaw)
|
|
95
|
+
throw new Conflict("Workflow ownership changed during setup.");
|
|
96
|
+
const actual = files.read(target.skill, MANAGED_SKILL_FILE_LIMIT);
|
|
97
|
+
const before = journal.before?.current?.contents ?? null;
|
|
98
|
+
const after = journal.after.current?.contents ?? null;
|
|
99
|
+
if (actual !== before && actual !== after)
|
|
100
|
+
throw new Conflict("Your workflow changed locally. Passport preserved it.");
|
|
101
|
+
const selected = direction === "after" ? journal.after : journal.before;
|
|
102
|
+
const desired = selected?.current?.contents ?? null;
|
|
103
|
+
if (actual !== desired) {
|
|
104
|
+
if (desired === null)
|
|
105
|
+
files.remove(target.skill, actual, MANAGED_SKILL_FILE_LIMIT);
|
|
106
|
+
else
|
|
107
|
+
files.write(target.skill, desired, actual, MANAGED_SKILL_FILE_LIMIT);
|
|
108
|
+
}
|
|
109
|
+
checkpoint?.("artifact");
|
|
110
|
+
if (selected)
|
|
111
|
+
files.write(target.record, serialize(selected), currentRecord, MANAGED_SKILL_RECORD_LIMIT);
|
|
112
|
+
else if (currentRecord !== null)
|
|
113
|
+
files.remove(target.record, currentRecord, MANAGED_SKILL_RECORD_LIMIT);
|
|
114
|
+
checkpoint?.("record");
|
|
115
|
+
const rawJournal = files.read(target.journal, MANAGED_SKILL_JOURNAL_LIMIT);
|
|
116
|
+
if (rawJournal === null || !same(readJournal(rawJournal, journal.source), journal)) {
|
|
117
|
+
throw new Conflict("Workflow recovery record changed during setup.");
|
|
118
|
+
}
|
|
119
|
+
files.remove(target.journal, rawJournal, MANAGED_SKILL_JOURNAL_LIMIT);
|
|
120
|
+
if (desired === null)
|
|
121
|
+
files.removeEmpty(dirname(target.skill));
|
|
122
|
+
}
|
|
123
|
+
function transact(files, source, before, after, options) {
|
|
124
|
+
const target = paths(files, source);
|
|
125
|
+
const journal = { version: 1, source, before: before.record, after };
|
|
126
|
+
files.write(target.journal, serialize(journal), null, MANAGED_SKILL_JOURNAL_LIMIT);
|
|
127
|
+
try {
|
|
128
|
+
options.checkpoint?.("journal");
|
|
129
|
+
completeTransaction(files, journal, "after", options.checkpoint);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
// A normal I/O failure attempts to restore the prior state. A killed
|
|
133
|
+
// process leaves the durable journal for the next explicit operation.
|
|
134
|
+
try {
|
|
135
|
+
completeTransaction(files, journal, "before");
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
/* Preserve recovery evidence. */
|
|
139
|
+
}
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function result(request, fields) {
|
|
144
|
+
return { protocol: "passport.managed-skills", version: 1, operation: request.operation, ...fields };
|
|
145
|
+
}
|
|
146
|
+
function change(files, request, options) {
|
|
147
|
+
const source = request.source;
|
|
148
|
+
let before = inspect(files, source, request.package);
|
|
149
|
+
if (before.status.reviewToken !== request.expectedState) {
|
|
150
|
+
throw new Conflict("The workflow changed since review. Refresh its status and try again.");
|
|
151
|
+
}
|
|
152
|
+
if (before.journalRaw !== null) {
|
|
153
|
+
completeTransaction(files, readJournal(before.journalRaw, source), "after");
|
|
154
|
+
before = inspect(files, source, request.package);
|
|
155
|
+
}
|
|
156
|
+
if (before.status.state === "modified")
|
|
157
|
+
throw new Conflict("Your workflow changed locally. Passport preserved it. Restore or move your edit before trying again.");
|
|
158
|
+
const current = before.record?.current ?? null;
|
|
159
|
+
let next;
|
|
160
|
+
let state;
|
|
161
|
+
let message;
|
|
162
|
+
if (request.operation === "install") {
|
|
163
|
+
const artifact = request.package;
|
|
164
|
+
next = { package: artifact, contents: skillContents(skillName(source), artifact) };
|
|
165
|
+
if (current && same(current, next)) {
|
|
166
|
+
return result(request, {
|
|
167
|
+
ok: true,
|
|
168
|
+
state: "installed",
|
|
169
|
+
message: "This workflow version is already installed.",
|
|
170
|
+
filesChanged: 0,
|
|
171
|
+
status: before.status
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
state = "installed";
|
|
175
|
+
message = current
|
|
176
|
+
? "Workflow updated. Start a new client session to use the new guidance."
|
|
177
|
+
: "Workflow installed. Start a new client session to use it.";
|
|
178
|
+
}
|
|
179
|
+
else if (request.operation === "uninstall") {
|
|
180
|
+
if (!current)
|
|
181
|
+
return result(request, {
|
|
182
|
+
ok: true,
|
|
183
|
+
state: "removed",
|
|
184
|
+
message: "This workflow is already removed.",
|
|
185
|
+
filesChanged: 0,
|
|
186
|
+
status: before.status
|
|
187
|
+
});
|
|
188
|
+
next = null;
|
|
189
|
+
state = "removed";
|
|
190
|
+
message = "Managed workflow removed. Personal skills are preserved.";
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
next = before.record?.previous ?? null;
|
|
194
|
+
if (!next)
|
|
195
|
+
throw new Conflict("No previous workflow version is available to restore.");
|
|
196
|
+
state = "rolled_back";
|
|
197
|
+
message = "Previous workflow version restored. Start a new client session to use it.";
|
|
198
|
+
}
|
|
199
|
+
const after = { version: 1, source, current: next, previous: current };
|
|
200
|
+
transact(files, source, before, after, options);
|
|
201
|
+
return result(request, {
|
|
202
|
+
ok: true,
|
|
203
|
+
state,
|
|
204
|
+
message,
|
|
205
|
+
filesChanged: 1,
|
|
206
|
+
status: inspect(files, source, request.package).status
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
export function runManagedSkills(input, options = {}) {
|
|
210
|
+
let request = { protocol: "passport.managed-skills", version: 1, operation: "status" };
|
|
211
|
+
try {
|
|
212
|
+
request = validateRequest(input);
|
|
213
|
+
if (process.platform === "win32")
|
|
214
|
+
return result(request, {
|
|
215
|
+
ok: false,
|
|
216
|
+
state: "failed",
|
|
217
|
+
filesChanged: 0,
|
|
218
|
+
message: "Managed workflow skills currently support macOS and Linux. Windows installation is not available yet."
|
|
219
|
+
});
|
|
220
|
+
const files = new ManagedSkillFiles(options.home ?? homedir());
|
|
221
|
+
if (request.operation === "list") {
|
|
222
|
+
const items = files.listRecords().flatMap((entry) => {
|
|
223
|
+
const raw = files.read(join(files.registry, entry), MANAGED_SKILL_RECORD_LIMIT);
|
|
224
|
+
const pending = raw === null
|
|
225
|
+
? files.read(join(files.registry, entry.replace(/\.json$/, ".pending.json")), MANAGED_SKILL_JOURNAL_LIMIT)
|
|
226
|
+
: null;
|
|
227
|
+
if (raw === null && pending === null)
|
|
228
|
+
return [];
|
|
229
|
+
const stored = object(JSON.parse((raw ?? pending)));
|
|
230
|
+
const source = validateSource(stored.source);
|
|
231
|
+
if (`${skillName(source)}.json` !== entry)
|
|
232
|
+
throw new Conflict("Invalid managed workflow inventory.");
|
|
233
|
+
if (request.scope &&
|
|
234
|
+
(request.scope.serverOrigin !== source.serverOrigin || request.scope.workspaceId !== source.workspaceId))
|
|
235
|
+
return [];
|
|
236
|
+
return [inspect(files, source).status];
|
|
237
|
+
});
|
|
238
|
+
return result(request, {
|
|
239
|
+
ok: true,
|
|
240
|
+
state: "installed",
|
|
241
|
+
message: "Local workflow inventory checked.",
|
|
242
|
+
filesChanged: 0,
|
|
243
|
+
items
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
if (request.operation === "status") {
|
|
247
|
+
const status = inspect(files, request.source, request.package).status;
|
|
248
|
+
return result(request, {
|
|
249
|
+
ok: true,
|
|
250
|
+
state: status.state,
|
|
251
|
+
message: "Local workflow state checked.",
|
|
252
|
+
filesChanged: 0,
|
|
253
|
+
status
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
return files.withLock(skillName(request.source), () => change(files, request, options));
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
return result(request, {
|
|
260
|
+
ok: false,
|
|
261
|
+
state: error instanceof Conflict ? "conflict" : "failed",
|
|
262
|
+
filesChanged: 0,
|
|
263
|
+
message: error instanceof Conflict
|
|
264
|
+
? error.message
|
|
265
|
+
: "Passport could not safely complete workflow setup. Check the client and local folder permissions, then retry."
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** Public local installer protocol. No credentials, executable hooks, arbitrary
|
|
2
|
+
* paths, or network operations cross this boundary. */
|
|
3
|
+
export type ManagedSkillClient = "claude-code" | "codex";
|
|
4
|
+
export type ManagedSkillSource = {
|
|
5
|
+
serverOrigin: string;
|
|
6
|
+
workspaceId: string;
|
|
7
|
+
profileId: string;
|
|
8
|
+
workflowKey: string;
|
|
9
|
+
client: ManagedSkillClient;
|
|
10
|
+
};
|
|
11
|
+
export type ManagedSkillPackage = {
|
|
12
|
+
revisionId: string;
|
|
13
|
+
revisionVersion: number;
|
|
14
|
+
name: string;
|
|
15
|
+
description: string;
|
|
16
|
+
instructions: string;
|
|
17
|
+
};
|
|
18
|
+
export type ManagedSkillsOperation = "list" | "status" | "install" | "uninstall" | "rollback";
|
|
19
|
+
export type ManagedSkillsRequest = {
|
|
20
|
+
protocol: "passport.managed-skills";
|
|
21
|
+
version: 1;
|
|
22
|
+
operation: ManagedSkillsOperation;
|
|
23
|
+
source?: ManagedSkillSource;
|
|
24
|
+
package?: ManagedSkillPackage;
|
|
25
|
+
/** Required for mutation; obtained from a fresh status result. */
|
|
26
|
+
expectedState?: string;
|
|
27
|
+
scope?: Pick<ManagedSkillSource, "serverOrigin" | "workspaceId">;
|
|
28
|
+
};
|
|
29
|
+
export type ManagedSkillState = "missing" | "installed" | "update_available" | "modified" | "recovery_needed" | "removed" | "rolled_back" | "conflict" | "failed";
|
|
30
|
+
export type ManagedSkillStatus = {
|
|
31
|
+
source: ManagedSkillSource;
|
|
32
|
+
skillName: string;
|
|
33
|
+
state: ManagedSkillState;
|
|
34
|
+
installedRevision: string | null;
|
|
35
|
+
installedVersion: number | null;
|
|
36
|
+
hasRollback: boolean;
|
|
37
|
+
/** Opaque local compare-and-swap evidence; contains no source content. */
|
|
38
|
+
reviewToken: string;
|
|
39
|
+
};
|
|
40
|
+
export type ManagedSkillsResult = {
|
|
41
|
+
protocol: "passport.managed-skills";
|
|
42
|
+
version: 1;
|
|
43
|
+
ok: boolean;
|
|
44
|
+
operation: ManagedSkillsOperation;
|
|
45
|
+
state: ManagedSkillState;
|
|
46
|
+
message: string;
|
|
47
|
+
filesChanged: number;
|
|
48
|
+
status?: ManagedSkillStatus;
|
|
49
|
+
items?: ManagedSkillStatus[];
|
|
50
|
+
};
|
|
51
|
+
/** Structural input keeps this leaf independent of transport projections. */
|
|
52
|
+
export type ManagedWorkflowManifest = {
|
|
53
|
+
schemaVersion: 1;
|
|
54
|
+
workspaceId: string;
|
|
55
|
+
profileId: string;
|
|
56
|
+
revisionId: string;
|
|
57
|
+
revisionVersion: number;
|
|
58
|
+
clientKey: string;
|
|
59
|
+
workflow: {
|
|
60
|
+
key: string;
|
|
61
|
+
name: string;
|
|
62
|
+
description: string;
|
|
63
|
+
instructions: string;
|
|
64
|
+
};
|
|
65
|
+
skills: Array<{
|
|
66
|
+
key: string;
|
|
67
|
+
name: string;
|
|
68
|
+
description: string;
|
|
69
|
+
instructions: string;
|
|
70
|
+
}>;
|
|
71
|
+
dependencies: Array<{
|
|
72
|
+
label: string;
|
|
73
|
+
kind: string;
|
|
74
|
+
ref: string;
|
|
75
|
+
}>;
|
|
76
|
+
};
|
|
77
|
+
/** Convert published instructions to one standalone skill. Dependency state is
|
|
78
|
+
* deliberately not embedded: authorization remains live at the gateway. */
|
|
79
|
+
export declare function managedSkillFromManifest(manifest: ManagedWorkflowManifest, serverOrigin: string): {
|
|
80
|
+
source: ManagedSkillSource;
|
|
81
|
+
package: ManagedSkillPackage;
|
|
82
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Convert published instructions to one standalone skill. Dependency state is
|
|
2
|
+
* deliberately not embedded: authorization remains live at the gateway. */
|
|
3
|
+
export function managedSkillFromManifest(manifest, serverOrigin) {
|
|
4
|
+
if (manifest.schemaVersion !== 1 || !["codex", "claude-code"].includes(manifest.clientKey)) {
|
|
5
|
+
throw new Error("This client does not support Passport-managed workflow skills.");
|
|
6
|
+
}
|
|
7
|
+
const dependencies = manifest.dependencies.map((dependency) => `- ${dependency.label}`).join("\n");
|
|
8
|
+
const guidance = manifest.skills.map((skill) => `### ${skill.name}\n\n${skill.instructions}`).join("\n\n");
|
|
9
|
+
return {
|
|
10
|
+
source: {
|
|
11
|
+
serverOrigin,
|
|
12
|
+
workspaceId: manifest.workspaceId,
|
|
13
|
+
profileId: manifest.profileId,
|
|
14
|
+
workflowKey: manifest.workflow.key,
|
|
15
|
+
client: manifest.clientKey
|
|
16
|
+
},
|
|
17
|
+
package: {
|
|
18
|
+
revisionId: manifest.revisionId,
|
|
19
|
+
revisionVersion: manifest.revisionVersion,
|
|
20
|
+
name: manifest.workflow.name,
|
|
21
|
+
description: manifest.workflow.description,
|
|
22
|
+
instructions: [
|
|
23
|
+
manifest.workflow.instructions,
|
|
24
|
+
dependencies ? `## Required tools\n\n${dependencies}` : "",
|
|
25
|
+
"Use the employee's approved Passport connections. Respect current access rules and required approvals.",
|
|
26
|
+
guidance ? `## Team guidance\n\n${guidance}` : ""
|
|
27
|
+
]
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
.join("\n\n")
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Stats } from "node:fs";
|
|
2
|
+
/** All paths are derived from a canonical home and a fixed vendor namespace. */
|
|
3
|
+
export declare class ManagedSkillFiles {
|
|
4
|
+
readonly home: string;
|
|
5
|
+
readonly registry: string;
|
|
6
|
+
constructor(home: string);
|
|
7
|
+
directories(path: string, create?: boolean): Array<{
|
|
8
|
+
path: string;
|
|
9
|
+
stat: Stats;
|
|
10
|
+
}>;
|
|
11
|
+
recheck(entries: Array<{
|
|
12
|
+
path: string;
|
|
13
|
+
stat: Stats;
|
|
14
|
+
}>): void;
|
|
15
|
+
read(path: string, maxBytes: number): string | null;
|
|
16
|
+
existsDirectory(path: string): boolean;
|
|
17
|
+
write(path: string, content: string, expected: string | null, maxBytes: number): void;
|
|
18
|
+
remove(path: string, expected: string, maxBytes: number): void;
|
|
19
|
+
sync(path: string): void;
|
|
20
|
+
listRecords(): string[];
|
|
21
|
+
removeEmpty(path: string): void;
|
|
22
|
+
withLock<T>(name: string, run: () => T): T;
|
|
23
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { closeSync, constants, fstatSync, fsyncSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, realpathSync, renameSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { readBoundedUtf8File } from "./boundedFile.js";
|
|
5
|
+
const missing = (error) => error.code === "ENOENT";
|
|
6
|
+
function owned(stat) {
|
|
7
|
+
return typeof process.getuid !== "function" || stat.uid === process.getuid();
|
|
8
|
+
}
|
|
9
|
+
/** All paths are derived from a canonical home and a fixed vendor namespace. */
|
|
10
|
+
export class ManagedSkillFiles {
|
|
11
|
+
home;
|
|
12
|
+
registry;
|
|
13
|
+
constructor(home) {
|
|
14
|
+
this.home = realpathSync(home);
|
|
15
|
+
this.registry = join(this.home, ".passport", "managed-skills");
|
|
16
|
+
}
|
|
17
|
+
directories(path, create = false) {
|
|
18
|
+
const child = relative(this.home, resolve(path));
|
|
19
|
+
if (child === ".." || child.startsWith(`..${sep}`) || child.startsWith(sep))
|
|
20
|
+
throw new Error("Unsafe workflow path.");
|
|
21
|
+
const entries = [];
|
|
22
|
+
let current = this.home;
|
|
23
|
+
for (const segment of ["", ...child.split(sep).filter(Boolean)]) {
|
|
24
|
+
if (segment)
|
|
25
|
+
current = join(current, segment);
|
|
26
|
+
let stat;
|
|
27
|
+
try {
|
|
28
|
+
stat = lstatSync(current);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (!missing(error) || !create)
|
|
32
|
+
throw error;
|
|
33
|
+
mkdirSync(current, { mode: 0o700 });
|
|
34
|
+
stat = lstatSync(current);
|
|
35
|
+
}
|
|
36
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || !owned(stat) || (stat.mode & 0o022) !== 0) {
|
|
37
|
+
throw new Error("Workflow folders must be owned by you and cannot be links or shared writable folders.");
|
|
38
|
+
}
|
|
39
|
+
entries.push({ path: current, stat });
|
|
40
|
+
}
|
|
41
|
+
return entries;
|
|
42
|
+
}
|
|
43
|
+
recheck(entries) {
|
|
44
|
+
for (const { path, stat } of entries) {
|
|
45
|
+
const after = lstatSync(path);
|
|
46
|
+
if (!after.isDirectory() ||
|
|
47
|
+
after.isSymbolicLink() ||
|
|
48
|
+
!owned(after) ||
|
|
49
|
+
after.ino !== stat.ino ||
|
|
50
|
+
after.dev !== stat.dev ||
|
|
51
|
+
(after.mode & 0o022) !== 0) {
|
|
52
|
+
throw new Error("Workflow folders changed during setup.");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
read(path, maxBytes) {
|
|
57
|
+
try {
|
|
58
|
+
const parents = this.directories(dirname(path));
|
|
59
|
+
const before = lstatSync(path);
|
|
60
|
+
if (!owned(before) || before.nlink !== 1 || (before.mode & 0o022) !== 0)
|
|
61
|
+
throw new Error("Unsafe workflow file ownership.");
|
|
62
|
+
const text = readBoundedUtf8File(path, maxBytes);
|
|
63
|
+
this.recheck(parents);
|
|
64
|
+
return text;
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (missing(error))
|
|
68
|
+
return null;
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
existsDirectory(path) {
|
|
73
|
+
try {
|
|
74
|
+
this.directories(path);
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (missing(error))
|
|
79
|
+
return false;
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
write(path, content, expected, maxBytes) {
|
|
84
|
+
if (Buffer.byteLength(content) > maxBytes)
|
|
85
|
+
throw new Error("Managed workflow exceeds the local size limit.");
|
|
86
|
+
const parents = this.directories(dirname(path), true);
|
|
87
|
+
const temporary = join(dirname(path), `.passport-write-${randomBytes(16).toString("hex")}`);
|
|
88
|
+
let descriptor;
|
|
89
|
+
try {
|
|
90
|
+
descriptor = openSync(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
91
|
+
writeFileSync(descriptor, content, "utf8");
|
|
92
|
+
fsyncSync(descriptor);
|
|
93
|
+
closeSync(descriptor);
|
|
94
|
+
descriptor = undefined;
|
|
95
|
+
this.recheck(parents);
|
|
96
|
+
if (this.read(path, maxBytes) !== expected)
|
|
97
|
+
throw new Error("Workflow changed while being written.");
|
|
98
|
+
renameSync(temporary, path);
|
|
99
|
+
this.sync(dirname(path));
|
|
100
|
+
this.recheck(parents);
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
if (descriptor !== undefined)
|
|
104
|
+
closeSync(descriptor);
|
|
105
|
+
try {
|
|
106
|
+
unlinkSync(temporary);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
/* A temporary-file cleanup must not obscure the transaction result. */
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
remove(path, expected, maxBytes) {
|
|
114
|
+
const parents = this.directories(dirname(path));
|
|
115
|
+
if (this.read(path, maxBytes) !== expected)
|
|
116
|
+
throw new Error("Workflow changed before removal.");
|
|
117
|
+
this.recheck(parents);
|
|
118
|
+
unlinkSync(path);
|
|
119
|
+
this.sync(dirname(path));
|
|
120
|
+
}
|
|
121
|
+
sync(path) {
|
|
122
|
+
const descriptor = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
123
|
+
try {
|
|
124
|
+
fsyncSync(descriptor);
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
closeSync(descriptor);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
listRecords() {
|
|
131
|
+
if (!this.existsDirectory(this.registry))
|
|
132
|
+
return [];
|
|
133
|
+
// Refuse an unexpectedly large inventory rather than allocate unbounded data.
|
|
134
|
+
const entries = readdirSync(this.registry);
|
|
135
|
+
if (entries.length > 1024)
|
|
136
|
+
throw new Error("Too many managed workflow records.");
|
|
137
|
+
const records = [
|
|
138
|
+
...new Set(entries
|
|
139
|
+
.filter((entry) => /^passport-[a-z0-9-]+-[a-f0-9]{16}(?:\.pending)?\.json$/.test(entry))
|
|
140
|
+
.map((entry) => entry.replace(/\.pending\.json$/, ".json")))
|
|
141
|
+
];
|
|
142
|
+
if (records.length > 256)
|
|
143
|
+
throw new Error("Too many managed workflows on this computer.");
|
|
144
|
+
return records;
|
|
145
|
+
}
|
|
146
|
+
removeEmpty(path) {
|
|
147
|
+
try {
|
|
148
|
+
this.directories(path);
|
|
149
|
+
rmdirSync(path);
|
|
150
|
+
this.sync(dirname(path));
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
if (!missing(error) && error.code !== "ENOTEMPTY")
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
withLock(name, run) {
|
|
158
|
+
this.directories(this.registry, true);
|
|
159
|
+
const path = join(this.registry, `${name}.lock`);
|
|
160
|
+
const token = `${process.pid}:${randomBytes(16).toString("hex")}`;
|
|
161
|
+
let descriptor;
|
|
162
|
+
try {
|
|
163
|
+
descriptor = openSync(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
if (error.code !== "EEXIST")
|
|
167
|
+
throw error;
|
|
168
|
+
const raw = this.read(path, 256);
|
|
169
|
+
const pid = raw?.match(/^(\d+):[a-f0-9]{32}$/)?.[1];
|
|
170
|
+
if (!pid)
|
|
171
|
+
throw new Error("Another workflow setup needs attention. Retry when it finishes.");
|
|
172
|
+
try {
|
|
173
|
+
process.kill(Number(pid), 0);
|
|
174
|
+
}
|
|
175
|
+
catch (probeError) {
|
|
176
|
+
if (probeError.code === "ESRCH") {
|
|
177
|
+
const before = lstatSync(path);
|
|
178
|
+
const retired = `${path}.retired-${randomBytes(8).toString("hex")}`;
|
|
179
|
+
renameSync(path, retired);
|
|
180
|
+
const after = lstatSync(retired);
|
|
181
|
+
if (before.ino !== after.ino || before.dev !== after.dev || readFileSync(retired, "utf8") !== raw) {
|
|
182
|
+
// Preserve a replacement lock if another process won the race.
|
|
183
|
+
// Linking is exclusive: it cannot overwrite a newly acquired lock.
|
|
184
|
+
try {
|
|
185
|
+
linkSync(retired, path);
|
|
186
|
+
unlinkSync(retired);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
/* Preserve the retired entry for its owner. */
|
|
190
|
+
}
|
|
191
|
+
throw new Error("Workflow lock changed. Retry setup.");
|
|
192
|
+
}
|
|
193
|
+
unlinkSync(retired);
|
|
194
|
+
return this.withLock(name, run);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
throw new Error("Another workflow setup is running. Retry when it finishes.");
|
|
198
|
+
}
|
|
199
|
+
const identity = fstatSync(descriptor);
|
|
200
|
+
try {
|
|
201
|
+
writeFileSync(descriptor, token);
|
|
202
|
+
fsyncSync(descriptor);
|
|
203
|
+
closeSync(descriptor);
|
|
204
|
+
descriptor = -1;
|
|
205
|
+
return run();
|
|
206
|
+
}
|
|
207
|
+
finally {
|
|
208
|
+
if (descriptor !== -1)
|
|
209
|
+
closeSync(descriptor);
|
|
210
|
+
const current = lstatSync(path);
|
|
211
|
+
if (current.ino === identity.ino && current.dev === identity.dev && this.read(path, 256) === token)
|
|
212
|
+
unlinkSync(path);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function managedSkillIpcArgsSupported(argv: readonly string[]): boolean;
|
|
2
|
+
export declare function readManagedSkillInput(input: AsyncIterable<string | Uint8Array>): Promise<unknown>;
|
|
3
|
+
/** Fixed local-only sidecar entry. Never print input, filesystem exceptions,
|
|
4
|
+
* stderr from children, or workflow instructions. */
|
|
5
|
+
export declare function managedSkillsIpcMain(): Promise<void>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { runManagedSkills } from "./managedSkills.js";
|
|
2
|
+
import { MANAGED_SKILL_INPUT_LIMIT } from "./managedSkillsValidation.js";
|
|
3
|
+
export function managedSkillIpcArgsSupported(argv) {
|
|
4
|
+
return JSON.stringify(argv.slice(-3)) === JSON.stringify(["managed-skills", "--stdin", "--json"]);
|
|
5
|
+
}
|
|
6
|
+
export async function readManagedSkillInput(input) {
|
|
7
|
+
const chunks = [];
|
|
8
|
+
let size = 0;
|
|
9
|
+
for await (const chunk of input) {
|
|
10
|
+
const bytes = Buffer.from(chunk);
|
|
11
|
+
size += bytes.byteLength;
|
|
12
|
+
if (size > MANAGED_SKILL_INPUT_LIMIT)
|
|
13
|
+
throw new Error("Managed workflow input is too large.");
|
|
14
|
+
chunks.push(bytes);
|
|
15
|
+
}
|
|
16
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks));
|
|
17
|
+
return JSON.parse(text);
|
|
18
|
+
}
|
|
19
|
+
/** Fixed local-only sidecar entry. Never print input, filesystem exceptions,
|
|
20
|
+
* stderr from children, or workflow instructions. */
|
|
21
|
+
export async function managedSkillsIpcMain() {
|
|
22
|
+
let result;
|
|
23
|
+
try {
|
|
24
|
+
// Node includes the script path while a compiled Deno sidecar does not;
|
|
25
|
+
// the final three arguments are stable in both launch modes.
|
|
26
|
+
if (!managedSkillIpcArgsSupported(process.argv))
|
|
27
|
+
throw new Error("Unsupported arguments.");
|
|
28
|
+
result = runManagedSkills(await readManagedSkillInput(process.stdin));
|
|
29
|
+
if (Buffer.byteLength(JSON.stringify(result)) > 256 * 1024)
|
|
30
|
+
throw new Error("Local inventory is too large.");
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
result = {
|
|
34
|
+
protocol: "passport.managed-skills",
|
|
35
|
+
version: 1,
|
|
36
|
+
ok: false,
|
|
37
|
+
operation: "status",
|
|
38
|
+
state: "failed",
|
|
39
|
+
filesChanged: 0,
|
|
40
|
+
message: "Passport could not read a valid managed workflow request."
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
44
|
+
}
|