@ignitionai/cogitator 0.5.1 → 0.5.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.
Files changed (63) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/LICENSE +21 -0
  3. package/README.md +80 -56
  4. package/dist/extensions/index.js +66 -29
  5. package/dist/src/app-updates.js +15 -0
  6. package/dist/src/auth-cli.js +18 -8
  7. package/dist/src/auth-http.js +13 -3
  8. package/dist/src/auth-state.js +41 -6
  9. package/dist/src/board.js +3 -1
  10. package/dist/src/browser-http.js +143 -0
  11. package/dist/src/classic-configuration.js +12 -4
  12. package/dist/src/cli.js +29 -2
  13. package/dist/src/config.js +10 -0
  14. package/dist/src/cron.js +91 -15
  15. package/dist/src/db.js +55 -3
  16. package/dist/src/durable/approvals.js +17 -2
  17. package/dist/src/durable/effect-gateway.js +23 -16
  18. package/dist/src/durable/manager.js +13 -4
  19. package/dist/src/durable/mission-runtime.js +204 -46
  20. package/dist/src/durable/recovery.js +37 -0
  21. package/dist/src/durable/workers.js +22 -14
  22. package/dist/src/execution/confinement.js +2 -1
  23. package/dist/src/execution/worker-supervisor.js +222 -0
  24. package/dist/src/git-import.js +187 -0
  25. package/dist/src/github-login.js +101 -0
  26. package/dist/src/history.js +6 -1
  27. package/dist/src/host-pi.js +9 -6
  28. package/dist/src/json-files.js +7 -4
  29. package/dist/src/machine-metrics.js +49 -0
  30. package/dist/src/machine-types.js +1 -0
  31. package/dist/src/mcp-cli.js +63 -0
  32. package/dist/src/mcp-http.js +164 -0
  33. package/dist/src/mcp-oauth.js +502 -0
  34. package/dist/src/mcp-server.js +6 -136
  35. package/dist/src/mcp-tokens.js +104 -0
  36. package/dist/src/mcp-tools.js +264 -0
  37. package/dist/src/native-auth.js +9 -2
  38. package/dist/src/packages.js +190 -0
  39. package/dist/src/paths.js +1 -0
  40. package/dist/src/pi-child.js +180 -0
  41. package/dist/src/pi-rpc-wrap.js +84 -0
  42. package/dist/src/pool.js +58 -7
  43. package/dist/src/project-setup.js +63 -0
  44. package/dist/src/provider-login.js +72 -13
  45. package/dist/src/reap-orphans.js +138 -0
  46. package/dist/src/registry.js +66 -43
  47. package/dist/src/schemas.js +19 -4
  48. package/dist/src/server-compat.js +173 -0
  49. package/dist/src/server-identity.js +69 -0
  50. package/dist/src/server.js +534 -41
  51. package/dist/src/shared-browser.js +357 -0
  52. package/dist/src/skills-import.js +13 -4
  53. package/dist/src/skills-upload.js +68 -0
  54. package/dist/src/spawner.js +20 -4
  55. package/dist/src/subagents.js +242 -0
  56. package/dist/src/team-queue.js +262 -0
  57. package/package.json +8 -3
  58. package/web/dist/assets/{BrandLogo3D-BvZPXohP.js → BrandLogo3D-CHGXwTbZ.js} +1 -1
  59. package/web/dist/assets/index-DGLkhrE7.css +1 -0
  60. package/web/dist/assets/index-P0WoXBgX.js +84 -0
  61. package/web/dist/index.html +2 -2
  62. package/web/dist/assets/index-7Dyl-4v7.css +0 -1
  63. package/web/dist/assets/index-CdqzcBU-.js +0 -83
@@ -5,7 +5,7 @@ import { createServer } from "node:net";
5
5
  import { join, resolve } from "node:path";
6
6
  import Database from "better-sqlite3";
7
7
  import { z } from "zod";
8
- import { assertOwner, AuthError, createNativeAuth, issueSetupKey, migrateAuth } from "./native-auth.js";
8
+ import { assertOwner, AuthError, createNativeAuth, issueSetupKey, migrateAuth, requiresMfa } from "./native-auth.js";
9
9
  export const authPaths = (home) => ({ directory: join(home, "auth"), required: join(home, "auth-required"),
10
10
  maintenance: join(home, "auth-maintenance"), config: join(home, "auth", "config.json"), db: join(home, "auth", "auth.sqlite"), socket: join(home, "auth", "owner.sock") });
11
11
  /** Conservative Unix sun_path limit, including space for its terminating NUL on supported platforms. */
@@ -106,7 +106,7 @@ export async function reservePort(port) {
106
106
  await new Promise((done, reject) => { server.once("error", reject); server.listen(port, "127.0.0.1", done); });
107
107
  return () => new Promise((done, reject) => server.close(err => err ? reject(err) : done()));
108
108
  }
109
- const configSchema = z.object({ origin: z.string().url(), secret: z.string().min(32), replayKey: z.string().min(32), ownerId: z.string().min(1) }).strict();
109
+ const configSchema = z.object({ origin: z.string().url(), secret: z.string().min(32), replayKey: z.string().min(32), ownerId: z.string().min(1), authentication: z.enum(["password", "password-mfa"]).optional() }).strict();
110
110
  export function validateOrigin(origin) {
111
111
  const url = new URL(origin);
112
112
  if (url.protocol !== "https:" || url.username || url.password || url.origin !== origin)
@@ -160,7 +160,7 @@ export function loadAuth(home, allowMaintenance = false) {
160
160
  } };
161
161
  }
162
162
  /** Caller holds the data-home lock and configured port reservation. No application catalog is opened. */
163
- export async function initializeOwner(home, origin, email, password) {
163
+ export async function initializeOwner(home, origin, email, password, authentication = "password") {
164
164
  validateOwnerSocketPath(home);
165
165
  validateOrigin(origin);
166
166
  z.email().parse(email);
@@ -191,11 +191,13 @@ export async function initializeOwner(home, origin, email, password) {
191
191
  throw new Error("Protected state already exists; use offline recovery.");
192
192
  if (config.origin !== origin)
193
193
  throw new Error("Resume initialization with the original HTTPS origin.");
194
+ if ((config.authentication ?? "password-mfa") !== authentication)
195
+ throw new Error("Resume initialization with the original authentication mode.");
194
196
  }
195
197
  else {
196
198
  if (existsSync(p.db))
197
199
  throw new Error("Missing auth secret; restore the private auth backup, never replace credentials in place.");
198
- config = { origin, secret: randomBytes(48).toString("base64url"), replayKey: randomBytes(48).toString("base64url"), ownerId: "pending" };
200
+ config = { origin, secret: randomBytes(48).toString("base64url"), replayKey: randomBytes(48).toString("base64url"), ownerId: "pending", authentication };
199
201
  privateWrite(p.config, JSON.stringify(config), true);
200
202
  }
201
203
  if (!existsSync(p.db))
@@ -231,7 +233,7 @@ export async function initializeOwner(home, origin, email, password) {
231
233
  privateWrite(`${p.config}.tmp`, JSON.stringify(config), true);
232
234
  renameSync(`${p.config}.tmp`, p.config);
233
235
  syncDirectory(p.directory);
234
- const key = issueSetupKey(db, config);
236
+ const key = requiresMfa(config) ? issueSetupKey(db, config) : "";
235
237
  assertOwner(db, config);
236
238
  finishMaintenance(home);
237
239
  return key;
@@ -262,7 +264,7 @@ export async function recoverOwner(home, password) {
262
264
  db.exec("DELETE FROM twoFactor; DELETE FROM session; DELETE FROM verification;");
263
265
  db.prepare("UPDATE authPolicy SET claimId=NULL, claimExpires=0, setupDigest=NULL, setupExpires=0 WHERE singleton=1").run();
264
266
  }).immediate();
265
- const key = issueSetupKey(db, config);
267
+ const key = requiresMfa(config) ? issueSetupKey(db, config) : "";
266
268
  assertOwner(db, config);
267
269
  finishMaintenance(home);
268
270
  return key;
@@ -275,6 +277,8 @@ export async function recoverOwner(home, password) {
275
277
  export function replaceSetupKey(home) {
276
278
  const state = loadAuth(home);
277
279
  try {
280
+ if (!requiresMfa(state.config))
281
+ throw new Error("Password-only authentication does not require a setup key.");
278
282
  if (state.db.prepare("SELECT twoFactorEnabled FROM user WHERE id=?").get(state.config.ownerId).twoFactorEnabled)
279
283
  throw new Error("Already enrolled; use offline recovery.");
280
284
  maintenance(home);
@@ -287,3 +291,34 @@ export function replaceSetupKey(home) {
287
291
  state.db.close();
288
292
  }
289
293
  }
294
+ /** Offline OS-owner operation: caller holds the home lock and reserves the HTTP port. */
295
+ export function setAuthenticationMode(home, authentication) {
296
+ if (authentication !== "password" && authentication !== "password-mfa")
297
+ throw new Error("Invalid authentication mode.");
298
+ const state = loadAuth(home, true);
299
+ try {
300
+ maintenance(home);
301
+ state.db.transaction(() => {
302
+ state.db.exec("DELETE FROM session; DELETE FROM verification; DELETE FROM twoFactor;");
303
+ state.db.prepare("UPDATE user SET twoFactorEnabled=0 WHERE id=?").run(state.config.ownerId);
304
+ state.db.prepare("UPDATE authPolicy SET setupDigest=NULL, setupExpires=0, claimId=NULL, claimExpires=0 WHERE singleton=1").run();
305
+ }).immediate();
306
+ const config = { ...state.config, authentication };
307
+ const p = authPaths(home);
308
+ const tmp = `${p.config}.tmp`;
309
+ if (existsSync(tmp)) {
310
+ checkPrivate(tmp);
311
+ unlinkSync(tmp);
312
+ }
313
+ privateWrite(tmp, JSON.stringify(config), true);
314
+ renameSync(tmp, p.config);
315
+ syncDirectory(p.directory);
316
+ const key = requiresMfa(config) ? issueSetupKey(state.db, config) : "";
317
+ assertOwner(state.db, config);
318
+ finishMaintenance(home);
319
+ return key;
320
+ }
321
+ finally {
322
+ state.db.close();
323
+ }
324
+ }
package/dist/src/board.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { githubConfigured, githubEnv } from "./github-login.js";
2
+ import { getPaths } from "./paths.js";
1
3
  import { checkRequestAuthorization } from "./request-authorization.js";
2
4
  import { execFile } from "node:child_process";
3
5
  import { join } from "node:path";
@@ -24,7 +26,7 @@ const GH_ISSUE_LIMIT = 200;
24
26
  function gh(wsDir, args, timeoutMs = GH_TIMEOUT_MS) {
25
27
  checkRequestAuthorization();
26
28
  return new Promise((resolve) => {
27
- execFile("gh", args, { cwd: wsDir, timeout: timeoutMs, maxBuffer: GH_MAX_BUFFER }, (err, stdout, stderr) => {
29
+ execFile("gh", args, { cwd: wsDir, timeout: timeoutMs, maxBuffer: GH_MAX_BUFFER, env: githubConfigured(getPaths()) ? githubEnv(getPaths()) : process.env }, (err, stdout, stderr) => {
28
30
  resolve({ ok: !err, stdout: String(stdout), stderr: String(stderr).trim() });
29
31
  });
30
32
  });
@@ -0,0 +1,143 @@
1
+ import { WebSocketServer } from 'ws';
2
+ import { z } from 'zod';
3
+ import { BrowserProblem } from './shared-browser.js';
4
+ import { AuthError, ownerSession, requireApproved, sessionById } from './native-auth.js';
5
+ import { browserHeaders, validateBoundary, localCheck } from './auth-http.js';
6
+ import { requestAuthorization } from './request-authorization.js';
7
+ export function browserRoutes(browser, privateRequest, send, protectedMode) {
8
+ const ui = (fn) => async (ctx) => {
9
+ if (privateRequest(ctx.req))
10
+ throw new AuthError('FORBIDDEN', 403);
11
+ try {
12
+ ctx.check();
13
+ const value = await fn(ctx);
14
+ ctx.check();
15
+ send(ctx.res, 200, value);
16
+ }
17
+ catch (error) {
18
+ if (error instanceof BrowserProblem || error instanceof z.ZodError)
19
+ send(ctx.res, 400, { error: error instanceof BrowserProblem ? error.message : 'BROWSER_INVALID_INPUT' });
20
+ else if (error instanceof AuthError)
21
+ throw error;
22
+ else
23
+ send(ctx.res, 400, { error: 'BROWSER_ACTION_FAILED' });
24
+ }
25
+ };
26
+ return [
27
+ ['GET', '/api/browser/profiles', ui(() => ({ profiles: browser.list(), installed: browser.installed() }))],
28
+ ['POST', '/api/browser/profiles', ui(ctx => browser.create(ctx.body))],
29
+ ['PUT', '/api/browser/profiles/:id', ui(ctx => browser.update(ctx.params.id, ctx.body))],
30
+ ['GET', '/api/browser/profiles/:id/tabs', ui(async (ctx) => ({ tabs: await browser.tabs(ctx.params.id) }))],
31
+ ['POST', '/api/browser/profiles/:id/control', ui(ctx => browser.control(ctx.params.id, z.object({ human: z.boolean() }).strict().parse(ctx.body).human))],
32
+ ['POST', '/api/browser/profiles/:id/action', ui(ctx => browser.act(ctx.params.id, ctx.body))],
33
+ ['POST', '/api/browser/profiles/:id/stop', ui(ctx => browser.stop(ctx.params.id))],
34
+ ['POST', '/api/browser/agent', async (ctx) => {
35
+ // Agent identity is attached by the bundled extension, not accepted on the public HTTP surface.
36
+ if (!privateRequest(ctx.req) && protectedMode)
37
+ throw new AuthError('FORBIDDEN', 403);
38
+ try {
39
+ const b = z.object({ conversationId: z.string().uuid(), profileId: z.string().uuid().optional(), command: z.unknown().optional() }).strict().parse(ctx.body);
40
+ ctx.check();
41
+ const result = b.profileId ? await browser.act(b.profileId, b.command, b.conversationId) : { profiles: browser.agentProfiles(b.conversationId) };
42
+ ctx.check();
43
+ send(ctx.res, 200, result);
44
+ }
45
+ catch (error) {
46
+ if (error instanceof BrowserProblem || error instanceof z.ZodError)
47
+ send(ctx.res, 400, { error: error instanceof BrowserProblem ? error.message : 'BROWSER_INVALID_INPUT' });
48
+ else if (error instanceof AuthError)
49
+ throw error;
50
+ else
51
+ send(ctx.res, 400, { error: 'BROWSER_ACTION_FAILED' });
52
+ }
53
+ }],
54
+ ];
55
+ }
56
+ const cleanup = new WeakMap();
57
+ export async function closeSharedBrowser(server) { await cleanup.get(server)?.(); }
58
+ export function attachBrowserView(app, browser, home, state) {
59
+ const ws = new WebSocketServer({ noServer: true, maxPayload: 20000, perMessageDeflate: false });
60
+ const dispose = async () => { for (const peer of ws.clients)
61
+ peer.terminate(); ws.close(); await browser.dispose(); };
62
+ cleanup.set(app, dispose);
63
+ app.once('close', () => { void dispose(); });
64
+ app.on('upgrade', async (req, socket, head) => {
65
+ try {
66
+ const match = /^\/api\/browser\/view\/([a-f0-9-]{36})\/([a-f0-9-]{36})$/.exec(req.url ?? '');
67
+ if (!match)
68
+ throw new Error('route');
69
+ let check = () => localCheck(home);
70
+ if (state) {
71
+ state.assertAvailable();
72
+ validateBoundary(req, state);
73
+ if (req.headers.origin !== state.config.origin)
74
+ throw new Error('origin');
75
+ const session = await ownerSession(state.auth, state.db, state.config, browserHeaders(req));
76
+ requireApproved(session, state.config);
77
+ check = () => { state.assertAvailable(); requireApproved(sessionById(state.db, state.config, session.id), state.config); };
78
+ }
79
+ else {
80
+ const expected = `http://${req.headers.host}`;
81
+ if (req.headers.origin !== expected || !/^(127\.0\.0\.1|localhost|\[::1\])(:\d+)?$/.test(req.headers.host ?? ''))
82
+ throw new Error('origin');
83
+ }
84
+ check();
85
+ const [profileId, tabId] = [match[1], match[2]];
86
+ if (!(await browser.tabs(profileId)).some(t => t.id === tabId))
87
+ throw new Error('tab');
88
+ check();
89
+ ws.handleUpgrade(req, socket, head, peer => {
90
+ let stop;
91
+ let busy = false;
92
+ const alive = () => { check(); if (peer.readyState !== peer.OPEN)
93
+ throw new Error('closed'); };
94
+ void browser.watch(profileId, tabId, frame => { if (peer.bufferedAmount < 512000 && peer.readyState === peer.OPEN)
95
+ peer.send(frame); }, alive)
96
+ .then(value => { stop = value; if (peer.readyState !== peer.OPEN)
97
+ void stop(); }).catch(() => peer.close(1011));
98
+ const timer = setInterval(() => { try {
99
+ alive();
100
+ peer.send(JSON.stringify({ type: 'state', humanControl: browser.list().find(p => p.id === profileId)?.humanControl }));
101
+ }
102
+ catch {
103
+ peer.close(1008);
104
+ } }, 1000);
105
+ peer.on('message', data => {
106
+ if (busy)
107
+ return;
108
+ busy = true;
109
+ void requestAuthorization.run(alive, async () => {
110
+ try {
111
+ alive();
112
+ const message = JSON.parse(data.toString());
113
+ if (message.action === 'type') {
114
+ const input = z.object({ action: z.literal('type'), text: z.string().max(16000) }).strict().parse(message);
115
+ await browser.type(profileId, tabId, input.text);
116
+ }
117
+ else {
118
+ // UI transport is deliberately limited to human input, not arbitrary CDP or script execution.
119
+ if (!['click', 'press', 'scroll'].includes(message.action))
120
+ throw new BrowserProblem('BROWSER_INVALID_INPUT');
121
+ await browser.act(profileId, { ...message, tabId });
122
+ }
123
+ alive();
124
+ peer.send(JSON.stringify({ type: 'ack' }));
125
+ }
126
+ catch (error) {
127
+ if (peer.readyState === peer.OPEN)
128
+ peer.send(JSON.stringify({ type: 'error', error: error instanceof BrowserProblem ? error.message : 'BROWSER_INPUT_FAILED' }));
129
+ }
130
+ finally {
131
+ busy = false;
132
+ }
133
+ });
134
+ });
135
+ peer.on('error', () => peer.terminate());
136
+ peer.once('close', () => { clearInterval(timer); void stop?.(); });
137
+ });
138
+ }
139
+ catch {
140
+ socket.end('HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n');
141
+ }
142
+ });
143
+ }
@@ -5,9 +5,13 @@ import { checkRequestAuthorization } from "./request-authorization.js";
5
5
  import { THINKING_LEVELS } from "./schemas.js";
6
6
  export class ConfigurationError extends Error {
7
7
  status;
8
- constructor(message, status = 502) {
8
+ code;
9
+ provider;
10
+ constructor(message, status = 502, code = "CONFIGURATION_FAILED", provider) {
9
11
  super(message);
10
12
  this.status = status;
13
+ this.code = code;
14
+ this.provider = provider;
11
15
  }
12
16
  }
13
17
  function conversation(db, id) {
@@ -25,11 +29,15 @@ async function readTuple(client) {
25
29
  return { provider: state.model.provider, model: state.model.id, thinking: state.thinkingLevel };
26
30
  }
27
31
  async function select(client, target) {
32
+ const available = await client.getAvailableModels();
33
+ if (!available.some(m => m.provider === target.provider)) {
34
+ throw new ConfigurationError("Provider connection required", 409, "PROVIDER_NOT_CONFIGURED", target.provider);
35
+ }
36
+ if (!available.some(m => m.provider === target.provider && m.id === target.model)) {
37
+ throw new ConfigurationError("Model unavailable for this provider", 422, "MODEL_UNAVAILABLE", target.provider);
38
+ }
28
39
  const before = await readTuple(client);
29
40
  const thinking = target.thinking ?? before.thinking;
30
- if (!(await client.getAvailableModels()).some(m => m.provider === target.provider && m.id === target.model)) {
31
- throw new ConfigurationError("unsupported model", 422);
32
- }
33
41
  if (before.provider !== target.provider || before.model !== target.model)
34
42
  await client.setModel(target.provider, target.model);
35
43
  const levels = await client.getAvailableThinkingLevels();
package/dist/src/cli.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import { closeSharedBrowser } from "./browser-http.js";
2
3
  import { EventEmitter } from "node:events";
3
4
  import { createServer } from "node:http";
4
5
  import { piCliPath, piCodingAgent } from "./host-pi.js";
@@ -11,10 +12,15 @@ import { getPaths } from "./paths.js";
11
12
  import { PiPool } from "./pool.js";
12
13
  import { createApp } from "./server.js";
13
14
  import { authCommand } from "./auth-cli.js";
15
+ import { mcpCommand } from "./mcp-cli.js";
14
16
  import { loadAuth, lockHome, protectedRequired } from "./auth-state.js";
15
17
  import { listenOwnerSocket } from "./owner-transport.js";
16
18
  import { makeSpawner } from "./spawner.js";
19
+ import { disposeSetprivShims, guardedRpcSpawn, reapStaleSetprivShims } from "./pi-child.js";
17
20
  import { mkdirSync } from "node:fs";
21
+ import { packageVersion } from "./pi.js";
22
+ import { isProcessAlive, removeServerIdentity, writeServerIdentity } from "./server-identity.js";
23
+ import { assertNoLiveInstance, probeServer } from "./server-compat.js";
18
24
  const SWEEP_INTERVAL_MS = 60_000;
19
25
  const CRON_TICK_MS = 30_000;
20
26
  const paths = getPaths();
@@ -23,9 +29,13 @@ try {
23
29
  const args = process.argv.slice(2);
24
30
  if (args[0] === "auth")
25
31
  await authCommand(paths.home, PORT, args.slice(1));
32
+ else if (args[0] === "mcp") {
33
+ mkdirSync(paths.home, { recursive: true });
34
+ mcpCommand(paths.home, PORT, args.slice(1));
35
+ }
26
36
  else {
27
37
  if (args.length > 1 || args.some(arg => arg !== "--auth-required"))
28
- throw new Error("Usage: cogitator [--auth-required] | cogitator auth <init|recover|setup-key>");
38
+ throw new Error("Usage: cogitator [--auth-required] | cogitator auth <init|recover|setup-key> | cogitator mcp token <create|list|revoke>");
29
39
  await start(required || args.length === 1);
30
40
  }
31
41
  }
@@ -36,21 +46,33 @@ catch (err) {
36
46
  async function start(explicit) {
37
47
  mkdirSync(paths.home, { recursive: true });
38
48
  const unlock = process.platform === "win32" ? () => { } : lockHome(paths.home);
49
+ // Home verrouillé : les shims pdeathsig-* restants viennent d'un hôte mort (kill -9) et
50
+ // ne sont référencés par aucun process vivant. Best-effort, jamais bloquant au démarrage.
51
+ try {
52
+ reapStaleSetprivShims(paths.home);
53
+ }
54
+ catch { /* nettoyage opportuniste */ }
39
55
  let auth;
40
56
  let db;
41
57
  const server = createServer((_req, res) => { res.writeHead(503, { "content-type": "application/json", "cache-control": "no-store" }); res.end('{"error":"AUTH_UNAVAILABLE"}'); });
42
58
  let privateServer;
43
59
  try {
44
60
  auth = protectedRequired(paths.home, explicit) ? loadAuth(paths.home) : undefined;
61
+ // Un pidfile encore revendiqué par un serveur vivant qui répond interdit le takeover :
62
+ // on s'arrête proprement plutôt que d'écraser l'identité d'une instance en service.
63
+ await assertNoLiveInstance(paths.home, { probeServer, isAlive: isProcessAlive, host: HOST });
45
64
  // Detect even an old incompatible binary on the configured port before catalog/seed side effects.
46
65
  await new Promise((done, reject) => { server.once("error", reject); server.listen(PORT, HOST, done); });
66
+ // Identité publiée dès que le port est tenu : l'extension `/cogitator` compare alors
67
+ // les versions et peut cibler ce pid précis lors d'une mise à jour.
68
+ writeServerIdentity(paths.home, { pid: process.pid, version: packageVersion(), port: PORT, startedAt: new Date().toISOString() });
47
69
  privateServer = auth ? await listenOwnerSocket(server, paths.home) : undefined;
48
70
  const opened = openDb(paths.db);
49
71
  db = opened.db;
50
72
  seedDefaultAgents(db, paths);
51
73
  const catalog = db;
52
74
  const pool = new PiPool({
53
- factory: (opts) => new piCodingAgent.RpcClient({ cliPath: piCliPath, cwd: opts.cwd, args: opts.args, env: opts.env }),
75
+ factory: (opts) => new piCodingAgent.RpcClient(guardedRpcSpawn(piCliPath, opts)),
54
76
  callbacks: { onStatus: (convId, status) => setConversationStatus(catalog, convId, status) },
55
77
  });
56
78
  const events = new EventEmitter();
@@ -67,10 +89,13 @@ async function start(explicit) {
67
89
  stopping = true;
68
90
  clearInterval(sweeper);
69
91
  cron.stop();
92
+ await closeSharedBrowser(server);
70
93
  privateServer?.closeAllConnections();
71
94
  server.closeAllConnections();
72
95
  await Promise.all([close(server), ...(privateServer ? [close(privateServer)] : [])]);
96
+ removeServerIdentity(paths.home, process.pid);
73
97
  await pool.dispose();
98
+ disposeSetprivShims();
74
99
  auth?.db.close();
75
100
  catalog.close();
76
101
  unlock();
@@ -85,9 +110,11 @@ async function start(explicit) {
85
110
  await close(privateServer);
86
111
  if (server.listening)
87
112
  await close(server);
113
+ removeServerIdentity(paths.home, process.pid);
88
114
  auth?.db.close();
89
115
  db?.close();
90
116
  unlock();
117
+ disposeSetprivShims();
91
118
  throw err;
92
119
  }
93
120
  }
@@ -12,3 +12,13 @@ export function authenticationRequired(env = process.env) {
12
12
  throw new Error("COGITATOR_AUTH_REQUIRED must be unset, 0, or 1.");
13
13
  return value === "1";
14
14
  }
15
+ const DEFAULT_REAP_INTERVAL_MS = 15_000;
16
+ /** Intervalle du reap d'orphelins (ms). `COGITATOR_REAP_INTERVAL_MS=0` désactive. Défaut 15 s. */
17
+ export function reapIntervalMs(env = process.env) {
18
+ const raw = env.COGITATOR_REAP_INTERVAL_MS;
19
+ if (raw === undefined || raw === "")
20
+ return DEFAULT_REAP_INTERVAL_MS;
21
+ if (!/^\d+$/.test(raw))
22
+ return DEFAULT_REAP_INTERVAL_MS;
23
+ return Number(raw);
24
+ }
package/dist/src/cron.js CHANGED
@@ -3,12 +3,66 @@ import { CronExpressionParser } from "cron-parser";
3
3
  import { getAgent } from "./agents.js";
4
4
  import { createConversation, getConversation } from "./conversations.js";
5
5
  import { spawnConfigFromPreset } from "./spawn.js";
6
+ import { enqueue, originHop, } from "./team-queue.js";
6
7
  /** Bornes du service (surchargeables par les options pour les tests). */
7
- const DEFAULT_RUN_TIMEOUT_MS = 15 * 60_000;
8
8
  const DEFAULT_TICK_MS = 30_000;
9
9
  /** busy_policy=queue : on attend la fin du run actif, avec garde-fou. */
10
10
  const QUEUE_POLL_MS = 2_000;
11
11
  const QUEUE_WAIT_MAX_MS = 10 * 60_000;
12
+ const QUEUE_ITEM_POLL_MS = 20;
13
+ const TERMINAL_QUEUE = new Set(["done", "failed", "interrupted", "cancelled"]);
14
+ export function waitForQueueItem(db, itemId, pollMs = QUEUE_ITEM_POLL_MS) {
15
+ return new Promise((resolve, reject) => {
16
+ const tick = () => {
17
+ const row = db.prepare("SELECT * FROM queue_item WHERE id = ?").get(itemId);
18
+ if (!row) {
19
+ reject(new Error("queue_item disparu"));
20
+ return;
21
+ }
22
+ if (TERMINAL_QUEUE.has(row.status)) {
23
+ resolve(row);
24
+ return;
25
+ }
26
+ setTimeout(tick, pollMs);
27
+ };
28
+ tick();
29
+ });
30
+ }
31
+ /** Met le prompt cron en file sur la lane de la conversation cible, puis attend le statut de l'item. */
32
+ export function queueCronRun(db, team, input) {
33
+ const inherited = originHop(db, input.conversationId);
34
+ const { id, laneId } = enqueue(db, {
35
+ conversationId: input.conversationId,
36
+ body: input.prompt,
37
+ fromAgentId: input.fromAgentId ?? null,
38
+ hop: inherited.hop,
39
+ chainId: inherited.chainId,
40
+ idempotencyKey: input.idempotencyKey,
41
+ });
42
+ void team.kick(laneId).catch(() => undefined);
43
+ return waitForQueueItem(db, id);
44
+ }
45
+ function cronStatusFromItem(item) {
46
+ switch (item.status) {
47
+ case "done":
48
+ return { status: "ok" };
49
+ case "failed": {
50
+ const error = item.error ?? "livraison échouée";
51
+ return { status: /timeout|stall|sans progrès|progress/i.test(error) ? "timeout" : "error", error };
52
+ }
53
+ case "interrupted":
54
+ return { status: "error", error: item.error ?? "interrupted" };
55
+ case "cancelled":
56
+ return { status: "error", error: "cancelled" };
57
+ case "queued":
58
+ case "delivering":
59
+ return { status: "error", error: `item encore ${item.status}` };
60
+ default: {
61
+ const _never = item.status;
62
+ return _never;
63
+ }
64
+ }
65
+ }
12
66
  // ---------- DB ops ----------
13
67
  export function listTasks(db) {
14
68
  const rows = db.prepare("SELECT * FROM cron_task ORDER BY name").all();
@@ -122,12 +176,26 @@ export class CronService {
122
176
  opts;
123
177
  timer = null;
124
178
  firing = new Set(); // garde anti-recouvrement dans un tick
179
+ team;
180
+ lastTickAt = null;
125
181
  constructor(opts) {
126
182
  this.opts = opts;
183
+ this.team = opts.team;
184
+ }
185
+ /** Branche le dispatcher créé par createApp (même instance que les messages UI). */
186
+ attachTeam(team) {
187
+ this.team = team;
188
+ }
189
+ requireTeam() {
190
+ if (!this.team)
191
+ throw new Error("file durable absente (LaneDispatcher)");
192
+ return this.team;
127
193
  }
128
194
  /** Boucle de tick ; appelée par le timer et testable directement. */
129
195
  async tick(now = new Date()) {
130
196
  const { db } = this.opts;
197
+ const previousTick = this.lastTickAt ?? now;
198
+ this.lastTickAt = now;
131
199
  const tasks = db.prepare("SELECT * FROM cron_task WHERE enabled = 1").all();
132
200
  for (const task of tasks) {
133
201
  if (this.firing.has(task.id))
@@ -136,26 +204,32 @@ export class CronService {
136
204
  if (!next || next > now)
137
205
  continue;
138
206
  if (!task.catchup) {
139
- // rattrapage désactivé : on avance au prochain créneau sans exécuter
140
- db.prepare("UPDATE cron_task SET last_run_at = ? WHERE id = ?").run(now.toISOString(), task.id);
141
- continue;
207
+ // Seul le retard antérieur à cette instance est ignoré. Une occurrence
208
+ // tombée entre deux ticks du service reste une exécution normale.
209
+ const regular = nextAfter({ ...task, last_run_at: previousTick.toISOString() }, now);
210
+ if (!regular || regular > now) {
211
+ db.prepare("UPDATE cron_task SET last_run_at = ? WHERE id = ?").run(now.toISOString(), task.id);
212
+ continue;
213
+ }
142
214
  }
143
- void this.fireNow(task.id).catch(() => undefined);
215
+ void this.fireNow(task.id, now).catch(() => undefined);
144
216
  }
145
217
  }
146
218
  start(intervalMs = DEFAULT_TICK_MS) {
147
219
  if (this.timer)
148
220
  return;
221
+ this.lastTickAt = new Date();
149
222
  this.timer = setInterval(() => this.tick().catch(() => undefined), intervalMs);
150
223
  }
151
224
  stop() {
152
225
  if (this.timer)
153
226
  clearInterval(this.timer);
154
227
  this.timer = null;
228
+ this.lastTickAt = null;
155
229
  }
156
230
  /** Tire une exécution (manuelle ou planifiée). Crée toujours un CronRun (O3). */
157
- async fireNow(taskId) {
158
- const { db, spawner, pool } = this.opts;
231
+ async fireNow(taskId, now = new Date()) {
232
+ const { db, pool } = this.opts;
159
233
  const task = getTask(db, taskId);
160
234
  if (!task)
161
235
  return null;
@@ -167,7 +241,7 @@ export class CronService {
167
241
  }
168
242
  this.firing.add(taskId);
169
243
  const runId = createRun(db, taskId);
170
- db.prepare("UPDATE cron_task SET last_run_at = ? WHERE id = ?").run(new Date().toISOString(), taskId);
244
+ db.prepare("UPDATE cron_task SET last_run_at = ? WHERE id = ?").run(now.toISOString(), taskId);
171
245
  this.opts.notify?.({ type: "cron_run_started", taskId, runId, name: task.name });
172
246
  let convId = null;
173
247
  try {
@@ -180,14 +254,16 @@ export class CronService {
180
254
  }
181
255
  const conv = this.createRunConversation(task, preset);
182
256
  convId = conv.id;
183
- await spawner.ensure(conv);
184
- // s'abonner AVANT de prompt : un LLM rapide peut settled avant l'abonnement sinon
185
- const settled = pool.waitForSettled(conv.id, this.opts.runTimeoutMs ?? DEFAULT_RUN_TIMEOUT_MS);
186
- await pool.prompt(conv.id, task.prompt);
187
- await settled;
257
+ const item = await queueCronRun(db, this.requireTeam(), {
258
+ conversationId: conv.id,
259
+ prompt: task.prompt,
260
+ fromAgentId: preset.id,
261
+ idempotencyKey: `cron:${runId}`,
262
+ });
263
+ const mapped = cronStatusFromItem(item);
188
264
  const finalConv = db.prepare("SELECT session_file FROM conversation WHERE id = ?").get(conv.id);
189
- finishRun(db, runId, "ok", finalConv.session_file);
190
- this.opts.notify?.({ type: "cron_run_finished", taskId, runId, name: task.name, status: "ok", sessionFile: finalConv.session_file });
265
+ finishRun(db, runId, mapped.status, finalConv.session_file, mapped.error);
266
+ this.opts.notify?.({ type: "cron_run_finished", taskId, runId, name: task.name, status: mapped.status, sessionFile: finalConv.session_file, error: mapped.error });
191
267
  return this.getRun(runId);
192
268
  }
193
269
  catch (err) {
package/dist/src/db.js CHANGED
@@ -132,6 +132,51 @@ CREATE INDEX IF NOT EXISTS idx_group_message_group ON group_message(group_id, cr
132
132
  `;
133
133
  // v6 → v7 : runtime_kind figé à la création (classic | durable, Plan 0002 §6 ; D2/D3)
134
134
  const MIGRATE_V6_TO_V7 = `ALTER TABLE conversation ADD COLUMN runtime_kind TEXT NOT NULL DEFAULT 'classic';`;
135
+ // v8 → v9 : files durables (pi-openbot lot 2, prototype Q1) — une lane par (conversation, kind),
136
+ // livraison une par une, statut `interrupted` après crash, idempotency_key, limite de sauts.
137
+ const MIGRATE_V8_TO_V9 = `
138
+ CREATE TABLE IF NOT EXISTS lane (
139
+ id TEXT PRIMARY KEY,
140
+ conversation_id TEXT NOT NULL REFERENCES conversation(id) ON DELETE CASCADE,
141
+ kind TEXT NOT NULL DEFAULT 'chat',
142
+ paused INTEGER NOT NULL DEFAULT 0,
143
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
144
+ UNIQUE (conversation_id, kind)
145
+ );
146
+ CREATE TABLE IF NOT EXISTS queue_item (
147
+ id TEXT PRIMARY KEY,
148
+ lane_id TEXT NOT NULL REFERENCES lane(id) ON DELETE CASCADE,
149
+ seq INTEGER NOT NULL,
150
+ body TEXT NOT NULL,
151
+ images TEXT,
152
+ from_agent_id TEXT REFERENCES agent_preset(id) ON DELETE SET NULL,
153
+ chain_id TEXT,
154
+ hop INTEGER NOT NULL DEFAULT 0,
155
+ status TEXT NOT NULL DEFAULT 'queued'
156
+ CHECK (status IN ('queued','delivering','done','failed','interrupted','cancelled')),
157
+ idempotency_key TEXT,
158
+ error TEXT,
159
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
160
+ started_at TEXT,
161
+ finished_at TEXT,
162
+ UNIQUE (lane_id, seq),
163
+ UNIQUE (lane_id, idempotency_key)
164
+ );
165
+ CREATE INDEX IF NOT EXISTS idx_queue_item_lane_status ON queue_item(lane_id, status, seq);
166
+ `;
167
+ // v9 → v10 : les images d'un message sont persistées avec l'item (#31) — la Map mémoire du
168
+ // LaneDispatcher ne survivait pas au redémarrage, le tour repartait sans images.
169
+ const MIGRATE_V9_TO_V10 = `ALTER TABLE queue_item ADD COLUMN images TEXT;`;
170
+ /**
171
+ * Applique v10. Une base neuve crée déjà `queue_item.images` (CREATE TABLE de MIGRATE_V8_TO_V9),
172
+ * donc l'ALTER n'est exécuté que si la colonne manque (base v9 existante).
173
+ */
174
+ function migrateV9ToV10(db) {
175
+ const columns = db.prepare("PRAGMA table_info(queue_item)").all();
176
+ if (!columns.some((column) => column.name === "images"))
177
+ db.exec(MIGRATE_V9_TO_V10);
178
+ db.pragma("user_version = 10");
179
+ }
135
180
  export function openDb(dbPath) {
136
181
  const db = new Database(dbPath);
137
182
  db.pragma("journal_mode = WAL");
@@ -145,8 +190,9 @@ export function openDb(dbPath) {
145
190
  db.exec(MIGRATE_V5_TO_V6);
146
191
  db.exec(MIGRATE_V6_TO_V7);
147
192
  db.exec("ALTER TABLE conversation ADD COLUMN pending_configuration TEXT");
148
- db.pragma("user_version = 8");
149
- return { db, version: 8 };
193
+ db.exec(MIGRATE_V8_TO_V9);
194
+ migrateV9ToV10(db);
195
+ return { db, version: 10 };
150
196
  }
151
197
  if (version < 2) {
152
198
  // FK OFF pendant la migration (pratique SQLite standard ; les parents peuvent être créés plus tard)
@@ -179,5 +225,11 @@ export function openDb(dbPath) {
179
225
  db.exec("ALTER TABLE conversation ADD COLUMN pending_configuration TEXT");
180
226
  db.pragma("user_version = 8");
181
227
  }
182
- return { db, version: 8 };
228
+ if (version < 9) {
229
+ db.exec(MIGRATE_V8_TO_V9);
230
+ db.pragma("user_version = 9");
231
+ }
232
+ if (version < 10)
233
+ migrateV9ToV10(db);
234
+ return { db, version: 10 };
183
235
  }