toolroll 0.9.127 → 0.9.129
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 +36 -0
- package/README.md +2 -1
- package/dist/accent-colors.js +3 -1
- package/dist/approval-authority.js +2 -1
- package/dist/assignment.js +55 -2
- package/dist/backup.js +6 -0
- package/dist/browser/terminal.css +1 -1
- package/dist/browser/workspace.css +2 -2
- package/dist/browser/workspace.js +9 -9
- package/dist/central-recovery-stage.js +3 -3
- package/dist/companion/access.js +161 -0
- package/dist/companion/actions.js +559 -0
- package/dist/companion/bonjour.js +45 -0
- package/dist/companion/daemon.js +1346 -0
- package/dist/companion/direct-server.js +177 -0
- package/dist/companion/framing.js +498 -0
- package/dist/companion/noise.js +461 -0
- package/dist/companion/passkey-gate.js +287 -0
- package/dist/companion/paths.js +90 -0
- package/dist/companion/reads.js +279 -0
- package/dist/companion/relay-auth.js +265 -0
- package/dist/companion/relay-server.js +870 -0
- package/dist/companion/schema.js +225 -0
- package/dist/companion/service.js +580 -0
- package/dist/companion/store.js +324 -0
- package/dist/companion/webauthn-test-support.js +44 -0
- package/dist/companion/webauthn.js +398 -0
- package/dist/contracts/console-api.js +18 -0
- package/dist/demo.js +1 -1
- package/dist/design/kit.css +36 -3
- package/dist/local-landing.js +47 -0
- package/dist/models-ui.js +5 -2
- package/dist/operate.js +19 -3
- package/dist/person-proof.js +5 -1
- package/dist/pull-request-flow.js +75 -2
- package/dist/qr.js +385 -0
- package/dist/release-adapters.js +2 -1
- package/dist/restore-retirement.js +2 -0
- package/dist/scope.js +67 -4
- package/dist/serve.js +31 -4
- package/dist/server/pages.js +9 -2
- package/dist/server/phone-engine-api.js +59 -0
- package/dist/server/route-table.js +11 -0
- package/dist/server/settings-phone.js +492 -0
- package/dist/server/settings-scripts.js +2 -0
- package/dist/server/settings-sections.js +6 -2
- package/dist/server/settings.js +8 -2
- package/dist/settings-groups-ui.js +2 -1
- package/dist/settings-proofs.js +6 -0
- package/dist/skills-ui.js +11 -6
- package/dist/spend-ui.js +3 -2
- package/dist/spend.js +8 -1
- package/dist/store.js +37 -4
- package/dist/tailnet.js +42 -0
- package/dist/toolroll-update.js +3 -1
- package/dist/youiyoux-intake.js +8 -0
- package/package.json +7 -2
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
import { acceptAndCompleteAsOperator, acceptWindowCovers, assignmentOf } from "../assignment.js";
|
|
2
|
+
import { land, readLanding } from "../local-landing.js";
|
|
3
|
+
import { verifyApproverStanding } from "../principal.js";
|
|
4
|
+
import { completeAndOpenPullRequest, mergeAsPerson } from "../pull-request-flow.js";
|
|
5
|
+
import { approvalWindowCovers, approve } from "../scope.js";
|
|
6
|
+
import { requestContext } from "../server/request-context.js";
|
|
7
|
+
import { claimDeviceIdempotency, consumePasskeyChallenge, deviceAccountStands, getAccountPasskey, getPairedDevice, getPasskeyChallenge, insertAccountPasskey, listActiveAccountPasskeys, pruneDeviceIdempotency, prunePasskeyChallenges, reconcileIdempotencyOnStartup, savePasskeyChallenge, settleDeviceIdempotency, } from "./store.js";
|
|
8
|
+
import { acceptBindingOf, buildActionChallenge, canonicalJson, derivePasskeyUserHandle, minutesWords, normalizeAcceptNote, passkeyProofDetail, PASSKEY_CHALLENGE_TTL_MS, PASSKEY_ORIGIN, PASSKEY_RP_ID, PASSKEY_RP_NAME, randomChallenge32, sha256, verifyRegistrationResponse, } from "./webauthn.js";
|
|
9
|
+
import { activePasskeyLease, clearPasskeyLeases, passkeyApprovalWindowMinutes, startPasskeyLease, verifyPasskeyAction, } from "./passkey-gate.js";
|
|
10
|
+
export const APPROVALS_UNLOCKED_ACTION = "approvals unlocked";
|
|
11
|
+
export const PASSKEY_ENROLLMENT_WINDOW_MS = 5 * 60 * 1000;
|
|
12
|
+
const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
13
|
+
const TASK_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,199}$/;
|
|
14
|
+
export class CompanionActionError extends Error {
|
|
15
|
+
code;
|
|
16
|
+
constructor(code, message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.code = code;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function parseAssertion(raw) {
|
|
22
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
23
|
+
throw new CompanionActionError("invalid_params", "assertion object is required");
|
|
24
|
+
}
|
|
25
|
+
const obj = raw;
|
|
26
|
+
const credentialId = obj["credentialId"];
|
|
27
|
+
const clientDataJSON = obj["clientDataJSON"];
|
|
28
|
+
const authenticatorData = obj["authenticatorData"];
|
|
29
|
+
const signature = obj["signature"];
|
|
30
|
+
const userHandle = obj["userHandle"];
|
|
31
|
+
if (typeof credentialId !== "string" || credentialId.length === 0 ||
|
|
32
|
+
typeof clientDataJSON !== "string" || clientDataJSON.length === 0 ||
|
|
33
|
+
typeof authenticatorData !== "string" || authenticatorData.length === 0 ||
|
|
34
|
+
typeof signature !== "string" || signature.length === 0 ||
|
|
35
|
+
typeof userHandle !== "string" || userHandle.length === 0) {
|
|
36
|
+
throw new CompanionActionError("invalid_params", "assertion is missing required WebAuthn fields");
|
|
37
|
+
}
|
|
38
|
+
return { credentialId, clientDataJSON, authenticatorData, signature, userHandle };
|
|
39
|
+
}
|
|
40
|
+
function requireIdempotencyKey(params) {
|
|
41
|
+
const key = params["idempotencyKey"];
|
|
42
|
+
if (typeof key !== "string" || !UUID_V4.test(key)) {
|
|
43
|
+
throw new CompanionActionError("invalid_params", "idempotencyKey (UUIDv4) is required");
|
|
44
|
+
}
|
|
45
|
+
return key.toLowerCase();
|
|
46
|
+
}
|
|
47
|
+
function requireString(params, key) {
|
|
48
|
+
const val = params[key];
|
|
49
|
+
if (typeof val !== "string" || val.length === 0) {
|
|
50
|
+
throw new CompanionActionError("invalid_params", `${key} is required`);
|
|
51
|
+
}
|
|
52
|
+
return val;
|
|
53
|
+
}
|
|
54
|
+
export function createCompanionActions(deps) {
|
|
55
|
+
const { runtime } = deps;
|
|
56
|
+
const store = runtime.store;
|
|
57
|
+
const rpId = deps.rpId ?? PASSKEY_RP_ID;
|
|
58
|
+
const expectedOrigin = deps.expectedOrigin ?? PASSKEY_ORIGIN;
|
|
59
|
+
const windows = new Map();
|
|
60
|
+
const clearFreshLeases = (accountName, deviceId) => clearPasskeyLeases(store, accountName, deviceId);
|
|
61
|
+
const windowUntil = (principal) => {
|
|
62
|
+
const lease = activePasskeyLease(store, principal.deviceId, principal.accountName, now());
|
|
63
|
+
return lease === null ? null : new Date(lease.expiresAtMs).toISOString();
|
|
64
|
+
};
|
|
65
|
+
const windowAfter = (principal, proof) => (proof.kind === "lease" ? null : startPasskeyLease(store, { deviceId: principal.deviceId, accountName: principal.accountName, challenge: proof.challenge }))
|
|
66
|
+
?? windowUntil(principal);
|
|
67
|
+
const proofOf = (principal, params, lease) => {
|
|
68
|
+
if (params["useFreshSession"] === true) {
|
|
69
|
+
if (!lease)
|
|
70
|
+
throw new CompanionActionError("passkey_step_up_required", "Confirm with your passkey for this action.");
|
|
71
|
+
return { kind: "lease", deviceId: principal.deviceId };
|
|
72
|
+
}
|
|
73
|
+
return liveProofOf(principal, params);
|
|
74
|
+
};
|
|
75
|
+
const liveProofOf = (principal, params) => ({ kind: "assertion", deviceId: principal.deviceId, challenge: requireString(params, "challenge"), assertion: parseAssertion(params["assertion"]), rpId, expectedOrigin });
|
|
76
|
+
const unlockMinutes = (params, windowMinutes) => {
|
|
77
|
+
const raw = params["minutes"];
|
|
78
|
+
if (raw === undefined || raw === null)
|
|
79
|
+
return windowMinutes;
|
|
80
|
+
if (typeof raw !== "number" || !Number.isInteger(raw) || raw <= 0)
|
|
81
|
+
throw new CompanionActionError("invalid_params", "minutes must be a positive whole number");
|
|
82
|
+
return Math.min(raw, windowMinutes);
|
|
83
|
+
};
|
|
84
|
+
const windowOff = () => new CompanionActionError("approvals_window_off", "Your Mac asks for your passkey on every approval, so approvals can't be unlocked.");
|
|
85
|
+
reconcileIdempotencyOnStartup(store.handle, runtime.clock());
|
|
86
|
+
pruneDeviceIdempotency(store.handle, runtime.clock());
|
|
87
|
+
prunePasskeyChallenges(store.handle, runtime.clock());
|
|
88
|
+
function now() {
|
|
89
|
+
return runtime.clock();
|
|
90
|
+
}
|
|
91
|
+
function standsApprover(principal) {
|
|
92
|
+
const accountName = principal.accountName;
|
|
93
|
+
const identity = store.workspaceIdentity();
|
|
94
|
+
const device = getPairedDevice(store.handle, principal.deviceId);
|
|
95
|
+
const account = store.accountOf(accountName);
|
|
96
|
+
if (device === null ||
|
|
97
|
+
device.revokedAt !== null ||
|
|
98
|
+
device.activatedAt === null ||
|
|
99
|
+
device.accountName !== accountName ||
|
|
100
|
+
device.recoveryGeneration !== identity.recoveryGeneration ||
|
|
101
|
+
identity.recoveryGeneration !== principal.recoveryGeneration ||
|
|
102
|
+
!deviceAccountStands(device, account)) {
|
|
103
|
+
throw new CompanionActionError("standing_revoked", "This iPhone is no longer paired.");
|
|
104
|
+
}
|
|
105
|
+
if (account === null || account.role !== "approver") {
|
|
106
|
+
throw new CompanionActionError("refused", "Only an approver account can authorize actions from iPhone.");
|
|
107
|
+
}
|
|
108
|
+
const repos = store.knownRepos().filter(r => runtime.visible(r) && store.accountCanAccess(accountName, r));
|
|
109
|
+
const verified = verifyApproverStanding(store, accountName, account.generation, repos);
|
|
110
|
+
if (!verified.ok) {
|
|
111
|
+
throw new CompanionActionError("standing_revoked", "Your account standing changed. Pair again.");
|
|
112
|
+
}
|
|
113
|
+
return { accountName, account, who: verified.who, deviceName: device.deviceName };
|
|
114
|
+
}
|
|
115
|
+
function isEnrollmentOpen(accountName, deviceId = null) {
|
|
116
|
+
const win = windows.get(accountName);
|
|
117
|
+
if (win === undefined)
|
|
118
|
+
return false;
|
|
119
|
+
if (now().getTime() > win.expiresAtMs) {
|
|
120
|
+
windows.delete(accountName);
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
return win.deviceId === null || deviceId === null || win.deviceId === deviceId;
|
|
124
|
+
}
|
|
125
|
+
function openEnrollmentWindow(accountName, deviceId = null) {
|
|
126
|
+
const expiresAtMs = now().getTime() + PASSKEY_ENROLLMENT_WINDOW_MS;
|
|
127
|
+
windows.set(accountName, { accountName, deviceId, expiresAtMs });
|
|
128
|
+
return { expiresAtMs };
|
|
129
|
+
}
|
|
130
|
+
function closeEnrollmentWindow(accountName) {
|
|
131
|
+
windows.delete(accountName);
|
|
132
|
+
}
|
|
133
|
+
async function withIdempotency(principal, method, idempotencyKey, canonicalTarget, work) {
|
|
134
|
+
const requestHash = sha256(canonicalJson({ method, target: canonicalTarget })).toString("hex");
|
|
135
|
+
const claim = claimDeviceIdempotency(store.handle, { deviceId: principal.deviceId, idempotencyKey, method, requestHash, now: now() });
|
|
136
|
+
if (claim.status === "conflict") {
|
|
137
|
+
throw new CompanionActionError("idempotency_key_conflict", "This idempotency key was already used for a different request.");
|
|
138
|
+
}
|
|
139
|
+
if (claim.status === "in_flight")
|
|
140
|
+
throw new CompanionActionError("in_progress", "That action is already running on your Mac.");
|
|
141
|
+
if (claim.status === "unknown") {
|
|
142
|
+
throw new CompanionActionError("outcome_unknown", "The previous attempt didn't finish cleanly. Refresh to check the current state.");
|
|
143
|
+
}
|
|
144
|
+
if (claim.status === "completed") {
|
|
145
|
+
const stored = JSON.parse(claim.outcomeJson);
|
|
146
|
+
if (stored.kind === "ok")
|
|
147
|
+
return stored.value;
|
|
148
|
+
if (stored.kind === "error" && typeof stored.code === "string" && typeof stored.message === "string") {
|
|
149
|
+
throw new CompanionActionError(stored.code, stored.message);
|
|
150
|
+
}
|
|
151
|
+
throw new CompanionActionError("outcome_unknown", "The previous attempt didn't finish cleanly. Refresh to check the current state.");
|
|
152
|
+
}
|
|
153
|
+
const settle = (state, outcome) => {
|
|
154
|
+
settleDeviceIdempotency(store.handle, principal.deviceId, idempotencyKey, state, outcome === null ? null : JSON.stringify(outcome), now());
|
|
155
|
+
pruneDeviceIdempotency(store.handle, now());
|
|
156
|
+
};
|
|
157
|
+
let outcome;
|
|
158
|
+
try {
|
|
159
|
+
outcome = await work();
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
if (error instanceof CompanionActionError && error.code !== "outcome_unknown") {
|
|
163
|
+
settle("completed", { kind: "error", code: error.code, message: error.message });
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
settle("unknown", null);
|
|
167
|
+
if (error instanceof CompanionActionError)
|
|
168
|
+
throw error;
|
|
169
|
+
throw new CompanionActionError("outcome_unknown", "Something went wrong on your Mac. Refresh to check the current state.");
|
|
170
|
+
}
|
|
171
|
+
settle("completed", { kind: "ok", value: outcome });
|
|
172
|
+
return outcome;
|
|
173
|
+
}
|
|
174
|
+
async function perform(principal, params, method, target, work) {
|
|
175
|
+
const standing = standsApprover(principal);
|
|
176
|
+
const idempotencyKey = requireIdempotencyKey(params);
|
|
177
|
+
return withIdempotency(principal, method, idempotencyKey, target, async () => {
|
|
178
|
+
const out = await work(standing);
|
|
179
|
+
deps.onMutated?.();
|
|
180
|
+
return out;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
openEnrollmentWindow,
|
|
185
|
+
isEnrollmentOpen,
|
|
186
|
+
closeEnrollmentWindow,
|
|
187
|
+
clearFreshLeases,
|
|
188
|
+
sweep: () => {
|
|
189
|
+
const t = now();
|
|
190
|
+
prunePasskeyChallenges(store.handle, t);
|
|
191
|
+
pruneDeviceIdempotency(store.handle, t);
|
|
192
|
+
for (const [k, w] of windows.entries()) {
|
|
193
|
+
if (t.getTime() > w.expiresAtMs)
|
|
194
|
+
windows.delete(k);
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
passkeyStatus: async (principal) => {
|
|
198
|
+
const { accountName } = standsApprover(principal);
|
|
199
|
+
const identity = store.workspaceIdentity();
|
|
200
|
+
const active = listActiveAccountPasskeys(store.handle, identity.id, accountName, identity.recoveryGeneration);
|
|
201
|
+
return {
|
|
202
|
+
registered: active.length > 0,
|
|
203
|
+
count: active.length,
|
|
204
|
+
enrollmentOpen: isEnrollmentOpen(accountName, principal.deviceId),
|
|
205
|
+
rpId,
|
|
206
|
+
windowMinutes: passkeyApprovalWindowMinutes(store),
|
|
207
|
+
freshUntil: windowUntil(principal),
|
|
208
|
+
};
|
|
209
|
+
},
|
|
210
|
+
passkeyLock: async (principal) => {
|
|
211
|
+
const { accountName } = standsApprover(principal);
|
|
212
|
+
clearFreshLeases(accountName, principal.deviceId);
|
|
213
|
+
return { ok: true, locked: true };
|
|
214
|
+
},
|
|
215
|
+
passkeyRegisterBegin: async (principal) => {
|
|
216
|
+
const { accountName } = standsApprover(principal);
|
|
217
|
+
if (!isEnrollmentOpen(accountName, principal.deviceId)) {
|
|
218
|
+
throw new CompanionActionError("enrollment_closed", "Open Settings → Phone on your Mac and click Enable passkey on iPhone first.");
|
|
219
|
+
}
|
|
220
|
+
const identity = store.workspaceIdentity();
|
|
221
|
+
const t = now();
|
|
222
|
+
prunePasskeyChallenges(store.handle, t);
|
|
223
|
+
const challenge = randomChallenge32();
|
|
224
|
+
const expiresAt = new Date(t.getTime() + PASSKEY_CHALLENGE_TTL_MS).toISOString();
|
|
225
|
+
const userHandle = derivePasskeyUserHandle(identity.id, accountName).toString("base64url");
|
|
226
|
+
const active = listActiveAccountPasskeys(store.handle, identity.id, accountName, identity.recoveryGeneration);
|
|
227
|
+
savePasskeyChallenge(store.handle, {
|
|
228
|
+
challenge,
|
|
229
|
+
kind: "register",
|
|
230
|
+
workspaceId: identity.id,
|
|
231
|
+
accountName,
|
|
232
|
+
deviceId: principal.deviceId,
|
|
233
|
+
recoveryGeneration: identity.recoveryGeneration,
|
|
234
|
+
bindingJson: canonicalJson({ kind: "register", deviceId: principal.deviceId }),
|
|
235
|
+
expiresAt,
|
|
236
|
+
});
|
|
237
|
+
return {
|
|
238
|
+
challenge,
|
|
239
|
+
rp: { id: rpId, name: PASSKEY_RP_NAME },
|
|
240
|
+
user: {
|
|
241
|
+
id: userHandle,
|
|
242
|
+
name: accountName,
|
|
243
|
+
displayName: deps.workspaceLabel(),
|
|
244
|
+
},
|
|
245
|
+
pubKeyCredParams: [{ type: "public-key", alg: -7 }],
|
|
246
|
+
authenticatorSelection: { residentKey: "required", userVerification: "required" },
|
|
247
|
+
attestation: "none",
|
|
248
|
+
excludeCredentials: active.map(c => ({ id: c.credentialId, type: "public-key" })),
|
|
249
|
+
expiresAt,
|
|
250
|
+
};
|
|
251
|
+
},
|
|
252
|
+
passkeyRegisterFinish: async (principal, params) => {
|
|
253
|
+
const challenge = requireString(params, "challenge");
|
|
254
|
+
const clientDataJSON = requireString(params, "clientDataJSON");
|
|
255
|
+
const attestationObject = requireString(params, "attestationObject");
|
|
256
|
+
const credentialIdParam = typeof params["credentialId"] === "string" ? params["credentialId"] : undefined;
|
|
257
|
+
const customLabel = typeof params["label"] === "string" && params["label"].trim().length > 0 ? params["label"].trim() : null;
|
|
258
|
+
return store.transact(() => {
|
|
259
|
+
const { accountName, deviceName } = standsApprover(principal);
|
|
260
|
+
if (!isEnrollmentOpen(accountName, principal.deviceId)) {
|
|
261
|
+
throw new CompanionActionError("enrollment_closed", "The passkey enrollment window closed. Start again on your Mac.");
|
|
262
|
+
}
|
|
263
|
+
const identity = store.workspaceIdentity();
|
|
264
|
+
const t = now();
|
|
265
|
+
const row = getPasskeyChallenge(store.handle, challenge);
|
|
266
|
+
if (row === null ||
|
|
267
|
+
row.kind !== "register" ||
|
|
268
|
+
row.consumedAt !== null ||
|
|
269
|
+
Date.parse(row.expiresAt) <= t.getTime() ||
|
|
270
|
+
row.workspaceId !== identity.id ||
|
|
271
|
+
row.accountName !== accountName ||
|
|
272
|
+
row.deviceId !== principal.deviceId ||
|
|
273
|
+
row.recoveryGeneration !== identity.recoveryGeneration) {
|
|
274
|
+
throw new CompanionActionError("passkey_failed", "That registration challenge expired or isn't valid. Try again.");
|
|
275
|
+
}
|
|
276
|
+
const verified = verifyRegistrationResponse({
|
|
277
|
+
payload: {
|
|
278
|
+
...(credentialIdParam === undefined ? {} : { credentialId: credentialIdParam }),
|
|
279
|
+
clientDataJSON,
|
|
280
|
+
attestationObject,
|
|
281
|
+
},
|
|
282
|
+
expectedChallenge: row.challenge,
|
|
283
|
+
rpId,
|
|
284
|
+
expectedOrigin,
|
|
285
|
+
});
|
|
286
|
+
if (!verified.ok) {
|
|
287
|
+
throw new CompanionActionError("passkey_failed", verified.message);
|
|
288
|
+
}
|
|
289
|
+
if (getAccountPasskey(store.handle, verified.credentialId) !== null) {
|
|
290
|
+
throw new CompanionActionError("passkey_failed", "That passkey is already registered.");
|
|
291
|
+
}
|
|
292
|
+
const consumed = consumePasskeyChallenge(store.handle, row.challenge, t);
|
|
293
|
+
if (!consumed) {
|
|
294
|
+
throw new CompanionActionError("passkey_failed", "Registration challenge was already used.");
|
|
295
|
+
}
|
|
296
|
+
const userHandle = derivePasskeyUserHandle(identity.id, accountName).toString("base64url");
|
|
297
|
+
const label = customLabel ?? deviceName;
|
|
298
|
+
const inserted = insertAccountPasskey(store.handle, {
|
|
299
|
+
credentialId: verified.credentialId,
|
|
300
|
+
workspaceId: identity.id,
|
|
301
|
+
accountName,
|
|
302
|
+
userHandle,
|
|
303
|
+
publicKeySpki: verified.publicKeySpki,
|
|
304
|
+
signCount: verified.signCount,
|
|
305
|
+
label,
|
|
306
|
+
createdAt: t.toISOString(),
|
|
307
|
+
recoveryGeneration: identity.recoveryGeneration,
|
|
308
|
+
});
|
|
309
|
+
if (!inserted) {
|
|
310
|
+
throw new CompanionActionError("passkey_failed", "That passkey is already registered.");
|
|
311
|
+
}
|
|
312
|
+
closeEnrollmentWindow(accountName);
|
|
313
|
+
deps.onMutated?.();
|
|
314
|
+
return { ok: true, credentialId: verified.credentialId, label };
|
|
315
|
+
});
|
|
316
|
+
},
|
|
317
|
+
approvalChallenge: async (principal, params) => {
|
|
318
|
+
const { accountName, who, deviceName } = standsApprover(principal);
|
|
319
|
+
const identity = store.workspaceIdentity();
|
|
320
|
+
const activeCreds = listActiveAccountPasskeys(store.handle, identity.id, accountName, identity.recoveryGeneration);
|
|
321
|
+
if (activeCreds.length === 0) {
|
|
322
|
+
throw new CompanionActionError("no_passkey", "Set up a passkey in Settings before approving from this iPhone.");
|
|
323
|
+
}
|
|
324
|
+
const verbRaw = requireString(params, "verb");
|
|
325
|
+
if (verbRaw !== "approve" && verbRaw !== "accept" && verbRaw !== "land" && verbRaw !== "ship" && verbRaw !== "unlock") {
|
|
326
|
+
throw new CompanionActionError("invalid_params", "verb must be approve, accept, land, ship, or unlock");
|
|
327
|
+
}
|
|
328
|
+
const verb = verbRaw;
|
|
329
|
+
const forceFresh = params["forceFresh"] === true;
|
|
330
|
+
const t = now();
|
|
331
|
+
prunePasskeyChallenges(store.handle, t);
|
|
332
|
+
let binding;
|
|
333
|
+
let windowCovers = false;
|
|
334
|
+
let wording = null;
|
|
335
|
+
if (verb === "approve") {
|
|
336
|
+
const taskId = requireString(params, "task");
|
|
337
|
+
if (!TASK_ID.test(taskId))
|
|
338
|
+
throw new CompanionActionError("invalid_params", "Invalid task id.");
|
|
339
|
+
const family = requestContext.run({ csrf: "", returnTo: "/", actor: accountName }, () => runtime.familyOf(taskId));
|
|
340
|
+
if (family === null || family.problem !== null || !runtime.visible(family.current.repo) || !store.accountCanAccess(accountName, family.current.repo)) {
|
|
341
|
+
throw new CompanionActionError("not_found", "No such task.");
|
|
342
|
+
}
|
|
343
|
+
const scope = store.getScope(family.current.id);
|
|
344
|
+
if (scope === null)
|
|
345
|
+
throw new CompanionActionError("refused", "This task has no plan waiting for approval.");
|
|
346
|
+
const sawDigest = typeof params["digest"] === "string" && params["digest"].length > 0 ? params["digest"] : scope.digest;
|
|
347
|
+
if (sawDigest !== scope.digest) {
|
|
348
|
+
throw new CompanionActionError("refused", "This plan changed. Refresh before approving.");
|
|
349
|
+
}
|
|
350
|
+
windowCovers = approvalWindowCovers(store, family.current.id, accountName);
|
|
351
|
+
binding = { verb: "approve", taskId: family.current.id, sawDigest };
|
|
352
|
+
}
|
|
353
|
+
else if (verb === "accept") {
|
|
354
|
+
const taskId = requireString(params, "task");
|
|
355
|
+
if (!TASK_ID.test(taskId))
|
|
356
|
+
throw new CompanionActionError("invalid_params", "Invalid task id.");
|
|
357
|
+
const before = assignmentOf(store, taskId, t, { principal: "operator", repos: who.repos }, runtime.evidenceRoot);
|
|
358
|
+
if (before === null || !runtime.visible(before.repo) || !store.accountCanAccess(accountName, before.repo)) {
|
|
359
|
+
throw new CompanionActionError("not_found", "No such task.");
|
|
360
|
+
}
|
|
361
|
+
if (before.receipt === null)
|
|
362
|
+
throw new CompanionActionError("refused", "This task has no result ready to accept.");
|
|
363
|
+
const runId = typeof params["runId"] === "number" ? params["runId"] : null;
|
|
364
|
+
const receiptDigest = typeof params["receiptDigest"] === "string" && params["receiptDigest"].length > 0 ? params["receiptDigest"] : before.receipt.digest;
|
|
365
|
+
const publish = params["publish"] === true;
|
|
366
|
+
if (receiptDigest !== before.receipt.digest || (runId !== null && runId !== before.receipt.runId)) {
|
|
367
|
+
throw new CompanionActionError("refused", "This result changed. Refresh before accepting.");
|
|
368
|
+
}
|
|
369
|
+
windowCovers = acceptWindowCovers(store, before, publish, runtime.evidenceRoot);
|
|
370
|
+
binding = acceptBindingOf({ taskId, runId, receiptDigest, note: params["note"], publish }, before.receipt.runId);
|
|
371
|
+
}
|
|
372
|
+
else if (verb === "land") {
|
|
373
|
+
const taskId = requireString(params, "task");
|
|
374
|
+
if (!TASK_ID.test(taskId))
|
|
375
|
+
throw new CompanionActionError("invalid_params", "Invalid task id.");
|
|
376
|
+
const into = typeof params["into"] === "string" && params["into"].length > 0 ? params["into"] : null;
|
|
377
|
+
const undo = params["undo"] === true;
|
|
378
|
+
const read = await readLanding(store, taskId, { repos: who.repos }, t, {
|
|
379
|
+
root: runtime.evidenceRoot,
|
|
380
|
+
...(deps.git === undefined ? {} : { git: deps.git }),
|
|
381
|
+
...(into === null ? {} : { into }),
|
|
382
|
+
...(undo ? { undo: true } : {}),
|
|
383
|
+
});
|
|
384
|
+
if (!read.ok)
|
|
385
|
+
throw new CompanionActionError("refused", read.message);
|
|
386
|
+
if (!("plan" in read))
|
|
387
|
+
throw new CompanionActionError("refused", "This result has already landed.");
|
|
388
|
+
const planKey = typeof params["key"] === "string" && params["key"].length > 0 ? params["key"] : read.plan.key;
|
|
389
|
+
if (planKey !== read.plan.key)
|
|
390
|
+
throw new CompanionActionError("refused", "The landing plan changed. Refresh first.");
|
|
391
|
+
binding = { verb: "land", taskId, planKey, into, undo };
|
|
392
|
+
}
|
|
393
|
+
else if (verb === "ship") {
|
|
394
|
+
const runIdRaw = params["runId"];
|
|
395
|
+
if (typeof runIdRaw !== "number" || !Number.isInteger(runIdRaw) || runIdRaw <= 0) {
|
|
396
|
+
throw new CompanionActionError("invalid_params", "runId is required");
|
|
397
|
+
}
|
|
398
|
+
const run = store.getRun(runIdRaw);
|
|
399
|
+
const taskRef = run === null ? null : store.refById(run.taskRef);
|
|
400
|
+
if (run === null || taskRef === null || taskRef.repo === null || !runtime.visible(taskRef.repo) || !store.accountCanAccess(accountName, taskRef.repo)) {
|
|
401
|
+
throw new CompanionActionError("not_found", "No such run.");
|
|
402
|
+
}
|
|
403
|
+
const publication = store.publicationForRun(runIdRaw);
|
|
404
|
+
const grant = store.publicationGrantFor(taskRef.repo);
|
|
405
|
+
if (publication === null || publication.prNumber === null) {
|
|
406
|
+
throw new CompanionActionError("refused", "This result has no open pull request to merge.");
|
|
407
|
+
}
|
|
408
|
+
const method = grant?.mergeMethod ?? "squash";
|
|
409
|
+
const anyway = params["anyway"] === true;
|
|
410
|
+
binding = { verb: "ship", runId: runIdRaw, taskId: taskRef.externalId, prNumber: publication.prNumber, headSha: publication.headSha, method, anyway };
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
const windowMinutes = passkeyApprovalWindowMinutes(store);
|
|
414
|
+
if (windowMinutes <= 0)
|
|
415
|
+
throw windowOff();
|
|
416
|
+
const minutes = unlockMinutes(params, windowMinutes);
|
|
417
|
+
binding = { verb: "unlock", minutes };
|
|
418
|
+
wording = `Unlock approvals on ${deviceName} for ${minutesWords(minutes)}`;
|
|
419
|
+
}
|
|
420
|
+
const built = buildActionChallenge({
|
|
421
|
+
workspaceId: identity.id,
|
|
422
|
+
recoveryGeneration: identity.recoveryGeneration,
|
|
423
|
+
accountName,
|
|
424
|
+
deviceId: principal.deviceId,
|
|
425
|
+
binding,
|
|
426
|
+
});
|
|
427
|
+
const allowCredentials = activeCreds.map(c => ({ id: c.credentialId, type: "public-key" }));
|
|
428
|
+
const freshUntil = !forceFresh && windowCovers ? windowUntil(principal) : null;
|
|
429
|
+
if (freshUntil !== null) {
|
|
430
|
+
return { challenge: "", rpId, allowCredentials, userVerification: "required", verb, wording: built.wording, expiresAt: freshUntil, binding, freshApproved: true, freshUntil };
|
|
431
|
+
}
|
|
432
|
+
const expiresAt = new Date(t.getTime() + PASSKEY_CHALLENGE_TTL_MS).toISOString();
|
|
433
|
+
savePasskeyChallenge(store.handle, {
|
|
434
|
+
challenge: built.challenge,
|
|
435
|
+
kind: verb,
|
|
436
|
+
workspaceId: identity.id,
|
|
437
|
+
accountName,
|
|
438
|
+
deviceId: principal.deviceId,
|
|
439
|
+
recoveryGeneration: identity.recoveryGeneration,
|
|
440
|
+
bindingJson: built.bindingJson,
|
|
441
|
+
expiresAt,
|
|
442
|
+
});
|
|
443
|
+
return {
|
|
444
|
+
challenge: built.challenge, rpId, allowCredentials, userVerification: "required", verb, wording: wording ?? built.wording, expiresAt, binding,
|
|
445
|
+
freshApproved: false, freshUntil: null,
|
|
446
|
+
};
|
|
447
|
+
},
|
|
448
|
+
passkeyUnlock: async (principal, params) => {
|
|
449
|
+
const { accountName } = standsApprover(principal);
|
|
450
|
+
const proof = liveProofOf(principal, params);
|
|
451
|
+
const windowMinutes = passkeyApprovalWindowMinutes(store);
|
|
452
|
+
if (windowMinutes <= 0)
|
|
453
|
+
throw windowOff();
|
|
454
|
+
const minutes = unlockMinutes(params, windowMinutes);
|
|
455
|
+
store.transact(() => {
|
|
456
|
+
const t = now();
|
|
457
|
+
const unlocked = verifyPasskeyAction(store, {
|
|
458
|
+
accountName, repo: null, anyProject: true, verb: "unlock", expectedBinding: { verb: "unlock", minutes }, proof, now: t, startsLease: { minutes },
|
|
459
|
+
});
|
|
460
|
+
if (!unlocked.ok)
|
|
461
|
+
throw new CompanionActionError("passkey_failed", unlocked.message);
|
|
462
|
+
store.recordAction({ at: t.toISOString(), actor: accountName, repo: null, taskId: null, runId: null, action: APPROVALS_UNLOCKED_ACTION, outcome: "passkey", source: "request",
|
|
463
|
+
detail: passkeyProofDetail({ at: t, generation: unlocked.accountGeneration, digest: `${minutes}m`, verb: "unlock", credentialId: unlocked.credentialId,
|
|
464
|
+
pairedDeviceId: unlocked.deviceId, be: unlocked.be, bs: unlocked.bs }) });
|
|
465
|
+
});
|
|
466
|
+
const freshUntil = startPasskeyLease(store, { deviceId: principal.deviceId, accountName, challenge: proof.challenge });
|
|
467
|
+
return { ok: true, unlocked: freshUntil !== null, minutes, windowMinutes, freshUntil };
|
|
468
|
+
},
|
|
469
|
+
taskApprove: async (principal, params) => {
|
|
470
|
+
const taskId = requireString(params, "task");
|
|
471
|
+
const sawDigest = requireString(params, "digest");
|
|
472
|
+
const proof = proofOf(principal, params, true);
|
|
473
|
+
return perform(principal, params, "task.approve", { taskId, sawDigest }, ({ accountName }) => {
|
|
474
|
+
const family = requestContext.run({ csrf: "", returnTo: "/", actor: accountName }, () => runtime.familyOf(taskId));
|
|
475
|
+
if (family === null || family.problem !== null || !runtime.visible(family.current.repo) || !store.accountCanAccess(accountName, family.current.repo)) {
|
|
476
|
+
throw new CompanionActionError("not_found", "No such task.");
|
|
477
|
+
}
|
|
478
|
+
const res = approve(store, family.current.id, accountName, now(), sawDigest, "", {}, undefined, undefined, proof);
|
|
479
|
+
if (!res.ok) {
|
|
480
|
+
if (res.reason === "password-now")
|
|
481
|
+
throw new CompanionActionError("passkey_step_up_required", "Confirm with your passkey to approve this task.");
|
|
482
|
+
if (res.reason === "changed")
|
|
483
|
+
throw new CompanionActionError("refused", "This plan changed. Refresh and review the latest terms.");
|
|
484
|
+
if (res.reason === "policy")
|
|
485
|
+
throw new CompanionActionError("refused", res.message);
|
|
486
|
+
if (res.reason === "second-approver")
|
|
487
|
+
return { ok: true, approved: false, secondApprover: true, have: res.have, need: res.need };
|
|
488
|
+
throw new CompanionActionError("refused", `Couldn't approve this task (${res.reason}).`);
|
|
489
|
+
}
|
|
490
|
+
return { ok: true, approved: true, task: family.current.id, digest: res.scope.digest, freshUntil: windowAfter(principal, proof) };
|
|
491
|
+
});
|
|
492
|
+
},
|
|
493
|
+
taskAccept: async (principal, params) => {
|
|
494
|
+
const taskId = requireString(params, "task");
|
|
495
|
+
const receiptDigest = requireString(params, "receiptDigest");
|
|
496
|
+
const runId = typeof params["runId"] === "number" ? params["runId"] : null;
|
|
497
|
+
const note = normalizeAcceptNote(params["note"]);
|
|
498
|
+
const publish = params["publish"] === true;
|
|
499
|
+
const proof = proofOf(principal, params, !publish);
|
|
500
|
+
return perform(principal, params, "task.accept", { taskId, runId, receiptDigest, note, publish }, ({ who }) => {
|
|
501
|
+
let openedPrUrl = null;
|
|
502
|
+
let openedPrNumber = null;
|
|
503
|
+
const done = publish
|
|
504
|
+
? acceptAndCompleteAsOperator(store, taskId, { runId, receiptDigest, note, publish: true, passkey: proof }, who, now(), runtime.evidenceRoot, digest => {
|
|
505
|
+
const targetRunId = runId ?? assignmentOf(store, taskId, now(), { principal: "operator", repos: who.repos }, runtime.evidenceRoot)?.receipt?.runId ?? null;
|
|
506
|
+
if (targetRunId === null)
|
|
507
|
+
return { ok: false, reason: "stale", message: "No result run found to publish." };
|
|
508
|
+
const pub = completeAndOpenPullRequest(store, { taskId, digest, runId: targetRunId, who, root: runtime.evidenceRoot }, now());
|
|
509
|
+
if (!pub.ok)
|
|
510
|
+
return { ok: false, reason: "not-publishable", message: pub.message };
|
|
511
|
+
openedPrUrl = pub.publication.prUrl;
|
|
512
|
+
openedPrNumber = pub.publication.prNumber;
|
|
513
|
+
return { ok: true, digest };
|
|
514
|
+
})
|
|
515
|
+
: acceptAndCompleteAsOperator(store, taskId, { runId, receiptDigest, note, publish: false, passkey: proof }, who, now(), runtime.evidenceRoot);
|
|
516
|
+
if (!done.ok)
|
|
517
|
+
throw new CompanionActionError(done.reason === "step-up-required" ? "passkey_step_up_required" : "refused", done.message);
|
|
518
|
+
return { ok: true, accepted: true, published: publish, prUrl: openedPrUrl, prNumber: openedPrNumber, freshUntil: windowAfter(principal, proof) };
|
|
519
|
+
});
|
|
520
|
+
},
|
|
521
|
+
taskLand: async (principal, params) => {
|
|
522
|
+
const taskId = requireString(params, "task");
|
|
523
|
+
const key = requireString(params, "key");
|
|
524
|
+
const into = typeof params["into"] === "string" && params["into"].length > 0 ? params["into"] : undefined;
|
|
525
|
+
const undo = params["undo"] === true;
|
|
526
|
+
const proof = liveProofOf(principal, params);
|
|
527
|
+
return perform(principal, params, "task.land", { taskId, key, into: into ?? null, undo }, async ({ who }) => {
|
|
528
|
+
const res = await land(store, taskId, { key, ...(into === undefined ? {} : { into }), ...(undo ? { undo: true } : {}) }, { kind: "passkey", who, proof, clock: runtime.clock }, { repos: who.repos }, now(), { root: runtime.evidenceRoot, ...(deps.git === undefined ? {} : { git: deps.git }) });
|
|
529
|
+
if (!res.ok)
|
|
530
|
+
throw new CompanionActionError("refused", res.message);
|
|
531
|
+
return { ok: true, landed: true, replayed: res.replayed, branch: res.branch, commit: res.commit, mode: res.mode, handoffCommand: res.handoff?.command ?? null, freshUntil: windowUntil(principal) };
|
|
532
|
+
});
|
|
533
|
+
},
|
|
534
|
+
taskShip: async (principal, params) => {
|
|
535
|
+
const runIdRaw = params["runId"];
|
|
536
|
+
if (typeof runIdRaw !== "number" || !Number.isInteger(runIdRaw) || runIdRaw <= 0) {
|
|
537
|
+
throw new CompanionActionError("invalid_params", "runId is required");
|
|
538
|
+
}
|
|
539
|
+
const anyway = params["anyway"] === true;
|
|
540
|
+
const proof = liveProofOf(principal, params);
|
|
541
|
+
return perform(principal, params, "task.ship", { runId: runIdRaw, anyway }, async ({ accountName }) => {
|
|
542
|
+
const res = await mergeAsPerson(store, {
|
|
543
|
+
runId: runIdRaw,
|
|
544
|
+
name: accountName,
|
|
545
|
+
passkey: proof,
|
|
546
|
+
anyway,
|
|
547
|
+
...(deps.publishExec === undefined ? {} : { exec: deps.publishExec }),
|
|
548
|
+
clock: runtime.clock,
|
|
549
|
+
});
|
|
550
|
+
if (!res.ok) {
|
|
551
|
+
if (res.reason === "unconfirmed")
|
|
552
|
+
throw new CompanionActionError("outcome_unknown", res.message);
|
|
553
|
+
throw new CompanionActionError("refused", res.message);
|
|
554
|
+
}
|
|
555
|
+
return { ok: true, shipped: true, commit: res.commit, freshUntil: windowUntil(principal) };
|
|
556
|
+
});
|
|
557
|
+
},
|
|
558
|
+
};
|
|
559
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
2
|
+
export const BONJOUR_SERVICE = "_toolroll._tcp";
|
|
3
|
+
const MACHINE_ID = /^[a-z2-7]{26}$/;
|
|
4
|
+
export function bonjourCommand(platform, port, machineId) {
|
|
5
|
+
if (!MACHINE_ID.test(machineId) || !Number.isInteger(port) || port < 1 || port > 65_535)
|
|
6
|
+
return null;
|
|
7
|
+
const name = `Toolroll ${machineId.slice(0, 6)}`;
|
|
8
|
+
const txt = `m=${machineId}`;
|
|
9
|
+
if (platform === "darwin")
|
|
10
|
+
return ["dns-sd", "-R", name, BONJOUR_SERVICE, "local", String(port), txt];
|
|
11
|
+
if (platform === "linux")
|
|
12
|
+
return ["avahi-publish-service", name, BONJOUR_SERVICE, String(port), txt];
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
export function startBonjour(opts) {
|
|
16
|
+
const log = opts.log ?? (() => { });
|
|
17
|
+
const argv = bonjourCommand(opts.platform ?? process.platform, opts.port, opts.machineId);
|
|
18
|
+
if (argv === null)
|
|
19
|
+
return { argv: null, stop: () => { } };
|
|
20
|
+
let child = null;
|
|
21
|
+
try {
|
|
22
|
+
child = (opts.spawn ?? nodeSpawn)(argv[0], argv.slice(1), { shell: false, stdio: "ignore", detached: false });
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
log(`phone access: not advertising on the local network (${error instanceof Error ? error.message : String(error)})`);
|
|
26
|
+
return { argv, stop: () => { } };
|
|
27
|
+
}
|
|
28
|
+
let stopped = false;
|
|
29
|
+
child.on("error", error => {
|
|
30
|
+
if (!stopped)
|
|
31
|
+
log(`phone access: not advertising on the local network (${error.code ?? error.message})`);
|
|
32
|
+
child = null;
|
|
33
|
+
});
|
|
34
|
+
child.on("exit", () => { child = null; });
|
|
35
|
+
child.unref?.();
|
|
36
|
+
return {
|
|
37
|
+
argv,
|
|
38
|
+
stop: () => {
|
|
39
|
+
stopped = true;
|
|
40
|
+
if (child !== null && child.exitCode === null)
|
|
41
|
+
child.kill("SIGTERM");
|
|
42
|
+
child = null;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|