Repository navigation
Expand file tree
/
Copy pathapp-log-session-resource.ts
More file actions
151 lines (142 loc) · 4.67 KB
/
Copy pathapp-log-session-resource.ts
File metadata and controls
151 lines (142 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import type { LogBackend } from '@agent-device/contracts/observability';
import type { AppLogCompletion, AppLogLiveHandle } from '@agent-device/contracts/app-log-runtime';
import type { DurableResourceEnvelope } from '@agent-device/contracts/durable-resource-envelope';
import type { PendingTransferGuard } from '@agent-device/contracts/async-lifecycle';
import type {
ResourceOwnershipFence,
RuntimeOwnerRef,
} from '@agent-device/contracts/platform-runtime';
import type { DeviceInfo } from '@agent-device/kernel/device';
import { normalizeError } from '@agent-device/kernel/errors';
import type { AppLogAdmissionLedger } from './app-log-admission-ledger.ts';
import {
createDurableCaptureResource,
type DurableCaptureFinishIntent,
} from '@agent-device/capture-kit/durable-capture-resource';
import { appLogResourceStore } from './app-log-resource-store.ts';
import type { SessionStore } from './session-store.ts';
import type { SessionRef, SessionState } from './session-state.ts';
import { bindSessionCapture } from './session-capture-binding.ts';
export type AppLogSessionSnapshot = Readonly<{
active: boolean;
state: 'active' | 'recovering' | 'ended' | 'failed' | 'inactive';
backend?: LogBackend;
startedAt?: number;
failureCode?: string;
failureMessage?: string;
hint?: string;
}>;
export const appLogDurableResource = createDurableCaptureResource<
'app-log',
AppLogLiveHandle,
AppLogCompletion,
SessionState
>({
resourceKind: 'app-log',
displayName: 'app-log',
store: appLogResourceStore,
sessionSlot: {
read: (session) => session.appLog,
replace: (session, appLog) => ({ ...session, appLog, appLogFailure: undefined }),
},
completionMetadata: (completion) => ({
backend: completion.backend,
outputPath: completion.outputPath,
completedAt: completion.completedAt,
}),
failedFinishPolicy: 'dispose-on-failed-finish',
messages: {
noActive: 'no app log stream active',
cleanupPendingHint:
'Keep app-log.resource.json and retry cleanup through its exact runtime owner.',
},
});
export function inspectSessionAppLog(session: SessionState): AppLogSessionSnapshot {
if (session.appLog) {
const snapshot = session.appLog.handle.inspect();
return {
active: snapshot.state === 'active' || snapshot.state === 'recovering',
...snapshot,
};
}
if (session.appLogFailure) {
return {
active: false,
state: 'failed',
backend: session.appLogFailure.backend,
failureCode: session.appLogFailure.code,
failureMessage: session.appLogFailure.message,
hint: session.appLogFailure.hint,
};
}
return { active: false, state: 'inactive' };
}
export function adoptStartedSessionAppLog(params: {
admissionLedger: AppLogAdmissionLedger;
ref: SessionRef;
sessionStore: SessionStore;
device: DeviceInfo;
owner: RuntimeOwnerRef;
fence: ResourceOwnershipFence;
pendingHandle: PendingTransferGuard<AppLogLiveHandle>;
envelope: DurableResourceEnvelope<'app-log'>;
throwIfCanceled(): void;
}): Promise<void> {
return appLogDurableResource.adoptStarted({
...params,
binding: bindSessionAppLog(params.sessionStore, params.ref),
});
}
export function finishSessionAppLog(params: {
session: SessionState;
sessionName: string;
sessionStore: SessionStore;
resourcePath: string;
intent: DurableCaptureFinishIntent;
}): Promise<AppLogCompletion> {
return appLogDurableResource.finishLive(params);
}
export function forceCleanupSessionAppLog(params: {
session: SessionState;
sessionName?: string;
sessionStore?: SessionStore;
resourcePath: string;
}): Promise<void> {
return appLogDurableResource.forceCleanupLive(params);
}
export function recordSessionAppLogFailure(params: {
ref: SessionRef;
sessionStore: SessionStore;
error: unknown;
backend?: LogBackend;
}): ReturnType<typeof normalizeError> {
const normalized = normalizeError(params.error);
const current = params.sessionStore.resolveCurrent(params.ref);
if (!current || current.appLog) return normalized;
params.sessionStore.update(params.ref, {
appLog: undefined,
appLogFailure: {
backend: params.backend,
code: normalized.code,
message: normalized.message,
hint: normalized.hint,
},
});
return normalized;
}
export function clearSessionAppLogFailure(params: {
ref: SessionRef;
sessionStore: SessionStore;
}): void {
params.sessionStore.update(params.ref, {
appLogFailure: undefined,
});
}
function bindSessionAppLog(sessionStore: SessionStore, ref: SessionRef) {
return bindSessionCapture(sessionStore, ref, {
read: (session) => session.appLog,
write: (appLog) => {
sessionStore.update(ref, { appLog, appLogFailure: undefined });
},
});
}