@amenophis1er/foreman 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/preflight.ts CHANGED
@@ -21,6 +21,7 @@ import { defaultInstance, describeInstance, effectiveConfigDir } from './instanc
21
21
  import { discoverOllama, ollamaHost } from './ollama.js';
22
22
  import { serveHint, tailnetUrl, type Tailnet } from './tailscale.js';
23
23
  import { codexHome, codexModels, readCodexAuth } from './codex.js';
24
+ import { browserCheck } from './browser.js';
24
25
  import { createRequire } from 'node:module';
25
26
  import { fileURLToPath } from 'node:url';
26
27
 
@@ -116,12 +117,17 @@ async function checkAuth(): Promise<Check> {
116
117
  const name = 'Credentials';
117
118
  const { mode, source, account } = await detectAuth();
118
119
 
120
+ // Missing credentials are a warning, not a refusal to start. The dashboard
121
+ // is where a first-time user reads what to do and where a project gets an
122
+ // API key or an endpoint of its own; a server that will not come up until
123
+ // something is logged in leaves them with a terminal line and no next step.
124
+ // Missions cannot start meanwhile — dispatch checks the provider itself.
119
125
  if (mode === 'none') {
120
126
  return {
121
127
  name,
122
- status: 'error',
123
- detail: 'none found — no missions can run',
124
- fix: 'Log in with Claude Code, or: export ANTHROPIC_API_KEY=sk-ant-...',
128
+ status: 'warn',
129
+ detail: 'none found — missions cannot start until a provider is set up',
130
+ fix: 'Sign in with Claude Code on this machine (claude, then /login) and restart · or export ANTHROPIC_API_KEY · or give a project an API key or endpoint in Settings → Provider',
125
131
  };
126
132
  }
127
133
 
@@ -142,8 +148,12 @@ async function checkAuth(): Promise<Check> {
142
148
  return { name, status: 'ok', detail: `${MODE_LABEL[mode]} — ${who}` };
143
149
  }
144
150
 
145
- function checkInstance(): Check {
146
- return { name: 'Claude Code', status: 'ok', detail: describeInstance(defaultInstance()) };
151
+ async function checkInstance(): Promise<Check> {
152
+ const detail = describeInstance(defaultInstance());
153
+ // A tick beside an install nobody is signed into contradicts the line
154
+ // above it; say so here too, and leave the fix to the Credentials row.
155
+ if ((await detectAuth()).mode === 'none') return { name: 'Claude Code', status: 'warn', detail: `${detail} · not signed in` };
156
+ return { name: 'Claude Code', status: 'ok', detail };
147
157
  }
148
158
 
149
159
  /**
@@ -204,7 +214,7 @@ async function checkCodex(): Promise<Check | null> {
204
214
  };
205
215
  }
206
216
 
207
- async function checkPort(port: number): Promise<Check> {
217
+ async function checkPort(port: number, envVar: 'PORT' | 'FOREMAN_SERVICES_PORT' = 'PORT'): Promise<Check> {
208
218
  const name = `Port ${port}`;
209
219
  const inUse = await new Promise<boolean>((resolve) => {
210
220
  const probe = net.createServer();
@@ -213,12 +223,17 @@ async function checkPort(port: number): Promise<Check> {
213
223
  probe.listen(port, '127.0.0.1');
214
224
  });
215
225
 
226
+ // The main port taken is fatal: nothing can be served. The services port
227
+ // taken is a fact to report — the dashboard still works, and only the
228
+ // crew's exposed previews are off until the port is freed or moved.
229
+ // PORT + 1 is a guess, and a second Foreman (a dev server beside the
230
+ // installed one) is the likeliest thing sitting on it.
216
231
  return inUse
217
232
  ? {
218
233
  name,
219
- status: 'error',
220
- detail: 'already in use',
221
- fix: `Another Foreman may be running. Stop it, or: PORT=${port + 1} npm start`,
234
+ status: envVar === 'PORT' ? 'error' : 'warn',
235
+ detail: envVar === 'PORT' ? 'already in use' : 'already in use — exposed dev servers will be unavailable',
236
+ fix: `Another Foreman may be running. Stop it, or: ${envVar}=${port + 1} npm start`,
222
237
  }
223
238
  : { name, status: 'ok', detail: 'free' };
224
239
  }
@@ -247,40 +262,8 @@ async function checkHome(root: string): Promise<Check> {
247
262
  * `npx playwright install chromium`).
248
263
  */
249
264
  async function checkBrowser(): Promise<Check> {
250
- const name = 'Browser';
251
- const want = (process.env.FOREMAN_BROWSER || 'chrome').toLowerCase();
252
- const candidates: Record<string, string[]> = {
253
- chrome: process.platform === 'darwin'
254
- ? ['/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', path.join(os.homedir(), 'Applications/Google Chrome.app/Contents/MacOS/Google Chrome')]
255
- : process.platform === 'win32'
256
- ? [
257
- path.join(process.env.ProgramFiles ?? 'C:\\Program Files', 'Google', 'Chrome', 'Application', 'chrome.exe'),
258
- path.join(process.env['ProgramFiles(x86)'] ?? 'C:\\Program Files (x86)', 'Google', 'Chrome', 'Application', 'chrome.exe'),
259
- path.join(process.env.LOCALAPPDATA ?? '', 'Google', 'Chrome', 'Application', 'chrome.exe'),
260
- ]
261
- : ['/usr/bin/google-chrome', '/usr/bin/google-chrome-stable', '/opt/google/chrome/chrome', '/usr/bin/chromium', '/usr/bin/chromium-browser', '/snap/bin/chromium'],
262
- msedge: process.platform === 'darwin' ? ['/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge']
263
- : process.platform === 'win32' ? [path.join(process.env['ProgramFiles(x86)'] ?? 'C:\\Program Files (x86)', 'Microsoft', 'Edge', 'Application', 'msedge.exe')]
264
- : ['/usr/bin/microsoft-edge', '/usr/bin/microsoft-edge-stable'],
265
- firefox: process.platform === 'darwin' ? ['/Applications/Firefox.app/Contents/MacOS/firefox'] : ['/usr/bin/firefox'],
266
- };
267
- let found: string | null = null;
268
- if (want === 'chromium') {
269
- // Playwright's own build, wherever the MCP's playwright-core says it lives.
270
- try {
271
- const req = createRequire(fileURLToPath(new URL('../node_modules/@playwright/mcp/cli.js', import.meta.url)));
272
- const { chromium } = req('playwright-core') as { chromium: { executablePath(): string } };
273
- const p = chromium.executablePath();
274
- if (await exists(p)) found = p;
275
- } catch { /* no playwright-core to ask */ }
276
- return found
277
- ? { name, status: 'ok', detail: `Playwright Chromium — ${found}` }
278
- : { name, status: 'warn', detail: 'FOREMAN_BROWSER=chromium but Playwright Chromium is not installed; browser missions will fail', fix: 'npx playwright install chromium' };
279
- }
280
- for (const p of candidates[want] ?? []) if (await exists(p)) { found = p; break; }
281
- return found
282
- ? { name, status: 'ok', detail: `${want === 'chrome' ? 'Google Chrome' : want} — ${found}` }
283
- : { name, status: 'warn', detail: `no ${want === 'chrome' ? 'Google Chrome' : want} found; missions with browser on will fail`, fix: 'Install Google Chrome, or: npx playwright install chromium && FOREMAN_BROWSER=chromium foreman' };
265
+ // One detection for the row and for the crew: see src/browser.ts.
266
+ return { name: 'Browser', ...(await browserCheck()) };
284
267
  }
285
268
 
286
269
  /** Where the phone can reach this. Says so plainly either way — the answer decides which links work. */
@@ -309,6 +292,8 @@ async function checkUi(distDir: string): Promise<Check> {
309
292
  */
310
293
  export async function preflight(opts: {
311
294
  port: number;
295
+ /** The second listener, for the crew's exposed dev servers — see services.ts. */
296
+ servicesPort: number;
312
297
  foremanHome: string;
313
298
  distDir: string;
314
299
  /** Detected by the server before preflight; null when not on a tailnet. */
@@ -321,6 +306,7 @@ export async function preflight(opts: {
321
306
  checkOllama(),
322
307
  checkCodex(),
323
308
  checkPort(opts.port),
309
+ checkPort(opts.servicesPort, 'FOREMAN_SERVICES_PORT'),
324
310
  checkBrowser(),
325
311
  checkHome(opts.foremanHome),
326
312
  checkUi(opts.distDir),
@@ -0,0 +1,55 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { reconcileRole } from './role-provider.js';
4
+
5
+ const models = [
6
+ { id: 'fable' }, { id: 'opus' }, { id: 'sonnet' }, { id: 'haiku' },
7
+ { id: 'gemma4:12b', providerId: 'ollama-local' },
8
+ { id: 'gpt-5.5', providerId: 'codex-local' },
9
+ { id: 'kimi-k3', providerId: 'prov-abc' },
10
+ ];
11
+
12
+ test('reconcileRole: a consistent pairing passes through untouched', () => {
13
+ assert.deepEqual(reconcileRole('director', 'fable', undefined, models), { providerId: undefined });
14
+ assert.deepEqual(reconcileRole('workers', 'gpt-5.5', 'codex-local', models), { providerId: 'codex-local' });
15
+ assert.deepEqual(reconcileRole('workers', 'gemma4:12b', 'ollama-local', models), { providerId: 'ollama-local' });
16
+ });
17
+
18
+ test('reconcileRole: an Anthropic alias on the Codex provider is corrected — the bug this exists for', () => {
19
+ const r = reconcileRole('director', 'fable', 'codex-local', models);
20
+ assert.equal(r.providerId, undefined);
21
+ assert.match(r.note ?? '', /fable is served by the project's provider, not codex-local/);
22
+ });
23
+
24
+ test('reconcileRole: a listed model moves to whichever provider lists it', () => {
25
+ assert.equal(reconcileRole('workers', 'gemma4:12b', undefined, models).providerId, 'ollama-local');
26
+ assert.equal(reconcileRole('workers', 'gpt-5.5', 'ollama-local', models).providerId, 'codex-local');
27
+ assert.equal(reconcileRole('director', 'kimi-k3', 'codex-local', models).providerId, 'prov-abc');
28
+ assert.equal(reconcileRole('director', 'FABLE', 'codex-local', models).providerId, undefined);
29
+ });
30
+
31
+ test('reconcileRole: a provider pin without a model is stale and dropped', () => {
32
+ const r = reconcileRole('director', undefined, 'codex-local', models);
33
+ assert.equal(r.providerId, undefined);
34
+ assert.match(r.note ?? '', /no model was chosen/);
35
+ assert.deepEqual(reconcileRole('director', undefined, undefined, models), { providerId: undefined });
36
+ assert.deepEqual(reconcileRole('director', '', ' ', models), { providerId: undefined });
37
+ });
38
+
39
+ test('reconcileRole: an unlisted model keeps its provider, unless it is a Claude id on a closed provider', () => {
40
+ // A custom endpoint's model the discovery call missed: trust the pairing.
41
+ assert.deepEqual(reconcileRole('workers', 'mystery-9b', 'prov-abc', models), { providerId: 'prov-abc' });
42
+ assert.deepEqual(reconcileRole('workers', 'mystery-9b', undefined, models), { providerId: undefined });
43
+ // A full claude-* id nobody listed still cannot go through Codex or Ollama.
44
+ const r = reconcileRole('director', 'claude-opus-4-1', 'codex-local', models);
45
+ assert.equal(r.providerId, undefined);
46
+ assert.match(r.note ?? '', /Anthropic model/);
47
+ assert.equal(reconcileRole('director', 'claude-opus-4-1', 'ollama-local', models).providerId, undefined);
48
+ // On a custom endpoint a claude-* id may be a real proxy route; left alone.
49
+ assert.equal(reconcileRole('director', 'claude-opus-4-1', 'prov-abc', models).providerId, 'prov-abc');
50
+ });
51
+
52
+ test('reconcileRole: works with an empty model list (discovery failed) by the closed-provider rule alone', () => {
53
+ assert.equal(reconcileRole('director', 'fable', 'codex-local', []).providerId, undefined);
54
+ assert.equal(reconcileRole('director', 'gpt-5.5', 'codex-local', []).providerId, 'codex-local');
55
+ });
@@ -0,0 +1,65 @@
1
+ /**
2
+ * A role's provider, checked against its model at dispatch.
3
+ *
4
+ * A run carries, per role, a model and the provider that serves it. The two
5
+ * are picked together in every picker, but they travel separately — through
6
+ * settings files, drafts, older builds and resumes — and once they drift a
7
+ * mission fails on its first turn with an upstream 400 nobody chose: the
8
+ * Claude alias `fable` sent through the Codex gateway, say, which then
9
+ * answers that the model "is not supported when using Codex". Nothing was
10
+ * checking that the provider actually serves the model.
11
+ *
12
+ * This is that check. The model list is the authority: a model the machine
13
+ * offers is served by whichever provider lists it, and a role is corrected
14
+ * to that provider whatever it arrived with. A model the list does not know
15
+ * is left alone, except for the one pairing that can never work: an
16
+ * Anthropic model on a provider that only serves its own catalogue.
17
+ */
18
+ import { ANTHROPIC_ALIASES } from './anthropic-models.js';
19
+
20
+ export interface KnownModel {
21
+ id: string;
22
+ /** Absent means the project's own provider (Anthropic by default). */
23
+ providerId?: string;
24
+ }
25
+
26
+ /** Providers that serve exactly the models they list and nothing else. */
27
+ const CLOSED_PROVIDERS = new Set(['codex-local', 'ollama-local']);
28
+
29
+ export interface Reconciled {
30
+ providerId?: string;
31
+ /** Set when the pairing had to change; one sentence, for the transcript. */
32
+ note?: string;
33
+ }
34
+
35
+ const isAnthropic = (model: string) => ANTHROPIC_ALIASES.has(model) || /^claude-/i.test(model);
36
+
37
+ /**
38
+ * The provider a role should run on, given what it was handed and what the
39
+ * machine offers. Returns the same provider id and no note when nothing is
40
+ * wrong, which is the common case.
41
+ */
42
+ export function reconcileRole(
43
+ role: 'director' | 'workers', model: string | undefined, providerId: string | undefined, models: KnownModel[],
44
+ ): Reconciled {
45
+ const given = providerId?.trim() || undefined;
46
+ // No model means "inherit the project's default", which lives on the
47
+ // project's provider. A provider id left behind without a model is stale.
48
+ if (!model) {
49
+ return given ? { providerId: undefined, note: `${role}: no model was chosen, so the ${given} provider pin was dropped.` } : { providerId: undefined };
50
+ }
51
+ const want = model.trim().toLowerCase();
52
+ const known = models.find((m) => m.id.toLowerCase() === want);
53
+ if (known) {
54
+ const served = known.providerId || undefined;
55
+ if (served === given) return { providerId: given };
56
+ return {
57
+ providerId: served,
58
+ note: `${role}: ${model} is served by ${served ?? 'the project\'s provider'}, not ${given ?? 'the project\'s provider'}; corrected.`,
59
+ };
60
+ }
61
+ if (given && CLOSED_PROVIDERS.has(given) && isAnthropic(model)) {
62
+ return { providerId: undefined, note: `${role}: ${model} is an Anthropic model and ${given} cannot serve it; running it on the project's provider instead.` };
63
+ }
64
+ return { providerId: given };
65
+ }