@peer-agent/protocol 0.1.0-rc.4 → 0.1.0-rc.41
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/acceptance-close-gate.d.ts +10 -1
- package/dist/acceptance-close-gate.js +12 -0
- package/dist/agent-supervision.d.ts +81 -0
- package/dist/agent-supervision.js +1 -0
- package/dist/attention.d.ts +44 -0
- package/dist/attention.js +1 -0
- package/dist/bot-access-policy.d.ts +29 -0
- package/dist/bot-access-policy.js +1 -0
- package/dist/bot-conversation-read.d.ts +25 -0
- package/dist/bot-conversation-read.js +9 -0
- package/dist/bot-model-selection.d.ts +13 -0
- package/dist/bot-model-selection.js +1 -0
- package/dist/browser-window.d.ts +48 -0
- package/dist/browser-window.js +23 -0
- package/dist/capability-admission.d.ts +20 -0
- package/dist/capability-admission.js +1 -0
- package/dist/delegation-coordination.d.ts +81 -0
- package/dist/delegation-coordination.js +1 -0
- package/dist/delegation.d.ts +216 -7
- package/dist/delegation.js +43 -39
- package/dist/document-reference.d.ts +83 -0
- package/dist/document-reference.js +1 -0
- package/dist/evidence-inspection.d.ts +64 -0
- package/dist/evidence-inspection.js +1 -0
- package/dist/goal.d.ts +50 -2
- package/dist/index.d.ts +56 -5
- package/dist/index.js +25 -0
- package/dist/input-prediction.d.ts +86 -0
- package/dist/input-prediction.js +48 -0
- package/dist/intent-coordination.d.ts +201 -0
- package/dist/intent-coordination.js +1 -0
- package/dist/interactive-result.d.ts +363 -0
- package/dist/interactive-result.js +215 -0
- package/dist/interactive-verification.d.ts +41 -0
- package/dist/interactive-verification.js +65 -0
- package/dist/local-git.d.ts +44 -0
- package/dist/local-git.js +3 -0
- package/dist/local-image.d.ts +53 -0
- package/dist/local-image.js +5 -0
- package/dist/model-reasoning.d.ts +13 -0
- package/dist/model-reasoning.js +18 -0
- package/dist/model-routing.d.ts +37 -3
- package/dist/model-routing.js +31 -5
- package/dist/project-agent-failure.d.ts +30 -0
- package/dist/project-agent-failure.js +44 -0
- package/dist/project-input-recovery.d.ts +65 -0
- package/dist/project-input-recovery.js +5 -0
- package/dist/project-recovery.d.ts +43 -0
- package/dist/project-recovery.js +1 -0
- package/dist/project.d.ts +81 -0
- package/dist/project.js +8 -5
- package/dist/remote-access.d.ts +15 -0
- package/dist/remote-access.js +1 -1
- package/dist/reply-delivery.d.ts +28 -0
- package/dist/reply-delivery.js +1 -0
- package/dist/resource-edit-execution.d.ts +68 -0
- package/dist/resource-edit-execution.js +96 -0
- package/dist/resource-edit.d.ts +237 -0
- package/dist/resource-edit.js +134 -0
- package/dist/resource-feedback-transport.d.ts +25 -0
- package/dist/resource-feedback-transport.js +93 -0
- package/dist/resource-feedback.d.ts +98 -0
- package/dist/resource-feedback.js +151 -0
- package/dist/resource-file-replace.d.ts +36 -0
- package/dist/resource-file-replace.js +40 -0
- package/dist/resource-reference-hints.d.ts +3 -0
- package/dist/resource-reference-hints.js +58 -0
- package/dist/resource-window.d.ts +222 -0
- package/dist/resource-window.js +252 -0
- package/dist/runtime-session-facts.d.ts +58 -0
- package/dist/runtime-session-facts.js +55 -0
- package/dist/session-control.d.ts +196 -0
- package/dist/session-control.js +48 -0
- package/dist/session-facts.d.ts +29 -0
- package/dist/session-facts.js +35 -0
- package/dist/system-context.d.ts +6 -0
- package/dist/task-overview.d.ts +90 -0
- package/dist/task-overview.js +322 -17
- package/dist/tool-activity-preview.d.ts +4 -0
- package/dist/tool-activity-preview.js +72 -0
- package/dist/usage-accounting.d.ts +1 -1
- package/dist/user-anchor.d.ts +17 -0
- package/dist/user-anchor.js +1 -0
- package/dist/work-coordination.d.ts +165 -0
- package/dist/work-coordination.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const FILE_RESULTS = new Map([
|
|
2
|
+
['read_file', new Set(['local_file_ref', 'file_read_result'])],
|
|
3
|
+
['write_file', new Set(['file_write_result'])],
|
|
4
|
+
['edit_file', new Set(['file_edit_result'])],
|
|
5
|
+
]);
|
|
6
|
+
function resourceHintRecord(value) {
|
|
7
|
+
if (typeof value === 'string') {
|
|
8
|
+
if (value.length > 1_000_000)
|
|
9
|
+
return null;
|
|
10
|
+
try {
|
|
11
|
+
return resourceHintRecord(JSON.parse(value));
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : null;
|
|
18
|
+
}
|
|
19
|
+
function resourceFileResult(value, depth = 0) {
|
|
20
|
+
const result = resourceHintRecord(value);
|
|
21
|
+
if (!result || depth > 4 || result.success === false || result.ok === false
|
|
22
|
+
|| ['failed', 'error', 'blocked', 'denied', 'cancelled'].includes(String(result.status)))
|
|
23
|
+
return null;
|
|
24
|
+
if (typeof result.kind === 'string' && typeof result.path === 'string')
|
|
25
|
+
return result;
|
|
26
|
+
// Only known transport envelopes; never search assistant prose or arbitrary properties.
|
|
27
|
+
for (const key of ['output', 'outputPreview', 'fileResult', 'legacyResult']) {
|
|
28
|
+
const nested = resourceFileResult(result[key], depth + 1);
|
|
29
|
+
if (nested)
|
|
30
|
+
return nested;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
/** Normalize same-turn navigation hints. No permission, Artifact or Evidence is created. */
|
|
35
|
+
export function botResourceReferences(rounds) {
|
|
36
|
+
const paths = new Map();
|
|
37
|
+
for (const rawRound of Array.isArray(rounds) ? rounds : []) {
|
|
38
|
+
const round = resourceHintRecord(rawRound);
|
|
39
|
+
for (const rawCall of Array.isArray(round?.toolCalls) ? round.toolCalls : []) {
|
|
40
|
+
const call = resourceHintRecord(rawCall);
|
|
41
|
+
const kinds = typeof call?.name === 'string' ? FILE_RESULTS.get(call.name) : null;
|
|
42
|
+
if (!kinds || !call)
|
|
43
|
+
continue;
|
|
44
|
+
const result = resourceFileResult(call.result);
|
|
45
|
+
if (!result || !kinds.has(String(result.kind)) || result.tool !== call.name
|
|
46
|
+
|| (result.status !== undefined && result.status !== 'success'))
|
|
47
|
+
continue;
|
|
48
|
+
const path = result.path;
|
|
49
|
+
if (typeof path !== 'string' || path.length > 8192 || /[\0\r\n]/.test(path) || !/^(?:\/|[A-Za-z]:[\\/])/.test(path))
|
|
50
|
+
continue;
|
|
51
|
+
if (!paths.has(path))
|
|
52
|
+
paths.set(path, { path, tool: call.name });
|
|
53
|
+
if (paths.size >= 256)
|
|
54
|
+
return [...paths.values()];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return [...paths.values()];
|
|
58
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import type { BotDocumentReference, LocalDocumentReadResult } from './document-reference.ts';
|
|
2
|
+
import type { EvidenceDeliveryBinding } from './evidence-inspection.ts';
|
|
3
|
+
import { type InteractiveOwner, type InteractiveResultReference } from './interactive-result.ts';
|
|
4
|
+
/** Read-only presentation. None of these fields grant access or identify a browser guest/writer. */
|
|
5
|
+
export type ResourceWindowResource = ({
|
|
6
|
+
readonly kind: 'document';
|
|
7
|
+
readonly reference: BotDocumentReference;
|
|
8
|
+
readonly line?: number;
|
|
9
|
+
readonly delivery?: EvidenceDeliveryBinding;
|
|
10
|
+
} & ({
|
|
11
|
+
readonly mode: 'preview' | 'source';
|
|
12
|
+
} | {
|
|
13
|
+
readonly mode: 'diff';
|
|
14
|
+
readonly comparison: 'working' | 'staged';
|
|
15
|
+
})) | {
|
|
16
|
+
readonly kind: 'directory';
|
|
17
|
+
readonly reference: BotDocumentReference;
|
|
18
|
+
} | {
|
|
19
|
+
readonly kind: 'comparison';
|
|
20
|
+
readonly reference: BotDocumentReference;
|
|
21
|
+
readonly fromRef: string;
|
|
22
|
+
readonly toRef?: string;
|
|
23
|
+
readonly comparison?: 'working' | 'staged';
|
|
24
|
+
} | {
|
|
25
|
+
readonly kind: 'interactive';
|
|
26
|
+
readonly reference: InteractiveResultReference;
|
|
27
|
+
};
|
|
28
|
+
export interface ResourceWindowNavigation {
|
|
29
|
+
readonly top: number;
|
|
30
|
+
readonly left: number;
|
|
31
|
+
readonly selected?: string;
|
|
32
|
+
readonly expanded?: readonly string[];
|
|
33
|
+
readonly filter?: string;
|
|
34
|
+
readonly mode?: 'preview' | 'source';
|
|
35
|
+
readonly positions?: readonly {
|
|
36
|
+
readonly key: string;
|
|
37
|
+
readonly top: number;
|
|
38
|
+
readonly left: number;
|
|
39
|
+
}[];
|
|
40
|
+
}
|
|
41
|
+
export interface ResourceWindowOpenRequest {
|
|
42
|
+
readonly resource: ResourceWindowResource;
|
|
43
|
+
readonly sourceTabId?: string;
|
|
44
|
+
readonly navigation?: ResourceWindowNavigation;
|
|
45
|
+
}
|
|
46
|
+
export interface ResourceWindowOwner {
|
|
47
|
+
readonly workspaceId: string;
|
|
48
|
+
readonly conversationId: string;
|
|
49
|
+
readonly task?: BotDocumentReference['task'];
|
|
50
|
+
}
|
|
51
|
+
export interface ResourceWindowIdentity {
|
|
52
|
+
readonly viewId: string;
|
|
53
|
+
readonly windowId: string;
|
|
54
|
+
readonly generation: number;
|
|
55
|
+
}
|
|
56
|
+
export interface ResourceWindowContext extends ResourceWindowIdentity {
|
|
57
|
+
readonly owner: ResourceWindowOwner;
|
|
58
|
+
readonly resource: ResourceWindowResource;
|
|
59
|
+
/** Factual presentation title resolved by the host; never an access grant. */
|
|
60
|
+
readonly title?: string;
|
|
61
|
+
readonly sourceTabId?: string;
|
|
62
|
+
readonly navigation?: ResourceWindowNavigation;
|
|
63
|
+
readonly sourceNavigation?: ResourceWindowNavigation;
|
|
64
|
+
readonly navigationVersion?: number;
|
|
65
|
+
readonly phase: 'prepared' | 'attached' | 'ready';
|
|
66
|
+
}
|
|
67
|
+
export type ResourceWindowStatus = 'invalid_request' | 'scope_denied' | 'permission_denied' | 'not_found' | 'workspace_unavailable' | 'revision_unavailable' | 'source_unavailable' | 'source_changed' | 'storage_unavailable' | 'limit_reached' | 'stale_view' | 'not_ready' | 'cancelled' | 'disposed' | 'content_cleaned' | 'incompatible_result' | 'result_unavailable';
|
|
68
|
+
export type ResourceWindowResult = {
|
|
69
|
+
readonly ok: true;
|
|
70
|
+
readonly context: ResourceWindowContext;
|
|
71
|
+
readonly persisted?: boolean;
|
|
72
|
+
readonly checkpointStatus?: 'owner_replaced';
|
|
73
|
+
readonly returnStatus?: 'returned' | 'source_tab_closed';
|
|
74
|
+
} | {
|
|
75
|
+
readonly ok: false;
|
|
76
|
+
readonly status: ResourceWindowStatus;
|
|
77
|
+
};
|
|
78
|
+
export interface ResourceWindowReturnRequest {
|
|
79
|
+
readonly requestId: string;
|
|
80
|
+
readonly context: ResourceWindowContext;
|
|
81
|
+
readonly expiresAt: number;
|
|
82
|
+
}
|
|
83
|
+
export interface ResourceWindowReturnAck {
|
|
84
|
+
readonly requestId: string;
|
|
85
|
+
readonly identity: ResourceWindowIdentity;
|
|
86
|
+
readonly status: 'returned' | 'source_tab_closed' | 'cancelled';
|
|
87
|
+
}
|
|
88
|
+
export interface ResourceWindowCheckpointRequest {
|
|
89
|
+
readonly requestId: string;
|
|
90
|
+
readonly identity: ResourceWindowIdentity;
|
|
91
|
+
}
|
|
92
|
+
export interface ResourceWindowCheckpointAck extends ResourceWindowCheckpointRequest {
|
|
93
|
+
readonly status: 'confirmed' | 'blocked';
|
|
94
|
+
}
|
|
95
|
+
export interface ResourceWindowCloseBlocked {
|
|
96
|
+
readonly identity: ResourceWindowIdentity;
|
|
97
|
+
readonly reason: 'save_failed' | 'unconfirmed';
|
|
98
|
+
}
|
|
99
|
+
export interface ResourceWindowHistoryEntry {
|
|
100
|
+
readonly id: string;
|
|
101
|
+
readonly owner: ResourceWindowOwner;
|
|
102
|
+
readonly request: ResourceWindowOpenRequest;
|
|
103
|
+
readonly savedAt: string;
|
|
104
|
+
readonly title?: string;
|
|
105
|
+
}
|
|
106
|
+
export interface ResourceWindowHistoryResult {
|
|
107
|
+
readonly ok: boolean;
|
|
108
|
+
readonly status: 'ok' | 'scope_denied' | 'storage_unavailable';
|
|
109
|
+
readonly entries: readonly ResourceWindowHistoryEntry[];
|
|
110
|
+
}
|
|
111
|
+
export interface ResourceLayoutSnapshot {
|
|
112
|
+
readonly formatVersion: 2;
|
|
113
|
+
readonly revision: number;
|
|
114
|
+
readonly order: readonly string[];
|
|
115
|
+
readonly recent: readonly string[];
|
|
116
|
+
readonly interactive: ResourceInteractiveSession;
|
|
117
|
+
readonly updatedAt: string;
|
|
118
|
+
}
|
|
119
|
+
export interface ResourceInteractiveTab {
|
|
120
|
+
readonly id: string;
|
|
121
|
+
readonly reference: InteractiveResultReference;
|
|
122
|
+
readonly title?: string;
|
|
123
|
+
readonly navigation?: Pick<ResourceWindowNavigation, 'top' | 'left'>;
|
|
124
|
+
}
|
|
125
|
+
/** Domain presentation references only; values, raw drafts and writer truth stay in the state store. */
|
|
126
|
+
export interface ResourceInteractiveSession {
|
|
127
|
+
readonly tabs: readonly ResourceInteractiveTab[];
|
|
128
|
+
readonly activeTabId: string | null;
|
|
129
|
+
}
|
|
130
|
+
export type ResourceRecentReference = {
|
|
131
|
+
readonly kind: 'document';
|
|
132
|
+
readonly path: string;
|
|
133
|
+
readonly relativeTo?: string;
|
|
134
|
+
readonly task?: BotDocumentReference['task'];
|
|
135
|
+
readonly revision?: BotDocumentReference['revision'];
|
|
136
|
+
} | {
|
|
137
|
+
readonly kind: 'comparison';
|
|
138
|
+
readonly path: string;
|
|
139
|
+
readonly relativeTo?: string;
|
|
140
|
+
readonly task?: BotDocumentReference['task'];
|
|
141
|
+
readonly comparison: {
|
|
142
|
+
readonly fromRef: string;
|
|
143
|
+
readonly toRef?: string;
|
|
144
|
+
readonly comparison?: 'working' | 'staged';
|
|
145
|
+
};
|
|
146
|
+
} | {
|
|
147
|
+
readonly kind: 'directory';
|
|
148
|
+
readonly path: string;
|
|
149
|
+
readonly relativeTo?: string;
|
|
150
|
+
readonly task?: BotDocumentReference['task'];
|
|
151
|
+
} | {
|
|
152
|
+
readonly kind: 'browser';
|
|
153
|
+
readonly url: string;
|
|
154
|
+
} | {
|
|
155
|
+
readonly kind: 'interactive';
|
|
156
|
+
readonly reference: InteractiveResultReference;
|
|
157
|
+
readonly title?: string;
|
|
158
|
+
};
|
|
159
|
+
export interface ResourcePresentationApi {
|
|
160
|
+
readLayout(request: {
|
|
161
|
+
readonly owner: InteractiveOwner;
|
|
162
|
+
}): Promise<{
|
|
163
|
+
readonly ok: true;
|
|
164
|
+
readonly snapshot: ResourceLayoutSnapshot | null;
|
|
165
|
+
} | {
|
|
166
|
+
readonly ok: false;
|
|
167
|
+
readonly code: string;
|
|
168
|
+
}>;
|
|
169
|
+
writeLayout(request: {
|
|
170
|
+
readonly owner: InteractiveOwner;
|
|
171
|
+
readonly expectedRevision: number;
|
|
172
|
+
} & Pick<ResourceLayoutSnapshot, 'order' | 'recent' | 'interactive'>): Promise<{
|
|
173
|
+
readonly ok: true;
|
|
174
|
+
readonly snapshot: ResourceLayoutSnapshot;
|
|
175
|
+
} | {
|
|
176
|
+
readonly ok: false;
|
|
177
|
+
readonly code: string;
|
|
178
|
+
}>;
|
|
179
|
+
}
|
|
180
|
+
/** The host supplies owner, task, revision and allowed root from its lease, never from this request. */
|
|
181
|
+
export interface ResourceWindowReadRequest {
|
|
182
|
+
readonly identity: ResourceWindowIdentity;
|
|
183
|
+
readonly operation: 'content' | 'directory' | 'diff';
|
|
184
|
+
/** Directory descendants only. Omitted for the originally bound resource. */
|
|
185
|
+
readonly path?: string;
|
|
186
|
+
readonly expectedObservation?: BotDocumentReference['expectedObservation'];
|
|
187
|
+
readonly loadCurrent?: boolean;
|
|
188
|
+
}
|
|
189
|
+
export interface ResourceWindowReadResult {
|
|
190
|
+
readonly ok: boolean;
|
|
191
|
+
readonly status: string;
|
|
192
|
+
readonly document?: LocalDocumentReadResult & {
|
|
193
|
+
readonly dataUrl?: string;
|
|
194
|
+
readonly media: 'text' | 'image';
|
|
195
|
+
};
|
|
196
|
+
readonly directory?: {
|
|
197
|
+
readonly entries: readonly {
|
|
198
|
+
readonly name: string;
|
|
199
|
+
readonly isDir: boolean;
|
|
200
|
+
readonly absPath: string;
|
|
201
|
+
}[];
|
|
202
|
+
readonly truncated?: boolean;
|
|
203
|
+
};
|
|
204
|
+
readonly diff?: {
|
|
205
|
+
readonly diffText: string;
|
|
206
|
+
readonly fromRef?: string;
|
|
207
|
+
readonly toRef?: string;
|
|
208
|
+
readonly untracked?: readonly string[];
|
|
209
|
+
readonly untrackedTruncated?: boolean;
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/** Only bounded hints for this kind of view; never accept a body, permission, browser instance or draft. */
|
|
213
|
+
export declare function normalizeResourceWindowNavigation(raw: unknown, kind: ResourceWindowResource['kind']): ResourceWindowNavigation | undefined;
|
|
214
|
+
export declare function normalizeResourceWindowRequest(raw: unknown): ResourceWindowOpenRequest | undefined;
|
|
215
|
+
export declare function normalizeResourceWindowOwner(raw: unknown): ResourceWindowOwner | undefined;
|
|
216
|
+
export declare function resourceWindowWorkspaceId(resource: ResourceWindowResource): string;
|
|
217
|
+
export declare function resourceWindowTask(resource: ResourceWindowResource): BotDocumentReference['task'] | undefined;
|
|
218
|
+
export declare function interactiveReferenceKey(reference: InteractiveResultReference): string;
|
|
219
|
+
export declare function normalizeResourceInteractiveSession(raw: unknown): ResourceInteractiveSession | undefined;
|
|
220
|
+
export declare function normalizeResourceRecentReference(raw: unknown): ResourceRecentReference | undefined;
|
|
221
|
+
export declare function normalizeResourceLayout(raw: unknown): Pick<ResourceLayoutSnapshot, 'order' | 'recent' | 'interactive'> | undefined;
|
|
222
|
+
export declare function normalizeResourceLayoutSnapshot(raw: unknown): ResourceLayoutSnapshot | undefined;
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { interactiveToken, normalizeInteractiveReference } from "./interactive-result.js";
|
|
2
|
+
const object = (value) => !!value && typeof value === 'object' && !Array.isArray(value);
|
|
3
|
+
const keys = (value, allowed) => Object.keys(value).every(key => allowed.includes(key));
|
|
4
|
+
const token = (value) => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,255}$/.test(value) && !value.includes('..');
|
|
5
|
+
const text = (value, max) => typeof value === 'string' && !!value.trim() && value.length <= max && !/[\u0000-\u001f]/.test(value);
|
|
6
|
+
const point = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= 10_000_000;
|
|
7
|
+
function reference(raw) {
|
|
8
|
+
if (!object(raw) || !keys(raw, ['workspaceId', 'path', 'relativeTo', 'task', 'kind', 'expectedObservation', 'revision']) || !token(raw.workspaceId) || !text(raw.path, 8192))
|
|
9
|
+
return;
|
|
10
|
+
if (raw.kind !== undefined && raw.kind !== 'file' && raw.kind !== 'directory' || raw.relativeTo !== undefined && !text(raw.relativeTo, 8192))
|
|
11
|
+
return;
|
|
12
|
+
let task, expectedObservation, revision;
|
|
13
|
+
if (raw.task !== undefined) {
|
|
14
|
+
if (!object(raw.task) || !keys(raw.task, ['sessionId', 'planId']) || !token(raw.task.sessionId) || !token(raw.task.planId))
|
|
15
|
+
return;
|
|
16
|
+
task = { sessionId: raw.task.sessionId, planId: raw.task.planId };
|
|
17
|
+
}
|
|
18
|
+
if (raw.expectedObservation !== undefined) {
|
|
19
|
+
const e = raw.expectedObservation;
|
|
20
|
+
if (!object(e) || !keys(e, ['resourceId', 'contentVersion']) || (typeof e.resourceId !== 'string' || !/^sha256:[a-f0-9]{64}$/.test(e.resourceId))
|
|
21
|
+
|| e.contentVersion !== undefined && (typeof e.contentVersion !== 'string' || !/^sha256:[a-f0-9]{64}$/.test(e.contentVersion)))
|
|
22
|
+
return;
|
|
23
|
+
expectedObservation = { resourceId: e.resourceId, ...(typeof e.contentVersion === 'string' ? { contentVersion: e.contentVersion } : {}) };
|
|
24
|
+
}
|
|
25
|
+
if (raw.revision !== undefined) {
|
|
26
|
+
const r = raw.revision;
|
|
27
|
+
if (!object(r) || !keys(r, ['kind', 'commit', 'repositoryId']) || r.kind !== 'git' || typeof r.commit !== 'string' || !/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(r.commit)
|
|
28
|
+
|| r.repositoryId !== undefined && !text(r.repositoryId, 1024))
|
|
29
|
+
return;
|
|
30
|
+
revision = { kind: 'git', commit: r.commit, ...(typeof r.repositoryId === 'string' ? { repositoryId: r.repositoryId } : {}) };
|
|
31
|
+
}
|
|
32
|
+
return { workspaceId: raw.workspaceId, path: raw.path, ...(raw.kind ? { kind: raw.kind } : {}),
|
|
33
|
+
...(typeof raw.relativeTo === 'string' ? { relativeTo: raw.relativeTo } : {}), ...(task ? { task } : {}), ...(expectedObservation ? { expectedObservation } : {}), ...(revision ? { revision } : {}) };
|
|
34
|
+
}
|
|
35
|
+
function resource(raw) {
|
|
36
|
+
if (!object(raw))
|
|
37
|
+
return;
|
|
38
|
+
if (raw.kind === 'interactive') {
|
|
39
|
+
if (!keys(raw, ['kind', 'reference']))
|
|
40
|
+
return;
|
|
41
|
+
const fixed = normalizeInteractiveReference(raw.reference);
|
|
42
|
+
return fixed ? { kind: 'interactive', reference: fixed } : undefined;
|
|
43
|
+
}
|
|
44
|
+
const ref = reference(raw.reference);
|
|
45
|
+
if (!ref)
|
|
46
|
+
return;
|
|
47
|
+
if (raw.kind === 'directory') {
|
|
48
|
+
if (!keys(raw, ['kind', 'reference']) || ref.revision || ref.expectedObservation)
|
|
49
|
+
return;
|
|
50
|
+
return { kind: 'directory', reference: { ...ref, kind: 'directory' } };
|
|
51
|
+
}
|
|
52
|
+
if (raw.kind === 'comparison') {
|
|
53
|
+
if (!keys(raw, ['kind', 'reference', 'fromRef', 'toRef', 'comparison']) || ref.revision || ref.expectedObservation || !text(raw.fromRef, 256)
|
|
54
|
+
|| raw.toRef !== undefined && !text(raw.toRef, 256) || raw.comparison !== undefined && (typeof raw.comparison !== 'string' || !['working', 'staged'].includes(raw.comparison)))
|
|
55
|
+
return;
|
|
56
|
+
// A working/index comparison cannot silently discard a supplied immutable endpoint.
|
|
57
|
+
if (raw.comparison && (raw.fromRef !== 'HEAD' || raw.toRef !== undefined))
|
|
58
|
+
return;
|
|
59
|
+
if (!raw.comparison && (!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(raw.fromRef)
|
|
60
|
+
|| typeof raw.toRef === 'string' && !/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(raw.toRef)))
|
|
61
|
+
return;
|
|
62
|
+
return { kind: 'comparison', reference: { ...ref, kind: 'directory' }, fromRef: raw.fromRef,
|
|
63
|
+
...(typeof raw.toRef === 'string' ? { toRef: raw.toRef } : {}), ...(raw.comparison ? { comparison: raw.comparison } : {}) };
|
|
64
|
+
}
|
|
65
|
+
if (raw.kind !== 'document' || !keys(raw, ['kind', 'reference', 'mode', 'comparison', 'line', 'delivery']) || ref.kind === 'directory'
|
|
66
|
+
|| typeof raw.mode !== 'string' || !['preview', 'source', 'diff'].includes(raw.mode) || ref.revision && raw.mode === 'diff'
|
|
67
|
+
|| raw.mode === 'diff' && raw.comparison === undefined
|
|
68
|
+
|| raw.comparison !== undefined && (raw.mode !== 'diff' || typeof raw.comparison !== 'string' || !['working', 'staged'].includes(raw.comparison))
|
|
69
|
+
|| raw.line !== undefined && (!Number.isSafeInteger(raw.line) || Number(raw.line) < 1 || Number(raw.line) > 1_000_000))
|
|
70
|
+
return;
|
|
71
|
+
let delivery;
|
|
72
|
+
if (raw.delivery !== undefined) {
|
|
73
|
+
const d = raw.delivery;
|
|
74
|
+
if (ref.revision || !object(d) || !keys(d, ['evidenceRef', 'artifactRef']) || !text(d.evidenceRef, 500) || /[\s\\]/.test(d.evidenceRef) || d.evidenceRef.includes('..') || !text(d.artifactRef, 8192))
|
|
75
|
+
return;
|
|
76
|
+
delivery = { evidenceRef: d.evidenceRef, artifactRef: d.artifactRef };
|
|
77
|
+
}
|
|
78
|
+
const common = { kind: 'document', reference: { ...ref, kind: 'file' },
|
|
79
|
+
...(typeof raw.line === 'number' ? { line: raw.line } : {}), ...(delivery ? { delivery } : {}) };
|
|
80
|
+
return raw.mode === 'diff' ? { ...common, mode: 'diff', comparison: raw.comparison }
|
|
81
|
+
: { ...common, mode: raw.mode };
|
|
82
|
+
}
|
|
83
|
+
/** Only bounded hints for this kind of view; never accept a body, permission, browser instance or draft. */
|
|
84
|
+
export function normalizeResourceWindowNavigation(raw, kind) {
|
|
85
|
+
if (!object(raw) || !keys(raw, kind === 'interactive' ? ['top', 'left'] : kind === 'directory' ? ['top', 'left', 'selected', 'expanded', 'filter'] : kind === 'comparison' ? ['top', 'left', 'selected', 'positions'] : ['top', 'left', 'mode', 'positions']) || !point(raw.top) || !point(raw.left))
|
|
86
|
+
return;
|
|
87
|
+
if (raw.selected !== undefined && !text(raw.selected, 8192) || raw.filter !== undefined && (typeof raw.filter !== 'string' || raw.filter.length > 256 || /[\u0000-\u001f]/.test(raw.filter))
|
|
88
|
+
|| raw.expanded !== undefined && (!Array.isArray(raw.expanded) || raw.expanded.length > 64 || !raw.expanded.every(value => text(value, 8192))))
|
|
89
|
+
return;
|
|
90
|
+
if (raw.mode !== undefined && raw.mode !== 'preview' && raw.mode !== 'source')
|
|
91
|
+
return;
|
|
92
|
+
const relative = (value) => text(value, 4096) && (value === '.' || !/^[\\/]/.test(value) && value.split('/').length <= 16 && value.split('/').every(part => part && part !== '.' && part !== '..' && !/[\\:]/.test(part)));
|
|
93
|
+
if (kind === 'directory' && (raw.selected !== undefined && !relative(raw.selected)
|
|
94
|
+
|| Array.isArray(raw.expanded) && !raw.expanded.every(relative)))
|
|
95
|
+
return;
|
|
96
|
+
if (raw.positions !== undefined && (!Array.isArray(raw.positions) || raw.positions.length > 12 || !raw.positions.every(value => object(value) && keys(value, ['key', 'top', 'left']) && text(value.key, 8192) && point(value.top) && point(value.left)
|
|
97
|
+
&& (kind !== 'document' || ['preview', 'source', 'diff'].includes(value.key)))))
|
|
98
|
+
return;
|
|
99
|
+
if (Array.isArray(raw.positions) && new Set(raw.positions.map(value => value.key)).size !== raw.positions.length)
|
|
100
|
+
return;
|
|
101
|
+
return { top: raw.top, left: raw.left, ...(typeof raw.selected === 'string' ? { selected: raw.selected } : {}),
|
|
102
|
+
...(typeof raw.filter === 'string' ? { filter: raw.filter } : {}), ...(Array.isArray(raw.expanded) ? { expanded: [...new Set(raw.expanded)] } : {}),
|
|
103
|
+
...(raw.mode ? { mode: raw.mode } : {}), ...(Array.isArray(raw.positions) ? { positions: raw.positions.map(value => ({ key: value.key, top: value.top, left: value.left })) } : {}) };
|
|
104
|
+
}
|
|
105
|
+
export function normalizeResourceWindowRequest(raw) {
|
|
106
|
+
if (!object(raw) || !keys(raw, ['resource', 'sourceTabId', 'navigation']) || raw.sourceTabId !== undefined && !token(raw.sourceTabId))
|
|
107
|
+
return;
|
|
108
|
+
const target = resource(raw.resource);
|
|
109
|
+
if (!target)
|
|
110
|
+
return;
|
|
111
|
+
const navigation = raw.navigation === undefined ? undefined : normalizeResourceWindowNavigation(raw.navigation, target.kind);
|
|
112
|
+
if (raw.navigation !== undefined && !navigation)
|
|
113
|
+
return;
|
|
114
|
+
return { resource: target, ...(typeof raw.sourceTabId === 'string' ? { sourceTabId: raw.sourceTabId } : {}), ...(navigation ? { navigation } : {}) };
|
|
115
|
+
}
|
|
116
|
+
export function normalizeResourceWindowOwner(raw) {
|
|
117
|
+
if (!object(raw) || !keys(raw, ['workspaceId', 'conversationId', 'task']) || !token(raw.workspaceId) || !token(raw.conversationId))
|
|
118
|
+
return;
|
|
119
|
+
if (raw.task !== undefined && (!object(raw.task) || !keys(raw.task, ['sessionId', 'planId']) || !token(raw.task.sessionId) || !token(raw.task.planId)))
|
|
120
|
+
return;
|
|
121
|
+
return { workspaceId: raw.workspaceId, conversationId: raw.conversationId,
|
|
122
|
+
...(object(raw.task) ? { task: { sessionId: raw.task.sessionId, planId: raw.task.planId } } : {}) };
|
|
123
|
+
}
|
|
124
|
+
export function resourceWindowWorkspaceId(resource) {
|
|
125
|
+
return resource.kind === 'interactive' ? resource.reference.owner.workspaceId : resource.reference.workspaceId;
|
|
126
|
+
}
|
|
127
|
+
export function resourceWindowTask(resource) {
|
|
128
|
+
return resource.kind === 'interactive' ? undefined : resource.reference.task;
|
|
129
|
+
}
|
|
130
|
+
export function interactiveReferenceKey(reference) {
|
|
131
|
+
return JSON.stringify([reference.owner.workspaceId, reference.owner.conversationId, reference.resultId, reference.contentVersion]);
|
|
132
|
+
}
|
|
133
|
+
const titleHint = (value) => typeof value === 'string' && !!value.trim() && value.length <= 160 && !value.includes('\0');
|
|
134
|
+
export function normalizeResourceInteractiveSession(raw) {
|
|
135
|
+
if (!object(raw) || !keys(raw, ['tabs', 'activeTabId']) || !Array.isArray(raw.tabs) || raw.tabs.length > 32
|
|
136
|
+
|| raw.activeTabId !== null && !interactiveToken(raw.activeTabId))
|
|
137
|
+
return;
|
|
138
|
+
const tabs = [];
|
|
139
|
+
const ids = new Set(), references = new Set();
|
|
140
|
+
for (const item of raw.tabs) {
|
|
141
|
+
if (!object(item) || !keys(item, ['id', 'reference', 'title', 'navigation']) || !interactiveToken(item.id) || item.title !== undefined && !titleHint(item.title))
|
|
142
|
+
return;
|
|
143
|
+
const reference = normalizeInteractiveReference(item.reference);
|
|
144
|
+
const navigation = item.navigation === undefined ? undefined : normalizeResourceWindowNavigation(item.navigation, 'interactive');
|
|
145
|
+
if (!reference || item.navigation !== undefined && !navigation || ids.has(item.id) || references.has(interactiveReferenceKey(reference)))
|
|
146
|
+
return;
|
|
147
|
+
ids.add(item.id);
|
|
148
|
+
references.add(interactiveReferenceKey(reference));
|
|
149
|
+
tabs.push({ id: item.id, reference, ...(item.title !== undefined ? { title: item.title } : {}), ...(navigation ? { navigation } : {}) });
|
|
150
|
+
}
|
|
151
|
+
if (raw.activeTabId !== null && !ids.has(raw.activeTabId))
|
|
152
|
+
return;
|
|
153
|
+
return { tabs, activeTabId: raw.activeTabId };
|
|
154
|
+
}
|
|
155
|
+
export function normalizeResourceRecentReference(raw) {
|
|
156
|
+
if (!object(raw))
|
|
157
|
+
return;
|
|
158
|
+
if (raw.kind === 'interactive') {
|
|
159
|
+
if (!keys(raw, ['kind', 'reference', 'title']) || raw.title !== undefined && !titleHint(raw.title))
|
|
160
|
+
return;
|
|
161
|
+
const reference = normalizeInteractiveReference(raw.reference);
|
|
162
|
+
return reference ? { kind: 'interactive', reference, ...(raw.title !== undefined ? { title: raw.title } : {}) } : undefined;
|
|
163
|
+
}
|
|
164
|
+
if (raw.kind === 'browser') {
|
|
165
|
+
if (!keys(raw, ['kind', 'url']) || !text(raw.url, 8192))
|
|
166
|
+
return;
|
|
167
|
+
try {
|
|
168
|
+
const url = new URL(raw.url);
|
|
169
|
+
if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password)
|
|
170
|
+
return;
|
|
171
|
+
return { kind: 'browser', url: url.href };
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (!['document', 'comparison', 'directory'].includes(String(raw.kind)) || !keys(raw, raw.kind === 'document' ? ['kind', 'path', 'relativeTo', 'task', 'revision'] : raw.kind === 'directory' ? ['kind', 'path', 'relativeTo', 'task'] : ['kind', 'path', 'relativeTo', 'task', 'comparison']))
|
|
178
|
+
return;
|
|
179
|
+
// A recent descriptor is a hint scoped by its containing owner. It never supplies a workspace or an access grant.
|
|
180
|
+
const ref = { workspaceId: 'presentation', path: raw.path, ...(raw.relativeTo !== undefined ? { relativeTo: raw.relativeTo } : {}),
|
|
181
|
+
...(raw.task !== undefined ? { task: raw.task } : {}), ...(raw.revision !== undefined ? { revision: raw.revision } : {}) };
|
|
182
|
+
if (raw.kind === 'directory') {
|
|
183
|
+
const request = normalizeResourceWindowRequest({ resource: { kind: 'directory', reference: ref } });
|
|
184
|
+
if (request?.resource.kind !== 'directory')
|
|
185
|
+
return;
|
|
186
|
+
const fixed = request.resource.reference;
|
|
187
|
+
return { kind: 'directory', path: fixed.path, ...(fixed.relativeTo ? { relativeTo: fixed.relativeTo } : {}), ...(fixed.task ? { task: fixed.task } : {}) };
|
|
188
|
+
}
|
|
189
|
+
if (raw.kind === 'document') {
|
|
190
|
+
const request = normalizeResourceWindowRequest({ resource: { kind: 'document', reference: ref, mode: 'preview' } });
|
|
191
|
+
if (request?.resource.kind !== 'document')
|
|
192
|
+
return;
|
|
193
|
+
const fixed = request.resource.reference;
|
|
194
|
+
return { kind: 'document', path: fixed.path, ...(fixed.relativeTo ? { relativeTo: fixed.relativeTo } : {}), ...(fixed.task ? { task: fixed.task } : {}), ...(fixed.revision ? { revision: fixed.revision } : {}) };
|
|
195
|
+
}
|
|
196
|
+
if (!object(raw.comparison) || !keys(raw.comparison, ['fromRef', 'toRef', 'comparison']))
|
|
197
|
+
return;
|
|
198
|
+
const request = normalizeResourceWindowRequest({ resource: { kind: 'comparison', reference: ref, ...raw.comparison } });
|
|
199
|
+
if (request?.resource.kind !== 'comparison')
|
|
200
|
+
return;
|
|
201
|
+
const fixed = request.resource;
|
|
202
|
+
return { kind: 'comparison', path: fixed.reference.path, ...(fixed.reference.relativeTo ? { relativeTo: fixed.reference.relativeTo } : {}), ...(fixed.reference.task ? { task: fixed.reference.task } : {}),
|
|
203
|
+
comparison: { fromRef: fixed.fromRef, ...(fixed.toRef ? { toRef: fixed.toRef } : {}), ...(fixed.comparison ? { comparison: fixed.comparison } : {}) } };
|
|
204
|
+
}
|
|
205
|
+
export function normalizeResourceLayout(raw) {
|
|
206
|
+
if (!object(raw) || !keys(raw, ['order', 'recent', 'interactive']))
|
|
207
|
+
return;
|
|
208
|
+
const interactive = normalizeResourceInteractiveSession(raw.interactive);
|
|
209
|
+
if (!interactive)
|
|
210
|
+
return;
|
|
211
|
+
const list = (value, limit) => Array.isArray(value) && value.length <= limit
|
|
212
|
+
&& value.every(key => typeof key === 'string' && key.length <= 8192 && !/[\u0000-\u001f]/.test(key)
|
|
213
|
+
&& (key === 'launcher' || /^(?:document|browser|conversation|interactive|directory):.+$/.test(key) || key === 'comparison:workspace')) && new Set(value).size === value.length;
|
|
214
|
+
if (!list(raw.order, 200) || !Array.isArray(raw.recent) || raw.recent.length > 16 || new TextEncoder().encode(JSON.stringify(raw)).byteLength > 256 * 1024)
|
|
215
|
+
return;
|
|
216
|
+
const mapped = new Set(interactive.tabs.map(tab => `interactive:${tab.id}`));
|
|
217
|
+
if (raw.order.some(key => key.startsWith('interactive:') && !mapped.has(key))
|
|
218
|
+
|| interactive.activeTabId !== null && !raw.order.includes(`interactive:${interactive.activeTabId}`))
|
|
219
|
+
return;
|
|
220
|
+
const recent = [];
|
|
221
|
+
for (const item of raw.recent) {
|
|
222
|
+
if (typeof item !== 'string' || item.length > 8192)
|
|
223
|
+
return;
|
|
224
|
+
if (list([item], 1)) {
|
|
225
|
+
recent.push(item);
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
const ref = normalizeResourceRecentReference(JSON.parse(item));
|
|
230
|
+
if (!ref)
|
|
231
|
+
return;
|
|
232
|
+
const descriptor = JSON.stringify(ref);
|
|
233
|
+
if (descriptor.length > 8192)
|
|
234
|
+
return;
|
|
235
|
+
recent.push(descriptor);
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (new Set(recent).size !== recent.length)
|
|
242
|
+
return;
|
|
243
|
+
const layout = { order: [...raw.order], recent, interactive };
|
|
244
|
+
return new TextEncoder().encode(JSON.stringify(layout)).byteLength <= 256 * 1024 ? layout : undefined;
|
|
245
|
+
}
|
|
246
|
+
export function normalizeResourceLayoutSnapshot(raw) {
|
|
247
|
+
if (!object(raw) || !keys(raw, ['formatVersion', 'revision', 'order', 'recent', 'interactive', 'updatedAt']) || raw.formatVersion !== 2
|
|
248
|
+
|| !Number.isSafeInteger(raw.revision) || Number(raw.revision) < 1 || typeof raw.updatedAt !== 'string' || raw.updatedAt.length > 40 || !Number.isFinite(Date.parse(raw.updatedAt)))
|
|
249
|
+
return;
|
|
250
|
+
const layout = normalizeResourceLayout({ order: raw.order, recent: raw.recent, interactive: raw.interactive });
|
|
251
|
+
return layout ? { formatVersion: 2, ...layout, revision: raw.revision, updatedAt: raw.updatedAt } : undefined;
|
|
252
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type WorkSessionStatus } from './delegation.ts';
|
|
2
|
+
import { type ExecutionOwnership, type FollowUpOwner } from './task-overview.ts';
|
|
3
|
+
export type RuntimeHandling = 'model' | 'host' | 'user' | 'none';
|
|
4
|
+
/** A checked view of existing session facts, never a second lifecycle or execution receipt. */
|
|
5
|
+
export interface RuntimeSessionFacts {
|
|
6
|
+
readonly sessionId: string;
|
|
7
|
+
readonly status: WorkSessionStatus | 'unknown';
|
|
8
|
+
readonly valid: boolean;
|
|
9
|
+
readonly executionOwnership: ExecutionOwnership;
|
|
10
|
+
readonly owner: FollowUpOwner;
|
|
11
|
+
readonly reason: string;
|
|
12
|
+
readonly handling: RuntimeHandling;
|
|
13
|
+
readonly pendingManaged: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare function runtimeSessionFacts(value: unknown): RuntimeSessionFacts;
|
|
16
|
+
/** Missing/invalid facts remain obligations; a valid public terminal does not invent live ownership. */
|
|
17
|
+
export declare function hasPendingRuntimeSessions(sessions: readonly unknown[], expectedIds?: readonly string[]): boolean;
|
|
18
|
+
export interface RuntimeSessionOwnership {
|
|
19
|
+
readonly repairSessionIds: readonly string[];
|
|
20
|
+
readonly hostSessionIds: readonly string[];
|
|
21
|
+
readonly waitingSessionIds: readonly string[];
|
|
22
|
+
readonly needsUser: boolean;
|
|
23
|
+
}
|
|
24
|
+
export declare function runtimeSessionOwnership(sessions: readonly unknown[]): RuntimeSessionOwnership;
|
|
25
|
+
export interface ResourceWaitScope {
|
|
26
|
+
readonly workspaceId: string;
|
|
27
|
+
readonly parentConversationId: string;
|
|
28
|
+
readonly workId: string;
|
|
29
|
+
readonly sessionIds: readonly string[];
|
|
30
|
+
readonly executionBindings?: Readonly<Record<string, {
|
|
31
|
+
readonly executionEpoch: string;
|
|
32
|
+
readonly goalRevision: number;
|
|
33
|
+
}>>;
|
|
34
|
+
}
|
|
35
|
+
export interface ResourceWaitReference {
|
|
36
|
+
readonly kind: 'resource';
|
|
37
|
+
readonly id: string;
|
|
38
|
+
readonly revision: string;
|
|
39
|
+
}
|
|
40
|
+
export interface ResourceWaitFact {
|
|
41
|
+
readonly sessionId: string;
|
|
42
|
+
readonly wait: ResourceWaitReference;
|
|
43
|
+
readonly reason: string;
|
|
44
|
+
}
|
|
45
|
+
export type ResourceWaitInspection = {
|
|
46
|
+
readonly state: 'waiting';
|
|
47
|
+
readonly current: ResourceWaitFact;
|
|
48
|
+
} | {
|
|
49
|
+
readonly state: 'ready';
|
|
50
|
+
readonly sessionId: string;
|
|
51
|
+
} | {
|
|
52
|
+
readonly state: 'stale' | 'unavailable';
|
|
53
|
+
readonly reason: string;
|
|
54
|
+
};
|
|
55
|
+
export interface ResourceWaitPort {
|
|
56
|
+
readonly read: (scope: ResourceWaitScope) => readonly ResourceWaitFact[];
|
|
57
|
+
readonly inspect: (scope: ResourceWaitScope, wait: ResourceWaitReference) => ResourceWaitInspection;
|
|
58
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { WORK_SESSION_STATUSES } from "./delegation.js";
|
|
2
|
+
import { decideFollowUp } from "./task-overview.js";
|
|
3
|
+
const record = (value) => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
4
|
+
const text = (value) => typeof value === 'string' ? value : '';
|
|
5
|
+
const statuses = new Set(WORK_SESSION_STATUSES);
|
|
6
|
+
const ownerships = new Set(['live', 'ended', 'unknown']);
|
|
7
|
+
const owners = new Set(['bot', 'user', 'none']);
|
|
8
|
+
const reasons = new Set(['settled', 'superseded', 'stop_confirmed', 'user_withdrawn',
|
|
9
|
+
'stop_unconfirmed', 'human_request', 'awaiting_human_gate', 'technical_block', 'local_persistence_failed', 'awaiting_acceptance_technical',
|
|
10
|
+
'verification_failed', 'live_execution', 'paused', 'unknown_facts', 'obligation_indexed']);
|
|
11
|
+
const hostReasons = new Set(['live_execution', 'stop_unconfirmed', 'unknown_facts', 'local_persistence_failed']);
|
|
12
|
+
export function runtimeSessionFacts(value) {
|
|
13
|
+
const row = record(value), origin = record(row.origin), cancellation = record(origin.cancellation);
|
|
14
|
+
const sessionId = text(row.sessionId), rawStatus = text(row.status);
|
|
15
|
+
const status = statuses.has(rawStatus) ? rawStatus : 'unknown';
|
|
16
|
+
const executionOwnership = ownerships.has(row.executionOwnership)
|
|
17
|
+
? row.executionOwnership : 'unknown';
|
|
18
|
+
const declared = row.followUp === undefined ? null : record(row.followUp);
|
|
19
|
+
const valid = Boolean(sessionId) && status !== 'unknown'
|
|
20
|
+
&& (row.executionOwnership === undefined || ownerships.has(row.executionOwnership))
|
|
21
|
+
&& (!declared || owners.has(declared.owner) && reasons.has(declared.reason))
|
|
22
|
+
&& ['resultConfirmationRequired', 'completionReviewRequired'].every(key => row[key] === undefined || typeof row[key] === 'boolean')
|
|
23
|
+
&& (row.needsUser === undefined || typeof row.needsUser === 'boolean' || Array.isArray(row.needsUser));
|
|
24
|
+
const human = row.resultConfirmationRequired === true || row.completionReviewRequired === true
|
|
25
|
+
|| row.needsUser === true || Array.isArray(row.needsUser) && row.needsUser.length > 0;
|
|
26
|
+
const fallback = status === 'unknown' ? null : decideFollowUp({ status }, {
|
|
27
|
+
accepted: status === 'accepted', humanGate: row.resultConfirmationRequired === true,
|
|
28
|
+
...(typeof cancellation.phase === 'string' ? { cancellationPhase: cancellation.phase } : {}),
|
|
29
|
+
...(cancellation.disposition === 'user_withdrawn' ? { cancellationDisposition: 'user_withdrawn' } : {}),
|
|
30
|
+
}, executionOwnership);
|
|
31
|
+
const owner = declared && owners.has(declared.owner) ? declared.owner : fallback?.owner || 'bot';
|
|
32
|
+
const reason = text(declared?.reason) || fallback?.reason || 'unknown_facts';
|
|
33
|
+
const stopping = reason !== 'obligation_indexed' && (['stopping', 'awaiting_outcome'].includes(status)
|
|
34
|
+
|| status === 'cancelled' && owner !== 'none');
|
|
35
|
+
const handling = !valid ? 'host' : human || owner === 'user' ? 'user'
|
|
36
|
+
: executionOwnership === 'live' || stopping || owner === 'bot' && hostReasons.has(reason) ? 'host'
|
|
37
|
+
: owner === 'none' ? 'none' : 'model';
|
|
38
|
+
return { sessionId, status, valid, executionOwnership, owner, reason, handling,
|
|
39
|
+
pendingManaged: reason === 'obligation_indexed' ? false : handling === 'host' || handling === 'user' || !['accepted', 'cancelled'].includes(status) };
|
|
40
|
+
}
|
|
41
|
+
/** Missing/invalid facts remain obligations; a valid public terminal does not invent live ownership. */
|
|
42
|
+
export function hasPendingRuntimeSessions(sessions, expectedIds = []) {
|
|
43
|
+
return sessions.some(session => runtimeSessionFacts(session).pendingManaged)
|
|
44
|
+
|| expectedIds.some(id => !sessions.some(session => record(session).sessionId === id));
|
|
45
|
+
}
|
|
46
|
+
export function runtimeSessionOwnership(sessions) {
|
|
47
|
+
const facts = sessions.map(runtimeSessionFacts);
|
|
48
|
+
return {
|
|
49
|
+
repairSessionIds: facts.filter(row => row.handling === 'model').map(row => row.sessionId),
|
|
50
|
+
hostSessionIds: facts.filter(row => row.handling === 'host').map(row => row.sessionId),
|
|
51
|
+
waitingSessionIds: facts.filter(row => row.handling === 'host'
|
|
52
|
+
&& (row.executionOwnership === 'live' || row.reason === 'live_execution')).map(row => row.sessionId),
|
|
53
|
+
needsUser: facts.some(row => row.handling === 'user'),
|
|
54
|
+
};
|
|
55
|
+
}
|