maka-agent 0.2.0-dev.69.20261004 → 0.2.0-dev.74.20261008

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.
Files changed (57) hide show
  1. package/LICENSE +3 -3
  2. package/THIRD_PARTY_NOTICES.txt +2 -2
  3. package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
  4. package/node_modules/@maka/core/dist/archive-retention.js +109 -0
  5. package/node_modules/@maka/core/dist/runtime-policy/connection-catalog-codec.js +8 -4
  6. package/node_modules/@maka/core/package.json +1 -0
  7. package/node_modules/@maka/mcp/dist/oauth.js +7 -2
  8. package/node_modules/@maka/mcp/package.json +1 -1
  9. package/node_modules/@maka/runtime-host/dist/protocol/connection-effects.js +11 -2
  10. package/node_modules/@maka/runtime-host/dist/protocol/index.js +6 -1
  11. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +5 -1
  12. package/node_modules/@maka/runtime-host/dist/protocol/storage-retention.js +144 -0
  13. package/node_modules/@maka/runtime-host/dist/server/archive-retention-coordinator.js +546 -0
  14. package/node_modules/@maka/runtime-host/dist/server/connection-effect-coordinator.js +1 -1
  15. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +12 -0
  16. package/node_modules/@maka/runtime-host/dist/server/session-retirement-coordinator.js +73 -7
  17. package/node_modules/@maka/runtime-host/dist/server/storage-maintenance.js +34 -4
  18. package/node_modules/@maka/storage/dist/archive-retention-store.js +124 -0
  19. package/node_modules/@maka/storage/dist/context-offload-store.js +4 -0
  20. package/node_modules/@maka/storage/dist/execution-stores.js +3 -0
  21. package/node_modules/@maka/storage/dist/runtime-policy/connection-catalog-document.js +48 -16
  22. package/node_modules/@maka/storage/dist/runtime-policy/coordinator.js +2 -2
  23. package/node_modules/@maka/storage/dist/runtime-policy/document-io.js +1 -1
  24. package/node_modules/@maka/storage/dist/runtime-policy-stores.js +1 -1
  25. package/node_modules/@maka/storage/dist/session-bundle-policy.js +9 -0
  26. package/node_modules/@maka/storage/dist/session-store.js +17 -0
  27. package/node_modules/@maka/storage/dist/sqlite-context-offload-store.js +34 -0
  28. package/node_modules/@maka/storage/dist/sqlite-session-catalog-query.js +48 -5
  29. package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +111 -1
  30. package/node_modules/@maka/storage/dist/sqlite-session-role-scope.js +3 -3
  31. package/node_modules/@maka/storage/dist/storage-writer-composition.js +2 -0
  32. package/node_modules/@maka/storage/package.json +1 -0
  33. package/node_modules/@modelcontextprotocol/client/dist/index-D_CiY-T3.d.cts.map +1 -0
  34. package/node_modules/@modelcontextprotocol/client/dist/index-D_QDtbiv.d.mts.map +1 -0
  35. package/node_modules/@modelcontextprotocol/client/dist/index.cjs +100 -55
  36. package/node_modules/@modelcontextprotocol/client/dist/index.d.cts.map +1 -1
  37. package/node_modules/@modelcontextprotocol/client/dist/index.d.mts.map +1 -1
  38. package/node_modules/@modelcontextprotocol/client/dist/index.mjs +100 -55
  39. package/node_modules/@modelcontextprotocol/client/dist/shimsWorkerd.cjs +1 -1
  40. package/node_modules/@modelcontextprotocol/client/dist/shimsWorkerd.mjs +1 -1
  41. package/node_modules/@modelcontextprotocol/client/dist/{src-D0qEN74R.cjs → src-oZBjf3U1.cjs} +21 -4
  42. package/node_modules/@modelcontextprotocol/client/dist/{src-DDAAhLnO.mjs → src-xKF0PJN-.mjs} +16 -5
  43. package/node_modules/@modelcontextprotocol/client/dist/stdio.cjs +1 -1
  44. package/node_modules/@modelcontextprotocol/client/dist/stdio.mjs +1 -1
  45. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/dist/{auth--n_Yzytt.d.cts.map → auth-Anm-lwWi.d.mts.map} +1 -1
  46. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/dist/{auth-CGP0BDVq.mjs → auth-BNDyLTqp.mjs} +5 -3
  47. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/dist/{auth-4-zilcqg.cjs → auth-D1CeV_Jr.cjs} +5 -3
  48. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/dist/{auth-YUQV3RRv.d.mts.map → auth-V9XJSCfa.d.cts.map} +1 -1
  49. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/dist/index.cjs +1 -1
  50. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/dist/index.mjs +1 -1
  51. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/dist/internal.cjs +1 -1
  52. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/dist/internal.mjs +1 -1
  53. package/node_modules/@modelcontextprotocol/client/node_modules/@modelcontextprotocol/core/package.json +1 -1
  54. package/node_modules/@modelcontextprotocol/client/package.json +3 -3
  55. package/package.json +1 -1
  56. package/node_modules/@modelcontextprotocol/client/dist/index-BCtJi0p5.d.mts.map +0 -1
  57. package/node_modules/@modelcontextprotocol/client/dist/index-CEk3qkZM.d.cts.map +0 -1
@@ -0,0 +1,546 @@
1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+ import { ARCHIVE_RETENTION_DAY_MS, ARCHIVE_RETENTION_HOLD_MS, archiveRetentionClockStart, archiveRetentionDeadline, archiveRetentionGapThreshold, } from '@maka/core/archive-retention';
20
+ import { generalizedErrorMessage } from '@maka/core/redaction';
21
+ import { sessionRevisionFamilyId } from '@maka/core/session';
22
+ import { RuntimePolicyStoreError } from '@maka/storage/runtime-policy-stores';
23
+ /** Revision families a sweep tick may delete. */
24
+ export const ARCHIVE_RETENTION_FAMILIES_PER_TICK = 8;
25
+ const CANDIDATE_PAGE = 64;
26
+ const DISABLED = Object.freeze({
27
+ version: 1,
28
+ revision: 0,
29
+ enabled: false,
30
+ days: 30,
31
+ });
32
+ /**
33
+ * The opt-in retention for archived tasks (#5899). One Host document holds the
34
+ * setting and the latest results; the `storage.retention.*` operations are the
35
+ * only writer of the setting, and a maintenance lane runs the sweep.
36
+ *
37
+ * A sweep deletes nothing before `enabledAt + days`, pauses while the wall
38
+ * clock reads earlier than a time the Host has already seen, and deletes each
39
+ * family through `session.remove`'s own path with a guard that rechecks the
40
+ * policy, the archive and pin state, the elapsed time and the plan under the
41
+ * removal admission. A setting change waits for the family in flight, so once
42
+ * `storage.retention.set` answers, no deletion admitted under the old setting
43
+ * is still running.
44
+ */
45
+ export class HostArchiveRetentionCoordinator {
46
+ handlers = {
47
+ 'storage.retention.query': () => this.#query(),
48
+ 'storage.retention.set': (input) => this.#set(input),
49
+ };
50
+ #store;
51
+ #catalog;
52
+ #retirement;
53
+ #now;
54
+ #log;
55
+ #document;
56
+ #loading;
57
+ /** Serializes every read-modify-write of the document. */
58
+ #writes = Promise.resolve();
59
+ /** Setting changes in flight; while one is pending a sweep admits and starts nothing. */
60
+ #changing = 0;
61
+ /** The sweep step running now, which a setting change waits for. */
62
+ #ticking;
63
+ /**
64
+ * The latest Host time observed, in memory only. After a restart the floor
65
+ * is `enabledAt`, the last sweep's time and, on every sweep, the newest time
66
+ * Session metadata recorded.
67
+ */
68
+ #observedAt = 0;
69
+ /** Whether `#observedAt` is a time this process saw, rather than the persisted floor. */
70
+ #observedThisRun = false;
71
+ #pass;
72
+ #draining = false;
73
+ constructor(options) {
74
+ this.#store = options.document;
75
+ this.#catalog = options.catalog;
76
+ this.#retirement = options.retirement;
77
+ this.#now = options.now ?? Date.now;
78
+ this.#log = options.log ?? ((message) => console.info(`[runtime-host] ${message}`));
79
+ }
80
+ beginDrain() {
81
+ this.#draining = true;
82
+ }
83
+ /** One bounded sweep step; true while candidates remain. */
84
+ async sweep() {
85
+ if (this.#draining)
86
+ return false;
87
+ // A setting change is waiting for this lane; look again shortly.
88
+ if (this.#changing > 0)
89
+ return true;
90
+ const tick = this.#tick();
91
+ this.#ticking = tick;
92
+ try {
93
+ return await tick;
94
+ }
95
+ finally {
96
+ if (this.#ticking === tick)
97
+ this.#ticking = undefined;
98
+ }
99
+ }
100
+ async #tick() {
101
+ const document = await this.#load();
102
+ if (!document.enabled || document.enabledAt === undefined) {
103
+ this.#pass = undefined;
104
+ return false;
105
+ }
106
+ const setting = {
107
+ revision: document.revision,
108
+ days: document.days,
109
+ enabledAt: document.enabledAt,
110
+ };
111
+ const now = this.#now();
112
+ const previous = this.#observedAt;
113
+ const observedThisRun = this.#observedThisRun;
114
+ this.#observedAt = Math.max(previous, now);
115
+ this.#observedThisRun = true;
116
+ const deadline = archiveRetentionDeadline(setting.enabledAt, setting.days);
117
+ const gap = archiveRetentionGapThreshold(setting.days);
118
+ // A hold ends when its day is over, before or after the deadline alike.
119
+ let hold = document.latest?.hold;
120
+ let heartbeatCleared = false;
121
+ if (hold && now >= hold.until) {
122
+ heartbeatCleared = this.#heartbeatDue(document, now);
123
+ await this.#clearHold(heartbeatCleared ? now : undefined);
124
+ hold = undefined;
125
+ }
126
+ // A forward jump is caught even when it lands short of the deadline. A
127
+ // running Host measures from what it saw; a fresh process from the
128
+ // persisted floor and the newest metadata time, which says when the Host
129
+ // last ran: one MAX query, once per process.
130
+ const since = observedThisRun
131
+ ? previous
132
+ : Math.max(previous, (await this.#catalog.readLatestSessionMetadataTime()) ?? 0);
133
+ if (now - since > gap)
134
+ return this.#hold(since, now);
135
+ // A Host can run for weeks without writing session metadata. Persist a
136
+ // coarse heartbeat so a later restart can tell that idle time was spent
137
+ // while the Host was running, rather than treating it as a clock jump.
138
+ const heartbeat = !heartbeatCleared && now >= previous && this.#heartbeatDue(this.#document ?? document, now);
139
+ // Nothing can be eligible before the policy itself is `days` old, and no
140
+ // candidate is read before then.
141
+ if (now <= deadline) {
142
+ if (heartbeat)
143
+ await this.#heartbeat(now);
144
+ return false;
145
+ }
146
+ if (now < previous)
147
+ return this.#pause(now, heartbeat);
148
+ const newest = await this.#catalog.readLatestSessionMetadataTime();
149
+ if (newest !== undefined && now < newest)
150
+ return this.#pause(now, heartbeat);
151
+ if (hold)
152
+ return false;
153
+ if (this.#pass?.revision !== setting.revision) {
154
+ this.#pass = {
155
+ revision: setting.revision,
156
+ seen: new Set(),
157
+ deleted: 0,
158
+ skippedBusy: 0,
159
+ needsReview: 0,
160
+ failed: 0,
161
+ bytes: 0,
162
+ };
163
+ }
164
+ const pass = this.#pass;
165
+ const page = await this.#catalog.listArchiveRetentionCandidates({
166
+ archivedBefore: now - setting.days * ARCHIVE_RETENTION_DAY_MS,
167
+ ...(pass.cursor ? { after: pass.cursor } : {}),
168
+ limit: CANDIDATE_PAGE,
169
+ });
170
+ const deletedBefore = pass.deleted;
171
+ let tried = 0;
172
+ let stopped = false;
173
+ for (const row of page) {
174
+ // Draining or a pending setting change: stop before the next family.
175
+ if (this.#draining || this.#changing > 0) {
176
+ stopped = true;
177
+ break;
178
+ }
179
+ if ('undecodable' in row) {
180
+ // A row that no longer decodes is counted once and passed over.
181
+ if (!pass.seen.has(`row:${row.sessionId}`)) {
182
+ pass.seen.add(`row:${row.sessionId}`);
183
+ pass.failed += 1;
184
+ }
185
+ }
186
+ else {
187
+ const family = sessionRevisionFamilyId(row.header);
188
+ if (!pass.seen.has(family)) {
189
+ if (tried === ARCHIVE_RETENTION_FAMILIES_PER_TICK) {
190
+ stopped = true;
191
+ break;
192
+ }
193
+ tried += 1;
194
+ pass.seen.add(family);
195
+ tally(pass, await this.#retirement.removeForRetention({ sessionId: row.header.id, expectedRevision: row.revision }, (plan) => this.#guard(plan, setting)));
196
+ }
197
+ }
198
+ const sessionId = 'undecodable' in row ? row.sessionId : row.header.id;
199
+ pass.cursor = {
200
+ ...(row.archivedAt === undefined ? {} : { archivedAt: row.archivedAt }),
201
+ sessionId,
202
+ };
203
+ }
204
+ if (stopped || page.length === CANDIDATE_PAGE) {
205
+ // A deletion is recorded as it happens, not only when the pass ends.
206
+ if (pass.deleted > deletedBefore)
207
+ await this.#record(now, pass, heartbeat);
208
+ return !this.#draining;
209
+ }
210
+ if (this.#pass === pass)
211
+ this.#pass = undefined;
212
+ await this.#finishPass(now, pass, heartbeat);
213
+ return false;
214
+ }
215
+ /**
216
+ * Runs under the removal admission, after the plan is stable and before any
217
+ * retirement work: anything that changed since the sweep read the task keeps
218
+ * it for this pass. Every setting change moves the revision.
219
+ */
220
+ async #guard(plan, setting) {
221
+ if (this.#changing > 0 || this.#document?.revision !== setting.revision) {
222
+ return 'ineligible';
223
+ }
224
+ if (plan.remove.some(({ header }) => !header.isArchived || header.isFlagged)) {
225
+ return 'ineligible';
226
+ }
227
+ // The archive time through the reader `requireArchivedForMs` judges by.
228
+ const archivedAt = await Promise.all(plan.remove.map(async ({ header }) => (await this.#catalog.readCatalogRecord(header.id)).summary.archivedAt));
229
+ const now = this.#now();
230
+ if (now < this.#observedAt)
231
+ return 'ineligible';
232
+ // A family is as young as its most recently archived member.
233
+ const start = Math.max(...archivedAt.map((time) => archiveRetentionClockStart(time, setting.enabledAt)));
234
+ if (now <= archiveRetentionDeadline(start, setting.days))
235
+ return 'ineligible';
236
+ // Unattended cleanup is more conservative than a manual delete.
237
+ if (plan.archiveSessionIds.length > 0 || plan.worktreeCount > 0)
238
+ return 'needs_review';
239
+ return undefined;
240
+ }
241
+ /**
242
+ * The wall clock moved ahead further than a sweep expects, whether set wrong
243
+ * or after a long time offline: delete nothing for a day, so a wrong clock
244
+ * can be noticed and the setting turned off. A further jump re-arms it.
245
+ */
246
+ async #hold(since, now) {
247
+ this.#pass = undefined;
248
+ this.#log('archive retention held: the clock moved ahead further than a sweep expects');
249
+ await this.#update((document) => ({
250
+ ...document,
251
+ latest: {
252
+ ...document.latest,
253
+ hold: { since, detectedAt: now, until: now + ARCHIVE_RETENTION_HOLD_MS },
254
+ },
255
+ }));
256
+ return false;
257
+ }
258
+ #heartbeatDue(document, now) {
259
+ if (!document.enabled || document.enabledAt === undefined)
260
+ return false;
261
+ const previous = document.latest?.observedAt ?? document.enabledAt;
262
+ return now > previous && now - previous >= ARCHIVE_RETENTION_DAY_MS;
263
+ }
264
+ async #heartbeat(now) {
265
+ await this.#serialized(async () => {
266
+ const document = await this.#load();
267
+ if (!document.enabled || document.enabledAt === undefined)
268
+ return;
269
+ const previous = document.latest?.observedAt ?? document.enabledAt;
270
+ if (now <= previous || now - previous < ARCHIVE_RETENTION_DAY_MS)
271
+ return;
272
+ await this.#write({
273
+ ...document,
274
+ latest: { ...document.latest, observedAt: now },
275
+ });
276
+ });
277
+ }
278
+ #clearHold(observedAt) {
279
+ return this.#update(({ latest, ...rest }) => {
280
+ const { hold: _hold, ...kept } = latest ?? {};
281
+ return {
282
+ ...rest,
283
+ ...(observedAt === undefined ? {} : { latest: { ...kept, observedAt } }),
284
+ ...(observedAt === undefined && Object.keys(kept).length > 0 ? { latest: kept } : {}),
285
+ };
286
+ });
287
+ }
288
+ async #pause(now, heartbeat = false) {
289
+ this.#pass = undefined;
290
+ // Recorded once: a clock that stays behind writes nothing further.
291
+ if (this.#document?.latest?.lastSweep?.paused)
292
+ return false;
293
+ this.#log('archive retention paused: the clock reads earlier than a time already recorded');
294
+ await this.#update((document) => ({
295
+ ...document,
296
+ latest: {
297
+ ...document.latest,
298
+ ...(heartbeat ? { observedAt: now } : {}),
299
+ lastSweep: { at: now, deleted: 0, skippedBusy: 0, needsReview: 0, failed: 0, paused: true },
300
+ },
301
+ }));
302
+ return false;
303
+ }
304
+ async #finishPass(now, pass, heartbeat) {
305
+ const previous = this.#document?.latest?.lastSweep;
306
+ const unchanged = pass.deleted === 0 &&
307
+ pass.skippedBusy === (previous?.skippedBusy ?? 0) &&
308
+ pass.needsReview === (previous?.needsReview ?? 0) &&
309
+ pass.failed === (previous?.failed ?? 0) &&
310
+ previous?.paused !== true;
311
+ if (unchanged) {
312
+ // A heartbeat alone does not change the latest cleanup result.
313
+ if (heartbeat)
314
+ await this.#heartbeat(now);
315
+ return;
316
+ }
317
+ if (pass.deleted + pass.skippedBusy + pass.needsReview + pass.failed > 0) {
318
+ this.#log(`archive retention deleted ${pass.deleted} tasks; kept ${pass.skippedBusy} busy and ` +
319
+ `${pass.needsReview} for review; ${pass.failed} failed`);
320
+ }
321
+ await this.#record(now, pass, heartbeat);
322
+ }
323
+ #record(now, pass, heartbeat = false) {
324
+ const lastSweep = {
325
+ at: now,
326
+ deleted: pass.deleted,
327
+ skippedBusy: pass.skippedBusy,
328
+ needsReview: pass.needsReview,
329
+ failed: pass.failed,
330
+ };
331
+ const lastDeletion = pass.deleted > 0
332
+ ? {
333
+ at: now,
334
+ count: pass.deleted,
335
+ ...(pass.bytes === undefined ? {} : { bytes: pass.bytes }),
336
+ }
337
+ : undefined;
338
+ return this.#serialized(async () => {
339
+ const document = await this.#load();
340
+ // A pass belongs to the setting it started under; a newer one starts afresh.
341
+ if (document.revision !== pass.revision)
342
+ return;
343
+ await this.#write({
344
+ ...document,
345
+ latest: {
346
+ ...document.latest,
347
+ ...(heartbeat ? { observedAt: now } : {}),
348
+ lastSweep,
349
+ ...(lastDeletion ? { lastDeletion } : {}),
350
+ },
351
+ });
352
+ });
353
+ }
354
+ async #query() {
355
+ if (this.#draining)
356
+ return draining();
357
+ try {
358
+ const document = await this.#load();
359
+ const enabledAt = document.enabled ? document.enabledAt : undefined;
360
+ const { families, firstStart } = await this.#catalog.countArchiveRetentionCandidates(enabledAt ?? this.#now());
361
+ return {
362
+ ok: true,
363
+ result: {
364
+ ...settingOf(document),
365
+ preview: {
366
+ count: families,
367
+ ...(enabledAt !== undefined && families > 0 && firstStart !== undefined
368
+ ? { eligibleAt: archiveRetentionDeadline(firstStart, document.days) }
369
+ : {}),
370
+ },
371
+ ...(document.latest?.lastSweep ? { lastSweep: document.latest.lastSweep } : {}),
372
+ ...(document.latest?.lastDeletion ? { lastDeletion: document.latest.lastDeletion } : {}),
373
+ // A hold whose day is over is not reported, cleared or not.
374
+ ...(document.latest?.hold && this.#now() < document.latest.hold.until
375
+ ? { hold: document.latest.hold }
376
+ : {}),
377
+ },
378
+ };
379
+ }
380
+ catch (error) {
381
+ reportFailure('read', error);
382
+ return {
383
+ ok: false,
384
+ error: { code: 'persistence_failed', message: 'Retention setting could not be read' },
385
+ };
386
+ }
387
+ }
388
+ async #set(input) {
389
+ if (this.#draining)
390
+ return draining();
391
+ this.#changing += 1;
392
+ try {
393
+ // A family admitted under the current setting finishes, and is recorded,
394
+ // before the setting changes; nothing new is admitted meanwhile.
395
+ await this.#ticking?.catch(() => undefined);
396
+ return await this.#serialized(async () => {
397
+ const current = await this.#load();
398
+ if (current.revision !== input.expectedRevision) {
399
+ return {
400
+ ok: true,
401
+ result: {
402
+ kind: 'revision_conflict',
403
+ expectedRevision: input.expectedRevision,
404
+ actualRevision: current.revision,
405
+ },
406
+ };
407
+ }
408
+ if (current.enabled === input.enabled && current.days === input.days) {
409
+ return { ok: true, result: { kind: 'committed', setting: settingOf(current) } };
410
+ }
411
+ // Any change restarts the clock: enabling, or new days while enabled.
412
+ // A clock behind a time already recorded never backdates the deadline.
413
+ const newest = input.enabled
414
+ ? await this.#catalog.readLatestSessionMetadataTime()
415
+ : undefined;
416
+ const now = this.#now();
417
+ const enabledAt = Math.max(now, this.#observedAt, newest ?? 0);
418
+ this.#observedAt = Math.max(this.#observedAt, now);
419
+ this.#observedThisRun = true;
420
+ const { enabledAt: _previous, latest, ...rest } = current;
421
+ const lastSweep = latest?.lastSweep && withoutPause(latest.lastSweep);
422
+ const nextLatest = {
423
+ ...(input.enabled ? { observedAt: now } : {}),
424
+ ...(lastSweep ? { lastSweep } : {}),
425
+ ...(latest?.lastDeletion ? { lastDeletion: latest.lastDeletion } : {}),
426
+ };
427
+ const next = {
428
+ ...rest,
429
+ revision: current.revision + 1,
430
+ enabled: input.enabled,
431
+ days: input.days,
432
+ ...(input.enabled ? { enabledAt } : {}),
433
+ ...(Object.keys(nextLatest).length > 0 ? { latest: nextLatest } : {}),
434
+ };
435
+ await this.#write(next);
436
+ this.#pass = undefined;
437
+ return { ok: true, result: { kind: 'committed', setting: settingOf(next) } };
438
+ });
439
+ }
440
+ catch (error) {
441
+ if (error instanceof RuntimePolicyStoreError && error.code === 'commit_outcome_unknown') {
442
+ return {
443
+ ok: false,
444
+ error: {
445
+ code: 'commit_outcome_unknown',
446
+ message: 'Retention setting commit outcome is unknown',
447
+ },
448
+ };
449
+ }
450
+ reportFailure('save', error);
451
+ return {
452
+ ok: false,
453
+ error: { code: 'persistence_failed', message: 'Retention setting could not be saved' },
454
+ };
455
+ }
456
+ finally {
457
+ this.#changing -= 1;
458
+ }
459
+ }
460
+ #update(change) {
461
+ return this.#serialized(async () => this.#write(change(await this.#load())));
462
+ }
463
+ async #write(next) {
464
+ try {
465
+ await this.#store.write(next);
466
+ }
467
+ catch (error) {
468
+ // The file may or may not hold `next`; read it again before trusting either.
469
+ this.#document = undefined;
470
+ throw error;
471
+ }
472
+ this.#document = next;
473
+ }
474
+ #serialized(operation) {
475
+ const run = this.#writes.then(operation, operation);
476
+ this.#writes = run.catch(() => undefined);
477
+ return run;
478
+ }
479
+ #load() {
480
+ if (this.#document)
481
+ return Promise.resolve(this.#document);
482
+ this.#loading ??= this.#read().finally(() => {
483
+ this.#loading = undefined;
484
+ });
485
+ return this.#loading;
486
+ }
487
+ async #read() {
488
+ const read = await this.#store.read();
489
+ let document;
490
+ if (read.kind === 'valid') {
491
+ document = read.document;
492
+ }
493
+ else {
494
+ // Never act on a document that cannot be fully validated: off, until
495
+ // the setting is written again.
496
+ if (read.kind === 'invalid') {
497
+ this.#log('archive retention is off: its setting document could not be read');
498
+ }
499
+ document = DISABLED;
500
+ }
501
+ this.#observedAt = Math.max(this.#observedAt, document.enabledAt ?? 0, document.latest?.observedAt ?? 0, document.latest?.lastSweep?.at ?? 0, document.latest?.hold?.detectedAt ?? 0);
502
+ this.#document = document;
503
+ return document;
504
+ }
505
+ }
506
+ function withoutPause(sweep) {
507
+ const { paused: _paused, ...rest } = sweep;
508
+ return rest;
509
+ }
510
+ function settingOf(document) {
511
+ return {
512
+ revision: document.revision,
513
+ enabled: document.enabled,
514
+ days: document.days,
515
+ ...(document.enabledAt === undefined ? {} : { enabledAt: document.enabledAt }),
516
+ };
517
+ }
518
+ function tally(pass, outcome) {
519
+ switch (outcome.kind) {
520
+ case 'removed':
521
+ pass.deleted += 1;
522
+ pass.bytes =
523
+ pass.bytes === undefined || outcome.bytes === undefined
524
+ ? undefined
525
+ : pass.bytes + outcome.bytes;
526
+ return;
527
+ case 'held':
528
+ if (outcome.reason === 'needs_review')
529
+ pass.needsReview += 1;
530
+ return;
531
+ case 'busy':
532
+ pass.skippedBusy += 1;
533
+ return;
534
+ case 'failed':
535
+ pass.failed += 1;
536
+ return;
537
+ case 'skipped':
538
+ return;
539
+ }
540
+ }
541
+ function reportFailure(action, error) {
542
+ console.error(`[runtime-host] archive retention could not ${action} its setting: ${generalizedErrorMessage(error)}`);
543
+ }
544
+ function draining() {
545
+ return { ok: false, error: { code: 'host_draining', message: 'Runtime Host is draining' } };
546
+ }
@@ -81,7 +81,7 @@ export class HostConnectionEffectCoordinator {
81
81
  source: 'fetched',
82
82
  fetchedAt: this.#now(),
83
83
  };
84
- const completion = await this.#complete(() => this.#stores.operations.completeModelFetch(prepared.ticket, result));
84
+ const completion = await this.#complete(() => this.#stores.operations.completeModelFetch(prepared.ticket, result, input.preserveSelection));
85
85
  return completion.kind === 'committed'
86
86
  ? {
87
87
  kind: 'committed',
@@ -155,6 +155,7 @@ import { HostWorkspaceResolver } from './workspace-resolver.js';
155
155
  import { HostSessionRetirementCoordinator } from './session-retirement-coordinator.js';
156
156
  import { HostStorageMaintenance } from './storage-maintenance.js';
157
157
  import { HostStorageUsageCoordinator } from './storage-usage-coordinator.js';
158
+ import { HostArchiveRetentionCoordinator } from './archive-retention-coordinator.js';
158
159
  import { HostSessionRevisionCoordinator } from './session-revision-coordinator.js';
159
160
  import { HostSessionEffectCoordinator } from './session-effect-coordinator.js';
160
161
  import { SessionContinuityCoordinator } from './session-continuity-coordinator.js';
@@ -2310,9 +2311,15 @@ export async function createExecutionRuntimeHostComposition(context, options = {
2310
2311
  const hostedExecutions = new HostHostedExecutionCoordinator((input, signal) => hostedExecutionRunner.run(input, signal), context.requestDrain);
2311
2312
  let recoverySessions = [];
2312
2313
  const storageUsage = new HostStorageUsageCoordinator({ footprint: storage.footprint });
2314
+ const archiveRetention = new HostArchiveRetentionCoordinator({
2315
+ document: storage.archiveRetention,
2316
+ catalog: stores.sessionStore,
2317
+ retirement: sessionRetirement,
2318
+ });
2313
2319
  const storageMaintenance = new HostStorageMaintenance({
2314
2320
  artifacts: openedArtifactStore,
2315
2321
  contextOffload: openedContextOffloadStore,
2322
+ retention: archiveRetention,
2316
2323
  onError: (name, error) => console.error(`[runtime-host] ${name} will retry: ${generalizedErrorMessage(error)}`),
2317
2324
  });
2318
2325
  domainModules = [
@@ -2326,6 +2333,11 @@ export async function createExecutionRuntimeHostComposition(context, options = {
2326
2333
  handlers: [storageUsage.handlers],
2327
2334
  drain: [() => storageUsage.beginDrain()],
2328
2335
  }),
2336
+ createRuntimeHostDomainModule({
2337
+ id: 'archive-retention',
2338
+ handlers: [archiveRetention.handlers],
2339
+ drain: [() => archiveRetention.beginDrain()],
2340
+ }),
2329
2341
  createRuntimeHostDomainModule({
2330
2342
  id: 'plugin-platform',
2331
2343
  handlers: [pluginPlatformCoordinator.handlers],