@zq-silk/yui 0.15.9 → 0.15.11

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 (100) hide show
  1. package/dist/agent/launchEnvironment.js +7 -0
  2. package/dist/cli/commandCatalog.js +26 -6
  3. package/dist/cli/interactionPolicy.js +3 -3
  4. package/dist/cli/updateOrchestrator.js +24 -1
  5. package/dist/cli/updatePorts.js +7 -3
  6. package/dist/cli/upgradeCommand.js +42 -2
  7. package/dist/cli.js +336 -93
  8. package/dist/commands/globalRoleCommands.js +314 -4
  9. package/dist/commands/projectCommands.js +6 -7
  10. package/dist/commands/releaseCommands.js +18 -0
  11. package/dist/commands/taskActor.js +25 -0
  12. package/dist/commands/taskCommands.js +511 -83
  13. package/dist/commands/taskIntegrationCommands.js +16 -38
  14. package/dist/commands/taskIntegrationQueueCommands.js +1 -1
  15. package/dist/commands/taskRemoteDeliveryCommand.js +6 -6
  16. package/dist/commands/taskRoleRuntimeStatus.js +35 -0
  17. package/dist/context/taskContext.js +38 -2
  18. package/dist/controller/agentHostObservation.js +155 -0
  19. package/dist/controller/clientRuntime.js +17 -2
  20. package/dist/controller/controller.js +3 -0
  21. package/dist/controller/fileSchedulerStoreAdapter.js +446 -13
  22. package/dist/controller/globalInputDelivery.js +119 -0
  23. package/dist/controller/jobControl.js +6 -2
  24. package/dist/controller/resourceInventory.js +14 -4
  25. package/dist/controller/resourceInventoryLinux.js +2 -6
  26. package/dist/controller/runtime.js +81 -6
  27. package/dist/controller/runtimeEventInbox.js +32 -3
  28. package/dist/controller/runtimeEventProcessor.js +26 -6
  29. package/dist/controller/runtimeHookRunFence.js +75 -19
  30. package/dist/controller/structuredProviderObservation.js +133 -70
  31. package/dist/coordination/workMailboxQueue.js +5 -0
  32. package/dist/execution/workItemExecutionProjection.js +1 -1
  33. package/dist/executor/agentExecutor.js +64 -4
  34. package/dist/executor/executorRegistry.js +3 -0
  35. package/dist/executor/fileRoleLaunchPlanner.js +78 -118
  36. package/dist/integration/deliveryObligation.js +2 -1
  37. package/dist/integration/gitIntegrationService.js +312 -382
  38. package/dist/integration/integrationAttempt.js +30 -4
  39. package/dist/integration/integrationQueueService.js +7 -7
  40. package/dist/integration/integrationSourceApplication.js +323 -0
  41. package/dist/message/globalInterrupt.js +33 -0
  42. package/dist/message/inputControlResolution.js +106 -0
  43. package/dist/message/message.js +367 -0
  44. package/dist/message/messageContinuation.js +126 -3
  45. package/dist/message/taskInterrupt.js +34 -0
  46. package/dist/observability/orchestrationMetrics.js +1 -1
  47. package/dist/release/releaseHandover.js +22 -0
  48. package/dist/release/releaseWorkflowPorts.js +15 -7
  49. package/dist/repository/gitWorkspace.js +72 -15
  50. package/dist/repository/taskWorkspaceCoordinator.js +134 -0
  51. package/dist/repository/taskWorkspacePreparer.js +120 -49
  52. package/dist/repository/workItemCandidateSnapshot.js +34 -0
  53. package/dist/resources/resourceDiscovery.js +3 -2
  54. package/dist/runtime/agentHost.js +152 -72
  55. package/dist/runtime/agentHostCompatibility.js +127 -0
  56. package/dist/runtime/agentHostProtocol.js +53 -0
  57. package/dist/runtime/executionEnvironment.js +0 -19
  58. package/dist/runtime/launchBroker.js +6 -0
  59. package/dist/runtime/sessionReconciliation.js +4 -4
  60. package/dist/runtime/taskRuntimeIsolation.js +30 -6
  61. package/dist/runtime/tmuxAdapters.js +5 -3
  62. package/dist/scheduler/operatorEvent.js +4 -0
  63. package/dist/scheduler/taskExecutionProjection.js +12 -1
  64. package/dist/scheduler/wakeReason.js +7 -1
  65. package/dist/scheduler/wakeupQueue.js +2 -0
  66. package/dist/setup/setupCommand.js +26 -8
  67. package/dist/storage/homeLayout.js +130 -0
  68. package/dist/storage/migrations/collapseWorktreeLayout.js +963 -0
  69. package/dist/storage/migrations/integrationContinuation.js +104 -0
  70. package/dist/storage/migrations/unifyHomeLayout.js +925 -0
  71. package/dist/storage/sqliteSchema.js +136 -4
  72. package/dist/storage/sqliteStore.js +40 -1
  73. package/dist/storage/storageVersions.js +1 -1
  74. package/dist/storage/storeRpc.js +1 -0
  75. package/dist/storage/upgrade/upgradeOrchestrator.js +95 -2
  76. package/dist/task/archiveDiagnostics.js +128 -0
  77. package/dist/task/nextAction.js +44 -11
  78. package/dist/web/assets/client/app.js +55 -0
  79. package/dist/web/assets/client/components.js +1 -0
  80. package/dist/web/assets/client/i18n.js +6 -0
  81. package/dist/web/assets/client/taskSurface.js +106 -0
  82. package/dist/web/assets/client/view.js +7 -4
  83. package/dist/web/assets/shell.js +23 -0
  84. package/dist/web/assets/styles/layout.js +1 -1
  85. package/dist/web/assets/styles/widgets.js +12 -0
  86. package/dist/web/webServer.js +117 -1
  87. package/dist/web/webSnapshot.js +4 -3
  88. package/dist/web/webTaskSurface.js +222 -5
  89. package/dist/workspace/workItemChangeSetManager.js +18 -2
  90. package/docs/release-workflow.md +39 -0
  91. package/docs/release-workflow.zh-CN.md +29 -0
  92. package/docs/sqlite-control-plane-design.md +223 -1
  93. package/docs/testing/verification-levels.md +24 -0
  94. package/docs/testing/verification-levels.zh-CN.md +11 -0
  95. package/package.json +1 -1
  96. package/skills/yui-leader/references/execution.md +151 -49
  97. package/skills/yui-leader/references/integration.md +52 -2
  98. package/skills/yui-operator/SKILL.md +6 -1
  99. package/skills/yui-runtime/SKILL.md +27 -0
  100. package/skills/yui-runtime/references/publication.md +20 -0
@@ -24,6 +24,9 @@ import { createHash } from "node:crypto";
24
24
  import { REMOVE_RUNTIME_GENERATION_SQL, removeRuntimeGenerationRecords } from "./migrations/removeRuntimeGeneration.js";
25
25
  import { migrateAgentRunContract } from "./migrations/agentRunContract.js";
26
26
  import { migrateArtifactsToGit } from "./migrations/artifactsToGit.js";
27
+ import { migrateIntegrationContinuation } from "./migrations/integrationContinuation.js";
28
+ import { UNIFY_HOME_LAYOUT_SQL, migrateUnifyHomeLayout } from "./migrations/unifyHomeLayout.js";
29
+ import { COLLAPSE_WORKTREE_LAYOUT_SQL, migrateCollapseWorktreeLayout } from "./migrations/collapseWorktreeLayout.js";
27
30
  import { CURRENT_STORAGE_VERSION, MIN_SUPPORTED_STORAGE_VERSION } from "./storageVersions.js";
28
31
  /** Telemetry retention bounds (§4.4). Open question 3 in §11; defaults from the design. */
29
32
  export const TELEMETRY_KEEP_PER_RUN = 200;
@@ -643,6 +646,27 @@ CREATE TABLE IF NOT EXISTS context_snapshots (
643
646
  CREATE UNIQUE INDEX IF NOT EXISTS idx_context_snapshots_scope_sequence
644
647
  ON context_snapshots(task_id, scope, COALESCE(scope_ref, ''), sequence);
645
648
  `;
649
+ /**
650
+ * The Global-owned Message store (decision-3 §9/§11). It extends the Message
651
+ * store to a Global owner exactly as `global_role_session_sets` parallels
652
+ * `role_session_sets`: keyed by the Global Role `name` with an FK to
653
+ * `global_roles`, never by a fabricated `task_id`, and never a new private
654
+ * queue. Ids are minted from the existing `global_sequences` counter, so a
655
+ * Global input has an explicit owner and an authorizable reference without
656
+ * reusing Task record shapes or Task permissions.
657
+ */
658
+ const GLOBAL_ROLE_MESSAGE_SQL = `
659
+ CREATE TABLE IF NOT EXISTS global_role_messages (
660
+ name TEXT NOT NULL,
661
+ message_id TEXT NOT NULL,
662
+ seq INTEGER NOT NULL,
663
+ payload TEXT NOT NULL,
664
+ created_at TEXT NOT NULL,
665
+ PRIMARY KEY (name, message_id),
666
+ FOREIGN KEY (name) REFERENCES global_roles(name)
667
+ );
668
+ CREATE INDEX IF NOT EXISTS idx_global_role_messages_seq ON global_role_messages(name, seq);
669
+ `;
646
670
  const MIGRATION_1_SQL = [
647
671
  BASELINE_CORE_SQL,
648
672
  BASELINE_JOB_AND_RELEASE_SQL,
@@ -1143,6 +1167,103 @@ UPDATE review_rounds SET payload = json_set(payload, '$.executionGroup.lanes', j
1143
1167
  // satisfies "optional still requires a migration declaration".
1144
1168
  sql: "SELECT 1; -- artifacts move to per-Task local Git; see migrateArtifactsToGit",
1145
1169
  migrateData: migrateArtifactsToGit
1170
+ },
1171
+ {
1172
+ version: 20,
1173
+ name: "integration-conflict-continuation",
1174
+ introducedIn: "0.16.0",
1175
+ // Widen Integration status with conflicted; declare optional sourceProgress
1176
+ // (Git cursor/reflog action) and checkInputDigest. Exact old bound FF Job
1177
+ // receipts supply these facts; unprovable history remains unchanged.
1178
+ // Frozen Context and events are never rewritten.
1179
+ sql: "SELECT 1; -- Integration Git progress and exact check admission",
1180
+ migrateData: migrateIntegrationContinuation
1181
+ },
1182
+ {
1183
+ version: 21,
1184
+ name: "force-archive-independent-cleanup",
1185
+ introducedIn: "0.16.0",
1186
+ // Declare optional task.archived force/cleanup/warnings/retainedResources
1187
+ // audit fields and task.archive-cleanup resource/status/detail/paths events;
1188
+ // runtime.event-obsolete may retain the full originalEvent as source evidence.
1189
+ // Archived Tasks may retain exact runtime/workspace/mailbox ownership.
1190
+ // Existing archive, completion and Publication history is unchanged.
1191
+ sql: "SELECT 1; -- archive intent and cleanup evidence remain separate facts"
1192
+ },
1193
+ {
1194
+ version: 22,
1195
+ name: "controller-owned-agent-host-ingress",
1196
+ introducedIn: "0.16.0",
1197
+ // Add optional `host` source metadata to the stable Inbox v1 observation
1198
+ // envelope. Such facts contain no resolved Run: current Controller alone
1199
+ // validates ownership and commits observations, process custody and native
1200
+ // account locations. Existing Inbox facts and domain history stay intact.
1201
+ // Host status capabilities/transport diagnostics are live, not stored.
1202
+ sql: "SELECT 1; -- Controller-owned Host ingress; no historical data rewrite"
1203
+ },
1204
+ {
1205
+ version: 23,
1206
+ name: "unified-message-input-control",
1207
+ introducedIn: "0.16.1",
1208
+ // Widen the Message payload with an optional durable input action
1209
+ // (queue/steer) plus its stable requestId, and an optional interrupt-then
1210
+ // handoff claim (requestId + exact target AgentRun). Existing messages carry
1211
+ // neither field and keep their current queue semantics and delivery history;
1212
+ // no native Turn identity is synthesized and no history is rewritten. The
1213
+ // same contiguous migration adds the Global-owned Message store
1214
+ // (global_role_messages), extending the Message store to a Global owner
1215
+ // exactly as global_role_session_sets parallels role_session_sets: an
1216
+ // explicit Global owner and authorizable reference, no fabricated Task and
1217
+ // no new private global queue (decision-3 §9/§11).
1218
+ sql: `SELECT 1; -- Message input actions, exact Session delivery pins, provider receipts and interrupt-then claims; Global Session provider bindings and native interrupt evidence\n${GLOBAL_ROLE_MESSAGE_SQL}`
1219
+ },
1220
+ {
1221
+ version: 24,
1222
+ name: "unify-home-layout",
1223
+ introducedIn: "0.15.9",
1224
+ // Unify every Yui self-managed path under a single canonical YUI_HOME. The
1225
+ // managed Git worktrees move from the out-of-Home `defaultWorkspace` into
1226
+ // `<home>/workspaces/{worktree,tasks}` and the provider runtimes from the
1227
+ // `<home>.task-runtimes` sibling into `<home>/runtime/task-runtimes`. Only
1228
+ // the durable Git worktree tree is physically relocated, by COPY (never
1229
+ // rename-away): its replica is digest-verified against the source and
1230
+ // atomically published while the original is PRESERVED as the rollback
1231
+ // anchor; the regenerable task views and disposable runtimes are pointer
1232
+ // rewrites only. The copied worktrees are then repaired, and only the
1233
+ // persisted pointers the runtime trusts as live are rewritten. Frozen
1234
+ // evidence (Context snapshots, terminal Run Git evidence, candidate
1235
+ // snapshots, terminal Jobs, reports) and self-healing/self-rediscovered
1236
+ // records (Task cwd, active Role workspaces, views, resource registry) are
1237
+ // left intact; a queued/running Job bound under a relocating root is refused
1238
+ // rather than silently moved. The data/filesystem transform is the
1239
+ // substantive step; the SQL is inert.
1240
+ sql: UNIFY_HOME_LAYOUT_SQL,
1241
+ migrateData: migrateUnifyHomeLayout
1242
+ },
1243
+ {
1244
+ version: 25,
1245
+ name: "collapse-worktree-layout",
1246
+ introducedIn: "0.15.9",
1247
+ // Collapse the two-layer managed workspace layout into a single layer of real
1248
+ // Git worktrees addressed by Task/owner and Project. Each managed worktree
1249
+ // moves from its per-Project physical root
1250
+ // (`<home>/workspaces/worktree/<projectName>/<taskKey>/<roleKey>`) to the
1251
+ // location the v19 symlink view used to point at
1252
+ // (`<home>/workspaces/tasks/<taskId>/<owner>/<projectDirectory>`), so the
1253
+ // logical entry path and the physical worktree become the same directory and
1254
+ // the symlink-view indirection is gone; an integration attempt moves under a
1255
+ // new `tasks/<taskId>/integrations/<id>/` owner root, addressed by the bound
1256
+ // Project directory rather than the Project name. Only the durable Git
1257
+ // worktrees are physically relocated, by COPY (never rename-away): each
1258
+ // replica is digest-verified against the source and atomically published over
1259
+ // the regenerable view symlink while the original is PRESERVED as the
1260
+ // rollback anchor. The copied worktrees are then repaired, and only the
1261
+ // persisted pointers the runtime trusts as live are rewritten. Frozen
1262
+ // evidence and self-healing records are left intact; a queued/running Job
1263
+ // bound under a relocating worktree is refused rather than silently moved.
1264
+ // The data/filesystem transform is the substantive step; the SQL is inert.
1265
+ sql: COLLAPSE_WORKTREE_LAYOUT_SQL,
1266
+ migrateData: migrateCollapseWorktreeLayout
1146
1267
  }
1147
1268
  ]);
1148
1269
  for (let index = 0; index < MIGRATIONS.length; index += 1) {
@@ -1386,8 +1507,14 @@ export function migrateSqliteSchema(db, options) {
1386
1507
  throw new SqliteSchemaMigrationError(`Storage version ${applied.currentVersion} requires an explicit upgrade to `
1387
1508
  + `${CURRENT_STORAGE_VERSION}`, "admission");
1388
1509
  }
1510
+ // Bound the applied steps to `throughVersion` (default and clamp: head), so a
1511
+ // migration test can stop at one step's intermediate state while production
1512
+ // always advances to head. The validate-mode guard above still fires on the
1513
+ // FULL pending set — the bound never lets an ordinary open partially migrate.
1514
+ const targetVersion = Math.min(options.throughVersion ?? CURRENT_STORAGE_VERSION, CURRENT_STORAGE_VERSION);
1515
+ const toApply = pending.filter((migration) => migration.version <= targetVersion);
1389
1516
  const newlyApplied = [];
1390
- for (const migration of pending) {
1517
+ for (const migration of toApply) {
1391
1518
  db.exec(migration.sql);
1392
1519
  migration.migrateData?.(db);
1393
1520
  const appliedAt = new Date().toISOString();
@@ -1400,12 +1527,16 @@ export function migrateSqliteSchema(db, options) {
1400
1527
  // earlier and is validated when the real upgrade later completes it).
1401
1528
  if (effectiveTarget >= CURRENT_STORAGE_VERSION)
1402
1529
  validateSchemaObjects(db);
1403
- return newlyApplied;
1530
+ // The true head reached: the highest already-applied version or, when this
1531
+ // run advanced the ledger, the last step it committed. Bounded runs report
1532
+ // the intermediate head; an unbounded run reports CURRENT_STORAGE_VERSION.
1533
+ const head = newlyApplied.at(-1) ?? applied.currentVersion;
1534
+ return { newlyApplied, head };
1404
1535
  };
1405
- const newlyApplied = options.mode === "apply" && !db.inTransaction
1536
+ const outcome = options.mode === "apply" && !db.inTransaction
1406
1537
  ? db.transaction(migrate)()
1407
1538
  : migrate();
1408
- return { applied: newlyApplied, version: CURRENT_STORAGE_VERSION };
1539
+ return { applied: outcome.newlyApplied, version: outcome.head };
1409
1540
  }
1410
1541
  /** The names of every table the schema creates (for tests/introspection). */
1411
1542
  export const SQLITE_SCHEMA_TABLES = [
@@ -1421,6 +1552,7 @@ export const SQLITE_SCHEMA_TABLES = [
1421
1552
  "projects",
1422
1553
  "global_roles",
1423
1554
  "global_role_session_sets",
1555
+ "global_role_messages",
1424
1556
  "global_sequences",
1425
1557
  "tasks_catalog",
1426
1558
  "managed_workspaces",
@@ -45,6 +45,7 @@ import { validateConfiguredAgent } from "../agent/agent.js";
45
45
  import { validateTaskBrief } from "../brief/taskBrief.js";
46
46
  import { consumePendingBatch, mailboxTargetKey, validateWorkMailbox } from "../coordination/workMailbox.js";
47
47
  import { validateContextSnapshot } from "../context/contextSnapshot.js";
48
+ import { validateGlobalRoleMessage } from "../message/message.js";
48
49
  import { runPurposeAdmitsTaskState } from "../agentRun/agentRun.js";
49
50
  import { compareRuntimeSessionCandidates, projectRuntimeSessionCandidate } from "../runtime/runtimeSessionCandidate.js";
50
51
  import { sessionOwnerProcessKey } from "../runtime/sessionOwnerIdentity.js";
@@ -548,6 +549,8 @@ export class SqliteTaskStore {
548
549
  validateTaskRole(record);
549
550
  if (table === "global_roles")
550
551
  validateGlobalRole(record);
552
+ if (table === "global_role_messages")
553
+ validateGlobalRoleMessage(record);
551
554
  return record;
552
555
  }
553
556
  #sortById(rows, idOf) {
@@ -764,7 +767,7 @@ export class SqliteTaskStore {
764
767
  activeRunRefs.push(`${task.id}/${run.id}`);
765
768
  }
766
769
  for (const attempt of this.listIntegrationAttempts(task.id)) {
767
- if (attempt.status === "running" || attempt.status === "blocked") {
770
+ if (["running", "blocked", "conflicted", "validating"].includes(attempt.status)) {
768
771
  unresolvedIntegrationRefs.push(`${task.id}/${attempt.id}`);
769
772
  }
770
773
  }
@@ -855,6 +858,42 @@ export class SqliteTaskStore {
855
858
  this.#saveRuntimeSessionCandidate(sessions);
856
859
  });
857
860
  }
861
+ // -- global role messages ---------------------------------------------------
862
+ nextGlobalRoleMessageId() { return this.#nextGlobalId("global-message"); }
863
+ saveGlobalRoleMessage(message) {
864
+ validateGlobalRoleMessage(message);
865
+ this.#requireGlobalRole(message.roleName);
866
+ this.#mutate(() => {
867
+ const seq = this.#globalMessageSequence(message.id);
868
+ this.#db.prepare(`INSERT INTO global_role_messages (name, message_id, seq, payload, created_at) VALUES (?, ?, ?, ?, ?)`).run(message.roleName, message.id, seq, this.#json(message), message.createdAt);
869
+ });
870
+ }
871
+ listGlobalRoleMessages(roleName) {
872
+ return this.#sortById(this.#listPayload("global_role_messages", "name = ?", [roleName]), (message) => message.id);
873
+ }
874
+ updateGlobalRoleMessage(message) {
875
+ validateGlobalRoleMessage(message);
876
+ this.#requireGlobalRole(message.roleName);
877
+ this.#mutate(() => {
878
+ const seq = this.#globalMessageSequence(message.id);
879
+ const result = this.#db.prepare(`UPDATE global_role_messages SET seq = ?, payload = ? WHERE name = ? AND message_id = ?`).run(seq, this.#json(message), message.roleName, message.id);
880
+ if (result.changes === 0) {
881
+ throw new StorageRecordError(`Global message not found: ${message.roleName}/${message.id}`);
882
+ }
883
+ });
884
+ }
885
+ #requireGlobalRole(name) {
886
+ const row = this.#db.prepare("SELECT 1 FROM global_roles WHERE name = ?").get(name);
887
+ if (row === undefined)
888
+ throw new StorageRecordError(`Global Role not found: ${name}`);
889
+ }
890
+ /** A Global message id is `global-message-<n>`; its numeric tail is its seq. */
891
+ #globalMessageSequence(id) {
892
+ const match = /^global-message-([1-9]\d*)$/.exec(id);
893
+ if (match === null)
894
+ throw new StorageRecordError(`Global message id is invalid: ${id}`);
895
+ return Number(match[1]);
896
+ }
858
897
  // -- tasks ------------------------------------------------------------------
859
898
  nextTaskId() { return this.#nextGlobalId("task"); }
860
899
  saveTask(task) {
@@ -13,4 +13,4 @@
13
13
  * intermediate Yui releases.
14
14
  */
15
15
  export const MIN_SUPPORTED_STORAGE_VERSION = 1;
16
- export const CURRENT_STORAGE_VERSION = 19;
16
+ export const CURRENT_STORAGE_VERSION = 25;
@@ -46,6 +46,7 @@ const READ_ONLY_STORE_METHODS = new Set([
46
46
  "getGlobalRole",
47
47
  "getGlobalRoleSessionSet",
48
48
  "listGlobalRoleSessionSets",
49
+ "listGlobalRoleMessages",
49
50
  "listTasks",
50
51
  "getTask",
51
52
  "readNextActionFacts",
@@ -1,6 +1,7 @@
1
- import { basename, dirname, join } from "node:path";
1
+ import { join } from "node:path";
2
2
  import { copyFileSync, existsSync, mkdirSync, rmSync } from "node:fs";
3
3
  import Database from "better-sqlite3";
4
+ import { inspectAgentHostCompatibility, describeAgentHostUpgradeBlockers } from "../../runtime/agentHostCompatibility.js";
4
5
  import { RUNTIME_OBSERVATION_TASK_EVENT, createRuntimeObservation } from "../../runtime/runtimeObservation.js";
5
6
  import { RUNTIME_PROCESS_EXIT_TASK_EVENT, validateRuntimeProcessExitObservation } from "../../runtime/processExitObservation.js";
6
7
  import { validateAgentProfile } from "../../profile/agentProfile.js";
@@ -10,6 +11,9 @@ import { validateRun } from "../../agentRun/agentRun.js";
10
11
  import { validateDurableJob } from "../../job/durableJob.js";
11
12
  import { validateWorkItem } from "../../workItem/workItem.js";
12
13
  import { SqliteTaskStore } from "../sqliteStore.js";
14
+ import { storageBackupRoot } from "../homeLayout.js";
15
+ import { preflightUnifyHomeLayout } from "../migrations/unifyHomeLayout.js";
16
+ import { preflightCollapseWorktreeLayout } from "../migrations/collapseWorktreeLayout.js";
13
17
  import { migrateSqliteSchema, storageMigrationPlan } from "../sqliteSchema.js";
14
18
  import { CURRENT_DATABASE_FILENAME, inspectStorageSchema } from "../storageSchema.js";
15
19
  import { CURRENT_STORAGE_VERSION, MIN_SUPPORTED_STORAGE_VERSION } from "../storageVersions.js";
@@ -38,6 +42,15 @@ export async function runStorageUpgrade(options) {
38
42
  ? classification.classification.blocker.action
39
43
  : "Use a compatible Yui release.");
40
44
  }
45
+ const incompatibleHosts = await inspectAgentHostCompatibility(options.home);
46
+ if (incompatibleHosts.length > 0) {
47
+ return {
48
+ ...blocked(state.status === "current" ? currentClassification() : migratableClassification(state.currentVersion), "host-compatibility", `Existing Agent Hosts cannot safely use the target Controller/storage:\n${describeAgentHostUpgradeBlockers(incompatibleHosts)}`, "Keep the current Controller, Home and Host processes unchanged. Inspect the named Sessions and original pending "
49
+ + "inputs/results; let current work settle, then use explicitly authorized Session replacement/cleanup at a safe "
50
+ + "boundary before retrying the upgrade. This target cannot hot-patch legacy Host memory."),
51
+ hosts: incompatibleHosts
52
+ };
53
+ }
41
54
  if (state.status === "current") {
42
55
  try {
43
56
  validateCurrentStore(options.home);
@@ -73,6 +86,21 @@ export async function runStorageUpgrade(options) {
73
86
  + `to ${CURRENT_STORAGE_VERSION}.`), "corruption", "The storage migration registry is incomplete.", "Install a Yui release that carries the complete migration chain.");
74
87
  }
75
88
  const classification = migratableClassification(state.currentVersion);
89
+ // Genuine pre-migration safety check, shared by ALL migrating modes. The
90
+ // unify-home data migration (18->19) physically relocates on-disk trees and
91
+ // rewrites live launch pointers, so before any mode proceeds we run its READ-
92
+ // ONLY preflight and refuse up front on the same conditions the migration would
93
+ // throw on inside the apply transaction: a queued/running Job bound under a
94
+ // relocating root, or a conflicting relocation target. The migration is applied
95
+ // offline (after the operator has stopped this Home's writers), so the preflight
96
+ // does not scan for live processes. Surfacing these here — before the Controller
97
+ // is stopped or a backup is taken — is what makes `--dry-run` and the updater's
98
+ // `--update-preflight` a genuine readiness signal rather than a mere list of
99
+ // schema steps, while the in-transaction checks remain the authoritative guard
100
+ // against anything that starts after this point.
101
+ const migrationBlocked = preflightMigrationBlockers(options.home, plan, classification);
102
+ if (migrationBlocked !== null)
103
+ return migrationBlocked;
76
104
  if (options.mode === "update-preflight") {
77
105
  return {
78
106
  outcome: "update-preflight",
@@ -223,7 +251,10 @@ async function createDatabaseBackup(home, sourceVersion, now) {
223
251
  const source = join(home, CURRENT_DATABASE_FILENAME);
224
252
  if (!existsSync(source))
225
253
  throw new Error("The authoritative yui.db is missing.");
226
- const backupDirectory = join(dirname(home), `${basename(home)}-backups`);
254
+ // Backups live inside Home so a single canonical root contains every
255
+ // self-managed artifact; the upgrade fence still protects them from the
256
+ // live database because they are a distinct partition from yui.db.
257
+ const backupDirectory = storageBackupRoot(home);
227
258
  mkdirSync(backupDirectory, { recursive: true, mode: 0o700 });
228
259
  const timestamp = now.toISOString().replaceAll(":", "-");
229
260
  const backupPath = join(backupDirectory, `pre-storage-v${sourceVersion}-${timestamp}.db`);
@@ -313,6 +344,68 @@ function blocked(classification, stage, message, action) {
313
344
  sceneUnchanged: true
314
345
  };
315
346
  }
347
+ /**
348
+ * Run each path-relocating data migration's READ-ONLY preflight against the
349
+ * current (pre-upgrade) database and, when any finds a blocker, return a
350
+ * `blocked` result the caller surfaces before touching the Controller or the
351
+ * Home. Returns `null` when the plan carries no path-relocating data migration,
352
+ * or when every preflight is clear.
353
+ *
354
+ * Both the 18->19 unify and the 19->20 collapse migrations physically relocate
355
+ * managed worktrees and share the same blocker shape (in-flight Job, conflicting
356
+ * relocation target). When a Home is upgraded across both in one run, their
357
+ * blockers are aggregated so the operator sees every readiness problem at once.
358
+ *
359
+ * The database is opened read-only so the checks cannot mutate the authoritative
360
+ * store, and the handle is always closed. An unexpected failure to evaluate a
361
+ * preflight is itself a fail-closed blocker: we must not advance to an
362
+ * irreversible migration on an unverifiable readiness signal.
363
+ */
364
+ function preflightMigrationBlockers(home, plan, classification) {
365
+ const relocatesUnify = plan.some((step) => step.name === "unify-home-layout");
366
+ const relocatesCollapse = plan.some((step) => step.name === "collapse-worktree-layout");
367
+ // Only meaningful when the plan actually includes a path-relocating migration.
368
+ if (!relocatesUnify && !relocatesCollapse)
369
+ return null;
370
+ let blockers;
371
+ try {
372
+ const database = new Database(join(home, CURRENT_DATABASE_FILENAME), {
373
+ readonly: true,
374
+ fileMustExist: true
375
+ });
376
+ try {
377
+ blockers = [];
378
+ if (relocatesUnify)
379
+ blockers.push(...preflightUnifyHomeLayout(database).blockers);
380
+ if (relocatesCollapse)
381
+ blockers.push(...preflightCollapseWorktreeLayout(database).blockers);
382
+ }
383
+ finally {
384
+ database.close();
385
+ }
386
+ }
387
+ catch (error) {
388
+ return {
389
+ ...blocked(classification, "in-flight", `The storage migration readiness check could not be completed: ${messageOf(error)}`, "Resolve the reported problem, confirm no Job is queued or running against a managed "
390
+ + "workspace, then rerun the upgrade.")
391
+ };
392
+ }
393
+ if (blockers.length === 0)
394
+ return null;
395
+ return {
396
+ outcome: "blocked",
397
+ stage: "in-flight",
398
+ message: "Refusing to migrate: the Home is not safe to relocate under the current layout yet. "
399
+ + blockers.map((entry) => entry.detail).join("; ") + ".",
400
+ action: "Resolve the reported condition(s) — let a queued/running Job finish or cancel it, and "
401
+ + "clear any conflicting relocation target — then rerun the upgrade. The migration is applied "
402
+ + "offline, so first confirm the Controller and any execution writers for this Home are "
403
+ + "stopped. The authoritative Home is unchanged.",
404
+ blockers: blockers.map((entry) => ({ reason: entry.reason, detail: entry.detail })),
405
+ classification,
406
+ sceneUnchanged: true
407
+ };
408
+ }
316
409
  function messageOf(error) {
317
410
  return error instanceof Error ? error.message : String(error);
318
411
  }
@@ -0,0 +1,128 @@
1
+ import { createTaskEvent } from "../event/taskEvent.js";
2
+ import { StorageRecordError, StorageConflictError, StorageCancelledError } from "../storage/taskStore.js";
3
+ import { StorageSchemaError } from "../storage/storageSchema.js";
4
+ import { managedWorkspaceKey } from "../worktree/managedWorkspace.js";
5
+ export class ArchiveAuditPersistenceError extends Error {
6
+ constructor(cause) {
7
+ super(`Archive audit could not be persisted: ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
8
+ this.name = "ArchiveAuditPersistenceError";
9
+ }
10
+ }
11
+ export function isArchivePersistenceFailure(error) {
12
+ return error instanceof ArchiveAuditPersistenceError
13
+ || error instanceof StorageRecordError || error instanceof StorageConflictError
14
+ || error instanceof StorageCancelledError || error instanceof StorageSchemaError
15
+ || (error instanceof Error && (("code" in error && String(error.code).startsWith("SQLITE_"))
16
+ || (error.cause !== undefined && error.cause !== error && isArchivePersistenceFailure(error.cause))));
17
+ }
18
+ /** Current resource references, not another cleanup queue or writable status. */
19
+ export function archiveRetainedResources(store, task) {
20
+ const workspaces = store.listManagedWorkspaces(task.id);
21
+ const resources = workspaces.map(workspace => ({
22
+ resource: managedWorkspaceKey(workspace.owner),
23
+ detail: "Managed workspace retained; removal has not been recorded.",
24
+ paths: [workspace.root, ...workspace.entries.filter(e => e.access === "write").map(e => e.path)]
25
+ }));
26
+ if (task.cwd !== undefined && !workspaces.some(w => w.owner.type === "task")) {
27
+ resources.push({ resource: `task:${task.id}`, detail: "Task workspace reference retained.", paths: [task.cwd] });
28
+ }
29
+ for (const run of store.listRuns(task.id).filter(r => r.status === "active")) {
30
+ resources.push({ resource: `run:${task.id}/${run.id}`,
31
+ detail: `AgentRun remains active (${run.roleName}); archive is not stop evidence.` });
32
+ }
33
+ for (const job of store.listDurableJobs(task.id).filter(j => ["queued", "running", "unknown-needs-attention"].includes(j.status))) {
34
+ resources.push({ resource: `job:${task.id}/${job.id}`, detail: `DurableJob remains ${job.status}.` });
35
+ }
36
+ for (const attempt of store.listIntegrationAttempts(task.id).filter(a => ["running", "blocked", "conflicted", "validating"].includes(a.status))) {
37
+ resources.push({ resource: `integration-attempt:${task.id}/${attempt.id}`,
38
+ detail: `Integration remains ${attempt.status}.` });
39
+ }
40
+ for (const item of store.listWorkItems(task.id).filter(i => i.status === "open")) {
41
+ resources.push({ resource: `work-item:${task.id}/${item.id}`, detail: "WorkItem remains unaccepted." });
42
+ }
43
+ for (const round of store.listReviewRounds(task.id).filter(r => !["completed", "failed"].includes(r.status))) {
44
+ resources.push({ resource: `review-round:${task.id}/${round.id}`, detail: `Review remains ${round.status}.` });
45
+ }
46
+ for (const input of store.listInputRequests(task.id).filter(i => i.status === "open")) {
47
+ resources.push({ resource: `input:${task.id}/${input.id}`, detail: "Unresolved input preserved; not answered by archive." });
48
+ }
49
+ for (const role of store.listRoles(task.id)) {
50
+ const sessions = store.getTaskRoleSessionSet(task.id, role.name);
51
+ for (const session of Object.values(sessions?.sessions ?? {})) {
52
+ if (session.status === "ended")
53
+ continue;
54
+ resources.push({ resource: `session:${task.id}/${role.name}/${session.agentId}/${session.nativeSessionId}`,
55
+ detail: `Session is ${session.status}; physical quiescence is not established by archive.` });
56
+ }
57
+ const provider = sessions?.providerBinding;
58
+ if (provider !== undefined && provider !== null && provider.run != null
59
+ && !["completed", "failed", "cancelled"].includes(provider.run.status)) {
60
+ resources.push({ resource: `provider-input:${task.id}/${role.name}/${provider.run.attemptId}`,
61
+ detail: `Provider input is ${provider.run.status}; exact input identity is retained.` });
62
+ }
63
+ }
64
+ for (const mailbox of store.listWorkMailboxes()) {
65
+ const target = mailbox.target;
66
+ if (!("taskId" in target) || target.taskId !== task.id)
67
+ continue;
68
+ if (mailbox.processing === null && mailbox.pending === null)
69
+ continue;
70
+ resources.push({
71
+ resource: `mailbox:${target.kind}:${task.id}${"roleName" in target ? `/${target.roleName}` : ""}`,
72
+ detail: `Original mailbox retained${mailbox.processing === null ? "" : ` (${mailbox.processing.batchId})`}; not replayed or acknowledged by archive.`
73
+ });
74
+ }
75
+ return resources;
76
+ }
77
+ export function archiveDeliveryWarnings(delivery) {
78
+ return delivery.projects.filter(p => p.codeDelivery !== "none" && (!p.merged || !p.verified))
79
+ .map(p => ({ resource: `delivery:${delivery.taskId}/${p.projectId}`,
80
+ detail: `${p.coverage}: ${p.reason} Merged=${p.merged}; verified=${p.verified}.` }));
81
+ }
82
+ /** Every completed/failed resource attempt is committed before trying the next.
83
+ * Do not catch storage failures: callers must not report unrecorded cleanup as success.
84
+ */
85
+ export function recordArchiveCleanup(store, taskId, diagnostic, status, now = new Date()) {
86
+ try {
87
+ store.transaction(tx => {
88
+ if (tx.getTask(taskId)?.status !== "archived")
89
+ throw new Error(`Task is not archived: ${taskId}.`);
90
+ tx.saveEvent(taskId, createTaskEvent(tx.nextEventId(taskId), taskId, "task.archive-cleanup", {
91
+ resource: diagnostic.resource, detail: diagnostic.detail, status,
92
+ ...(diagnostic.paths === undefined ? {} : { paths: JSON.stringify(diagnostic.paths) })
93
+ }, now));
94
+ });
95
+ }
96
+ catch (error) {
97
+ throw new ArchiveAuditPersistenceError(error);
98
+ }
99
+ }
100
+ export function taskArchiveDiagnostics(store, task) {
101
+ const events = store.listEvents(task.id);
102
+ const archive = events.filter(e => e.type === "task.archived").at(-1);
103
+ const cleanup = events.filter(e => e.type === "task.archive-cleanup");
104
+ const warnings = archive?.payload.warnings === undefined
105
+ ? [] : JSON.parse(archive.payload.warnings);
106
+ // These are historical attempt diagnostics; current references below remain
107
+ // authoritative even when explicit later resource cleanup has succeeded.
108
+ for (const event of cleanup.filter(e => e.payload.status === "retained")) {
109
+ warnings.push({ resource: event.payload.resource, detail: event.payload.detail,
110
+ ...(event.payload.paths === undefined ? {} : { paths: JSON.parse(event.payload.paths) }) });
111
+ }
112
+ const forced = archive?.payload.force === "true";
113
+ const cleanupFinished = !forced || cleanup.some(e => e.payload.status === "finished");
114
+ if (forced && !cleanupFinished)
115
+ warnings.push({ resource: `task:${task.id}`,
116
+ detail: "Archive committed; cleanup has not finished. Retained references require explicit inspection; archive retry does not replay cleanup." });
117
+ return { archived: task.status === "archived", forced,
118
+ disposition: archive?.payload.workspaceDisposition ?? null,
119
+ cleanupFinished, warnings, retainedResources: archiveRetainedResources(store, task),
120
+ cleanupEvents: cleanup };
121
+ }
122
+ export function renderArchiveDiagnostics(data) {
123
+ return [
124
+ `Archived: ${data.archived}; forced: ${data.forced}; cleanup finished: ${data.cleanupFinished}`,
125
+ ...data.warnings.map(w => `Warning [${w.resource}]: ${w.detail}`),
126
+ ...data.retainedResources.map(r => `Retained [${r.resource}]: ${r.detail}${r.paths ? ` (${r.paths.join(", ")})` : ""}`)
127
+ ].join("\n") + "\n";
128
+ }
@@ -121,6 +121,16 @@ export function projectNextAction(facts) {
121
121
  recommendedCommand: `yui task activate ${task.id}`
122
122
  });
123
123
  }
124
+ const conflictedIntegration = facts.integrations.find(attempt => attempt.status === "conflicted");
125
+ if (conflictedIntegration !== undefined) {
126
+ return buildAction(facts, {
127
+ kind: "resolve-integration-conflicts",
128
+ reason: `Integration ${conflictedIntegration.id} has Git conflicts; the Leader resolves its workspace and continues without prior resolve or user approval.`,
129
+ refs: [ref("integration", conflictedIntegration.id)],
130
+ preconditions: [{ fact: "Git conflict resolution belongs to the Leader", satisfied: true }],
131
+ recommendedCommand: `yui task integration continue ${task.id}/${conflictedIntegration.id}`
132
+ });
133
+ }
124
134
  const checkingIntegration = facts.integrations.find(attempt => attempt.status === "running" && attempt.jobId !== undefined);
125
135
  if (checkingIntegration !== undefined) {
126
136
  const job = facts.integrationJobs?.find(job => job.id === checkingIntegration.jobId);
@@ -146,6 +156,21 @@ export function projectNextAction(facts) {
146
156
  recommendedCommand: `yui task integration continue ${task.id}/${checkingIntegration.id}`
147
157
  });
148
158
  }
159
+ const unfinishedIntegration = facts.integrations.find(attempt => attempt.status === "running" || attempt.status === "validating");
160
+ if (unfinishedIntegration !== undefined) {
161
+ return buildAction(facts, {
162
+ kind: "integrate-work-item",
163
+ reason: `Integration ${unfinishedIntegration.id} is unfinished. ${unfinishedIntegration.summary ?? "Continue from its persisted source and check evidence."}`,
164
+ refs: [ref("integration", unfinishedIntegration.id)],
165
+ preconditions: [{ fact: "Inspect exact Integration evidence before any retry", satisfied: true }],
166
+ recommendedCommand: `yui task integration continue ${task.id}/${unfinishedIntegration.id}`,
167
+ alternatives: [{
168
+ kind: "inspect-integration",
169
+ reason: "If completion cannot be proved, preserve evidence and choose formal abort and an authorized alternative.",
170
+ recommendedCommand: `yui task integration show ${task.id}/${unfinishedIntegration.id}`
171
+ }]
172
+ });
173
+ }
149
174
  const candidateReady = facts.workItems
150
175
  .find((item) => (item.status === "open" && item.currentCandidateId !== undefined));
151
176
  if (candidateReady !== undefined) {
@@ -320,27 +345,35 @@ export function projectNextAction(facts) {
320
345
  return synthesisSelectionAction(facts, "work", item.id);
321
346
  }
322
347
  const refs = [ref("work-item", item.id)];
348
+ const direct = item.assignee === undefined;
323
349
  return buildAction(facts, {
324
350
  kind: "implement-current-work-item",
325
- reason: `Work Item ${item.id} is ${item.status}; dispatch or continue its implementation.`,
351
+ reason: direct
352
+ ? `Work Item ${item.id} has no managed assignee; the Leader can execute it directly.`
353
+ : `Work Item ${item.id} is assigned to ${item.assignee}; dispatch or continue that assignment.`,
326
354
  refs,
327
355
  preconditions: [
328
356
  { fact: `Work Item is ${item.status}`, satisfied: true, ref: refs[0] }
329
357
  ],
330
- recommendedCommand: `yui task work dispatch ${task.id}/${item.id}`,
331
- alternatives: [
332
- {
333
- kind: "execute-directly",
334
- reason: "Execute the bounded Work Item directly when the Leader's current context and authority are sufficient.",
335
- refs
336
- },
358
+ recommendedCommand: direct
359
+ ? `yui task work update ${task.id}/${item.id} running`
360
+ : `yui task work dispatch ${task.id}/${item.id}`,
361
+ alternatives: direct ? [
362
+ ...(item.writeProjectIds.length === 0 ? [] : [{
363
+ kind: "isolate-work-item",
364
+ reason: "Prepare and inspect the WorkItem-owned code workspace before direct implementation.",
365
+ recommendedCommand: `yui task work isolate ${task.id}/${item.id}`,
366
+ refs
367
+ }]),
337
368
  {
338
369
  kind: "native-subagent",
339
- reason: "Use native implementer subagents when bounded work benefits from specialist attention or parallel fan-out inside the Leader Session.",
370
+ reason: "Use a bounded native child only when authorized and useful within the Leader's current scope.",
340
371
  refs
341
372
  }
342
- ],
343
- judgmentRequired: "Leader must choose the execution path: direct execution, a native subagent, or managed Task Role dispatch."
373
+ ] : [],
374
+ judgmentRequired: direct
375
+ ? "Honor explicit execution preferences. For code, use the WorkItem-owned workspace and integrate its Candidate before acceptance; no self-dispatch is needed."
376
+ : "Preserve the current managed Assignment and original results; do not switch ownership merely to follow a different default."
344
377
  });
345
378
  }
346
379
  if (deliveryWorkItems.length === 0