@peer-agent/protocol 0.1.0-beta.4 → 0.1.0-rc.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/dist/index.d.ts CHANGED
@@ -6,6 +6,8 @@ export type { DesktopPreviewRequest, DesktopPreviewReply } from './desktop-previ
6
6
  export type { UiDeliveryIdentity, UiDeliveryRequirement, UiDeliveryObservation, UiDeliveryJudgment, UiDeliveryGap } from './ui-delivery-verification.ts';
7
7
  export { REMOTE_PROTOCOL_VERSION, parseRemoteReadRequest, admitRemoteRead } from './remote-access.ts';
8
8
  export type { RemoteReadRequest, RemoteReadContext, RemoteReadRejection, RemoteReadAdmission } from './remote-access.ts';
9
+ export { REMOTE_PROJECT_LIMITS, parseRemoteProjectRequest, admitRemoteProject, parseRemoteDelegationProjection } from './remote-access.ts';
10
+ export type { RemoteProjectGrant, RemoteProjectRequest, RemoteProjectContext, RemoteProjectRejection, RemoteProjectAdmission, RemoteDelegationProjection } from './remote-access.ts';
9
11
  export type LocalAccessLevel = 'ask_before_local' | 'session_local' | 'restricted_local' | 'full_local';
10
12
  export type { ConversationLifetimeUsage, ProviderRequestUsage, RuntimeTurnUsage, UsageAmounts, UsageScope, } from './usage-accounting.ts';
11
13
  export type CapabilityRiskLevel = 'L0_inert' | 'L1_local_read' | 'L2_local_write' | 'L3_external_write' | 'L4_privileged' | 'L5_destructive';
@@ -867,3 +869,4 @@ export * from './delegation.ts';
867
869
  export * from './model-routing.ts';
868
870
  export * from './project.ts';
869
871
  export * from './objectives.ts';
872
+ export * from './remote-access-settings.ts';
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ export { buildModelMenuGroups, modelMenuChannelName } from "./model-menu.js";
2
2
  export { evaluateUiDelivery } from "./ui-delivery-verification.js";
3
3
  export { DESKTOP_PREVIEW_PROTOCOL, DESKTOP_PREVIEW_CAPABILITY, DESKTOP_PREVIEW_TOOL } from "./desktop-preview.js";
4
4
  export { REMOTE_PROTOCOL_VERSION, parseRemoteReadRequest, admitRemoteRead } from "./remote-access.js";
5
+ export { REMOTE_PROJECT_LIMITS, parseRemoteProjectRequest, admitRemoteProject, parseRemoteDelegationProjection } from "./remote-access.js";
5
6
  export function resolveLlmModelOptionValues(definitions, values) {
6
7
  if (!definitions?.length)
7
8
  return {};
@@ -35,3 +36,4 @@ export * from "./delegation.js";
35
36
  export * from "./model-routing.js";
36
37
  export * from "./project.js";
37
38
  export * from "./objectives.js";
39
+ export * from "./remote-access-settings.js";
package/dist/project.d.ts CHANGED
@@ -4,6 +4,116 @@
4
4
  */
5
5
  import type { ProjectModelPolicy } from './model-routing.ts';
6
6
  import type { PendingApprovalState, WorkSessionStatus } from './delegation.ts';
7
+ export interface DiagnosticText {
8
+ readonly length: number;
9
+ readonly sha256: string;
10
+ }
11
+ export interface ProjectAgentDiagnosticTiming {
12
+ readonly startedAt: string;
13
+ readonly finishedAt: string;
14
+ readonly durationMs: number;
15
+ readonly outcome: 'done' | 'error' | 'preempted';
16
+ }
17
+ export interface ProjectDiagnostics {
18
+ readonly schemaVersion: 1;
19
+ readonly generatedAt: string | null;
20
+ readonly errors: readonly string[];
21
+ readonly scheduler: {
22
+ readonly stats: {
23
+ readonly active: number | null;
24
+ readonly waiting: number | null;
25
+ readonly limit: number | null;
26
+ };
27
+ readonly queue: readonly {
28
+ readonly workspace: DiagnosticText | null;
29
+ readonly plan: DiagnosticText | null;
30
+ readonly priority: string;
31
+ readonly enqueuedAt: string | null;
32
+ }[];
33
+ readonly projects: readonly {
34
+ readonly workspace: DiagnosticText | null;
35
+ readonly slots: {
36
+ readonly read: number | null;
37
+ readonly write: number | null;
38
+ readonly isolated: number | null;
39
+ };
40
+ readonly queue: readonly {
41
+ readonly session: DiagnosticText | null;
42
+ readonly priority: string;
43
+ readonly enqueuedAt: string | null;
44
+ readonly reason: string | null;
45
+ }[];
46
+ }[];
47
+ } | null;
48
+ readonly bots: readonly {
49
+ readonly identity: DiagnosticText | null;
50
+ readonly workspace: '.';
51
+ readonly errors: readonly string[];
52
+ readonly lease: {
53
+ readonly holder: DiagnosticText | null;
54
+ readonly surface: string;
55
+ readonly pid: number | null;
56
+ readonly acquiredAt: string | null;
57
+ readonly heartbeatAt: string | null;
58
+ } | null;
59
+ readonly input: {
60
+ readonly depth: number | null;
61
+ readonly executionDepth: number | null;
62
+ readonly cursor: DiagnosticText | null;
63
+ readonly executedCursor: DiagnosticText | null;
64
+ } | null;
65
+ readonly inbox: {
66
+ readonly cursor: number | null;
67
+ readonly events: readonly {
68
+ readonly identity: DiagnosticText | null;
69
+ readonly seq: number | null;
70
+ readonly at: string | null;
71
+ readonly kind: string;
72
+ }[];
73
+ } | null;
74
+ readonly approvals: readonly {
75
+ readonly identity: DiagnosticText | null;
76
+ readonly state: 'open' | 'stale';
77
+ readonly capability: DiagnosticText | null;
78
+ readonly summary: DiagnosticText | null;
79
+ readonly at: string | null;
80
+ }[];
81
+ readonly objectives: readonly {
82
+ readonly identity: DiagnosticText | null;
83
+ readonly status: string;
84
+ readonly autonomy: string;
85
+ readonly watches: readonly {
86
+ readonly identity: DiagnosticText | null;
87
+ readonly kind: string;
88
+ readonly source: string;
89
+ readonly paths: readonly string[];
90
+ readonly lastObservationAt: string | null;
91
+ readonly nextRunAt: string | null;
92
+ readonly pending: boolean;
93
+ readonly unavailable: boolean;
94
+ }[];
95
+ }[];
96
+ readonly turns: readonly {
97
+ readonly identity: DiagnosticText | null;
98
+ readonly startedAt: string | null;
99
+ readonly finishedAt: string | null;
100
+ readonly durationMs: number | null;
101
+ readonly outcome: string;
102
+ }[];
103
+ }[];
104
+ }
105
+ export type ProjectDiagnosticsRequest = {
106
+ readonly action: 'read' | 'export';
107
+ };
108
+ export type ProjectDiagnosticsResult = {
109
+ readonly ok: true;
110
+ readonly report: ProjectDiagnostics;
111
+ readonly saved?: boolean;
112
+ readonly cancelled?: boolean;
113
+ } | {
114
+ readonly ok: false;
115
+ readonly code: 'INVALID_INPUT' | 'DIAGNOSTICS_UNAVAILABLE' | 'DIAGNOSTICS_SAVE_FAILED';
116
+ };
7
117
  export interface ProjectRegistryEntry {
8
118
  readonly workspaceId: string;
9
119
  readonly path: string;
@@ -12,6 +122,17 @@ export interface ProjectRegistryEntry {
12
122
  readonly previousPaths: readonly string[];
13
123
  readonly remoteAlias?: string;
14
124
  }
125
+ export interface ProjectHostTakeoverRequest {
126
+ readonly workspaceId: string;
127
+ }
128
+ export type ProjectHostTakeoverResult = {
129
+ readonly ok: true;
130
+ readonly requested: boolean;
131
+ readonly alreadyHost?: boolean;
132
+ } | {
133
+ readonly ok: false;
134
+ readonly code: 'PROJECT_AGENT_DISABLED' | 'NOT_FOUND' | 'HOST_UNAVAILABLE' | 'INVALID_INPUT';
135
+ };
15
136
  export type BotAvatar = {
16
137
  readonly kind: 'generated';
17
138
  readonly shape: string;
@@ -0,0 +1,21 @@
1
+ import type { RemoteProjectGrant } from './remote-access.ts';
2
+ /** Local settings contract. Callers cannot supply a delegation version in a patch. */
3
+ export interface RemoteAccessSettings {
4
+ enabled: boolean;
5
+ gatewayOrigin: string;
6
+ workspaceId: string;
7
+ projectGrants: readonly RemoteProjectGrant[];
8
+ workspaceIds: readonly string[];
9
+ delegationVersion: number;
10
+ }
11
+ export interface RemoteAccessPatch {
12
+ enabled?: boolean;
13
+ gatewayOrigin?: string;
14
+ workspaceId?: string;
15
+ projectGrants?: readonly RemoteProjectGrant[];
16
+ }
17
+ export interface RemoteAccessSummary {
18
+ at: number;
19
+ operation: 'project.list' | 'project.conversation.read' | 'project.session.read' | 'project.input.submit';
20
+ workspaceId?: string;
21
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -50,3 +50,87 @@ export type RemoteReadAdmission = {
50
50
  export declare function parseRemoteReadRequest(value: unknown): RemoteReadAdmission;
51
51
  /** New submissions only. Duplicate receipt lookup belongs to the persistent host ledger. */
52
52
  export declare function admitRemoteRead(value: unknown, context: RemoteReadContext): RemoteReadAdmission;
53
+ export declare const REMOTE_PROJECT_LIMITS: Readonly<{
54
+ projects: 200;
55
+ messages: 50;
56
+ text: 4000;
57
+ requestBytes: number;
58
+ delegationBytes: number;
59
+ resultBytes: number;
60
+ }>;
61
+ export interface RemoteProjectGrant {
62
+ readonly workspaceId: string;
63
+ readonly allowProjectRead: boolean;
64
+ readonly allowProjectMessage: boolean;
65
+ }
66
+ interface RemoteProjectRequestBase {
67
+ readonly protocolVersion: typeof REMOTE_PROTOCOL_VERSION;
68
+ readonly type: 'project.submit';
69
+ readonly requestId: string;
70
+ readonly ownerId: string;
71
+ readonly deviceId: string;
72
+ readonly bindingVersion: number;
73
+ readonly connectionEpoch: number;
74
+ readonly delegationVersion: number;
75
+ readonly expiresAt: number;
76
+ }
77
+ export type RemoteProjectRequest = RemoteProjectRequestBase & ({
78
+ readonly operation: 'project.list';
79
+ } | {
80
+ readonly operation: 'project.conversation.read';
81
+ readonly workspaceId: string;
82
+ readonly limit: number;
83
+ readonly before: string | null;
84
+ } | {
85
+ readonly operation: 'project.session.read';
86
+ readonly workspaceId: string;
87
+ readonly sessionId: string;
88
+ } | {
89
+ readonly operation: 'project.input.submit';
90
+ readonly workspaceId: string;
91
+ readonly text: string;
92
+ });
93
+ export interface RemoteProjectContext extends Omit<RemoteReadContext, 'delegation' | 'task'> {
94
+ readonly delegation: {
95
+ readonly version: number;
96
+ readonly expiresAt: number;
97
+ readonly revoked: boolean;
98
+ readonly workspaceIds: readonly string[];
99
+ readonly allowProjectRead: boolean;
100
+ readonly allowProjectMessage: boolean;
101
+ readonly projectGrants: readonly RemoteProjectGrant[];
102
+ };
103
+ readonly project: {
104
+ readonly workspaceId: string;
105
+ readonly status: string;
106
+ } | null;
107
+ readonly session: {
108
+ readonly workspaceId: string;
109
+ readonly sessionId: string;
110
+ } | null;
111
+ }
112
+ export type RemoteProjectRejection = RemoteReadRejection | 'PROJECT_DENIED' | 'MESSAGE_DENIED' | 'INPUT_TOO_LONG';
113
+ export type RemoteProjectAdmission = {
114
+ readonly ok: true;
115
+ readonly request: RemoteProjectRequest;
116
+ } | {
117
+ readonly ok: false;
118
+ readonly code: RemoteProjectRejection;
119
+ };
120
+ /** Each operation has one exact shape; input acceptance is never a tool grant. */
121
+ export declare function parseRemoteProjectRequest(value: unknown): RemoteProjectAdmission;
122
+ /** The local context remains authoritative, including each bot's capability bits. */
123
+ export declare function admitRemoteProject(value: unknown, context: RemoteProjectContext): RemoteProjectAdmission;
124
+ export interface RemoteDelegationProjection {
125
+ version: number;
126
+ workspaceIds: string[];
127
+ allowTaskRead: boolean;
128
+ allowResultExport: boolean;
129
+ expiresAt: number;
130
+ allowProjectRead?: boolean;
131
+ allowProjectMessage?: boolean;
132
+ projectGrants?: RemoteProjectGrant[];
133
+ }
134
+ /** Shared transport shape only; this projection is never device-side authority. */
135
+ export declare function parseRemoteDelegationProjection(value: unknown, now?: number): RemoteDelegationProjection | null;
136
+ export {};
@@ -56,3 +56,130 @@ export function admitRemoteRead(value, context) {
56
56
  return deny('REQUEST_EXPIRED');
57
57
  return parsed;
58
58
  }
59
+ export const REMOTE_PROJECT_LIMITS = Object.freeze({
60
+ projects: 200, messages: 50, text: 4000,
61
+ requestBytes: 32 * 1024, delegationBytes: 128 * 1024, resultBytes: 2 * 1024 * 1024,
62
+ });
63
+ const projectBaseFields = [
64
+ 'protocolVersion', 'type', 'requestId', 'ownerId', 'deviceId',
65
+ 'bindingVersion', 'connectionEpoch', 'delegationVersion', 'expiresAt', 'operation',
66
+ ];
67
+ const projectFields = {
68
+ 'project.list': [],
69
+ 'project.conversation.read': ['workspaceId', 'limit', 'before'],
70
+ 'project.session.read': ['workspaceId', 'sessionId'],
71
+ 'project.input.submit': ['workspaceId', 'text'],
72
+ };
73
+ /** Each operation has one exact shape; input acceptance is never a tool grant. */
74
+ export function parseRemoteProjectRequest(value) {
75
+ const deny = (code) => ({ ok: false, code });
76
+ if (!value || typeof value !== 'object' || Array.isArray(value))
77
+ return deny('INVALID_REQUEST');
78
+ const r = value;
79
+ if (r.protocolVersion !== REMOTE_PROTOCOL_VERSION)
80
+ return deny('PROTOCOL_UNSUPPORTED');
81
+ const extra = typeof r.operation === 'string' && Object.hasOwn(projectFields, r.operation)
82
+ ? projectFields[r.operation] : undefined;
83
+ if (!extra || r.type !== 'project.submit')
84
+ return deny('INVALID_REQUEST');
85
+ const allowed = new Set([...projectBaseFields, ...extra]);
86
+ if (Object.keys(r).length !== allowed.size || Object.keys(r).some(key => !allowed.has(key))
87
+ || !['requestId', 'ownerId', 'deviceId'].every(key => identifier(r[key]))
88
+ || !['bindingVersion', 'connectionEpoch', 'delegationVersion', 'expiresAt'].every(key => integer(r[key]))
89
+ || (r.operation !== 'project.list' && !identifier(r.workspaceId)))
90
+ return deny('INVALID_REQUEST');
91
+ if (r.operation === 'project.conversation.read'
92
+ && (!integer(r.limit) || r.limit > REMOTE_PROJECT_LIMITS.messages
93
+ || !(r.before === null || typeof r.before === 'string' && /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,255}$/.test(r.before)))) {
94
+ return deny('INVALID_REQUEST');
95
+ }
96
+ if (r.operation === 'project.session.read' && !identifier(r.sessionId))
97
+ return deny('INVALID_REQUEST');
98
+ if (r.operation === 'project.input.submit') {
99
+ if (typeof r.text !== 'string' || !r.text.trim() || /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/.test(r.text)) {
100
+ return deny('INVALID_REQUEST');
101
+ }
102
+ if (Array.from(r.text).length > REMOTE_PROJECT_LIMITS.text)
103
+ return deny('INPUT_TOO_LONG');
104
+ }
105
+ return { ok: true, request: { ...r } };
106
+ }
107
+ /** The local context remains authoritative, including each bot's capability bits. */
108
+ export function admitRemoteProject(value, context) {
109
+ const parsed = parseRemoteProjectRequest(value);
110
+ if (!parsed.ok)
111
+ return parsed;
112
+ const r = parsed.request, d = context.delegation;
113
+ const deny = (code) => ({ ok: false, code });
114
+ if (context.bindingRevoked || r.ownerId !== context.ownerId || r.deviceId !== context.deviceId
115
+ || r.bindingVersion !== context.bindingVersion)
116
+ return deny('IDENTITY_UNBOUND');
117
+ if (!Number.isFinite(context.now) || !Number.isFinite(d.expiresAt) || d.revoked
118
+ || d.expiresAt <= context.now || r.delegationVersion !== d.version)
119
+ return deny('DELEGATION_EXPIRED');
120
+ if (!d.allowProjectRead)
121
+ return deny('PROJECT_DENIED');
122
+ if (r.operation !== 'project.list') {
123
+ if (!d.workspaceIds.includes(r.workspaceId))
124
+ return deny('WORKSPACE_DENIED');
125
+ const grant = d.projectGrants.find(item => item.workspaceId === r.workspaceId);
126
+ if (!grant?.allowProjectRead || !context.project || context.project.workspaceId !== r.workspaceId
127
+ || context.project.status !== 'active')
128
+ return deny('PROJECT_DENIED');
129
+ if (r.operation === 'project.input.submit' && (!d.allowProjectMessage || !grant.allowProjectMessage)) {
130
+ return deny('MESSAGE_DENIED');
131
+ }
132
+ if (r.operation === 'project.session.read' && (!context.session
133
+ || context.session.workspaceId !== r.workspaceId || context.session.sessionId !== r.sessionId))
134
+ return deny('TASK_DENIED');
135
+ }
136
+ if (!context.online)
137
+ return deny('DEVICE_OFFLINE');
138
+ if (r.connectionEpoch !== context.connectionEpoch)
139
+ return deny('STALE_CONNECTION');
140
+ if (r.expiresAt <= context.now || r.expiresAt - context.now > 30_000)
141
+ return deny('REQUEST_EXPIRED');
142
+ return parsed;
143
+ }
144
+ /** Shared transport shape only; this projection is never device-side authority. */
145
+ export function parseRemoteDelegationProjection(value, now = Date.now()) {
146
+ if (!Number.isSafeInteger(now) || now < 0 || !value || typeof value !== 'object' || Array.isArray(value))
147
+ return null;
148
+ const r = value;
149
+ const legacy = ['version', 'workspaceIds', 'allowTaskRead', 'allowResultExport', 'expiresAt'];
150
+ const extended = Object.hasOwn(r, 'projectGrants');
151
+ const fields = extended ? [...legacy, 'allowProjectRead', 'allowProjectMessage', 'projectGrants'] : legacy;
152
+ if (Object.keys(r).length !== fields.length || Object.keys(r).some(key => !fields.includes(key))
153
+ || !integer(r.version) || !integer(r.expiresAt) || r.expiresAt <= now
154
+ || !Array.isArray(r.workspaceIds) || !r.workspaceIds.every(identifier)
155
+ || r.workspaceIds.length > (extended ? REMOTE_PROJECT_LIMITS.projects + 32 : 32)
156
+ || !extended && r.workspaceIds.length === 0
157
+ || typeof r.allowTaskRead !== 'boolean' || typeof r.allowResultExport !== 'boolean')
158
+ return null;
159
+ const result = { version: r.version, workspaceIds: [...r.workspaceIds],
160
+ allowTaskRead: r.allowTaskRead, allowResultExport: r.allowResultExport, expiresAt: r.expiresAt };
161
+ if (extended) {
162
+ if (typeof r.allowProjectRead !== 'boolean' || typeof r.allowProjectMessage !== 'boolean'
163
+ || !Array.isArray(r.projectGrants) || r.projectGrants.length > REMOTE_PROJECT_LIMITS.projects)
164
+ return null;
165
+ const seen = new Set(), grants = [];
166
+ for (const raw of r.projectGrants) {
167
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)
168
+ || Object.keys(raw).sort().join(',') !== 'allowProjectMessage,allowProjectRead,workspaceId'
169
+ || !identifier(raw.workspaceId) || seen.has(raw.workspaceId)
170
+ || typeof raw.allowProjectRead !== 'boolean' || typeof raw.allowProjectMessage !== 'boolean'
171
+ || raw.allowProjectMessage && !raw.allowProjectRead
172
+ || raw.allowProjectRead && !result.workspaceIds.includes(raw.workspaceId))
173
+ return null;
174
+ seen.add(raw.workspaceId);
175
+ grants.push({ ...raw });
176
+ }
177
+ if (r.allowProjectRead !== grants.some(row => row.allowProjectRead)
178
+ || r.allowProjectMessage !== grants.some(row => row.allowProjectMessage))
179
+ return null;
180
+ result.allowProjectRead = r.allowProjectRead;
181
+ result.allowProjectMessage = r.allowProjectMessage;
182
+ result.projectGrants = grants;
183
+ }
184
+ return result;
185
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peer-agent/protocol",
3
- "version": "0.1.0-beta.4",
3
+ "version": "0.1.0-rc.1",
4
4
  "description": "Shared host-neutral protocol contracts for Peer Agent runtimes and clients.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -37,6 +37,7 @@
37
37
  "typescript": "^5.9.3"
38
38
  },
39
39
  "scripts": {
40
+ "test": "node --test src/*.test.ts",
40
41
  "build": "tsc -p tsconfig.build.json",
41
42
  "typecheck": "tsc -p tsconfig.json"
42
43
  }