@moxuli/moremote 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +31 -0
- package/README.md +73 -2
- package/bin/moremote-cli.js +34 -0
- package/bin/moremote-service.js +7 -0
- package/dist/bindings.d.ts +6 -0
- package/dist/bindings.js +4 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +325 -0
- package/dist/cloud-client.d.ts +43 -0
- package/dist/cloud-client.js +128 -0
- package/dist/config.d.ts +36 -0
- package/dist/config.js +68 -0
- package/dist/control-transport.d.ts +4 -0
- package/dist/control-transport.js +68 -0
- package/dist/control.d.ts +3 -0
- package/dist/control.js +297 -0
- package/dist/controls.d.ts +15 -0
- package/dist/controls.js +36 -0
- package/dist/crypto.d.ts +23 -0
- package/dist/crypto.js +68 -0
- package/dist/evidence.d.ts +19 -0
- package/dist/evidence.js +4 -0
- package/dist/journal.d.ts +104 -0
- package/dist/journal.js +132 -0
- package/dist/limits.d.ts +21 -0
- package/dist/limits.js +8 -0
- package/dist/official-client.d.ts +120 -0
- package/dist/official-client.js +162 -0
- package/dist/operations.d.ts +71 -0
- package/dist/operations.js +210 -0
- package/dist/pairing.d.ts +19 -0
- package/dist/pairing.js +68 -0
- package/dist/permission.d.ts +9 -0
- package/dist/permission.js +66 -0
- package/dist/projection.d.ts +24 -0
- package/dist/projection.js +35 -0
- package/dist/resources.d.ts +76 -0
- package/dist/resources.js +299 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.js +375 -0
- package/dist/service.d.ts +21 -0
- package/dist/service.js +310 -0
- package/dist/supervisor.d.ts +15 -0
- package/dist/supervisor.js +175 -0
- package/dist/update-state.d.ts +30 -0
- package/dist/update-state.js +158 -0
- package/dist/updater.d.ts +9 -0
- package/dist/updater.js +152 -0
- package/docs/README.md +23 -0
- package/docs/STATE.md +155 -0
- package/docs/configuration.md +37 -0
- package/docs/conventions.md +9 -0
- package/docs/decisions/0001-managed-updates.md +19 -0
- package/docs/plans/2026-11-10-managed-updates.md +26 -0
- package/docs/plans/2026-11-10-notification-delivery.md +118 -0
- package/docs/releasing.md +34 -0
- package/docs/specs/01-architecture.md +102 -0
- package/docs/specs/02-encrypted-delivery.md +194 -0
- package/docs/specs/03-api-contract.md +397 -0
- package/docs/specs/04-control-and-receipt-lifecycle.md +175 -0
- package/docs/specs/05-endpoint-and-lifecycle.md +57 -0
- package/docs/specs/06-managed-updates.md +55 -0
- package/licenses/Paseo-0.15.3.txt +211 -0
- package/package.json +37 -4
- package/vectors/bearer-v1.json +6 -0
- package/vectors/envelope-v1.json +24 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { exact, object, text, LIMITS } from './limits.js';
|
|
2
|
+
import { decode, openEnvelope, sealEnvelope, validateEnvelope } from './crypto.js';
|
|
3
|
+
import { occurrenceBinding } from './bindings.js';
|
|
4
|
+
import { validatePermissionInput } from './permission.js';
|
|
5
|
+
import { evidenceForCommand } from './evidence.js';
|
|
6
|
+
export function parseCommand(value, id) {
|
|
7
|
+
const c = object(value);
|
|
8
|
+
text(c.agentId);
|
|
9
|
+
if (c.type === 'send') {
|
|
10
|
+
exact(c, ['type', 'agentId', 'text', 'messageId']);
|
|
11
|
+
text(c.text, 60000);
|
|
12
|
+
if (c.messageId !== id)
|
|
13
|
+
throw Error('MESSAGE_ID_MISMATCH');
|
|
14
|
+
}
|
|
15
|
+
else if (c.type === 'permission') {
|
|
16
|
+
exact(c, ['type', 'agentId', 'requestId', 'requestBinding', 'response']);
|
|
17
|
+
text(c.requestId);
|
|
18
|
+
decode(c.requestBinding, 32);
|
|
19
|
+
const r = object(c.response);
|
|
20
|
+
if (r.behavior === 'allow') {
|
|
21
|
+
exact(r, ['behavior', 'selectedActionId', 'updatedInput', 'updatedPermissions']);
|
|
22
|
+
if (r.updatedInput !== undefined)
|
|
23
|
+
object(r.updatedInput);
|
|
24
|
+
if (r.updatedPermissions !== undefined && (!Array.isArray(r.updatedPermissions) || r.updatedPermissions.length > 20 || r.updatedPermissions.some(v => !v || typeof v !== 'object' || Array.isArray(v))))
|
|
25
|
+
throw Error('INVALID_PERMISSIONS');
|
|
26
|
+
}
|
|
27
|
+
else if (r.behavior === 'deny') {
|
|
28
|
+
exact(r, ['behavior', 'selectedActionId', 'message', 'interrupt']);
|
|
29
|
+
if (r.message !== undefined)
|
|
30
|
+
text(r.message, 8192);
|
|
31
|
+
if (r.interrupt !== undefined && typeof r.interrupt !== 'boolean')
|
|
32
|
+
throw Error('INVALID_RESPONSE');
|
|
33
|
+
}
|
|
34
|
+
else
|
|
35
|
+
throw Error('INVALID_RESPONSE');
|
|
36
|
+
if (r.selectedActionId !== undefined)
|
|
37
|
+
text(r.selectedActionId);
|
|
38
|
+
}
|
|
39
|
+
else
|
|
40
|
+
throw Error('UNSUPPORTED_COMMAND');
|
|
41
|
+
if (Buffer.byteLength(JSON.stringify(c)) > LIMITS.plainBytes)
|
|
42
|
+
throw Error('COMMAND_LIMIT');
|
|
43
|
+
return c;
|
|
44
|
+
}
|
|
45
|
+
export function validateCommand(value, id, current) {
|
|
46
|
+
const c = parseCommand(value, id);
|
|
47
|
+
if (!current || current.id !== c.agentId)
|
|
48
|
+
throw Error('AGENT_NOT_CURRENT');
|
|
49
|
+
if (c.type === 'permission') {
|
|
50
|
+
const request = current.pendingPermissions?.find(r => r.id === c.requestId);
|
|
51
|
+
if (!request || request.requestBinding !== c.requestBinding)
|
|
52
|
+
throw Error('STALE_REQUEST_INSTANCE');
|
|
53
|
+
const action = c.response.selectedActionId;
|
|
54
|
+
if (!request.actions?.length || !action || !request.actions.some(a => a.id === action && a.behavior === c.response.behavior))
|
|
55
|
+
throw Error('STALE_ACTION');
|
|
56
|
+
validatePermissionInput(request, c.response);
|
|
57
|
+
}
|
|
58
|
+
return c;
|
|
59
|
+
}
|
|
60
|
+
export async function boundCurrent(ctx, agentId) { const current = await ctx.official.current(agentId); if (!current)
|
|
61
|
+
return null; const source = ctx.official.requestContext?.(); if (!source)
|
|
62
|
+
return current; const requests = current.pendingPermissions || []; ctx.journal.syncRequests(source.serverId, agentId, source.generation, requests); return { ...current, pendingPermissions: await Promise.all(requests.map(async (r) => { const occurrence = ctx.journal.currentOccurrence(source.serverId, agentId, r.id); if (!occurrence)
|
|
63
|
+
return { ...r, requestBinding: undefined }; return { ...r, occurrence, requestBinding: await occurrenceBinding(ctx.pair, occurrence) }; })) }; }
|
|
64
|
+
async function outcome(ctx, e, state, now, code) {
|
|
65
|
+
const old = ctx.journal.get(e.pairId, e.id);
|
|
66
|
+
if (old.state === 'accepted' && state === 'unknown')
|
|
67
|
+
state = 'accepted';
|
|
68
|
+
if (old.state === 'resolved' || old.state === 'rejected')
|
|
69
|
+
state = old.state;
|
|
70
|
+
if (old.receipt && old.state === state) {
|
|
71
|
+
if (old.sentRevision < old.revision) {
|
|
72
|
+
await ctx.cloud.receipt(e.id, { state, revision: old.revision, envelope: old.receipt });
|
|
73
|
+
ctx.journal.receiptSent(e.pairId, e.id, old.revision);
|
|
74
|
+
}
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (now >= e.expiresAt + LIMITS.retentionMs)
|
|
78
|
+
return;
|
|
79
|
+
const revision = old.revision + 1;
|
|
80
|
+
const envelope = await sealEnvelope(decode(ctx.pair.rootKey, 32), { version: 1, pairId: e.pairId, id: e.id, channel: 'receipt', issuedAt: now, expiresAt: now + LIMITS.snapshotMs }, { operationId: e.id, state, revision, ...(code ? { code } : {}) });
|
|
81
|
+
ctx.journal.outcome(e.pairId, e.id, state, envelope, code, revision);
|
|
82
|
+
await ctx.cloud.receipt(e.id, { state, revision, envelope });
|
|
83
|
+
ctx.journal.receiptSent(e.pairId, e.id, revision);
|
|
84
|
+
}
|
|
85
|
+
/** Read-only recovery, including after command payload expiry or pairing revoke. */
|
|
86
|
+
export async function reconcileJournal(ctx, now = Date.now()) {
|
|
87
|
+
let failed = false;
|
|
88
|
+
for (const record of ctx.journal.pending(ctx.pair.pairId)) {
|
|
89
|
+
if (record.state === 'prepared')
|
|
90
|
+
continue;
|
|
91
|
+
try {
|
|
92
|
+
const state = record.evidence && ctx.official.reconcileEvidence ? await ctx.official.reconcileEvidence(record.evidence, ctx.pair) : null;
|
|
93
|
+
await outcome(ctx, record, state || 'unknown', now, state ? undefined : 'UNCONFIRMED');
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
failed = true;
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
ctx.journal.reconciled(record.pairId, record.id, now);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return failed;
|
|
103
|
+
}
|
|
104
|
+
export async function executeOperation(ctx, input, now) {
|
|
105
|
+
const clock = ctx.clock || (now === undefined ? Date.now : () => now);
|
|
106
|
+
now = now ?? clock();
|
|
107
|
+
const e = validateEnvelope(input, now);
|
|
108
|
+
if (e.channel !== 'command' || e.pairId !== ctx.pair.pairId || !ctx.pair.active || ctx.pair.relayRetiredAt !== undefined)
|
|
109
|
+
throw Error('PAIR_NOT_ACTIVE');
|
|
110
|
+
const value = await openEnvelope(decode(ctx.pair.rootKey, 32), e, now);
|
|
111
|
+
const command = parseCommand(value, e.id);
|
|
112
|
+
const { record, fresh } = ctx.journal.prepare(e);
|
|
113
|
+
if (fresh)
|
|
114
|
+
ctx.journal.evidence(e.pairId, e.id, evidenceForCommand(command, ctx.official.requestContext?.().serverId || ''));
|
|
115
|
+
if (!fresh) {
|
|
116
|
+
if (record.state === 'prepared')
|
|
117
|
+
return;
|
|
118
|
+
if (record.receipt && (record.state === 'accepted' || record.state === 'resolved' || record.state === 'rejected')) {
|
|
119
|
+
await outcome(ctx, e, record.state, now);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const reconciled = await ctx.official.reconcile(command, ctx.pair);
|
|
123
|
+
await outcome(ctx, e, reconciled || 'unknown', now, reconciled ? undefined : 'UNCONFIRMED');
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (ctx.journal.blocked(e.pairId, e.id))
|
|
127
|
+
return;
|
|
128
|
+
const current = await boundCurrent(ctx, command.agentId);
|
|
129
|
+
let invalid = false;
|
|
130
|
+
try {
|
|
131
|
+
validateCommand(value, e.id, current);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
invalid = true;
|
|
135
|
+
}
|
|
136
|
+
if (command.type === 'permission') {
|
|
137
|
+
const occurrence = current?.pendingPermissions?.find(r => r.id === command.requestId)?.occurrence;
|
|
138
|
+
if (occurrence)
|
|
139
|
+
ctx.journal.evidenceOccurrence(e.pairId, e.id, occurrence.occurrenceId);
|
|
140
|
+
}
|
|
141
|
+
if (e.expiresAt <= clock() || ctx.journal.blocked(e.pairId, e.id))
|
|
142
|
+
return;
|
|
143
|
+
// Irrevocable local commit before cloud dispatch. Recovery cannot reacquire or replay it.
|
|
144
|
+
ctx.journal.dispatched(e.pairId, e.id);
|
|
145
|
+
let boundary;
|
|
146
|
+
try {
|
|
147
|
+
boundary = await ctx.cloud.dispatch(e.id);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
await outcome(ctx, e, 'unknown', clock(), 'DISPATCH_UNCONFIRMED');
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (!boundary.dispatchAllowed) {
|
|
154
|
+
await outcome(ctx, e, 'unknown', clock(), 'DISPATCH_DENIED');
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (e.expiresAt <= clock() || ctx.journal.blocked(e.pairId, e.id)) {
|
|
158
|
+
await outcome(ctx, e, 'rejected', clock(), 'EXPIRED_OR_LOCALLY_REVOKED');
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (invalid) {
|
|
162
|
+
await outcome(ctx, e, 'rejected', clock(), 'STALE_OR_INVALID_REQUEST');
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
let refreshed;
|
|
166
|
+
try {
|
|
167
|
+
refreshed = await boundCurrent(ctx, command.agentId);
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
await outcome(ctx, e, 'rejected', clock(), 'OFFICIAL_UNAVAILABLE');
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
validateCommand(value, e.id, refreshed);
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
await outcome(ctx, e, 'rejected', clock(), 'STALE_OR_INVALID_REQUEST');
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (command.type === 'permission' && ctx.official.requestContext) {
|
|
181
|
+
const occurrence = refreshed?.pendingPermissions?.find(r => r.id === command.requestId)?.occurrence;
|
|
182
|
+
if (!occurrence || !ctx.journal.isCurrent(occurrence)) {
|
|
183
|
+
await outcome(ctx, e, 'rejected', clock(), 'STALE_REQUEST_INSTANCE');
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (e.expiresAt <= clock() || ctx.journal.blocked(e.pairId, e.id)) {
|
|
188
|
+
await outcome(ctx, e, 'rejected', clock(), 'EXPIRED_OR_LOCALLY_REVOKED');
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
let state;
|
|
192
|
+
let code;
|
|
193
|
+
try {
|
|
194
|
+
if (command.type === 'send') {
|
|
195
|
+
await ctx.official.send(command);
|
|
196
|
+
state = 'accepted';
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
await ctx.official.permission(command);
|
|
200
|
+
state = 'resolved';
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
state = await ctx.official.reconcile(command, ctx.pair).catch(() => null) || 'unknown';
|
|
205
|
+
if (state === 'unknown')
|
|
206
|
+
code = 'OFFICIAL_UNCONFIRMED';
|
|
207
|
+
}
|
|
208
|
+
// Receipt transport failure cannot erase a separately persisted positive admission fact.
|
|
209
|
+
await outcome(ctx, e, state, clock(), code);
|
|
210
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CloudClient } from './cloud-client.js';
|
|
2
|
+
import { type Pair, type Config } from './config.js';
|
|
3
|
+
export declare function tokenHash(token: string): string;
|
|
4
|
+
export declare function makeOffer(apiOrigin: string, now?: number): {
|
|
5
|
+
pair: Pair;
|
|
6
|
+
offer: {
|
|
7
|
+
version: number;
|
|
8
|
+
apiOrigin: string;
|
|
9
|
+
pairId: string;
|
|
10
|
+
enrollmentToken: string | undefined;
|
|
11
|
+
rootKey: string;
|
|
12
|
+
expiresAt: number | undefined;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export declare function verifyClaim(pair: Pick<Pair, 'pairId' | 'rootKey' | 'expiresAt'>, pending: unknown, now?: number): Promise<{
|
|
16
|
+
claimId: string;
|
|
17
|
+
phoneTokenHash: string;
|
|
18
|
+
}>;
|
|
19
|
+
export declare function activatePending(config: Config, dir: string, pair: Pair, cloud: CloudClient, daemonServerId: string, hostName?: string): Promise<boolean>;
|
package/dist/pairing.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { randomUUID, randomBytes, createHash } from 'node:crypto';
|
|
2
|
+
import { hostname } from 'node:os';
|
|
3
|
+
import { decode, openEnvelope, sealEnvelope } from './crypto.js';
|
|
4
|
+
import { exact, object, UUID, LIMITS } from './limits.js';
|
|
5
|
+
import { saveConfig } from './config.js';
|
|
6
|
+
export function tokenHash(token) { return createHash('sha256').update(token).digest('hex'); }
|
|
7
|
+
export function makeOffer(apiOrigin, now = Date.now()) {
|
|
8
|
+
const pair = { pairId: randomUUID(), apiOrigin, rootKey: randomBytes(32).toString('base64url'), hostToken: randomBytes(32).toString('base64url'), enrollmentToken: randomBytes(32).toString('base64url'), expiresAt: now + LIMITS.enrollmentMs, active: false };
|
|
9
|
+
return { pair, offer: { version: 1, apiOrigin, pairId: pair.pairId, enrollmentToken: pair.enrollmentToken, rootKey: pair.rootKey, expiresAt: pair.expiresAt } };
|
|
10
|
+
}
|
|
11
|
+
export async function verifyClaim(pair, pending, now) {
|
|
12
|
+
const clock = now === undefined ? Date.now : () => now;
|
|
13
|
+
now = now ?? clock();
|
|
14
|
+
if (!Number.isSafeInteger(pair.expiresAt) || now >= pair.expiresAt)
|
|
15
|
+
throw Error('PAIRING_EXPIRED');
|
|
16
|
+
const p = object(pending);
|
|
17
|
+
if (typeof p.claimId !== 'string' || !UUID.test(p.claimId) || typeof p.phoneTokenHash !== 'string' || !/^([a-f0-9]{64})$/.test(p.phoneTokenHash))
|
|
18
|
+
throw Error('INVALID_CLAIM');
|
|
19
|
+
const envelope = object(p.proof);
|
|
20
|
+
if (envelope.pairId !== pair.pairId || envelope.id !== p.claimId || envelope.channel !== 'enrollment' || Number(envelope.issuedAt) < pair.expiresAt - LIMITS.enrollmentMs || Number(envelope.expiresAt) > pair.expiresAt)
|
|
21
|
+
throw Error('CLAIM_CONTEXT_MISMATCH');
|
|
22
|
+
const body = object(await openEnvelope(decode(pair.rootKey, 32), p.proof, now));
|
|
23
|
+
exact(body, ['type', 'claimId', 'phoneTokenHash']);
|
|
24
|
+
if (body.type !== 'phoneHello' || body.claimId !== p.claimId || body.phoneTokenHash !== p.phoneTokenHash)
|
|
25
|
+
throw Error('CLAIM_PROOF_MISMATCH');
|
|
26
|
+
if (clock() >= pair.expiresAt)
|
|
27
|
+
throw Error('PAIRING_EXPIRED');
|
|
28
|
+
return { claimId: p.claimId, phoneTokenHash: p.phoneTokenHash };
|
|
29
|
+
}
|
|
30
|
+
export async function activatePending(config, dir, pair, cloud, daemonServerId, hostName = hostname()) {
|
|
31
|
+
if (pair.revokedAt !== undefined || pair.relayRetiredAt !== undefined)
|
|
32
|
+
return false;
|
|
33
|
+
if (pair.active)
|
|
34
|
+
return true;
|
|
35
|
+
const result = await cloud.pair();
|
|
36
|
+
const state = object(result.pair);
|
|
37
|
+
if (state.state === 'revoked' || state.state === 'expired') {
|
|
38
|
+
pair.active = false;
|
|
39
|
+
pair.relayRetiredAt ??= Date.now();
|
|
40
|
+
await saveConfig(dir, config);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (!Number.isSafeInteger(pair.expiresAt) || Date.now() >= pair.expiresAt)
|
|
44
|
+
throw Error('PAIRING_EXPIRED');
|
|
45
|
+
if (!state.pendingClaim)
|
|
46
|
+
return false;
|
|
47
|
+
const accepted = await verifyClaim(pair, state.pendingClaim);
|
|
48
|
+
if (pair.claimId && (pair.claimId !== accepted.claimId || pair.phoneTokenHash !== accepted.phoneTokenHash))
|
|
49
|
+
throw Error('CLAIM_REPLACEMENT_REFUSED');
|
|
50
|
+
pair.claimId = accepted.claimId;
|
|
51
|
+
pair.phoneTokenHash = accepted.phoneTokenHash;
|
|
52
|
+
pair.daemonServerId = daemonServerId;
|
|
53
|
+
if (!pair.activationProof) {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
pair.activationProof = await sealEnvelope(decode(pair.rootKey, 32), { version: 1, pairId: pair.pairId, id: accepted.claimId, channel: 'activation', issuedAt: now, expiresAt: Math.min(pair.expiresAt || now + LIMITS.enrollmentMs, now + LIMITS.enrollmentMs) }, { type: 'companionHello', ...accepted, daemonServerId, hostName: hostName.slice(0, 256) });
|
|
56
|
+
}
|
|
57
|
+
// Bind accepted phone durably before activation; execute only after exact cloud confirmation.
|
|
58
|
+
await saveConfig(dir, config);
|
|
59
|
+
if (Date.now() >= pair.expiresAt)
|
|
60
|
+
throw Error('PAIRING_EXPIRED');
|
|
61
|
+
await cloud.activate({ claimId: accepted.claimId, proof: pair.activationProof });
|
|
62
|
+
if (Date.now() >= pair.expiresAt)
|
|
63
|
+
throw Error('PAIRING_EXPIRED');
|
|
64
|
+
pair.active = true;
|
|
65
|
+
delete pair.enrollmentToken;
|
|
66
|
+
await saveConfig(dir, config);
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AgentPermissionResponse } from '@getpaseo/protocol/agent-types';
|
|
2
|
+
interface RequestInput {
|
|
3
|
+
kind?: string;
|
|
4
|
+
input?: unknown;
|
|
5
|
+
suggestions?: unknown[];
|
|
6
|
+
}
|
|
7
|
+
/** Matches PermissionCardRequest.response: copy input, edit original strings, add exact question-header answers. */
|
|
8
|
+
export declare function validatePermissionInput(request: RequestInput, response: AgentPermissionResponse): void;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { object, text } from './limits.js';
|
|
3
|
+
function validSelectedLabels(answer, labels, multi) { if (!multi)
|
|
4
|
+
return labels.includes(answer); let positions = new Set([0]); for (const label of labels) {
|
|
5
|
+
const next = new Set(positions);
|
|
6
|
+
for (const pos of positions) {
|
|
7
|
+
const prefix = pos === 0 ? label : ', ' + label;
|
|
8
|
+
if (answer.startsWith(prefix, pos))
|
|
9
|
+
next.add(pos + prefix.length);
|
|
10
|
+
}
|
|
11
|
+
positions = next;
|
|
12
|
+
} return answer.length > 0 && positions.has(answer.length); }
|
|
13
|
+
/** Matches PermissionCardRequest.response: copy input, edit original strings, add exact question-header answers. */
|
|
14
|
+
export function validatePermissionInput(request, response) {
|
|
15
|
+
if (response.behavior !== 'allow')
|
|
16
|
+
return;
|
|
17
|
+
if (response.updatedPermissions?.some((grant, index, all) => !request.suggestions?.some(offered => isDeepStrictEqual(grant, offered)) || all.slice(0, index).some(other => isDeepStrictEqual(grant, other))))
|
|
18
|
+
throw Error('UNSUPPORTED_PERMISSION_UPDATE');
|
|
19
|
+
const input = request.input === undefined ? {} : object(request.input);
|
|
20
|
+
let questions = [];
|
|
21
|
+
if (request.kind === 'question') {
|
|
22
|
+
if (!Array.isArray(input.questions) || !input.questions.length)
|
|
23
|
+
throw Error('UNSUPPORTED_QUESTION_FORM');
|
|
24
|
+
if (input.questions.length > 20)
|
|
25
|
+
throw Error('QUESTION_LIMIT');
|
|
26
|
+
questions = input.questions.map(q => object(q));
|
|
27
|
+
}
|
|
28
|
+
const headers = new Set();
|
|
29
|
+
for (const q of questions) {
|
|
30
|
+
text(q.header);
|
|
31
|
+
text(q.question, 8192);
|
|
32
|
+
if (headers.has(q.header) || !Array.isArray(q.options) || q.options.length > 32)
|
|
33
|
+
throw Error('INVALID_QUESTION');
|
|
34
|
+
headers.add(q.header);
|
|
35
|
+
}
|
|
36
|
+
const updated = response.updatedInput === undefined ? input : object(response.updatedInput);
|
|
37
|
+
for (const [key, value] of Object.entries(input)) {
|
|
38
|
+
if (typeof value === 'string') {
|
|
39
|
+
if (typeof updated[key] !== 'string')
|
|
40
|
+
throw Error('INPUT_SHAPE_CHANGED');
|
|
41
|
+
}
|
|
42
|
+
else if (!isDeepStrictEqual(value, updated[key]))
|
|
43
|
+
throw Error('INPUT_SHAPE_CHANGED');
|
|
44
|
+
}
|
|
45
|
+
for (const key of Object.keys(updated))
|
|
46
|
+
if (!Object.hasOwn(input, key) && !(questions.length && key === 'answers'))
|
|
47
|
+
throw Error('INPUT_SCOPE_CHANGED');
|
|
48
|
+
if (questions.length) {
|
|
49
|
+
const answers = object(updated.answers);
|
|
50
|
+
if (Object.keys(answers).length !== questions.length || Object.keys(answers).some(k => !headers.has(k)))
|
|
51
|
+
throw Error('INCOMPLETE_ANSWERS');
|
|
52
|
+
for (const q of questions) {
|
|
53
|
+
const answer = answers[q.header];
|
|
54
|
+
if (typeof answer !== 'string' || Buffer.byteLength(answer) > 8192)
|
|
55
|
+
throw Error('INVALID_ANSWER');
|
|
56
|
+
const options = q.options;
|
|
57
|
+
const labels = options.map(o => text(object(o).label, 4096));
|
|
58
|
+
const allowOther = options.length === 0 || q.allowOther === true || q.isOther === true;
|
|
59
|
+
const allowEmpty = q.allowEmpty === true && allowOther;
|
|
60
|
+
if (!allowEmpty && !answer.trim())
|
|
61
|
+
throw Error('INCOMPLETE_ANSWERS');
|
|
62
|
+
if (!allowOther && !validSelectedLabels(answer, labels, q.multiSelect === true))
|
|
63
|
+
throw Error('INVALID_ANSWER_CHOICE');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { OfficialClient } from './official-client.js';
|
|
2
|
+
import type { Pair } from './config.js';
|
|
3
|
+
import { type OperationJournal } from './journal.js';
|
|
4
|
+
export declare function projectSnapshot(source: Awaited<ReturnType<OfficialClient['snapshot']>>, pair: Pair, journal: OperationJournal, generation: string): Promise<{
|
|
5
|
+
tasks: {
|
|
6
|
+
pendingRequests: {
|
|
7
|
+
requestId: string;
|
|
8
|
+
requestBinding: string;
|
|
9
|
+
request: import("@getpaseo/protocol/agent-types").AgentPermissionRequest;
|
|
10
|
+
}[];
|
|
11
|
+
agentId: string;
|
|
12
|
+
title: string | null;
|
|
13
|
+
status: "error" | "initializing" | "idle" | "running" | "closed";
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
requiresAttention: boolean;
|
|
16
|
+
attentionReason: "permission" | "finished" | "error" | null;
|
|
17
|
+
}[];
|
|
18
|
+
tasksTruncated: boolean;
|
|
19
|
+
version: number;
|
|
20
|
+
daemonServerId: string;
|
|
21
|
+
hostName: string;
|
|
22
|
+
measuredAt: number;
|
|
23
|
+
usage: unknown;
|
|
24
|
+
}>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { canonical, digest } from './journal.js';
|
|
2
|
+
import { occurrenceBinding } from './bindings.js';
|
|
3
|
+
export async function projectSnapshot(source, pair, journal, generation) {
|
|
4
|
+
const tasks = [];
|
|
5
|
+
let tasksTruncated = false;
|
|
6
|
+
for (const task of source.tasks) {
|
|
7
|
+
if (Buffer.byteLength(task.agentId) > 256) {
|
|
8
|
+
tasksTruncated = true;
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
const pendingRequests = [];
|
|
12
|
+
for (const pending of task.pendingRequests) {
|
|
13
|
+
if (Buffer.byteLength(pending.requestId) > 256 || Buffer.byteLength(JSON.stringify(pending.request)) > 8192) {
|
|
14
|
+
tasksTruncated = true;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
const occurrence = journal.currentOccurrence(source.daemonServerId, task.agentId, pending.requestId);
|
|
18
|
+
if (!occurrence || occurrence.generation !== generation || occurrence.fingerprint !== digest(canonical(pending.request)))
|
|
19
|
+
continue;
|
|
20
|
+
const requestBinding = await occurrenceBinding(pair, occurrence);
|
|
21
|
+
if (!journal.isCurrent(occurrence))
|
|
22
|
+
continue;
|
|
23
|
+
pendingRequests.push({ requestId: pending.requestId, requestBinding, request: pending.request });
|
|
24
|
+
}
|
|
25
|
+
tasks.push({ ...task, pendingRequests });
|
|
26
|
+
}
|
|
27
|
+
const result = { ...source, tasks, tasksTruncated };
|
|
28
|
+
while (Buffer.byteLength(JSON.stringify(result)) > 60000 && result.tasks.length) {
|
|
29
|
+
result.tasks.pop();
|
|
30
|
+
result.tasksTruncated = true;
|
|
31
|
+
}
|
|
32
|
+
if (Buffer.byteLength(JSON.stringify(result)) > 60000)
|
|
33
|
+
throw Error('SNAPSHOT_LIMIT');
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { type Stats } from 'node:fs';
|
|
2
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
3
|
+
export declare const JOURNAL_APPLICATION_ID = 1297236785;
|
|
4
|
+
export declare const RUNTIME_OWNER = "MoRemote CLI runtime";
|
|
5
|
+
export interface ResourceIdentity {
|
|
6
|
+
dev: number;
|
|
7
|
+
ino: number;
|
|
8
|
+
}
|
|
9
|
+
export interface RuntimeOwner {
|
|
10
|
+
owner: typeof RUNTIME_OWNER;
|
|
11
|
+
version: 1;
|
|
12
|
+
instanceId: string;
|
|
13
|
+
pid: number;
|
|
14
|
+
startedAt: number;
|
|
15
|
+
token: string;
|
|
16
|
+
}
|
|
17
|
+
export interface RuntimeLock extends RuntimeOwner {
|
|
18
|
+
staging?: ResourceIdentity;
|
|
19
|
+
}
|
|
20
|
+
export interface RuntimeRecord extends RuntimeOwner {
|
|
21
|
+
socket: ResourceIdentity | null;
|
|
22
|
+
}
|
|
23
|
+
export interface RuntimeResources {
|
|
24
|
+
lock: {
|
|
25
|
+
value: RuntimeLock;
|
|
26
|
+
identity: ResourceIdentity;
|
|
27
|
+
} | null;
|
|
28
|
+
record: {
|
|
29
|
+
value: RuntimeRecord;
|
|
30
|
+
identity: ResourceIdentity;
|
|
31
|
+
} | null;
|
|
32
|
+
socket: ResourceIdentity | null;
|
|
33
|
+
staged: {
|
|
34
|
+
path: string;
|
|
35
|
+
identity: ResourceIdentity;
|
|
36
|
+
} | null;
|
|
37
|
+
}
|
|
38
|
+
export declare function hasCode(error: unknown, code: string): boolean;
|
|
39
|
+
export declare function sameResource(left: ResourceIdentity, right: ResourceIdentity): boolean;
|
|
40
|
+
export declare function inspectResource(path: string, kind: 'file' | 'directory' | 'socket', optional?: boolean, privateMode?: boolean): Stats | null;
|
|
41
|
+
export declare function readPrivateText(path: string, limit: number, encoding?: 'utf8' | 'utf16le'): {
|
|
42
|
+
text: string;
|
|
43
|
+
identity: {
|
|
44
|
+
dev: number;
|
|
45
|
+
ino: number;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export declare function assertStateRoot(dir: string): void;
|
|
49
|
+
export declare function readRuntimeOwner(path: string): {
|
|
50
|
+
value: RuntimeLock;
|
|
51
|
+
identity: ResourceIdentity;
|
|
52
|
+
};
|
|
53
|
+
export declare function readRuntimeRecord(path: string): {
|
|
54
|
+
value: {
|
|
55
|
+
socket: ResourceIdentity | null;
|
|
56
|
+
owner: typeof RUNTIME_OWNER;
|
|
57
|
+
version: 1;
|
|
58
|
+
instanceId: string;
|
|
59
|
+
pid: number;
|
|
60
|
+
startedAt: number;
|
|
61
|
+
token: string;
|
|
62
|
+
};
|
|
63
|
+
identity: {
|
|
64
|
+
dev: number;
|
|
65
|
+
ino: number;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export declare function runtimeMatches(lock: RuntimeOwner, record: RuntimeOwner): boolean;
|
|
69
|
+
export declare function inspectRuntime(dir: string): RuntimeResources;
|
|
70
|
+
export declare function validateJournalPath(path: string): ResourceIdentity | null;
|
|
71
|
+
export declare function removeOwnedResource(path: string, identity: ResourceIdentity, kind?: 'file' | 'socket', privateMode?: boolean): boolean;
|
|
72
|
+
export declare function createPrivateFile(path: string, text: string): ResourceIdentity;
|
|
73
|
+
export declare function openOwnedJournal(path: string): DatabaseSync;
|
|
74
|
+
export declare function writePrivateAtomic(path: string, value: unknown): ResourceIdentity;
|
|
75
|
+
export declare function assertTaskResource(dir: string, platform?: NodeJS.Platform): void;
|
|
76
|
+
export declare function assertStateResource(dir: string, name: string): void;
|