feat(companion): add task PWA and original execution controls
This commit is contained in:
@@ -2,10 +2,13 @@ import fs from 'node:fs';
|
||||
import { randomBytes, randomUUID } from 'node:crypto';
|
||||
import { companionError, companionHash, companionName, deviceGrant, COMPANION_SCHEMA } from '../../shared/contracts/companion.mjs';
|
||||
import { assertLocalTree } from '../desktop/file-state.mjs';
|
||||
import { isId } from '../../shared/contracts/execution.mjs';
|
||||
|
||||
const key = 'mobileCompanion';
|
||||
const instant = value => new Date(value).toISOString();
|
||||
const secret = () => randomBytes(32).toString('base64url');
|
||||
const dictionary = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
const date = value => typeof value === 'string' && Number.isFinite(Date.parse(value));
|
||||
export class DeviceRegistry {
|
||||
constructor(store, identity, { clock = Date.now } = {}) {
|
||||
this.store = store; this.identity = identity; this.clock = clock; this.pairings = new Map();
|
||||
@@ -22,9 +25,20 @@ export class DeviceRegistry {
|
||||
}
|
||||
read() {
|
||||
const state = this.store.storage.meta(key);
|
||||
if (state && (state.schema !== COMPANION_SCHEMA || state.binding !== this.binding || !Number.isSafeInteger(state.revision)
|
||||
|| !state.devices || Array.isArray(state.devices) || Object.keys(state.devices).length > 128 || !Array.isArray(state.audit)
|
||||
|| !state.operations || Array.isArray(state.operations))) throw companionError('原手机登记无法核对;请保留数据并检查原Windows用户和目录。', 409);
|
||||
if (state && (state.schema !== COMPANION_SCHEMA || state.binding !== this.binding || !isId(state.gatewayId) || !Number.isSafeInteger(state.revision) || state.revision < 0
|
||||
|| !dictionary(state.devices) || Object.keys(state.devices).length > 128 || !Array.isArray(state.audit) || state.audit.length > 500
|
||||
|| !dictionary(state.operations) || Object.keys(state.operations).length > 2000
|
||||
|| Object.entries(state.devices).some(([id, device]) => !dictionary(device) || id !== device.id || !isId(id)
|
||||
|| typeof device.name !== 'string' || !device.name.trim() || device.name.length > 60 || /[\x00-\x1f\x7f]/.test(device.name)
|
||||
|| !Number.isSafeInteger(device.revision) || device.revision < 1 || !date(device.createdAt) || !date(device.expiresAt)
|
||||
|| device.revokedAt !== null && !date(device.revokedAt) || device.credentialHash !== null && !/^[a-f0-9]{64}$/.test(device.credentialHash)
|
||||
|| !Array.isArray(device.projectIds) || !device.projectIds.length || device.projectIds.length > 16 || device.projectIds.some(id => !isId(id)) || new Set(device.projectIds).size !== device.projectIds.length
|
||||
|| !dictionary(device.permissions) || Object.keys(device.permissions).length !== 3 || device.permissions.observe !== true || typeof device.permissions.approve !== 'boolean' || typeof device.permissions.stop !== 'boolean')
|
||||
|| Object.entries(state.operations).some(([id, value]) => !dictionary(value) || id !== value.deviceId + ':' + value.requestId
|
||||
|| !isId(value.deviceId) || !isId(value.requestId) || !isId(value.projectId) || !isId(value.sessionId) || !isId(value.taskId) || !isId(value.runId)
|
||||
|| !['approve', 'stop'].includes(value.action) || !['pending', 'completed', 'unknown'].includes(value.status) || !/^[a-f0-9]{64}$/.test(value.hash)))) {
|
||||
throw companionError('原手机登记无法核对;请保留数据并检查原Windows用户和目录。', 409);
|
||||
}
|
||||
return state ?? { schema: COMPANION_SCHEMA, binding: this.binding, gatewayId: randomUUID(), revision: 0, devices: {}, audit: [], operations: {} };
|
||||
}
|
||||
mutate(action) {
|
||||
|
||||
@@ -21,7 +21,7 @@ export function mobileSession(store, runtime, session, redact = String) {
|
||||
fingerprint: approvalBinding(session, value), actionable: runtime.waiters.get(value.id)?.runId === session.runId })),
|
||||
changes: session.changes.filter(value => value.taskId === session.taskId).slice(-100).map(value => pick(value, ['id', 'path', 'reason', 'status', 'sha256', 'baseSha256', 'additions', 'deletions'])),
|
||||
checks: checks.slice(-12).map(value => ({ ...pick(value, ['id', 'status', 'summary', 'requiresVerification', 'createdAt', 'finishedAt']),
|
||||
results: (value.results ?? []).map(item => pick(item, ['name', 'status', 'exitCode', 'counts', 'durationMs'])) })),
|
||||
results: (value.results ?? []).map(item => pick(item, ['name', 'status', 'exitCode', 'tests', 'durationMs'])) })),
|
||||
reports: [...store.core.artifact.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId && reportKinds.has(value.kind))
|
||||
.slice(-10).map(value => pick(value, ['id', 'kind', 'sha256', 'bytes', 'createdAt', 'taskId', 'runId'])),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user