@ordewell/web 0.15.0 → 0.15.2

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.
@@ -227,6 +227,22 @@ var WebConfig = class extends core.BaseConfig {
227
227
  return out.length > 0 ? out : [...core.DEFAULT_RUNNERS];
228
228
  }
229
229
  };
230
+
231
+ // server/adapters/daemonPlannerChoice.ts
232
+ var PLANNER_CHOICE_ENV = ["AI_PROVIDER", "ORCHESTRATOR_MODEL", "ORDEWELL_PLANNER_EFFORT"];
233
+ function daemonPlannerChoice(opts) {
234
+ return {
235
+ current: () => opts.config().planner,
236
+ savedProvider: () => process.env.AI_PROVIDER ?? "",
237
+ save: ({ provider, model, effort }) => {
238
+ if (provider !== void 0) process.env.AI_PROVIDER = provider;
239
+ if (model !== void 0) process.env.ORCHESTRATOR_MODEL = model;
240
+ if (effort !== void 0) process.env.ORDEWELL_PLANNER_EFFORT = effort;
241
+ },
242
+ notePick: opts.notePick,
243
+ memory: opts.memory
244
+ };
245
+ }
230
246
  function scanWorkspacesImpl(deps) {
231
247
  const { fsModule, pathModule, cwd, home, projectDir } = deps;
232
248
  const paths = /* @__PURE__ */ new Set();
@@ -269,8 +285,11 @@ function scanWorkspaces(sea = {}) {
269
285
  projectDir: core.isSea(sea) ? void 0 : path__namespace.resolve(__dirname, "..", "..", "..", "..")
270
286
  });
271
287
  }
288
+ function isSavedCredential(key) {
289
+ return core.PROVIDER_CREDENTIAL_ENV.has(key) || core.WEB_SEARCH_SETTINGS_ENV.includes(key);
290
+ }
272
291
  function saveCredentials(env, file = core.globalEnvFile()) {
273
- const credentials = Object.fromEntries(Object.entries(env).filter(([key]) => core.PROVIDER_CREDENTIAL_ENV.has(key)).map(([key, value]) => [key, core.removesCredential(key, value) ? void 0 : value]));
292
+ const credentials = Object.fromEntries(Object.entries(env).filter(([key]) => isSavedCredential(key)).map(([key, value]) => [key, core.removesCredential(key, value) ? void 0 : value]));
274
293
  if (Object.keys(credentials).length === 0) return [];
275
294
  try {
276
295
  core.writeEnvFileVars(file, credentials);
@@ -289,7 +308,7 @@ function loadSavedCredentials(env = process.env, file = core.globalEnvFile()) {
289
308
  return;
290
309
  }
291
310
  for (const [key, value] of Object.entries(saved)) {
292
- if (core.PROVIDER_CREDENTIAL_ENV.has(key) && env[key] === void 0) env[key] = value;
311
+ if (isSavedCredential(key) && env[key] === void 0) env[key] = value;
293
312
  }
294
313
  }
295
314
  var MAX_OUTPUT_BYTES = 4 * 1024 * 1024;
@@ -630,11 +649,12 @@ var OrchestratorPool = class {
630
649
  */
631
650
  pickedModel;
632
651
  /**
633
- * The last planner write a route made, so each lands after the ones made
634
- * before it: a switch still waiting for its catalog must not write over a
635
- * pick made while it waited (ADR-0040).
652
+ * Every planner change, in every workspace, lands after the ones made
653
+ * before it: the environment it writes is the daemon's one, and a switch
654
+ * still waiting for its catalog must not write over a pick made while it
655
+ * waited (ADR-0040).
636
656
  */
637
- plannerWrites = Promise.resolve();
657
+ plannerChanges = new core.PlannerChangeApplier();
638
658
  runner;
639
659
  /** One probe per daemon, asked again whenever the runner list is refreshed (ADR-0033). */
640
660
  codexSandbox;
@@ -991,6 +1011,8 @@ var OrchestratorPool = class {
991
1011
  broadcast,
992
1012
  // The notification channel above is silent here; this is how a client hears how the run isolates.
993
1013
  onNotice: (notice) => this.broadcast(sessionId, workspace, notice),
1014
+ // The planner gate asks a signed-out planner again with this workspace's own probe (ADR-0052).
1015
+ signIn: this.sessionSignIn(workspace),
994
1016
  modelResolver: this.resolverFor(workspace),
995
1017
  settings: () => this.runtimeSettings(),
996
1018
  sessionId,
@@ -1134,38 +1156,31 @@ var OrchestratorPool = class {
1134
1156
  await this.discoverUnheld(workspace, consulted);
1135
1157
  }
1136
1158
  /**
1137
- * {@link updateSettings} as a route makes it. A write that changes who plans
1138
- * lands after the planner writes made before it and, where its judgement
1139
- * reads a catalog not yet held, waits for that one; every other write — a
1140
- * key, an allowlist, a limit — lands at once (ADR-0040).
1159
+ * Save a settings write, as a route makes it. A write that changes who
1160
+ * plans goes through the planner-change rule, after the planner changes
1161
+ * made before it, waiting for a catalog only where it decides the answer;
1162
+ * every other write (a key, an allowlist, a limit) lands at once (ADR-0040).
1141
1163
  */
1142
1164
  async writeSettings(changes2, sessionId, requestWorkspace) {
1143
1165
  const change = plannerChangeOf(changes2, admittedEnv(changes2)?.accepted);
1144
- if (!changesPlanner(change)) return this.updateSettings(changes2, sessionId, requestWorkspace);
1145
- const turn = this.plannerWrites.then(async () => {
1146
- const workspace = this.settingsWorkspace(sessionId, requestWorkspace);
1147
- const judging = this.catalogJudging(change, workspace);
1148
- await this.discoverUnheld(workspace, judging ? [judging] : []);
1149
- const incoming = change.provider !== void 0 && change.provider !== this.plannerProviderInEffect(workspace) ? core.runnerForProvider(change.provider) : void 0;
1150
- if (incoming) await this.refuseBlockedRunner(incoming, workspace);
1151
- return this.updateSettings(changes2, sessionId, requestWorkspace);
1152
- });
1153
- this.plannerWrites = turn.catch(() => void 0);
1154
- return turn;
1166
+ if (!changesPlanner(change)) return this.saveSettings(changes2, sessionId, requestWorkspace);
1167
+ const workspace = this.settingsWorkspace(sessionId, requestWorkspace);
1168
+ const selection = await this.plannerChanges.apply(change, this.plannerChangeHost(workspace));
1169
+ return this.saveSettings(changes2, sessionId, requestWorkspace, selection);
1155
1170
  }
1156
- /**
1157
- * The provider whose catalog `selectPlanner` reads to judge `change`: a
1158
- * switch with no model of its own restores the remembered pick only from
1159
- * the incoming catalog, and an agent's effort alone survives only where its
1160
- * model in effect offers it. An explicit model is the user's word whatever
1161
- * the catalog lists, and a cold catalog refuses no effort, so it reads none.
1162
- */
1163
- catalogJudging(change, workspace) {
1164
- const current = this.plannerProviderInEffect(workspace);
1165
- const provider = change.provider ?? current;
1166
- const model = change.model?.trim();
1167
- if (provider !== current) return model ? void 0 : provider;
1168
- return model === void 0 && change.effort !== void 0 && core.runnerForProvider(provider) ? provider : void 0;
1171
+ /** Where a planner change in `workspace` reads its state and catalogs, and saves its answer. */
1172
+ plannerChangeHost(workspace) {
1173
+ return {
1174
+ store: daemonPlannerChoice({
1175
+ config: () => this.plannerConfig(void 0, workspace),
1176
+ memory: this.plannerModelMemory,
1177
+ notePick: (model, provider) => {
1178
+ this.pickedModel = { model, provider, workspace: workspaceKey(workspace) };
1179
+ }
1180
+ }),
1181
+ catalogs: this.resolverFor(workspace),
1182
+ refuseBlockedRunner: (runner) => this.refuseBlockedRunner(runner, workspace)
1183
+ };
1169
1184
  }
1170
1185
  /**
1171
1186
  * Discover the catalogs of `providers` that `workspace`'s resolver does not
@@ -1177,30 +1192,26 @@ var OrchestratorPool = class {
1177
1192
  await Promise.all([...new Set(providers)].map((provider) => resolver.plannerCatalog(provider)));
1178
1193
  }
1179
1194
  /**
1180
- * The provider planning right now. A vendor planner is resolved from its
1181
- * model id, so this is read again after a write rather than carried over.
1195
+ * Everything in a settings write but its planner change, which `selection`
1196
+ * says was already applied: the planner's own variables are then the rule's
1197
+ * to write, not the body's.
1182
1198
  */
1183
- plannerProviderInEffect(workspace) {
1184
- return this.plannerConfig(void 0, workspace).aiProvider;
1185
- }
1186
- updateSettings(changes2, sessionId, requestWorkspace) {
1199
+ saveSettings(changes2, sessionId, requestWorkspace, selection) {
1187
1200
  const workspace = this.settingsWorkspace(sessionId, requestWorkspace);
1188
- const current = this.plannerConfig(void 0, workspace).planner;
1189
1201
  const admission = admittedEnv(changes2);
1190
1202
  const envChanges = admission ? admission.accepted : void 0;
1191
- const plannerChange = plannerChangeOf(changes2, envChanges);
1192
- const selection = changesPlanner(plannerChange) ? core.selectPlanner(current, plannerChange, this.plannerModelMemory, this.plannerCatalogFor(plannerChange.provider ?? current.provider, workspace)) : void 0;
1193
- const agentPick = selection?.remember && selection.remember.provider === current.provider && core.isAgentPick(current.provider, selection.remember.model) ? selection.remember : void 0;
1194
- if (agentPick) this.plannerModelMemory.remember(agentPick.provider, agentPick.model, agentPick.effort);
1195
1203
  let unsavedEnvKeys = [];
1196
1204
  if (envChanges) {
1197
1205
  let touched = false;
1206
+ let saved = false;
1198
1207
  for (const [key, value] of Object.entries(envChanges)) {
1208
+ if (selection && isPlannerChoiceEnv(key)) continue;
1199
1209
  if (core.removesCredential(key, value)) delete process.env[key];
1200
1210
  else process.env[key] = value;
1201
1211
  if (core.PROVIDER_CREDENTIAL_ENV.has(key)) touched = true;
1212
+ if (isSavedCredential(key)) saved = true;
1202
1213
  }
1203
- if (touched && changes2.inherited !== true) unsavedEnvKeys = saveCredentials(envChanges);
1214
+ if (saved && changes2.inherited !== true) unsavedEnvKeys = saveCredentials(envChanges);
1204
1215
  if (envChanges.ORDEWELL_MAX_PARALLEL !== void 0) {
1205
1216
  for (const [id, session] of this.sessions) {
1206
1217
  session.reschedule().catch((err) => {
@@ -1215,13 +1226,6 @@ var OrchestratorPool = class {
1215
1226
  for (const installation of /* @__PURE__ */ new Set([this.runnerInstallation, ...this.workspaceInstallations.values()])) installation.refresh?.();
1216
1227
  }
1217
1228
  }
1218
- if (selection) {
1219
- if (plannerChange.provider !== void 0 || plannerChange.model !== void 0) process.env.ORCHESTRATOR_MODEL = selection.next.model;
1220
- process.env.ORDEWELL_PLANNER_EFFORT = selection.next.effort;
1221
- if (plannerChange.model !== void 0) {
1222
- this.pickedModel = { model: selection.next.model, provider: this.plannerProviderInEffect(workspace), workspace: workspaceKey(workspace) };
1223
- }
1224
- }
1225
1229
  if (changes2.modelAllowlist && typeof changes2.modelAllowlist === "object") {
1226
1230
  const allowlist = changes2.modelAllowlist;
1227
1231
  for (const [runner, ids] of Object.entries(allowlist)) {
@@ -1236,11 +1240,6 @@ var OrchestratorPool = class {
1236
1240
  this.settingsService.setAutonomousMode(changes2.autonomousMode);
1237
1241
  }
1238
1242
  if (envChanges) for (const monitor of this.accountLimits.values()) monitor.refresh();
1239
- if (selection?.remember && !agentPick) {
1240
- const { provider, model, effort } = selection.remember;
1241
- const switched = provider !== current.provider;
1242
- this.plannerModelMemory.remember(switched ? provider : this.plannerProviderInEffect(workspace), model, effort);
1243
- }
1244
1243
  const rejectedEnvKeys = admission?.rejected ?? [];
1245
1244
  const settings = this.getSettings(sessionId, workspace);
1246
1245
  if (rejectedEnvKeys.length > 0) settings.rejectedEnvKeys = rejectedEnvKeys;
@@ -1396,9 +1395,8 @@ var OrchestratorPool = class {
1396
1395
  );
1397
1396
  }
1398
1397
  async generatePlan(sessionId, goal, runners, workspace, modelOverride) {
1399
- const recheck = this.recheckSignedOutPlanner(workspace, this.plannerConfig(modelOverride, workspace).aiProvider);
1400
- if (recheck) await recheck;
1401
- const session = this.openPlanningSession(sessionId, runners, workspace, modelOverride);
1398
+ const opening = this.openPlanningSession(sessionId, runners, workspace, modelOverride);
1399
+ const session = opening instanceof Promise ? await opening : opening;
1402
1400
  return core.migratePlanState(await this.firstTurn(sessionId, session, () => session.generatePlan(goal, runners)));
1403
1401
  }
1404
1402
  /**
@@ -1436,20 +1434,16 @@ var OrchestratorPool = class {
1436
1434
  if (enabled && !on) await this.refuseBlockedRunner(id, workspace);
1437
1435
  this.setRunnerEnabled(id, enabled);
1438
1436
  }
1437
+ /** The probe and the catalog that judge `workspace`'s account, for {@link recheckRunnerSignIn} and a pick's judgment. */
1439
1438
  /**
1440
- * Judge the planner as the workspace's account is now, not as it was when the
1441
- * list was last read: the signed-out refusal itself offers the runner's own
1442
- * `auth login` as a way back, and that happens in another process. Only a
1443
- * recorded signed-out verdict is asked again — unknown never blocks, and a
1444
- * signed-in one is the runner's own failure to report. Null when there is
1445
- * nothing to ask, so the turn registers at once as it always did.
1439
+ * How a session's planner gate asks again in `workspace`, and a child's in
1440
+ * its own worktree. A worktree's probe and resolver view are the pool's, kept
1441
+ * for its lifetime as a root workspace's are: a child's end never lets them
1442
+ * go, since a Session may still be opening there, its probe out.
1446
1443
  */
1447
- recheckSignedOutPlanner(workspace, provider) {
1448
- const runner = core.runnerForProvider(provider);
1449
- if (!runner || core.recordedRunnerAuth(runner, workspace)?.state !== "signed-out") return null;
1450
- return core.verifyRunnerSignIn(runner, this.signInCheck(workspace)).then(() => void 0);
1444
+ sessionSignIn(workspace) {
1445
+ return { installation: this.signInCheck(workspace).installation, at: (child) => this.sessionSignIn(child) };
1451
1446
  }
1452
- /** The probe and the catalog that judge `workspace`'s account, for {@link verifyRunnerSignIn} and {@link recheckRunnerSignIn}. */
1453
1447
  signInCheck(workspace) {
1454
1448
  const installation = this.installationFor(workspace);
1455
1449
  return {
@@ -1465,10 +1459,18 @@ var OrchestratorPool = class {
1465
1459
  /**
1466
1460
  * Build the session a new planning conversation runs in and register it in
1467
1461
  * place of any live one, for {@link firstTurn} to run its first turn on.
1462
+ * A planner recorded signed out is asked again first, as the first turn's
1463
+ * gate would ask it (ADR-0052), since the judgment below comes before that
1464
+ * gate; with nothing to ask the session is registered in this tick.
1468
1465
  */
1469
1466
  openPlanningSession(sessionId, runners, workspace, modelOverride) {
1470
1467
  this.live(sessionId, workspace);
1471
1468
  const session = this.createSessionFor(sessionId, workspace, modelOverride);
1469
+ const asking = core.installedPlannerAnswered() ? session.askPlannerSignIn() : null;
1470
+ return asking ? asking.then(() => this.seatPlanningSession(sessionId, runners, workspace, session)) : this.seatPlanningSession(sessionId, runners, workspace, session);
1471
+ }
1472
+ seatPlanningSession(sessionId, runners, workspace, session) {
1473
+ this.live(sessionId, workspace);
1472
1474
  if (core.installedPlannerAnswered()) {
1473
1475
  session.assertPlannerReady();
1474
1476
  session.assertRunnersEnabled(runners);
@@ -1485,49 +1487,39 @@ var OrchestratorPool = class {
1485
1487
  * assistant message lives in conversationHistory).
1486
1488
  */
1487
1489
  async startPlanning(sessionId, goal, runners, workspace, modelOverride, options) {
1488
- const recheck = this.recheckSignedOutPlanner(workspace, this.plannerConfig(modelOverride, workspace).aiProvider);
1489
- if (recheck) await recheck;
1490
- const session = this.openPlanningSession(sessionId, runners, workspace, modelOverride);
1490
+ const opening = this.openPlanningSession(sessionId, runners, workspace, modelOverride);
1491
+ const session = opening instanceof Promise ? await opening : opening;
1491
1492
  return this.firstTurn(sessionId, session, () => session.startPlanning(goal, runners, { surface: options?.surface }));
1492
1493
  }
1493
1494
  /**
1494
1495
  * Continue the planner dialogue with the user's reply: queued behind the
1495
1496
  * first turn, even while it waits for the installed-planner probe, and
1496
1497
  * refused only while a turn of another kind holds the conversation. The
1497
- * session's own planner gate waits for the probe and judges the planner, as
1498
- * it does for every turn.
1498
+ * session's own planner gate waits for the probe, asks a planner recorded
1499
+ * signed out again, and judges the planner, as it does for every turn.
1499
1500
  */
1500
1501
  async continuePlanning(sessionId, message, workspace, surface) {
1501
1502
  const session = this.session(sessionId, workspace);
1502
- const recheck = this.recheckSignedOutPlanner(session.workspaceRoot, session.plannerProvider);
1503
- if (recheck) await recheck;
1504
1503
  return session.continueConversation(message, { surface });
1505
1504
  }
1506
1505
  /** Condense a session's conversation. The summary turn is a planner turn, so `cancelPlanning` stops it. */
1507
1506
  async compactConversation(sessionId, workspace) {
1508
1507
  const session = this.session(sessionId, workspace);
1509
- const recheck = this.recheckSignedOutPlanner(session.workspaceRoot, session.plannerProvider);
1510
- if (recheck) await recheck;
1511
1508
  const compaction = await session.compactConversation();
1512
1509
  return { ...compaction, plan: session.planState };
1513
1510
  }
1514
1511
  /**
1515
1512
  * Send now (ADR-0023): the Session stops the planner turn in flight and
1516
1513
  * answers this message in the turn that replaces it, ahead of anything
1517
- * waiting. The recheck is the one a reply does before the gate: a login done
1518
- * with the runner's own command is seen here too.
1514
+ * waiting.
1519
1515
  */
1520
1516
  async forceSendPrompt(sessionId, message, workspace, surface) {
1521
1517
  const session = this.session(sessionId, workspace);
1522
- const recheck = this.recheckSignedOutPlanner(session.workspaceRoot, session.plannerProvider);
1523
- if (recheck) await recheck;
1524
1518
  return session.forceSendPrompt(message, { surface });
1525
1519
  }
1526
1520
  /** Retry a compaction paused on the planner's context: a planner turn, and gated as one. */
1527
1521
  async retryPlannerCompaction(sessionId, workspace) {
1528
1522
  const session = this.session(sessionId, workspace);
1529
- const recheck = this.recheckSignedOutPlanner(session.workspaceRoot, session.plannerProvider);
1530
- if (recheck) await recheck;
1531
1523
  return session.retryPlannerCompaction();
1532
1524
  }
1533
1525
  /**
@@ -1676,6 +1668,9 @@ function plannerChangeOf(changes2, envChanges) {
1676
1668
  function changesPlanner(change) {
1677
1669
  return Object.values(change).some((v) => v !== void 0);
1678
1670
  }
1671
+ function isPlannerChoiceEnv(key) {
1672
+ return PLANNER_CHOICE_ENV.includes(key);
1673
+ }
1679
1674
  function stringField(value) {
1680
1675
  return typeof value === "string" ? value : void 0;
1681
1676
  }