@qlairoslabs/acp-client 0.6.0 → 0.7.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/CONSUMER_GUIDE.md +618 -291
- package/README.md +25 -0
- package/package.json +1 -1
- package/src/agent-session.ts +2188 -96
- package/src/cli/index.ts +33 -5
- package/src/cli/managed.ts +220 -0
- package/src/command-descriptors.ts +22 -0
- package/src/controller.ts +87 -37
- package/src/index.ts +229 -3
- package/src/metering/engine.ts +9 -1
- package/src/metering/record.ts +2 -1
- package/src/persistence/activity-projection.ts +71 -0
- package/src/persistence/audit-log-reader.ts +506 -0
- package/src/persistence/audit-runtime.ts +162 -0
- package/src/persistence/checkpoint.ts +304 -0
- package/src/persistence/config.ts +538 -0
- package/src/persistence/configuration-state.ts +191 -0
- package/src/persistence/deletion.ts +227 -0
- package/src/persistence/file-registry-store.ts +740 -0
- package/src/persistence/integrity.ts +147 -0
- package/src/persistence/legacy-registry-import.ts +240 -0
- package/src/persistence/managed-checkpoint.ts +81 -0
- package/src/persistence/managed-creation.ts +456 -0
- package/src/persistence/managed-load.ts +172 -0
- package/src/persistence/managed-registry.ts +57 -0
- package/src/persistence/migration.ts +298 -0
- package/src/persistence/paths.ts +172 -0
- package/src/persistence/projection.ts +373 -0
- package/src/persistence/reconfiguration.ts +453 -0
- package/src/persistence/records.ts +873 -0
- package/src/persistence/reducer.ts +498 -0
- package/src/persistence/registry.ts +446 -0
- package/src/persistence/restoration.ts +689 -0
- package/src/persistence/session-log-writer.ts +480 -0
- package/src/persistence/state-runtime.ts +176 -0
- package/src/persistence/transcript-reader.ts +980 -0
- package/src/persistence/transcript-runtime.ts +667 -0
- package/src/persistence/transcript-scanner.ts +385 -0
- package/src/persistence/truncation.ts +276 -0
- package/src/repl/agent-session-commands.ts +74 -1
- package/src/repl/commands.ts +1 -1
- package/src/repl/index.ts +7 -1
- package/src/session-manager.ts +257 -7
- package/src/transcript.ts +3 -3
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { ConfigOption, ConfigOptions } from "../config-options.ts";
|
|
2
|
+
import type {
|
|
3
|
+
JsonValue,
|
|
4
|
+
PersistedSessionConfigSelection,
|
|
5
|
+
SessionConfigurationCheckpoint,
|
|
6
|
+
} from "./checkpoint.ts";
|
|
7
|
+
import type { ResolvedSessionConfigurationPersistence } from "./config.ts";
|
|
8
|
+
import { canonicalizeJson, sha256CanonicalJson } from "./integrity.ts";
|
|
9
|
+
|
|
10
|
+
export interface SnapshotSessionConfigurationOptions {
|
|
11
|
+
readonly adapterId: string;
|
|
12
|
+
readonly configOptions: ConfigOptions;
|
|
13
|
+
readonly policy: ResolvedSessionConfigurationPersistence;
|
|
14
|
+
readonly restorationTargets?: readonly PersistedSessionConfigSelection[];
|
|
15
|
+
readonly observedAt?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SnapshotSessionConfigurationAfterSelectionOptions
|
|
19
|
+
extends SnapshotSessionConfigurationOptions {
|
|
20
|
+
readonly selection: {
|
|
21
|
+
readonly configId: string;
|
|
22
|
+
readonly type: ConfigOption["type"];
|
|
23
|
+
readonly value: unknown;
|
|
24
|
+
readonly recordedAt?: string;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SnapshotAcceptedEffectiveConfigurationOptions
|
|
29
|
+
extends Omit<SnapshotSessionConfigurationOptions, "restorationTargets"> {
|
|
30
|
+
readonly recordedAt?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Convert the harness's latest complete option announcement into bounded continuation
|
|
35
|
+
* state. Values are inspected only after their IDs/types pass persistence policy.
|
|
36
|
+
*/
|
|
37
|
+
export function snapshotSessionConfiguration(
|
|
38
|
+
options: SnapshotSessionConfigurationOptions,
|
|
39
|
+
): SessionConfigurationCheckpoint {
|
|
40
|
+
const observedAt = options.observedAt ?? new Date().toISOString();
|
|
41
|
+
const announced = [...options.configOptions.values()].sort((a, b) =>
|
|
42
|
+
a.configId.localeCompare(b.configId)
|
|
43
|
+
);
|
|
44
|
+
const explicitlyIncluded = new Set(options.policy.include);
|
|
45
|
+
const excluded = new Set(options.policy.exclude);
|
|
46
|
+
const effective: Record<string, JsonValue> = {};
|
|
47
|
+
const omittedConfigIds: string[] = [];
|
|
48
|
+
|
|
49
|
+
for (const config of announced) {
|
|
50
|
+
if (!isAdmitted(config, options.policy, explicitlyIncluded, excluded)) {
|
|
51
|
+
omittedConfigIds.push(config.configId);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const value = toJsonValue(config.currentValue);
|
|
55
|
+
if (value === null && config.currentValue !== null) {
|
|
56
|
+
omittedConfigIds.push(config.configId);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
effective[config.configId] = value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const targets = (options.restorationTargets ?? [])
|
|
63
|
+
.filter((target) => isTargetAdmitted(target, options.policy, explicitlyIncluded, excluded))
|
|
64
|
+
.map((target) => structuredClone(target));
|
|
65
|
+
|
|
66
|
+
return Object.freeze({
|
|
67
|
+
adapterId: options.adapterId,
|
|
68
|
+
schemaDigest: sha256CanonicalJson(announced.map(schemaProjection)),
|
|
69
|
+
announcedConfigIds: Object.freeze(announced.map((config) => config.configId)),
|
|
70
|
+
restorationTargets: Object.freeze(targets),
|
|
71
|
+
effective: Object.freeze(effective),
|
|
72
|
+
omittedConfigIds: Object.freeze(omittedConfigIds),
|
|
73
|
+
restorationFidelity:
|
|
74
|
+
options.policy.values === "none" || omittedConfigIds.length > 0 ? "partial" : "exact",
|
|
75
|
+
observedAt,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Add one successful explicit selection to the ordered restoration targets. */
|
|
80
|
+
export function snapshotSessionConfigurationAfterSelection(
|
|
81
|
+
options: SnapshotSessionConfigurationAfterSelectionOptions,
|
|
82
|
+
): SessionConfigurationCheckpoint {
|
|
83
|
+
const recordedAt = options.selection.recordedAt ?? options.observedAt ?? new Date().toISOString();
|
|
84
|
+
const explicitlyIncluded = new Set(options.policy.include);
|
|
85
|
+
const excluded = new Set(options.policy.exclude);
|
|
86
|
+
const existing = (options.restorationTargets ?? [])
|
|
87
|
+
.filter((target) => target.configId !== options.selection.configId);
|
|
88
|
+
const value = toJsonValue(options.selection.value);
|
|
89
|
+
const candidate: PersistedSessionConfigSelection | null =
|
|
90
|
+
(value !== null || options.selection.value === null)
|
|
91
|
+
? {
|
|
92
|
+
configId: options.selection.configId,
|
|
93
|
+
type: options.selection.type,
|
|
94
|
+
value,
|
|
95
|
+
ordinal: existing.reduce((max, target) => Math.max(max, target.ordinal), -1) + 1,
|
|
96
|
+
source: "explicit_selection",
|
|
97
|
+
recordedAt,
|
|
98
|
+
}
|
|
99
|
+
: null;
|
|
100
|
+
const restorationTargets = candidate && isTargetAdmitted(
|
|
101
|
+
candidate,
|
|
102
|
+
options.policy,
|
|
103
|
+
explicitlyIncluded,
|
|
104
|
+
excluded,
|
|
105
|
+
)
|
|
106
|
+
? [...existing, candidate]
|
|
107
|
+
: existing;
|
|
108
|
+
return snapshotSessionConfiguration({ ...options, restorationTargets });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Make the policy-admitted current announcement the ordered future restoration target. */
|
|
112
|
+
export function snapshotAcceptedEffectiveConfiguration(
|
|
113
|
+
options: SnapshotAcceptedEffectiveConfigurationOptions,
|
|
114
|
+
): SessionConfigurationCheckpoint {
|
|
115
|
+
const recordedAt = options.recordedAt ?? options.observedAt ?? new Date().toISOString();
|
|
116
|
+
const effective = snapshotSessionConfiguration(options);
|
|
117
|
+
const restorationTargets: PersistedSessionConfigSelection[] = [];
|
|
118
|
+
for (const config of options.configOptions.values()) {
|
|
119
|
+
if (!(config.configId in effective.effective)) continue;
|
|
120
|
+
restorationTargets.push({
|
|
121
|
+
configId: config.configId,
|
|
122
|
+
type: config.type,
|
|
123
|
+
value: structuredClone(effective.effective[config.configId]!),
|
|
124
|
+
ordinal: restorationTargets.length,
|
|
125
|
+
source: "accepted_effective",
|
|
126
|
+
recordedAt,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return snapshotSessionConfiguration({ ...options, restorationTargets, observedAt: recordedAt });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function isAdmitted(
|
|
133
|
+
config: ConfigOption,
|
|
134
|
+
policy: ResolvedSessionConfigurationPersistence,
|
|
135
|
+
explicitlyIncluded: ReadonlySet<string>,
|
|
136
|
+
excluded: ReadonlySet<string>,
|
|
137
|
+
): boolean {
|
|
138
|
+
if (excluded.has(config.configId)) return false;
|
|
139
|
+
if (explicitlyIncluded.size > 0 && !explicitlyIncluded.has(config.configId)) return false;
|
|
140
|
+
if (policy.values === "none") return false;
|
|
141
|
+
if (policy.values === "all") return true;
|
|
142
|
+
if (config.type === "boolean") return typeof config.currentValue === "boolean";
|
|
143
|
+
if (config.type === "select") {
|
|
144
|
+
const current = tryCanonical(config.currentValue);
|
|
145
|
+
return current !== null && (config.allowedValues ?? []).some(
|
|
146
|
+
(choice) => tryCanonical(choice.value) === current,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
return explicitlyIncluded.has(config.configId);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function isTargetAdmitted(
|
|
153
|
+
target: PersistedSessionConfigSelection,
|
|
154
|
+
policy: ResolvedSessionConfigurationPersistence,
|
|
155
|
+
explicitlyIncluded: ReadonlySet<string>,
|
|
156
|
+
excluded: ReadonlySet<string>,
|
|
157
|
+
): boolean {
|
|
158
|
+
if (excluded.has(target.configId)) return false;
|
|
159
|
+
if (explicitlyIncluded.size > 0 && !explicitlyIncluded.has(target.configId)) return false;
|
|
160
|
+
if (policy.values === "none") return false;
|
|
161
|
+
return policy.values === "all" || target.type !== "unknown" || explicitlyIncluded.has(target.configId);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function schemaProjection(config: ConfigOption): Record<string, JsonValue> {
|
|
165
|
+
const allowedValues: JsonValue[] = [];
|
|
166
|
+
for (const choice of config.allowedValues ?? []) {
|
|
167
|
+
const value = toJsonValue(choice.value);
|
|
168
|
+
if (value !== null || choice.value === null) allowedValues.push(value);
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
configId: config.configId,
|
|
172
|
+
type: config.type,
|
|
173
|
+
allowedValues,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function tryCanonical(value: unknown): string | null {
|
|
178
|
+
try {
|
|
179
|
+
return canonicalizeJson(value);
|
|
180
|
+
} catch {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function toJsonValue(value: unknown): JsonValue | null {
|
|
186
|
+
try {
|
|
187
|
+
return JSON.parse(canonicalizeJson(value)) as JsonValue;
|
|
188
|
+
} catch {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { open, unlink } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import type { AgentSessionRecord, AgentSessionStore, DeletionMaintenance } from "./registry.ts";
|
|
4
|
+
import { AgentSessionStoreConflictError } from "./registry.ts";
|
|
5
|
+
import { sha256CanonicalJson } from "./integrity.ts";
|
|
6
|
+
import { withSessionLogWriterLock } from "./session-log-writer.ts";
|
|
7
|
+
|
|
8
|
+
export interface DeleteAgentSessionOptions {
|
|
9
|
+
/** Stable consumer-minted identity for retrying this exact deletion. */
|
|
10
|
+
readonly deletionId: string;
|
|
11
|
+
readonly expectedIncarnationId: string;
|
|
12
|
+
readonly expectedRecordRevision: number;
|
|
13
|
+
readonly deleteTranscript?: boolean;
|
|
14
|
+
readonly deleteAuditLog?: boolean;
|
|
15
|
+
readonly removeRegistryEntry?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class AgentSessionDeletionError extends Error {
|
|
19
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
20
|
+
super(message, options);
|
|
21
|
+
this.name = "AgentSessionDeletionError";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Execute the existing prepared-deletion transaction against one managed registry. */
|
|
26
|
+
export async function deleteManagedAgentSession(
|
|
27
|
+
store: AgentSessionStore,
|
|
28
|
+
agentSessionId: string,
|
|
29
|
+
options: DeleteAgentSessionOptions,
|
|
30
|
+
): Promise<void> {
|
|
31
|
+
const request = normalizeRequest(agentSessionId, options);
|
|
32
|
+
const requestDigest = sha256CanonicalJson(request);
|
|
33
|
+
const record = await store.getAgentSessionRecord(agentSessionId);
|
|
34
|
+
|
|
35
|
+
// A hard-deletion retry can no longer derive artifact paths from a record. The store
|
|
36
|
+
// checks the incarnation-scoped receipt before it examines this placeholder's details.
|
|
37
|
+
if (!record) {
|
|
38
|
+
const completed = await store.prepareMaintenance(
|
|
39
|
+
agentSessionId,
|
|
40
|
+
maintenance(request, requestDigest, null, null),
|
|
41
|
+
{ expectedRecordRevision: request.expectedRecordRevision },
|
|
42
|
+
);
|
|
43
|
+
if (completed.status === "already_completed") return;
|
|
44
|
+
throw new AgentSessionStoreConflictError("agent-session record does not exist");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (record.incarnationId !== request.expectedIncarnationId
|
|
48
|
+
|| record.recordRevision !== request.expectedRecordRevision) {
|
|
49
|
+
throw new AgentSessionStoreConflictError("agent-session record compare-and-swap failed");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const transcriptPath = availableTranscriptPath(record);
|
|
53
|
+
const auditLogPath = availableAuditLogPath(record);
|
|
54
|
+
if (request.removeRegistryEntry
|
|
55
|
+
&& ((!request.deleteTranscript && transcriptPath !== null)
|
|
56
|
+
|| (!request.deleteAuditLog && auditLogPath !== null))) {
|
|
57
|
+
throw new AgentSessionDeletionError(
|
|
58
|
+
"removeRegistryEntry requires deletion of every retained transcript and audit log",
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const paths = [...new Set([transcriptPath, auditLogPath].filter((path): path is string => path !== null))]
|
|
63
|
+
.sort((left, right) => left.localeCompare(right));
|
|
64
|
+
await withOrderedLogLocks(paths, async () => {
|
|
65
|
+
const proposed = maintenance(request, requestDigest, transcriptPath, auditLogPath);
|
|
66
|
+
const prepared = await store.prepareMaintenance(agentSessionId, proposed, {
|
|
67
|
+
expectedRecordRevision: request.expectedRecordRevision,
|
|
68
|
+
});
|
|
69
|
+
if (prepared.status === "already_completed") return;
|
|
70
|
+
if (prepared.maintenance.kind !== "deletion") {
|
|
71
|
+
throw new AgentSessionDeletionError("prepared maintenance is not a deletion");
|
|
72
|
+
}
|
|
73
|
+
const active = prepared.maintenance;
|
|
74
|
+
|
|
75
|
+
if (active.details.deleteTranscript && active.details.transcriptPath) {
|
|
76
|
+
await deleteAndSync(active.details.transcriptPath);
|
|
77
|
+
}
|
|
78
|
+
if (active.details.deleteAuditLog && active.details.auditLogPath) {
|
|
79
|
+
await deleteAndSync(active.details.auditLogPath);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (active.details.removeRegistryEntry) {
|
|
83
|
+
await store.finalizeMaintenance(
|
|
84
|
+
agentSessionId,
|
|
85
|
+
request.expectedIncarnationId,
|
|
86
|
+
request.deletionId,
|
|
87
|
+
requestDigest,
|
|
88
|
+
{ deleteRecord: true },
|
|
89
|
+
);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const mutationAt = active.startedAt < record.updatedAt ? record.updatedAt : active.startedAt;
|
|
94
|
+
const deletedAt = record.deletion?.deletedAt ?? mutationAt;
|
|
95
|
+
await store.finalizeMaintenance(
|
|
96
|
+
agentSessionId,
|
|
97
|
+
request.expectedIncarnationId,
|
|
98
|
+
request.deletionId,
|
|
99
|
+
requestDigest,
|
|
100
|
+
{
|
|
101
|
+
save: {
|
|
102
|
+
...record,
|
|
103
|
+
recordRevision: record.recordRevision + 1,
|
|
104
|
+
updatedAt: mutationAt,
|
|
105
|
+
deletion: {
|
|
106
|
+
deletedAt,
|
|
107
|
+
...(record.deletion?.transcriptDeletedAt === undefined
|
|
108
|
+
&& !(active.details.deleteTranscript && active.details.transcriptPath)
|
|
109
|
+
? {}
|
|
110
|
+
: { transcriptDeletedAt: record.deletion?.transcriptDeletedAt ?? mutationAt }),
|
|
111
|
+
...(record.deletion?.auditLogDeletedAt === undefined
|
|
112
|
+
&& !(active.details.deleteAuditLog && active.details.auditLogPath)
|
|
113
|
+
? {}
|
|
114
|
+
: { auditLogDeletedAt: record.deletion?.auditLogDeletedAt ?? mutationAt }),
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface NormalizedDeletionRequest {
|
|
123
|
+
readonly agentSessionId: string;
|
|
124
|
+
readonly deletionId: string;
|
|
125
|
+
readonly expectedIncarnationId: string;
|
|
126
|
+
readonly expectedRecordRevision: number;
|
|
127
|
+
readonly deleteTranscript: boolean;
|
|
128
|
+
readonly deleteAuditLog: boolean;
|
|
129
|
+
readonly removeRegistryEntry: boolean;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function normalizeRequest(
|
|
133
|
+
agentSessionId: string,
|
|
134
|
+
options: DeleteAgentSessionOptions,
|
|
135
|
+
): NormalizedDeletionRequest {
|
|
136
|
+
for (const [label, value] of [
|
|
137
|
+
["agentSessionId", agentSessionId],
|
|
138
|
+
["deletionId", options?.deletionId],
|
|
139
|
+
["expectedIncarnationId", options?.expectedIncarnationId],
|
|
140
|
+
] as const) {
|
|
141
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
142
|
+
throw new AgentSessionDeletionError(`${label} must be a nonblank string`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (!Number.isSafeInteger(options.expectedRecordRevision) || options.expectedRecordRevision < 1) {
|
|
146
|
+
throw new AgentSessionDeletionError("expectedRecordRevision must be a positive safe integer");
|
|
147
|
+
}
|
|
148
|
+
for (const field of ["deleteTranscript", "deleteAuditLog", "removeRegistryEntry"] as const) {
|
|
149
|
+
if (options[field] !== undefined && typeof options[field] !== "boolean") {
|
|
150
|
+
throw new AgentSessionDeletionError(`${field} must be boolean when provided`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
agentSessionId,
|
|
155
|
+
deletionId: options.deletionId,
|
|
156
|
+
expectedIncarnationId: options.expectedIncarnationId,
|
|
157
|
+
expectedRecordRevision: options.expectedRecordRevision,
|
|
158
|
+
deleteTranscript: options.deleteTranscript ?? false,
|
|
159
|
+
deleteAuditLog: options.deleteAuditLog ?? false,
|
|
160
|
+
removeRegistryEntry: options.removeRegistryEntry ?? false,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function maintenance(
|
|
165
|
+
request: NormalizedDeletionRequest,
|
|
166
|
+
requestDigest: string,
|
|
167
|
+
transcriptPath: string | null,
|
|
168
|
+
auditLogPath: string | null,
|
|
169
|
+
): DeletionMaintenance {
|
|
170
|
+
return {
|
|
171
|
+
kind: "deletion",
|
|
172
|
+
incarnationId: request.expectedIncarnationId,
|
|
173
|
+
baseRecordRevision: request.expectedRecordRevision,
|
|
174
|
+
operationId: request.deletionId,
|
|
175
|
+
requestDigest,
|
|
176
|
+
status: "prepared",
|
|
177
|
+
startedAt: new Date().toISOString(),
|
|
178
|
+
details: {
|
|
179
|
+
deleteTranscript: request.deleteTranscript,
|
|
180
|
+
transcriptPath,
|
|
181
|
+
deleteAuditLog: request.deleteAuditLog,
|
|
182
|
+
auditLogPath,
|
|
183
|
+
removeRegistryEntry: request.removeRegistryEntry,
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function availableTranscriptPath(record: AgentSessionRecord): string | null {
|
|
189
|
+
return record.transcript !== null && record.deletion?.transcriptDeletedAt === undefined
|
|
190
|
+
? record.transcript.path
|
|
191
|
+
: null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function availableAuditLogPath(record: AgentSessionRecord): string | null {
|
|
195
|
+
return record.auditLog !== null && record.deletion?.auditLogDeletedAt === undefined
|
|
196
|
+
? record.auditLog.path
|
|
197
|
+
: null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function withOrderedLogLocks<T>(
|
|
201
|
+
paths: readonly string[],
|
|
202
|
+
operation: () => Promise<T>,
|
|
203
|
+
index = 0,
|
|
204
|
+
): Promise<T> {
|
|
205
|
+
const path = paths[index];
|
|
206
|
+
if (path === undefined) return await operation();
|
|
207
|
+
return await withSessionLogWriterLock(path, () => withOrderedLogLocks(paths, operation, index + 1));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function deleteAndSync(path: string): Promise<void> {
|
|
211
|
+
try {
|
|
212
|
+
await unlink(path);
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
215
|
+
throw new AgentSessionDeletionError(`could not delete session artifact: ${path}`, { cause: error });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
let parent;
|
|
219
|
+
try {
|
|
220
|
+
parent = await open(dirname(path), "r");
|
|
221
|
+
await parent.sync();
|
|
222
|
+
} catch (error) {
|
|
223
|
+
throw new AgentSessionDeletionError(`could not synchronize deleted artifact directory: ${dirname(path)}`, { cause: error });
|
|
224
|
+
} finally {
|
|
225
|
+
await parent?.close().catch(() => undefined);
|
|
226
|
+
}
|
|
227
|
+
}
|