ai_co_discuss 0.5.21 → 0.5.23

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 (85) hide show
  1. package/.agents/skills/codiscuss-draft-cleanup/SKILL.md +16 -0
  2. package/.agents/skills/codiscuss-draft-cleanup/cleanup.py +70 -0
  3. package/.agents/skills/codiscuss-draft-cleanup/scan.py +70 -0
  4. package/.agents/skills/discuss-host/references/control.md +2 -2
  5. package/.agents/skills/discuss-host/references/workflow.md +4 -2
  6. package/.agents/skills/discuss-participant/SKILL.md +2 -0
  7. package/.agents/skills/local-stt-setup/SKILL.md +9 -8
  8. package/.agents/skills/local-tts-setup/SKILL.md +10 -9
  9. package/.agents/skills/share-card/SKILL.md +7 -6
  10. package/.agents/skills/share-card/references/share-card.md +34 -8
  11. package/.agents/skills/workflow-creator/SKILL.md +22 -14
  12. package/README.md +2 -0
  13. package/README.zh-CN.md +2 -0
  14. package/backend/free_room.py +64 -8
  15. package/backend/message_queue.py +9 -0
  16. package/backend/schema.sql +13 -1
  17. package/backend/server.py +289 -59
  18. package/docs/en/guide/admin.md +18 -0
  19. package/docs/en/guide/members.md +7 -1
  20. package/docs/en/guide/remote.md +1 -1
  21. package/docs/en/guide/rooms.md +14 -6
  22. package/docs/en/guide/voice.md +2 -0
  23. package/docs/en/guide/workflows.md +9 -5
  24. package/docs/en/guide/working.md +6 -4
  25. package/docs/guide/admin.md +18 -0
  26. package/docs/guide/members.md +7 -1
  27. package/docs/guide/remote.md +1 -1
  28. package/docs/guide/rooms.md +13 -6
  29. package/docs/guide/voice.md +2 -0
  30. package/docs/guide/workflows.md +9 -5
  31. package/docs/guide/working.md +6 -4
  32. package/frontend/locales/en/admin.json +58 -1
  33. package/frontend/locales/en/app.json +18 -1
  34. package/frontend/locales/en/backend.json +6 -0
  35. package/frontend/locales/en/core.json +4 -2
  36. package/frontend/locales/en/discussion.json +4 -1
  37. package/frontend/locales/en/room.json +85 -1
  38. package/gateway/gateway.mjs +49 -9
  39. package/package.json +3 -1
  40. package/runner/account_space.mjs +10 -0
  41. package/runner/adapters/antigravity_app.mjs +31 -4
  42. package/runner/adapters/antigravity_cli.mjs +9 -2
  43. package/runner/adapters/api.mjs +3 -2
  44. package/runner/adapters/api_pi.mjs +3 -2
  45. package/runner/adapters/api_pi_extension.mjs +5 -3
  46. package/runner/adapters/claude.mjs +32 -9
  47. package/runner/adapters/cli.mjs +2 -2
  48. package/runner/adapters/codex_app.mjs +192 -69
  49. package/runner/adapters/codex_cli.mjs +10 -5
  50. package/runner/adapters/common.mjs +414 -11
  51. package/runner/adapters/index.mjs +6 -0
  52. package/runner/admin_cli.mjs +22 -5
  53. package/runner/admin_panel.mjs +118 -5
  54. package/runner/admin_storage.mjs +105 -0
  55. package/runner/discussion_launch.mjs +328 -128
  56. package/runner/discussion_runner.mjs +12 -3
  57. package/runner/discussion_service.mjs +224 -37
  58. package/runner/discussion_skills.mjs +1 -1
  59. package/runner/expert_prompts.mjs +2 -2
  60. package/runner/experts.mjs +227 -0
  61. package/runner/file_diff.mjs +1 -1
  62. package/runner/free_runner.mjs +236 -51
  63. package/runner/i18n_en.mjs +39 -0
  64. package/runner/os_users.mjs +33 -1
  65. package/runner/project_files.mjs +7 -1
  66. package/runner/project_migration.mjs +245 -0
  67. package/runner/relay_channel.mjs +1 -1
  68. package/runner/remote_launch.mjs +4 -3
  69. package/runner/share_card.mjs +17 -1
  70. package/runner/share_shared.mjs +26 -9
  71. package/runner/site_deploy.mjs +2 -1
  72. package/runner/slash_names.mjs +12 -1
  73. package/runner/speech_directories.mjs +20 -0
  74. package/runner/storage_guard.mjs +383 -0
  75. package/runner/tts_providers.mjs +22 -21
  76. package/runner/workflows.mjs +211 -57
  77. package/web/admin.css +1 -1
  78. package/web/admin.js +8 -8
  79. package/web/app.js +589 -80
  80. package/web/i18n.js +4 -4
  81. package/web/markdown.css +1 -1
  82. package/web/modern.css +1 -1
  83. package/web/modern.js +68 -66
  84. package/workflows/decision-council/flow.json +216 -25
  85. package/workflows/trpg-classic/flow.json +151 -21
@@ -11,6 +11,7 @@ import { spawn, spawnSync } from 'node:child_process';
11
11
  import { randomBytes } from 'node:crypto';
12
12
  import { closeSync, copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, realpathSync, readdirSync, readFileSync, readSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
13
13
  import http from 'node:http';
14
+ import { clean_storage, storage_details } from './admin_storage.mjs';
14
15
  import { homedir, tmpdir } from 'node:os';
15
16
  import tls from 'node:tls';
16
17
  import { dirname, extname, join, resolve } from 'node:path';
@@ -26,6 +27,9 @@ import { ensure_tunnel, start_tunnel_setup, tunnel_setup_state, tunnel_status }
26
27
  import { machine_links } from './remote_launch.mjs';
27
28
  import { read_relay_health, request_relay_reconnect } from './relay_health.mjs';
28
29
  import { instance_recovery } from './instance_recovery.mjs';
30
+ import { mark_backup_release, reserve_project_root, set_project_migration } from '../gateway/gateway.mjs';
31
+ import { backup_operation, migration_folders, move_project_files } from './project_migration.mjs';
32
+ import { require_idle_os_user } from './os_users.mjs';
29
33
  import { DIRECT_ACCOUNTS_FILE } from '../gateway/direct.mjs';
30
34
  import { apply_login, cancel_login, LOGIN_METHODS, login_progress, login_status, logout, start_login, submit_login } from './cli_login.mjs';
31
35
  import { clear_instance_flag, create_os_user, install_shared_runtime, restrict_other_drives, instance_flag_set, instance_key_file, place_instance_key, read_os_users, remove_os_user, run_as_user, shared_cli_state, shared_executables, shared_paths,
@@ -383,7 +387,7 @@ function instance_running(port) {
383
387
 
384
388
  async function list_accounts(data) {
385
389
  const users = read_os_users(data);
386
- return Promise.all(Object.entries(read_accounts(data)).map(async ([name, account]) => ({ name, port: account.port, home: account_home(data, name), disabled: Boolean(account.disabled), protected: account.protected === true,
390
+ return Promise.all(Object.entries(read_accounts(data)).map(async ([name, account]) => ({ name, port: account.port, home: account_home(data, name), project_data_root: account.project_data_root || '', disabled: Boolean(account.disabled), protected: account.protected === true,
387
391
  os_user: users[name]?.user || '', running: await instance_running(account.port), keys: list_keys(instance_home(data, name)).length,
388
392
  // The AI environment is set up once account_env.mjs has run for its Windows user (it records skills in the service home).
389
393
  skills: Boolean(users[name]) && existsSync(join(instance_home(data, name), SKILL_RECORD)),
@@ -573,6 +577,7 @@ export async function protect_account(data, name, on, { place = place_instance_k
573
577
  /** Start or stop an account's instance: as its Windows user from the shared copy when it has one, otherwise as the owner. */
574
578
  function run_instance(data, name, command) {
575
579
  const account = read_accounts(data)[name];
580
+ if (command === 'start' && account?.project_migration) throw new Error('请先完成数据迁移 / Finish the pending data migration first');
576
581
  if (!account) throw panel_error('账号不存在:{value1}', { value1: name });
577
582
  const service = instance_home(data, name);
578
583
  // A protected instance starts knowing its key file; it then serves only callers that prove the key.
@@ -602,6 +607,104 @@ function run_instance(data, name, command) {
602
607
  });
603
608
  }
604
609
 
610
+ /** Stop the account and complete or resume the move recorded in the owner-only registry. */
611
+ export async function migrate_account_data(data, name, requested, { stop = run_instance, reserve = reserve_project_root } = {}) {
612
+ const account = read_accounts(data)[name], service = instance_home(data, name);
613
+ let job = account.project_migration;
614
+ if (!requested || (job && resolve(requested) !== job.target)) throw new Error('请指定原迁移目标 / Specify the migration destination');
615
+ if (!job && account.project_data_root && resolve(requested) === account.project_data_root) return { project_data_root: account.project_data_root };
616
+ const user = read_os_users(data)[name]?.user || '';
617
+ if (!job) {
618
+ const target = reserve(data, name, requested, user);
619
+ job = { target, previous: account.project_data_root || '', id: randomBytes(8).toString('hex') };
620
+ set_project_migration(data, name, job);
621
+ }
622
+ try {
623
+ await stop(data, name, 'stop');
624
+ const state = local_status(service);
625
+ if (state.pid || state.service_pid || await instance_state(account.port) !== 'down') throw new Error('实例仍在运行 / Instance is still running');
626
+ if (user) require_idle_os_user(user);
627
+ } catch (error) {
628
+ if (!account.project_migration) set_project_migration(data, name, null);
629
+ throw error;
630
+ }
631
+ const request = { ...job, service };
632
+ let receipts;
633
+ try { receipts = await migration_worker(data, name, request, user); } catch (error) {
634
+ if (error.rolled_back) set_project_migration(data, name, null);
635
+ throw error;
636
+ }
637
+ const kinds = {};
638
+ for (const kind of ['workspace', 'projects', 'drafts']) {
639
+ if (!receipts[kind]) continue;
640
+ const path = receipt_path(data, job.id, kind);
641
+ mkdirSync(dirname(path), { recursive: true });
642
+ const temp = path + '.' + randomBytes(8).toString('hex');
643
+ writeFileSync(temp, JSON.stringify(receipts[kind]), { flag: 'wx', mode: 0o600 });
644
+ renameSync(temp, path);
645
+ kinds[kind] = 'ready';
646
+ }
647
+ set_project_migration(data, name, null, job.target, Object.keys(kinds).length ? { ...job, kinds } : null);
648
+ return { project_data_root: job.target, backup_suffix: '.before-' + job.id };
649
+ }
650
+
651
+ function receipt_path(data, id, kind) {
652
+ return join(data, 'project-migration-receipts', id + '-' + kind + '.json');
653
+ }
654
+
655
+ /** Report current bytes for each registered backup, or why its identity could not be verified. */
656
+ export async function list_migration_backups(data, name) {
657
+ const account = read_accounts(data)[name], rows = [], service = instance_home(data, name);
658
+ const user = read_os_users(data)[name]?.user;
659
+ for (const job of account.migration_backups || []) {
660
+ for (const folder of migration_folders({ ...job, service })) {
661
+ const kind = folder.name;
662
+ if (!job.kinds[kind]) continue;
663
+ const row = { id: job.id, kind, path: folder.backup, state: job.kinds[kind] };
664
+ try {
665
+ const record = JSON.parse(readFileSync(receipt_path(data, job.id, kind), 'utf8'));
666
+ Object.assign(row, await migration_worker(data, name, { ...job, service, kind, record, action: 'list', releasing: row.state === 'releasing' }, user));
667
+ } catch (error) { row.error = error.message; }
668
+ rows.push(row);
669
+ }
670
+ }
671
+ return rows;
672
+ }
673
+
674
+ /** Release one confirmed backup, keeping its junction and active target in place. */
675
+ export async function release_migration_backup(data, name, body, stop = run_instance) {
676
+ const account = read_accounts(data)[name], { id, kind } = body;
677
+ const job = account.migration_backups?.find(item => item.id === id);
678
+ if (body.confirm !== true || !job || !['projects', 'workspace', 'drafts'].includes(kind) || !job.kinds[kind]) {
679
+ throw new Error('请选择已登记的备份并确认 / Select and confirm a registered backup');
680
+ }
681
+ const service = instance_home(data, name), user = read_os_users(data)[name]?.user;
682
+ const record = JSON.parse(readFileSync(receipt_path(data, id, kind), 'utf8'));
683
+ await stop(data, name, 'stop');
684
+ const state = local_status(service);
685
+ if (state.pid || state.service_pid || await instance_state(account.port) !== 'down') throw new Error('实例仍在运行 / Instance is still running');
686
+ if (user) require_idle_os_user(user);
687
+ const request = { ...job, kind, service, record, releasing: job.kinds[kind] === 'releasing' };
688
+ await migration_worker(data, name, { ...request, action: 'verify' }, user);
689
+ mark_backup_release(data, name, id, kind, false);
690
+ await migration_worker(data, name, { ...request, releasing: true, action: 'release' }, user);
691
+ mark_backup_release(data, name, id, kind, true);
692
+ rmSync(receipt_path(data, id, kind), { force: true });
693
+ return { released: kind, id };
694
+ }
695
+
696
+ async function migration_worker(data, name, request, user) {
697
+ if (user) {
698
+ const code = readFileSync(new URL('./project_migration.mjs', import.meta.url), 'utf8');
699
+ const output = run_as_user(data, name, `& ${ps_quote(process.execPath)} (Join-Path $Job 'move.mjs') (Join-Path $Job 'request.json')`,
700
+ { files: { 'move.mjs': code, 'request.json': JSON.stringify(request) }, timeout: 3600_000 });
701
+ const reply = JSON.parse(output);
702
+ if (!reply.result) throw Object.assign(new Error(reply.error || 'Migration incomplete'), { rolled_back: reply.rolled_back === true });
703
+ return reply.result;
704
+ }
705
+ return request.action ? backup_operation(request) : move_project_files(request);
706
+ }
707
+
605
708
  // ---- API keys an account's instance holds ----
606
709
 
607
710
  const KEY_FILE = /^[a-f0-9-]{36}\.json$/i;
@@ -674,7 +777,7 @@ function wanted_port(data, name, flag_set) {
674
777
  const job = update_job(data);
675
778
  if (job && job.status !== 'done' && Date.now() - job.started_at < UPDATE_STALE_MS) return;
676
779
  const account = read_accounts(data)[name];
677
- if (!account || account.disabled) return;
780
+ if (!account || account.disabled || account.project_migration) return;
678
781
  if (read_os_users(data)[name]) return flag_set(name) ? account.port : undefined;
679
782
  if (account.instance_wanted === undefined && local_status(instance_home(data, name)).website_running) {
680
783
  set_instance_wanted(data, name, true);
@@ -709,6 +812,8 @@ export function create_panel({ data, key, port, ops = {}, sessions = new Set() }
709
812
  // Accounts follow the way external members can reach them, in the background (#5660).
710
813
  const after_change = () => { op.reconcile_external(data).catch(() => {}); };
711
814
  const hosting = hosting_enabled(data);
815
+ if (path === '/api/storage' && method === 'GET') return storage_details(data);
816
+ if (path === '/api/storage/clean' && method === 'POST') return clean_storage(data, body);
712
817
  if (method === 'GET' && section === 'state') {
713
818
  const windows = process.platform === 'win32';
714
819
  const service = op.service_overview(data), bound = bound_site(data);
@@ -792,7 +897,9 @@ export function create_panel({ data, key, port, ops = {}, sessions = new Set() }
792
897
  if (method === 'POST' && section === 'clis' && name === 'install') return op.install_runtime(data);
793
898
  // Hosted accounts' Windows users kept off every drive but the system drive; what they run must already lie there.
794
899
  if (method === 'POST' && section === 'drives' && name === 'restrict') {
795
- return op.restrict_other_drives(data, Object.keys(read_os_users(data)).filter(account => read_accounts(data)[account]).map(account => account_home(data, account)));
900
+ const accounts = read_accounts(data);
901
+ return op.restrict_other_drives(data, Object.entries(accounts).flatMap(([id, account]) =>
902
+ [account_home(data, id), account.project_data_root, account.project_migration?.target].filter(Boolean)));
796
903
  }
797
904
  // The account entry customers sign in at: started and stopped here, and started again with the panel if it was left on.
798
905
  if (method === 'POST' && section === 'entry' && (name === 'start' || name === 'stop')) {
@@ -825,8 +932,14 @@ export function create_panel({ data, key, port, ops = {}, sessions = new Set() }
825
932
  if (method === 'POST') { save_hosted_prompt(data, body.text); return { ok: true }; }
826
933
  }
827
934
  if (section !== 'accounts') throw panel_error('接口不存在');
828
- if (method === 'POST' && !name) { const added = await op.add_account(data, String(body.name || '').trim()); after_change(); return added; }
935
+ if (method === 'POST' && !name) { const added = await op.add_account(data, String(body.name || '').trim(), undefined, body.project_data_root); after_change(); return added; }
829
936
  account();
937
+ if (method === 'POST' && action === 'migrate-data') return recovery.manage(name, action,
938
+ () => migrate_account_data(data, name, body.project_data_root, { stop: op.run_instance }));
939
+ if (method === 'POST' && read_accounts(data)[name].project_migration && action !== 'stop') throw new Error('请先完成数据迁移 / Finish the pending data migration first');
940
+ if (method === 'GET' && action === 'migration-backups') return list_migration_backups(data, name);
941
+ if (method === 'POST' && action === 'release-backup') return recovery.manage(name, action,
942
+ () => release_migration_backup(data, name, body, op.run_instance));
830
943
  if (method === 'POST' && action === 'password') return { password: op.reset_password(data, name) };
831
944
  if (method === 'POST' && action === 'protect') return recovery.manage(name, action, () => op.protect_account(data, name, body.on !== false));
832
945
  if (method === 'POST' && (action === 'disable' || action === 'enable')) { op.set_account_disabled(data, name, action === 'disable'); return { disabled: action === 'disable' }; }
@@ -847,7 +960,7 @@ export function create_panel({ data, key, port, ops = {}, sessions = new Set() }
847
960
  if (method === 'POST' && action === 'os-user') await change_external(data, name, false);
848
961
  if (method === 'POST' && action === 'os-user' && extra === 'remove') { set_instance_wanted(data, name, false); op.remove_os_user(data, name); after_change(); return { ok: true }; }
849
962
  if (method === 'POST' && action === 'os-user') {
850
- const result = op.create_os_user(data, name, account_home(data, name));
963
+ const result = op.create_os_user(data, name, account_home(data, name), { project_data_root: read_accounts(data)[name].project_data_root });
851
964
  op.use_shared_programs(data, name);
852
965
  after_change();
853
966
  return result;
@@ -0,0 +1,105 @@
1
+ /** Storage details and explicit scratch cleanup for the computer's administrator. */
2
+ import { existsSync, readFileSync } from 'node:fs';
3
+ import { basename, dirname, join, resolve } from 'node:path';
4
+ import { drafts_root } from './account_space.mjs';
5
+ import { DatabaseSync } from 'node:sqlite';
6
+ import { service_drafts } from './discussion_runner.mjs';
7
+ import { read_registry } from './discussion_service.mjs';
8
+ import { t } from './i18n.mjs';
9
+ import { member_drafts, reclaim_failed_scratch } from './free_runner.mjs';
10
+ import { speech_model_directories } from './speech_directories.mjs';
11
+ import { storage_disk, measure, DRAFT_LIMIT, HOME_LIMIT, HOME_WARN, LOG_TAIL, MAX_SCAN_FILES,
12
+ ROOM_DRAFT_LIMIT, SCRATCH_PRESSURE_RATIO } from './storage_guard.mjs';
13
+
14
+ function registered_rooms(home) {
15
+ const rooms = new Map();
16
+ let unreadable = 0;
17
+ for (const path of read_registry(home)) {
18
+ try {
19
+ const config = JSON.parse(readFileSync(path, 'utf8'));
20
+ const directory = resolve(config.data_directory || join(dirname(path), 'discussion-' + config.room));
21
+ const draft_directory = service_drafts(home, directory);
22
+ const members = new Map(Object.keys(config.members || config.participants || {}).map(name =>
23
+ [basename(member_drafts({ draft_directory }, name)), config.participants?.[name]?.label || name]));
24
+ rooms.set(basename(draft_directory), { room_id: config.room, site: config.site, directory, draft_directory, members });
25
+ } catch { unreadable++; }
26
+ }
27
+ return { rooms, unreadable };
28
+ }
29
+
30
+ /** Index only durable cleanup receipts. Directory names alone never establish eligibility. */
31
+ function candidates(registry) {
32
+ const paths = new Map();
33
+ for (const config of registry.rooms.values()) {
34
+ const database = join(config.directory, 'runtime.sqlite3');
35
+ if (!existsSync(database)) continue;
36
+ let db;
37
+ try {
38
+ db = new DatabaseSync(database, { readOnly: true });
39
+ for (const row of db.prepare("SELECT key,value FROM metadata WHERE key LIKE 'failed-scratch:%'").all()) {
40
+ const { member, id } = JSON.parse(row.value);
41
+ if (typeof member !== 'string' || !Number.isSafeInteger(id) || id <= 0) continue;
42
+ const directory = member_drafts(config, member);
43
+ paths.set(join(directory, '.scratch', String(id)), { key: row.key, config, member: basename(directory), bytes: 0 });
44
+ }
45
+ } catch { registry.unreadable++; }
46
+ finally { db?.close(); }
47
+ }
48
+ return paths;
49
+ }
50
+
51
+ /** Report logical byte counts, including unmatched historic room/member keys without reading any draft content. */
52
+ export async function storage_details(home) {
53
+ const registry = registered_rooms(home);
54
+ const locations = speech_model_directories(home);
55
+ const scratch = candidates(registry);
56
+ const drafts = drafts_root(home);
57
+ const snapshot = await measure(home, { members: true, drafts_root: drafts, scratch, exclude_paths: locations.map(item => item.path) });
58
+ for (const item of scratch.values()) {
59
+ const usage = item.config.cleanable ||= new Map();
60
+ usage.set(item.member, (usage.get(item.member) || 0) + item.bytes);
61
+ }
62
+ const models = [];
63
+ for (const location of locations) {
64
+ const result = await measure(location.path, { model_dirs: [] });
65
+ models.push({ ...location, name: location.program, bytes: result.total_bytes, truncated: result.truncated, unreadable: result.unreadable });
66
+ }
67
+ const rooms = [...new Set([...registry.rooms.keys(), ...Object.keys(snapshot.rooms)])].map(key => {
68
+ const config = registry.rooms.get(key);
69
+ const totals = snapshot.members?.[key] || {};
70
+ const members = [...new Set([...(config?.members.keys() || []), ...Object.keys(totals)])].map(key =>
71
+ ({ key, name: config?.members.get(key) || key, bytes: totals[key] || 0, scratch_bytes: config?.cleanable?.get(key) || 0 }));
72
+ return { key, room_id: config?.room_id || '', site: config?.site || '', bytes: snapshot.rooms[key] || 0,
73
+ scratch_bytes: members.reduce((sum, member) => sum + member.scratch_bytes, 0), members };
74
+ });
75
+ return { total_bytes: snapshot.total_bytes, draft_bytes: snapshot.draft_bytes, local_bytes: snapshot.local_bytes || 0, rooms, models,
76
+ model_bytes: models.reduce((sum, model) => sum + model.bytes, 0), disk: storage_disk(home, drafts),
77
+ limits: { home_limit_bytes: HOME_LIMIT, home_warn_bytes: HOME_WARN, draft_bytes: DRAFT_LIMIT, room_draft_bytes: ROOM_DRAFT_LIMIT,
78
+ warn_ratio: SCRATCH_PRESSURE_RATIO, log_bytes: LOG_TAIL, max_scan_files: MAX_SCAN_FILES },
79
+ unreadable: snapshot.unreadable + registry.unreadable + models.reduce((sum, model) => sum + model.unreadable, 0),
80
+ truncated: snapshot.truncated || models.some(model => model.truncated) };
81
+ }
82
+
83
+ /** Drain the same confirmed-ended records as pressure cleanup. Never infer eligibility from an on-disk name. */
84
+ export async function clean_storage(home, filter = {}) {
85
+ const registry = registered_rooms(home);
86
+ if (!filter || typeof filter !== 'object' || Array.isArray(filter) ||
87
+ Object.entries(filter).some(([key, value]) => !['room_id', 'room_key'].includes(key) || typeof value !== 'string' || !value))
88
+ throw new Error(t('存储清理范围无效'));
89
+ for (const [key, config] of registry.rooms) {
90
+ if ((filter.room_key && filter.room_key !== key) || (filter.room_id && filter.room_id !== config.room_id)) registry.rooms.delete(key);
91
+ }
92
+ const scratch = candidates(registry);
93
+ if (scratch.size) await measure(drafts_root(home), { model_dirs: [], scratch });
94
+ let removed = 0, freed_bytes = 0, failed = registry.unreadable;
95
+ for (const item of scratch.values()) {
96
+ let db;
97
+ try {
98
+ db = new DatabaseSync(join(item.config.directory, 'runtime.sqlite3'));
99
+ db.exec('PRAGMA busy_timeout=1000');
100
+ if (reclaim_failed_scratch(db, item.config, item.key)) { removed++; freed_bytes += item.bytes; }
101
+ } catch { failed++; }
102
+ finally { db?.close(); }
103
+ }
104
+ return { removed, freed_bytes, failed };
105
+ }