@askexenow/exe-os 0.8.41 → 0.8.43
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/dist/bin/backfill-conversations.js +805 -642
- package/dist/bin/backfill-responses.js +804 -641
- package/dist/bin/backfill-vectors.js +791 -634
- package/dist/bin/cleanup-stale-review-tasks.js +788 -631
- package/dist/bin/cli.js +1345 -660
- package/dist/bin/exe-agent.js +20 -1
- package/dist/bin/exe-assign.js +1503 -1343
- package/dist/bin/exe-boot.js +2518 -1798
- package/dist/bin/exe-call.js +39 -1
- package/dist/bin/exe-cloud.js +15 -1
- package/dist/bin/exe-dispatch.js +39 -2
- package/dist/bin/exe-doctor.js +790 -633
- package/dist/bin/exe-export-behaviors.js +792 -637
- package/dist/bin/exe-forget.js +145 -0
- package/dist/bin/exe-gateway.js +2500 -1877
- package/dist/bin/exe-heartbeat.js +147 -1
- package/dist/bin/exe-kill.js +795 -640
- package/dist/bin/exe-launch-agent.js +2168 -2008
- package/dist/bin/exe-link.js +28 -2
- package/dist/bin/exe-new-employee.js +25 -3
- package/dist/bin/exe-pending-messages.js +146 -1
- package/dist/bin/exe-pending-notifications.js +788 -631
- package/dist/bin/exe-pending-reviews.js +147 -1
- package/dist/bin/exe-rename.js +23 -0
- package/dist/bin/exe-review.js +490 -327
- package/dist/bin/exe-search.js +154 -3
- package/dist/bin/exe-session-cleanup.js +2466 -413
- package/dist/bin/exe-status.js +474 -317
- package/dist/bin/exe-team.js +474 -317
- package/dist/bin/git-sweep.js +2690 -150
- package/dist/bin/graph-backfill.js +794 -637
- package/dist/bin/graph-export.js +798 -641
- package/dist/bin/scan-tasks.js +2951 -44
- package/dist/bin/setup.js +62 -26
- package/dist/bin/shard-migrate.js +792 -637
- package/dist/bin/wiki-sync.js +794 -637
- package/dist/gateway/index.js +2504 -1895
- package/dist/hooks/bug-report-worker.js +2118 -576
- package/dist/hooks/commit-complete.js +2689 -149
- package/dist/hooks/error-recall.js +154 -3
- package/dist/hooks/ingest-worker.js +1439 -815
- package/dist/hooks/instructions-loaded.js +151 -0
- package/dist/hooks/notification.js +153 -2
- package/dist/hooks/post-compact.js +164 -0
- package/dist/hooks/pre-compact.js +3073 -101
- package/dist/hooks/pre-tool-use.js +151 -0
- package/dist/hooks/prompt-ingest-worker.js +1714 -1537
- package/dist/hooks/prompt-submit.js +2658 -1113
- package/dist/hooks/response-ingest-worker.js +170 -6
- package/dist/hooks/session-end.js +153 -2
- package/dist/hooks/session-start.js +154 -3
- package/dist/hooks/stop.js +151 -0
- package/dist/hooks/subagent-stop.js +151 -0
- package/dist/hooks/summary-worker.js +179 -7
- package/dist/index.js +278 -100
- package/dist/lib/cloud-sync.js +28 -2
- package/dist/lib/consolidation.js +69 -2
- package/dist/lib/database.js +19 -0
- package/dist/lib/device-registry.js +19 -0
- package/dist/lib/employee-templates.js +20 -1
- package/dist/lib/exe-daemon.js +236 -16
- package/dist/lib/hybrid-search.js +154 -3
- package/dist/lib/license.js +15 -1
- package/dist/lib/messaging.js +39 -2
- package/dist/lib/schedules.js +792 -637
- package/dist/lib/store.js +796 -636
- package/dist/lib/tasks.js +1614 -1091
- package/dist/lib/tmux-routing.js +149 -9
- package/dist/mcp/server.js +1825 -1138
- package/dist/mcp/tools/create-task.js +2280 -828
- package/dist/mcp/tools/list-tasks.js +2788 -159
- package/dist/mcp/tools/send-message.js +39 -2
- package/dist/mcp/tools/update-task.js +64 -0
- package/dist/runtime/index.js +235 -67
- package/dist/tui/App.js +1452 -644
- package/package.json +3 -2
package/dist/bin/exe-gateway.js
CHANGED
|
@@ -26,6 +26,61 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
26
26
|
};
|
|
27
27
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
28
|
|
|
29
|
+
// src/lib/state-bus.ts
|
|
30
|
+
var StateBus, orgBus;
|
|
31
|
+
var init_state_bus = __esm({
|
|
32
|
+
"src/lib/state-bus.ts"() {
|
|
33
|
+
"use strict";
|
|
34
|
+
StateBus = class {
|
|
35
|
+
handlers = /* @__PURE__ */ new Map();
|
|
36
|
+
globalHandlers = /* @__PURE__ */ new Set();
|
|
37
|
+
/** Emit an event to all subscribers */
|
|
38
|
+
emit(event) {
|
|
39
|
+
const typeHandlers = this.handlers.get(event.type);
|
|
40
|
+
if (typeHandlers) {
|
|
41
|
+
for (const handler of typeHandlers) {
|
|
42
|
+
try {
|
|
43
|
+
handler(event);
|
|
44
|
+
} catch {
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (const handler of this.globalHandlers) {
|
|
49
|
+
try {
|
|
50
|
+
handler(event);
|
|
51
|
+
} catch {
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Subscribe to a specific event type */
|
|
56
|
+
on(type, handler) {
|
|
57
|
+
if (!this.handlers.has(type)) {
|
|
58
|
+
this.handlers.set(type, /* @__PURE__ */ new Set());
|
|
59
|
+
}
|
|
60
|
+
this.handlers.get(type).add(handler);
|
|
61
|
+
}
|
|
62
|
+
/** Subscribe to ALL events */
|
|
63
|
+
onAny(handler) {
|
|
64
|
+
this.globalHandlers.add(handler);
|
|
65
|
+
}
|
|
66
|
+
/** Unsubscribe from a specific event type */
|
|
67
|
+
off(type, handler) {
|
|
68
|
+
this.handlers.get(type)?.delete(handler);
|
|
69
|
+
}
|
|
70
|
+
/** Unsubscribe from ALL events */
|
|
71
|
+
offAny(handler) {
|
|
72
|
+
this.globalHandlers.delete(handler);
|
|
73
|
+
}
|
|
74
|
+
/** Remove all listeners */
|
|
75
|
+
clear() {
|
|
76
|
+
this.handlers.clear();
|
|
77
|
+
this.globalHandlers.clear();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
orgBus = new StateBus();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
29
84
|
// src/gateway/crm-bridge.ts
|
|
30
85
|
var crm_bridge_exports = {};
|
|
31
86
|
__export(crm_bridge_exports, {
|
|
@@ -575,6 +630,13 @@ async function ensureSchema() {
|
|
|
575
630
|
});
|
|
576
631
|
} catch {
|
|
577
632
|
}
|
|
633
|
+
try {
|
|
634
|
+
await client.execute({
|
|
635
|
+
sql: `ALTER TABLE tasks ADD COLUMN session_scope TEXT`,
|
|
636
|
+
args: []
|
|
637
|
+
});
|
|
638
|
+
} catch {
|
|
639
|
+
}
|
|
578
640
|
try {
|
|
579
641
|
await client.execute({
|
|
580
642
|
sql: `ALTER TABLE memories ADD COLUMN task_id TEXT`,
|
|
@@ -1021,6 +1083,18 @@ async function ensureSchema() {
|
|
|
1021
1083
|
CREATE INDEX IF NOT EXISTS idx_session_kills_agent
|
|
1022
1084
|
ON session_kills(agent_id);
|
|
1023
1085
|
`);
|
|
1086
|
+
await client.execute(`
|
|
1087
|
+
CREATE TABLE IF NOT EXISTS global_procedures (
|
|
1088
|
+
id TEXT PRIMARY KEY,
|
|
1089
|
+
title TEXT NOT NULL,
|
|
1090
|
+
content TEXT NOT NULL,
|
|
1091
|
+
priority TEXT NOT NULL DEFAULT 'p0',
|
|
1092
|
+
domain TEXT,
|
|
1093
|
+
active INTEGER NOT NULL DEFAULT 1,
|
|
1094
|
+
created_at TEXT NOT NULL,
|
|
1095
|
+
updated_at TEXT NOT NULL
|
|
1096
|
+
)
|
|
1097
|
+
`);
|
|
1024
1098
|
await client.executeMultiple(`
|
|
1025
1099
|
CREATE TABLE IF NOT EXISTS conversations (
|
|
1026
1100
|
id TEXT PRIMARY KEY,
|
|
@@ -2135,6 +2209,71 @@ var init_shard_manager = __esm({
|
|
|
2135
2209
|
}
|
|
2136
2210
|
});
|
|
2137
2211
|
|
|
2212
|
+
// src/lib/global-procedures.ts
|
|
2213
|
+
var global_procedures_exports = {};
|
|
2214
|
+
__export(global_procedures_exports, {
|
|
2215
|
+
deactivateGlobalProcedure: () => deactivateGlobalProcedure,
|
|
2216
|
+
getGlobalProceduresBlock: () => getGlobalProceduresBlock,
|
|
2217
|
+
loadGlobalProcedures: () => loadGlobalProcedures,
|
|
2218
|
+
storeGlobalProcedure: () => storeGlobalProcedure
|
|
2219
|
+
});
|
|
2220
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
2221
|
+
async function loadGlobalProcedures() {
|
|
2222
|
+
const client = getClient();
|
|
2223
|
+
const result = await client.execute({
|
|
2224
|
+
sql: "SELECT * FROM global_procedures WHERE active = 1 ORDER BY priority ASC, created_at ASC",
|
|
2225
|
+
args: []
|
|
2226
|
+
});
|
|
2227
|
+
const procedures = result.rows;
|
|
2228
|
+
if (procedures.length > 0) {
|
|
2229
|
+
_cache = procedures.map((p) => `### ${p.title}
|
|
2230
|
+
${p.content}`).join("\n\n");
|
|
2231
|
+
} else {
|
|
2232
|
+
_cache = "";
|
|
2233
|
+
}
|
|
2234
|
+
_cacheLoaded = true;
|
|
2235
|
+
return procedures;
|
|
2236
|
+
}
|
|
2237
|
+
function getGlobalProceduresBlock() {
|
|
2238
|
+
if (!_cacheLoaded) return "";
|
|
2239
|
+
if (!_cache) return "";
|
|
2240
|
+
return `## Organization-Wide Procedures (MANDATORY \u2014 supersedes all other rules)
|
|
2241
|
+
|
|
2242
|
+
${_cache}
|
|
2243
|
+
`;
|
|
2244
|
+
}
|
|
2245
|
+
async function storeGlobalProcedure(input) {
|
|
2246
|
+
const id = randomUUID2();
|
|
2247
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
2248
|
+
const client = getClient();
|
|
2249
|
+
await client.execute({
|
|
2250
|
+
sql: `INSERT INTO global_procedures (id, title, content, priority, domain, active, created_at, updated_at)
|
|
2251
|
+
VALUES (?, ?, ?, ?, ?, 1, ?, ?)`,
|
|
2252
|
+
args: [id, input.title, input.content, input.priority ?? "p0", input.domain ?? null, now, now]
|
|
2253
|
+
});
|
|
2254
|
+
await loadGlobalProcedures();
|
|
2255
|
+
return id;
|
|
2256
|
+
}
|
|
2257
|
+
async function deactivateGlobalProcedure(id) {
|
|
2258
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
2259
|
+
const client = getClient();
|
|
2260
|
+
const result = await client.execute({
|
|
2261
|
+
sql: "UPDATE global_procedures SET active = 0, updated_at = ? WHERE id = ?",
|
|
2262
|
+
args: [now, id]
|
|
2263
|
+
});
|
|
2264
|
+
await loadGlobalProcedures();
|
|
2265
|
+
return result.rowsAffected > 0;
|
|
2266
|
+
}
|
|
2267
|
+
var _cache, _cacheLoaded;
|
|
2268
|
+
var init_global_procedures = __esm({
|
|
2269
|
+
"src/lib/global-procedures.ts"() {
|
|
2270
|
+
"use strict";
|
|
2271
|
+
init_database();
|
|
2272
|
+
_cache = "";
|
|
2273
|
+
_cacheLoaded = false;
|
|
2274
|
+
}
|
|
2275
|
+
});
|
|
2276
|
+
|
|
2138
2277
|
// src/lib/store.ts
|
|
2139
2278
|
var store_exports = {};
|
|
2140
2279
|
__export(store_exports, {
|
|
@@ -2214,6 +2353,11 @@ async function initStore(options) {
|
|
|
2214
2353
|
"version-query"
|
|
2215
2354
|
);
|
|
2216
2355
|
_nextVersion = (Number(vResult.rows[0]?.max_v) || 0) + 1;
|
|
2356
|
+
try {
|
|
2357
|
+
const { loadGlobalProcedures: loadGlobalProcedures2 } = await Promise.resolve().then(() => (init_global_procedures(), global_procedures_exports));
|
|
2358
|
+
await loadGlobalProcedures2();
|
|
2359
|
+
} catch {
|
|
2360
|
+
}
|
|
2217
2361
|
}
|
|
2218
2362
|
function classifyTier(record) {
|
|
2219
2363
|
if (record.tool_name === "commit_to_long_term_memory" && (record.importance ?? 0) >= 8) return 1;
|
|
@@ -2255,6 +2399,12 @@ async function writeMemory(record) {
|
|
|
2255
2399
|
supersedes_id: record.supersedes_id ?? null
|
|
2256
2400
|
};
|
|
2257
2401
|
_pendingRecords.push(dbRow);
|
|
2402
|
+
orgBus.emit({
|
|
2403
|
+
type: "memory_stored",
|
|
2404
|
+
agentId: record.agent_id,
|
|
2405
|
+
project: record.project_name,
|
|
2406
|
+
timestamp: record.timestamp
|
|
2407
|
+
});
|
|
2258
2408
|
const MAX_PENDING = 1e3;
|
|
2259
2409
|
if (_pendingRecords.length > MAX_PENDING) {
|
|
2260
2410
|
const dropped = _pendingRecords.length - MAX_PENDING;
|
|
@@ -2600,6 +2750,7 @@ var init_store = __esm({
|
|
|
2600
2750
|
init_database();
|
|
2601
2751
|
init_keychain();
|
|
2602
2752
|
init_config();
|
|
2753
|
+
init_state_bus();
|
|
2603
2754
|
INIT_MAX_RETRIES = 3;
|
|
2604
2755
|
INIT_RETRY_DELAY_MS = 1e3;
|
|
2605
2756
|
_pendingRecords = [];
|
|
@@ -2771,7 +2922,7 @@ __export(license_exports, {
|
|
|
2771
2922
|
validateLicense: () => validateLicense
|
|
2772
2923
|
});
|
|
2773
2924
|
import { readFileSync as readFileSync3, writeFileSync, existsSync as existsSync5, mkdirSync as mkdirSync2 } from "fs";
|
|
2774
|
-
import { randomUUID as
|
|
2925
|
+
import { randomUUID as randomUUID3 } from "crypto";
|
|
2775
2926
|
import path5 from "path";
|
|
2776
2927
|
import { jwtVerify, importSPKI } from "jose";
|
|
2777
2928
|
async function fetchRetry(url, init) {
|
|
@@ -2798,7 +2949,7 @@ function loadDeviceId() {
|
|
|
2798
2949
|
}
|
|
2799
2950
|
} catch {
|
|
2800
2951
|
}
|
|
2801
|
-
const id =
|
|
2952
|
+
const id = randomUUID3();
|
|
2802
2953
|
mkdirSync2(EXE_AI_DIR, { recursive: true });
|
|
2803
2954
|
writeFileSync(DEVICE_ID_PATH, id, "utf8");
|
|
2804
2955
|
return id;
|
|
@@ -2913,7 +3064,21 @@ function getCacheAgeMs() {
|
|
|
2913
3064
|
}
|
|
2914
3065
|
}
|
|
2915
3066
|
async function checkLicense() {
|
|
2916
|
-
|
|
3067
|
+
let key = loadLicense();
|
|
3068
|
+
if (!key) {
|
|
3069
|
+
try {
|
|
3070
|
+
const configPath = path5.join(EXE_AI_DIR, "config.json");
|
|
3071
|
+
if (existsSync5(configPath)) {
|
|
3072
|
+
const raw = JSON.parse(readFileSync3(configPath, "utf8"));
|
|
3073
|
+
const cloud = raw.cloud;
|
|
3074
|
+
if (cloud?.apiKey) {
|
|
3075
|
+
key = cloud.apiKey;
|
|
3076
|
+
saveLicense(key);
|
|
3077
|
+
}
|
|
3078
|
+
}
|
|
3079
|
+
} catch {
|
|
3080
|
+
}
|
|
3081
|
+
}
|
|
2917
3082
|
if (!key) return FREE_LICENSE;
|
|
2918
3083
|
const cached = await getCachedLicense();
|
|
2919
3084
|
if (cached && getCacheAgeMs() < CACHE_MAX_AGE_MS) return cached;
|
|
@@ -3099,7 +3264,7 @@ var whatsapp_exports = {};
|
|
|
3099
3264
|
__export(whatsapp_exports, {
|
|
3100
3265
|
WhatsAppAdapter: () => WhatsAppAdapter
|
|
3101
3266
|
});
|
|
3102
|
-
import { randomUUID as
|
|
3267
|
+
import { randomUUID as randomUUID4 } from "crypto";
|
|
3103
3268
|
import { homedir } from "os";
|
|
3104
3269
|
import { join } from "path";
|
|
3105
3270
|
import { mkdirSync as mkdirSync3 } from "fs";
|
|
@@ -3285,7 +3450,7 @@ var init_whatsapp = __esm({
|
|
|
3285
3450
|
const location = this.extractLocation(msg.message);
|
|
3286
3451
|
const dataCategory = location ? "location" : "message";
|
|
3287
3452
|
return {
|
|
3288
|
-
messageId: msg.key.id ??
|
|
3453
|
+
messageId: msg.key.id ?? randomUUID4(),
|
|
3289
3454
|
platform: "whatsapp",
|
|
3290
3455
|
senderId,
|
|
3291
3456
|
senderName: msg.pushName ?? void 0,
|
|
@@ -3330,7 +3495,7 @@ var init_whatsapp = __esm({
|
|
|
3330
3495
|
}
|
|
3331
3496
|
const timestamp = receipt.readTimestamp ?? receipt.receiptTimestamp ?? Date.now() / 1e3;
|
|
3332
3497
|
return {
|
|
3333
|
-
messageId:
|
|
3498
|
+
messageId: randomUUID4(),
|
|
3334
3499
|
platform: "whatsapp",
|
|
3335
3500
|
senderId: remoteJid.replace("@s.whatsapp.net", "").replace("@g.us", ""),
|
|
3336
3501
|
channelId: remoteJid,
|
|
@@ -3353,7 +3518,7 @@ var init_whatsapp = __esm({
|
|
|
3353
3518
|
const phone = id.replace("@s.whatsapp.net", "").replace("@g.us", "");
|
|
3354
3519
|
const name = contact.name ?? contact.notify ?? phone;
|
|
3355
3520
|
return {
|
|
3356
|
-
messageId:
|
|
3521
|
+
messageId: randomUUID4(),
|
|
3357
3522
|
platform: "whatsapp",
|
|
3358
3523
|
senderId: phone,
|
|
3359
3524
|
senderName: name,
|
|
@@ -3377,7 +3542,7 @@ var init_whatsapp = __esm({
|
|
|
3377
3542
|
const participants = (group.participants ?? []).map((p) => p.id ?? p);
|
|
3378
3543
|
const admins = (group.participants ?? []).filter((p) => p.admin === "admin" || p.admin === "superadmin").map((p) => p.id ?? p);
|
|
3379
3544
|
return {
|
|
3380
|
-
messageId:
|
|
3545
|
+
messageId: randomUUID4(),
|
|
3381
3546
|
platform: "whatsapp",
|
|
3382
3547
|
senderId: groupId,
|
|
3383
3548
|
channelId: groupId,
|
|
@@ -3402,7 +3567,7 @@ var init_whatsapp = __esm({
|
|
|
3402
3567
|
if (!reactionData) return null;
|
|
3403
3568
|
const remoteJid = key.remoteJid ?? "";
|
|
3404
3569
|
return {
|
|
3405
|
-
messageId:
|
|
3570
|
+
messageId: randomUUID4(),
|
|
3406
3571
|
platform: "whatsapp",
|
|
3407
3572
|
senderId: reactionData.key?.participant ?? reactionData.key?.remoteJid?.replace("@s.whatsapp.net", "") ?? "",
|
|
3408
3573
|
channelId: remoteJid,
|
|
@@ -3424,7 +3589,7 @@ var init_whatsapp = __esm({
|
|
|
3424
3589
|
if (!chatId) return null;
|
|
3425
3590
|
const caller = call.from?.replace("@s.whatsapp.net", "") ?? "";
|
|
3426
3591
|
return {
|
|
3427
|
-
messageId:
|
|
3592
|
+
messageId: randomUUID4(),
|
|
3428
3593
|
platform: "whatsapp",
|
|
3429
3594
|
senderId: caller,
|
|
3430
3595
|
channelId: chatId,
|
|
@@ -3770,7 +3935,7 @@ var slack_exports = {};
|
|
|
3770
3935
|
__export(slack_exports, {
|
|
3771
3936
|
SlackAdapter: () => SlackAdapter
|
|
3772
3937
|
});
|
|
3773
|
-
import { randomUUID as
|
|
3938
|
+
import { randomUUID as randomUUID5 } from "crypto";
|
|
3774
3939
|
var SlackAdapter;
|
|
3775
3940
|
var init_slack = __esm({
|
|
3776
3941
|
"src/gateway/adapters/slack.ts"() {
|
|
@@ -3812,7 +3977,7 @@ var init_slack = __esm({
|
|
|
3812
3977
|
if (event.subtype) return;
|
|
3813
3978
|
const isGroup = event.channel_type !== "im";
|
|
3814
3979
|
const normalized = {
|
|
3815
|
-
messageId: event.client_msg_id ?? event.ts ??
|
|
3980
|
+
messageId: event.client_msg_id ?? event.ts ?? randomUUID5(),
|
|
3816
3981
|
platform: "slack",
|
|
3817
3982
|
senderId: event.user ?? "",
|
|
3818
3983
|
channelId: event.channel ?? "",
|
|
@@ -3874,7 +4039,7 @@ var init_slack = __esm({
|
|
|
3874
4039
|
if (!event.text) return;
|
|
3875
4040
|
const isGroup = event.channel_type !== "im";
|
|
3876
4041
|
const normalized = {
|
|
3877
|
-
messageId: event.ts ??
|
|
4042
|
+
messageId: event.ts ?? randomUUID5(),
|
|
3878
4043
|
platform: "slack",
|
|
3879
4044
|
senderId: event.user ?? "",
|
|
3880
4045
|
senderName: event.user_profile?.display_name ?? event.user_profile?.real_name ?? void 0,
|
|
@@ -4097,7 +4262,7 @@ var email_exports = {};
|
|
|
4097
4262
|
__export(email_exports, {
|
|
4098
4263
|
EmailAdapter: () => EmailAdapter
|
|
4099
4264
|
});
|
|
4100
|
-
import { randomUUID as
|
|
4265
|
+
import { randomUUID as randomUUID6 } from "crypto";
|
|
4101
4266
|
import { createTransport } from "nodemailer";
|
|
4102
4267
|
function extractEmailAddress(from) {
|
|
4103
4268
|
const match = from.match(/<([^>]+)>/);
|
|
@@ -4179,7 +4344,7 @@ var init_email = __esm({
|
|
|
4179
4344
|
const senderEmail = extractEmailAddress(from);
|
|
4180
4345
|
const media = this.extractMedia(payload);
|
|
4181
4346
|
const normalized = {
|
|
4182
|
-
messageId: payload.message_id ??
|
|
4347
|
+
messageId: payload.message_id ?? randomUUID6(),
|
|
4183
4348
|
platform: "email",
|
|
4184
4349
|
senderId: senderEmail,
|
|
4185
4350
|
senderName: extractSenderName(from),
|
|
@@ -4240,7 +4405,7 @@ var webhook_exports = {};
|
|
|
4240
4405
|
__export(webhook_exports, {
|
|
4241
4406
|
WebhookAdapter: () => WebhookAdapter
|
|
4242
4407
|
});
|
|
4243
|
-
import { randomUUID as
|
|
4408
|
+
import { randomUUID as randomUUID7 } from "crypto";
|
|
4244
4409
|
function resolvePath(obj, path20) {
|
|
4245
4410
|
let current = obj;
|
|
4246
4411
|
for (const segment of path20.split(".")) {
|
|
@@ -4296,7 +4461,7 @@ var init_webhook = __esm({
|
|
|
4296
4461
|
if (!text || !senderId) return;
|
|
4297
4462
|
const channelId = this.fieldMap.channelId ? String(resolvePath(rawPayload, this.fieldMap.channelId) ?? senderId) : senderId;
|
|
4298
4463
|
const senderName = this.fieldMap.senderName ? String(resolvePath(rawPayload, this.fieldMap.senderName) ?? "") || void 0 : void 0;
|
|
4299
|
-
const messageId = this.fieldMap.messageId ? String(resolvePath(rawPayload, this.fieldMap.messageId) ??
|
|
4464
|
+
const messageId = this.fieldMap.messageId ? String(resolvePath(rawPayload, this.fieldMap.messageId) ?? randomUUID7()) : randomUUID7();
|
|
4300
4465
|
const timestamp = this.fieldMap.timestamp ? String(resolvePath(rawPayload, this.fieldMap.timestamp) ?? (/* @__PURE__ */ new Date()).toISOString()) : (/* @__PURE__ */ new Date()).toISOString();
|
|
4301
4466
|
const normalized = {
|
|
4302
4467
|
messageId,
|
|
@@ -4799,2159 +4964,2607 @@ var init_plan_limits = __esm({
|
|
|
4799
4964
|
}
|
|
4800
4965
|
});
|
|
4801
4966
|
|
|
4802
|
-
// src/lib/
|
|
4803
|
-
import
|
|
4804
|
-
import { readFileSync as readFileSync9, writeFileSync as writeFileSync4, mkdirSync as mkdirSync6, existsSync as existsSync10, appendFileSync } from "fs";
|
|
4967
|
+
// src/lib/notifications.ts
|
|
4968
|
+
import crypto3 from "crypto";
|
|
4805
4969
|
import path11 from "path";
|
|
4806
4970
|
import os6 from "os";
|
|
4807
|
-
import {
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4811
|
-
|
|
4812
|
-
|
|
4971
|
+
import {
|
|
4972
|
+
readFileSync as readFileSync9,
|
|
4973
|
+
readdirSync as readdirSync2,
|
|
4974
|
+
unlinkSync as unlinkSync2,
|
|
4975
|
+
existsSync as existsSync10,
|
|
4976
|
+
rmdirSync
|
|
4977
|
+
} from "fs";
|
|
4978
|
+
async function writeNotification(notification) {
|
|
4813
4979
|
try {
|
|
4814
|
-
|
|
4815
|
-
|
|
4816
|
-
|
|
4817
|
-
|
|
4818
|
-
|
|
4819
|
-
|
|
4820
|
-
|
|
4821
|
-
|
|
4822
|
-
|
|
4823
|
-
|
|
4824
|
-
|
|
4825
|
-
|
|
4826
|
-
|
|
4827
|
-
|
|
4828
|
-
|
|
4829
|
-
|
|
4830
|
-
|
|
4831
|
-
|
|
4832
|
-
|
|
4833
|
-
|
|
4980
|
+
const client = getClient();
|
|
4981
|
+
const id = crypto3.randomUUID();
|
|
4982
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
4983
|
+
await client.execute({
|
|
4984
|
+
sql: `INSERT INTO notifications (id, agent_id, agent_role, event, project, summary, task_file, read, created_at)
|
|
4985
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?)`,
|
|
4986
|
+
args: [
|
|
4987
|
+
id,
|
|
4988
|
+
notification.agentId,
|
|
4989
|
+
notification.agentRole,
|
|
4990
|
+
notification.event,
|
|
4991
|
+
notification.project,
|
|
4992
|
+
notification.summary,
|
|
4993
|
+
notification.taskFile ?? null,
|
|
4994
|
+
now
|
|
4995
|
+
]
|
|
4996
|
+
});
|
|
4997
|
+
} catch (err) {
|
|
4998
|
+
process.stderr.write(`[notifications] WRITE FAILED: ${err instanceof Error ? err.message : String(err)}
|
|
4999
|
+
`);
|
|
4834
5000
|
}
|
|
4835
|
-
writeFileSync4(lockFile, JSON.stringify({ pid: process.pid, timestamp: Date.now() }));
|
|
4836
|
-
return true;
|
|
4837
5001
|
}
|
|
4838
|
-
function
|
|
5002
|
+
async function markAsReadByTaskFile(taskFile) {
|
|
4839
5003
|
try {
|
|
4840
|
-
|
|
5004
|
+
const client = getClient();
|
|
5005
|
+
await client.execute({
|
|
5006
|
+
sql: "UPDATE notifications SET read = 1 WHERE task_file = ? AND read = 0",
|
|
5007
|
+
args: [taskFile]
|
|
5008
|
+
});
|
|
4841
5009
|
} catch {
|
|
4842
5010
|
}
|
|
4843
5011
|
}
|
|
4844
|
-
|
|
4845
|
-
|
|
4846
|
-
|
|
4847
|
-
|
|
4848
|
-
path11.dirname(thisFile),
|
|
4849
|
-
"..",
|
|
4850
|
-
"bin",
|
|
4851
|
-
"exe-export-behaviors.js"
|
|
4852
|
-
);
|
|
4853
|
-
return existsSync10(scriptPath) ? scriptPath : null;
|
|
4854
|
-
} catch {
|
|
4855
|
-
return null;
|
|
5012
|
+
var init_notifications = __esm({
|
|
5013
|
+
"src/lib/notifications.ts"() {
|
|
5014
|
+
"use strict";
|
|
5015
|
+
init_database();
|
|
4856
5016
|
}
|
|
4857
|
-
}
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
5017
|
+
});
|
|
5018
|
+
|
|
5019
|
+
// src/lib/session-kill-telemetry.ts
|
|
5020
|
+
import crypto4 from "crypto";
|
|
5021
|
+
async function recordSessionKill(input) {
|
|
4861
5022
|
try {
|
|
4862
|
-
const
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
5023
|
+
const client = getClient();
|
|
5024
|
+
await client.execute({
|
|
5025
|
+
sql: `INSERT INTO session_kills
|
|
5026
|
+
(id, session_name, agent_id, killed_at, reason,
|
|
5027
|
+
ticks_idle, estimated_tokens_saved)
|
|
5028
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
5029
|
+
args: [
|
|
5030
|
+
crypto4.randomUUID(),
|
|
5031
|
+
input.sessionName,
|
|
5032
|
+
input.agentId,
|
|
5033
|
+
(/* @__PURE__ */ new Date()).toISOString(),
|
|
5034
|
+
input.reason,
|
|
5035
|
+
input.ticksIdle ?? null,
|
|
5036
|
+
input.estimatedTokensSaved ?? null
|
|
5037
|
+
]
|
|
5038
|
+
});
|
|
4868
5039
|
} catch (err) {
|
|
4869
5040
|
process.stderr.write(
|
|
4870
|
-
`[
|
|
5041
|
+
`[session-kill-telemetry] write failed: ${err instanceof Error ? err.message : String(err)}
|
|
4871
5042
|
`
|
|
4872
5043
|
);
|
|
4873
|
-
return null;
|
|
4874
5044
|
}
|
|
4875
5045
|
}
|
|
4876
|
-
|
|
4877
|
-
|
|
4878
|
-
|
|
4879
|
-
|
|
4880
|
-
const suffix = instance != null && instance > 0 ? String(instance) : "";
|
|
4881
|
-
return `${employee}${suffix}-${exeSession}`;
|
|
4882
|
-
}
|
|
4883
|
-
function extractRootExe(name) {
|
|
4884
|
-
const match = name.match(/(exe\d+)$/);
|
|
4885
|
-
return match?.[1] ?? null;
|
|
4886
|
-
}
|
|
4887
|
-
function getParentExe(sessionKey) {
|
|
4888
|
-
try {
|
|
4889
|
-
const data = JSON.parse(readFileSync9(path11.join(SESSION_CACHE, `parent-exe-${sessionKey}.json`), "utf8"));
|
|
4890
|
-
return data.parentExe || null;
|
|
4891
|
-
} catch {
|
|
4892
|
-
return null;
|
|
5046
|
+
var init_session_kill_telemetry = __esm({
|
|
5047
|
+
"src/lib/session-kill-telemetry.ts"() {
|
|
5048
|
+
"use strict";
|
|
5049
|
+
init_database();
|
|
4893
5050
|
}
|
|
4894
|
-
}
|
|
4895
|
-
|
|
4896
|
-
|
|
4897
|
-
|
|
4898
|
-
|
|
4899
|
-
|
|
4900
|
-
|
|
4901
|
-
|
|
4902
|
-
|
|
4903
|
-
|
|
5051
|
+
});
|
|
5052
|
+
|
|
5053
|
+
// src/lib/tasks-crud.ts
|
|
5054
|
+
import crypto5 from "crypto";
|
|
5055
|
+
import path12 from "path";
|
|
5056
|
+
import { execSync as execSync4 } from "child_process";
|
|
5057
|
+
import { mkdir as mkdir4, writeFile as writeFile4, appendFile } from "fs/promises";
|
|
5058
|
+
import { existsSync as existsSync11, readFileSync as readFileSync10 } from "fs";
|
|
5059
|
+
async function writeCheckpoint(input) {
|
|
5060
|
+
const client = getClient();
|
|
5061
|
+
const row = await resolveTask(client, input.taskId);
|
|
5062
|
+
const taskId = String(row.id);
|
|
5063
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5064
|
+
const blockedByIds = [];
|
|
5065
|
+
if (row.blocked_by) {
|
|
5066
|
+
blockedByIds.push(String(row.blocked_by));
|
|
4904
5067
|
}
|
|
4905
|
-
|
|
4906
|
-
|
|
4907
|
-
|
|
4908
|
-
|
|
4909
|
-
|
|
4910
|
-
|
|
4911
|
-
|
|
4912
|
-
|
|
4913
|
-
|
|
4914
|
-
|
|
4915
|
-
}
|
|
5068
|
+
const checkpoint = {
|
|
5069
|
+
step: input.step,
|
|
5070
|
+
context_summary: input.contextSummary,
|
|
5071
|
+
files_touched: input.filesTouched ?? [],
|
|
5072
|
+
blocked_by_ids: blockedByIds,
|
|
5073
|
+
last_checkpoint_at: now
|
|
5074
|
+
};
|
|
5075
|
+
const result = await client.execute({
|
|
5076
|
+
sql: `UPDATE tasks SET checkpoint = ?, checkpoint_count = checkpoint_count + 1, updated_at = ? WHERE id = ?`,
|
|
5077
|
+
args: [JSON.stringify(checkpoint), now, taskId]
|
|
5078
|
+
});
|
|
5079
|
+
if (result.rowsAffected === 0) {
|
|
5080
|
+
throw new Error(`Checkpoint write failed: task ${taskId} not found`);
|
|
4916
5081
|
}
|
|
4917
|
-
|
|
5082
|
+
const countResult = await client.execute({
|
|
5083
|
+
sql: "SELECT checkpoint_count FROM tasks WHERE id = ?",
|
|
5084
|
+
args: [taskId]
|
|
5085
|
+
});
|
|
5086
|
+
const checkpointCount = Number(countResult.rows[0]?.checkpoint_count ?? 1);
|
|
5087
|
+
return { checkpointCount };
|
|
4918
5088
|
}
|
|
4919
|
-
function
|
|
4920
|
-
|
|
5089
|
+
function extractParentFromContext(contextBody) {
|
|
5090
|
+
if (!contextBody) return null;
|
|
5091
|
+
const match = contextBody.match(
|
|
5092
|
+
/Parent task:\s*([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i
|
|
5093
|
+
);
|
|
5094
|
+
return match ? match[1].toLowerCase() : null;
|
|
4921
5095
|
}
|
|
4922
|
-
function
|
|
4923
|
-
|
|
4924
|
-
if (!isAlive(base) && acquireSpawnLock2(base)) return 0;
|
|
4925
|
-
for (let i = 2; i <= maxInstances; i++) {
|
|
4926
|
-
const candidate = employeeSessionName(employeeName, exeSession, i);
|
|
4927
|
-
if (!isAlive(candidate) && acquireSpawnLock2(candidate)) return i;
|
|
4928
|
-
}
|
|
4929
|
-
return null;
|
|
5096
|
+
function slugify(title) {
|
|
5097
|
+
return title.toLowerCase().replace(/[^a-z0-9-]/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "");
|
|
4930
5098
|
}
|
|
4931
|
-
function
|
|
4932
|
-
|
|
4933
|
-
|
|
4934
|
-
|
|
4935
|
-
}
|
|
4936
|
-
|
|
5099
|
+
async function resolveTask(client, identifier) {
|
|
5100
|
+
let result = await client.execute({
|
|
5101
|
+
sql: "SELECT * FROM tasks WHERE id = ?",
|
|
5102
|
+
args: [identifier]
|
|
5103
|
+
});
|
|
5104
|
+
if (result.rows.length === 1) return result.rows[0];
|
|
5105
|
+
result = await client.execute({
|
|
5106
|
+
sql: "SELECT * FROM tasks WHERE task_file LIKE ?",
|
|
5107
|
+
args: [`%${identifier}%`]
|
|
5108
|
+
});
|
|
5109
|
+
if (result.rows.length === 1) return result.rows[0];
|
|
5110
|
+
if (result.rows.length > 1) {
|
|
5111
|
+
const exact = result.rows.filter(
|
|
5112
|
+
(r) => String(r.task_file).endsWith(`/${identifier}.md`)
|
|
5113
|
+
);
|
|
5114
|
+
if (exact.length === 1) return exact[0];
|
|
5115
|
+
const candidates = exact.length > 1 ? exact : result.rows;
|
|
5116
|
+
const active = candidates.filter(
|
|
5117
|
+
(r) => !["done", "cancelled"].includes(String(r.status))
|
|
5118
|
+
);
|
|
5119
|
+
if (active.length === 1) return active[0];
|
|
5120
|
+
const matches = (active.length > 1 ? active : candidates).map((r) => `${String(r.task_file)} (${String(r.status)}, ${String(r.id)})`).join(", ");
|
|
5121
|
+
throw new Error(
|
|
5122
|
+
`Multiple tasks match "${identifier}": ${matches}. Use a UUID to disambiguate.`
|
|
5123
|
+
);
|
|
4937
5124
|
}
|
|
4938
|
-
|
|
4939
|
-
|
|
4940
|
-
|
|
4941
|
-
|
|
4942
|
-
|
|
4943
|
-
|
|
5125
|
+
result = await client.execute({
|
|
5126
|
+
sql: "SELECT * FROM tasks WHERE title LIKE ?",
|
|
5127
|
+
args: [`%${identifier}%`]
|
|
5128
|
+
});
|
|
5129
|
+
if (result.rows.length === 1) return result.rows[0];
|
|
5130
|
+
if (result.rows.length > 1) {
|
|
5131
|
+
const active = result.rows.filter(
|
|
5132
|
+
(r) => !["done", "cancelled"].includes(String(r.status))
|
|
5133
|
+
);
|
|
5134
|
+
if (active.length === 1) return active[0];
|
|
5135
|
+
const matches = (active.length > 1 ? active : result.rows).map((r) => `"${String(r.title)}" (${String(r.status)}, ${String(r.id)})`).join(", ");
|
|
5136
|
+
throw new Error(
|
|
5137
|
+
`Multiple tasks match "${identifier}": ${matches}. Use a UUID to disambiguate.`
|
|
5138
|
+
);
|
|
4944
5139
|
}
|
|
5140
|
+
throw new Error(`Task not found: ${identifier}`);
|
|
4945
5141
|
}
|
|
4946
|
-
function
|
|
4947
|
-
const
|
|
4948
|
-
const
|
|
4949
|
-
|
|
4950
|
-
|
|
4951
|
-
|
|
4952
|
-
|
|
4953
|
-
|
|
4954
|
-
|
|
4955
|
-
|
|
4956
|
-
|
|
5142
|
+
async function createTaskCore(input) {
|
|
5143
|
+
const client = getClient();
|
|
5144
|
+
const id = crypto5.randomUUID();
|
|
5145
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5146
|
+
const slug = slugify(input.title);
|
|
5147
|
+
const taskFile = input.taskFile ?? `exe/${input.assignedTo}/${slug}.md`;
|
|
5148
|
+
let blockedById = null;
|
|
5149
|
+
const initialStatus = input.blockedBy ? "blocked" : "open";
|
|
5150
|
+
if (input.blockedBy) {
|
|
5151
|
+
const blocker = await resolveTask(client, input.blockedBy);
|
|
5152
|
+
blockedById = String(blocker.id);
|
|
4957
5153
|
}
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
|
|
4961
|
-
|
|
4962
|
-
|
|
4963
|
-
|
|
4964
|
-
|
|
4965
|
-
|
|
4966
|
-
|
|
4967
|
-
|
|
5154
|
+
let parentTaskId = null;
|
|
5155
|
+
let parentRef = input.parentTaskId;
|
|
5156
|
+
if (!parentRef) {
|
|
5157
|
+
const extracted = extractParentFromContext(input.context);
|
|
5158
|
+
if (extracted) {
|
|
5159
|
+
parentRef = extracted;
|
|
5160
|
+
process.stderr.write(
|
|
5161
|
+
"[create_task] auto-populated parent_task_id from context body \u2014 dispatchers should pass parent_task_id explicitly\n"
|
|
5162
|
+
);
|
|
5163
|
+
}
|
|
4968
5164
|
}
|
|
4969
|
-
|
|
4970
|
-
|
|
4971
|
-
|
|
4972
|
-
|
|
4973
|
-
|
|
4974
|
-
|
|
4975
|
-
|
|
4976
|
-
|
|
4977
|
-
|
|
5165
|
+
if (parentRef) {
|
|
5166
|
+
try {
|
|
5167
|
+
const parent = await resolveTask(client, parentRef);
|
|
5168
|
+
parentTaskId = String(parent.id);
|
|
5169
|
+
} catch (err) {
|
|
5170
|
+
if (!input.parentTaskId) {
|
|
5171
|
+
throw new Error(
|
|
5172
|
+
`create_task: parent reference "${parentRef}" in context body does not resolve to an existing task`
|
|
5173
|
+
);
|
|
4978
5174
|
}
|
|
5175
|
+
throw err;
|
|
4979
5176
|
}
|
|
4980
|
-
if (/Running…/.test(pane)) return "tool";
|
|
4981
|
-
if (BUSY_PATTERN.test(pane)) return "thinking";
|
|
4982
|
-
return "idle";
|
|
4983
|
-
} catch {
|
|
4984
|
-
return "offline";
|
|
4985
5177
|
}
|
|
4986
|
-
|
|
4987
|
-
|
|
4988
|
-
|
|
4989
|
-
|
|
4990
|
-
|
|
4991
|
-
|
|
4992
|
-
|
|
4993
|
-
logIntercom(`SKIP_EXE \u2192 ${targetSession} (exe sessions use prompt-submit hook)`);
|
|
4994
|
-
return "skipped_exe";
|
|
5178
|
+
let warning;
|
|
5179
|
+
const dupCheck = await client.execute({
|
|
5180
|
+
sql: "SELECT id FROM tasks WHERE title = ? AND assigned_to = ? AND status IN ('open', 'in_progress', 'blocked')",
|
|
5181
|
+
args: [input.title, input.assignedTo]
|
|
5182
|
+
});
|
|
5183
|
+
if (dupCheck.rows.length > 0) {
|
|
5184
|
+
warning = `similar active task already exists (${String(dupCheck.rows[0].id)}). Created new task anyway.`;
|
|
4995
5185
|
}
|
|
4996
|
-
if (
|
|
4997
|
-
|
|
4998
|
-
|
|
5186
|
+
if (input.baseDir) {
|
|
5187
|
+
try {
|
|
5188
|
+
await mkdir4(path12.join(input.baseDir, "exe", "output"), { recursive: true });
|
|
5189
|
+
await mkdir4(path12.join(input.baseDir, "exe", "research"), { recursive: true });
|
|
5190
|
+
await ensureArchitectureDoc(input.baseDir, input.projectName);
|
|
5191
|
+
await ensureGitignoreExe(input.baseDir);
|
|
5192
|
+
} catch {
|
|
5193
|
+
}
|
|
4999
5194
|
}
|
|
5195
|
+
const complexity = input.complexity ?? "standard";
|
|
5196
|
+
let sessionScope = null;
|
|
5000
5197
|
try {
|
|
5001
|
-
const
|
|
5002
|
-
|
|
5003
|
-
logIntercom(`SKIP \u2192 ${targetSession} (session not found)`);
|
|
5004
|
-
return "failed";
|
|
5005
|
-
}
|
|
5006
|
-
const sessionState = getSessionState(targetSession);
|
|
5007
|
-
if (sessionState === "no_claude") {
|
|
5008
|
-
queueIntercom(targetSession, "claude not running in session");
|
|
5009
|
-
recordDebounce(targetSession);
|
|
5010
|
-
logIntercom(`QUEUED \u2192 ${targetSession} (no claude process \u2014 raw shell detected)`);
|
|
5011
|
-
return "queued";
|
|
5012
|
-
}
|
|
5013
|
-
if (sessionState === "thinking" || sessionState === "tool") {
|
|
5014
|
-
queueIntercom(targetSession, "session busy at send time");
|
|
5015
|
-
recordDebounce(targetSession);
|
|
5016
|
-
logIntercom(`QUEUED \u2192 ${targetSession} (session busy, will retry from queue)`);
|
|
5017
|
-
return "queued";
|
|
5018
|
-
}
|
|
5019
|
-
if (transport.isPaneInCopyMode(targetSession)) {
|
|
5020
|
-
logIntercom(`COPY_MODE \u2192 ${targetSession} (exiting copy mode first)`);
|
|
5021
|
-
transport.sendKeys(targetSession, "q");
|
|
5022
|
-
}
|
|
5023
|
-
transport.sendKeys(targetSession, "/exe-intercom");
|
|
5024
|
-
recordDebounce(targetSession);
|
|
5025
|
-
logIntercom(`DELIVERED \u2192 ${targetSession} (fire-and-forget)`);
|
|
5026
|
-
return "delivered";
|
|
5198
|
+
const { resolveExeSession: resolveExeSession2 } = await Promise.resolve().then(() => (init_tmux_routing(), tmux_routing_exports));
|
|
5199
|
+
sessionScope = resolveExeSession2();
|
|
5027
5200
|
} catch {
|
|
5028
|
-
logIntercom(`FAIL \u2192 ${targetSession}`);
|
|
5029
|
-
return "failed";
|
|
5030
5201
|
}
|
|
5202
|
+
await client.execute({
|
|
5203
|
+
sql: `INSERT INTO tasks (id, title, assigned_to, assigned_by, project_name, priority, status, task_file, blocked_by, parent_task_id, reviewer, context, complexity, budget_tokens, budget_fallback_model, tokens_used, tokens_warned_at, session_scope, created_at, updated_at)
|
|
5204
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
5205
|
+
args: [
|
|
5206
|
+
id,
|
|
5207
|
+
input.title,
|
|
5208
|
+
input.assignedTo,
|
|
5209
|
+
input.assignedBy,
|
|
5210
|
+
input.projectName,
|
|
5211
|
+
input.priority,
|
|
5212
|
+
initialStatus,
|
|
5213
|
+
taskFile,
|
|
5214
|
+
blockedById,
|
|
5215
|
+
parentTaskId,
|
|
5216
|
+
input.reviewer ?? null,
|
|
5217
|
+
input.context,
|
|
5218
|
+
complexity,
|
|
5219
|
+
input.budgetTokens ?? null,
|
|
5220
|
+
input.budgetFallbackModel ?? null,
|
|
5221
|
+
0,
|
|
5222
|
+
null,
|
|
5223
|
+
sessionScope,
|
|
5224
|
+
now,
|
|
5225
|
+
now
|
|
5226
|
+
]
|
|
5227
|
+
});
|
|
5228
|
+
return {
|
|
5229
|
+
id,
|
|
5230
|
+
title: input.title,
|
|
5231
|
+
assignedTo: input.assignedTo,
|
|
5232
|
+
assignedBy: input.assignedBy,
|
|
5233
|
+
projectName: input.projectName,
|
|
5234
|
+
priority: input.priority,
|
|
5235
|
+
status: initialStatus,
|
|
5236
|
+
taskFile,
|
|
5237
|
+
createdAt: now,
|
|
5238
|
+
updatedAt: now,
|
|
5239
|
+
warning,
|
|
5240
|
+
budgetTokens: input.budgetTokens ?? null,
|
|
5241
|
+
budgetFallbackModel: input.budgetFallbackModel ?? null,
|
|
5242
|
+
tokensUsed: 0,
|
|
5243
|
+
tokensWarnedAt: null
|
|
5244
|
+
};
|
|
5031
5245
|
}
|
|
5032
|
-
function
|
|
5033
|
-
const
|
|
5034
|
-
|
|
5035
|
-
|
|
5036
|
-
|
|
5037
|
-
|
|
5246
|
+
async function listTasks(input) {
|
|
5247
|
+
const client = getClient();
|
|
5248
|
+
const conditions = [];
|
|
5249
|
+
const args = [];
|
|
5250
|
+
if (input.assignedTo) {
|
|
5251
|
+
conditions.push("assigned_to = ?");
|
|
5252
|
+
args.push(input.assignedTo);
|
|
5038
5253
|
}
|
|
5039
|
-
|
|
5040
|
-
|
|
5041
|
-
|
|
5042
|
-
|
|
5043
|
-
|
|
5044
|
-
if (rootExe && rootExe !== target) {
|
|
5045
|
-
process.stderr.write(`[intercom] notifyParentExe: dispatcher ${target} dead, falling back to root exe ${rootExe}
|
|
5046
|
-
`);
|
|
5047
|
-
const fallback = sendIntercom(rootExe);
|
|
5048
|
-
return fallback !== "failed";
|
|
5049
|
-
}
|
|
5050
|
-
return false;
|
|
5254
|
+
if (input.status) {
|
|
5255
|
+
conditions.push("status = ?");
|
|
5256
|
+
args.push(input.status);
|
|
5257
|
+
} else {
|
|
5258
|
+
conditions.push("status IN ('open', 'in_progress', 'blocked')");
|
|
5051
5259
|
}
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
|
|
5056
|
-
|
|
5260
|
+
if (input.projectName) {
|
|
5261
|
+
conditions.push("project_name = ?");
|
|
5262
|
+
args.push(input.projectName);
|
|
5263
|
+
}
|
|
5264
|
+
if (input.priority) {
|
|
5265
|
+
conditions.push("priority = ?");
|
|
5266
|
+
args.push(input.priority);
|
|
5057
5267
|
}
|
|
5058
5268
|
try {
|
|
5059
|
-
|
|
5060
|
-
|
|
5061
|
-
if (
|
|
5062
|
-
|
|
5269
|
+
const { resolveExeSession: resolveExeSession2 } = await Promise.resolve().then(() => (init_tmux_routing(), tmux_routing_exports));
|
|
5270
|
+
const session = resolveExeSession2();
|
|
5271
|
+
if (session) {
|
|
5272
|
+
conditions.push("(session_scope IS NULL OR session_scope = ?)");
|
|
5273
|
+
args.push(session);
|
|
5063
5274
|
}
|
|
5275
|
+
} catch {
|
|
5064
5276
|
}
|
|
5065
|
-
|
|
5066
|
-
|
|
5067
|
-
|
|
5068
|
-
|
|
5069
|
-
|
|
5070
|
-
|
|
5071
|
-
|
|
5277
|
+
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
5278
|
+
const result = await client.execute({
|
|
5279
|
+
sql: `SELECT * FROM tasks ${where} ORDER BY CASE status WHEN 'blocked' THEN 0 WHEN 'in_progress' THEN 1 WHEN 'open' THEN 2 ELSE 3 END, priority ASC, created_at DESC LIMIT 1000`,
|
|
5280
|
+
args
|
|
5281
|
+
});
|
|
5282
|
+
return result.rows.map((r) => ({
|
|
5283
|
+
id: String(r.id),
|
|
5284
|
+
title: String(r.title),
|
|
5285
|
+
assignedTo: String(r.assigned_to),
|
|
5286
|
+
assignedBy: String(r.assigned_by),
|
|
5287
|
+
projectName: String(r.project_name),
|
|
5288
|
+
priority: String(r.priority),
|
|
5289
|
+
status: String(r.status),
|
|
5290
|
+
taskFile: String(r.task_file),
|
|
5291
|
+
createdAt: String(r.created_at),
|
|
5292
|
+
updatedAt: String(r.updated_at),
|
|
5293
|
+
checkpointCount: Number(r.checkpoint_count ?? 0),
|
|
5294
|
+
budgetTokens: r.budget_tokens !== null ? Number(r.budget_tokens) : null,
|
|
5295
|
+
budgetFallbackModel: r.budget_fallback_model !== null ? String(r.budget_fallback_model) : null,
|
|
5296
|
+
tokensUsed: Number(r.tokens_used ?? 0),
|
|
5297
|
+
tokensWarnedAt: r.tokens_warned_at !== null ? Number(r.tokens_warned_at) : null
|
|
5298
|
+
}));
|
|
5299
|
+
}
|
|
5300
|
+
function checkStaleCompletion(taskContext, taskCreatedAt) {
|
|
5301
|
+
if (!taskContext) return null;
|
|
5302
|
+
if (!DELEGATION_KEYWORDS.test(taskContext)) return null;
|
|
5303
|
+
try {
|
|
5304
|
+
const since = new Date(taskCreatedAt).toISOString();
|
|
5305
|
+
const branch = execSync4(
|
|
5306
|
+
"git rev-parse --abbrev-ref HEAD 2>/dev/null",
|
|
5307
|
+
{ encoding: "utf8", timeout: 3e3 }
|
|
5308
|
+
).trim();
|
|
5309
|
+
const branchArg = branch && branch !== "HEAD" ? branch : "";
|
|
5310
|
+
const commitCount = execSync4(
|
|
5311
|
+
`git log --oneline --since="${since}" ${branchArg} 2>/dev/null | wc -l`,
|
|
5312
|
+
{ encoding: "utf8", timeout: 5e3 }
|
|
5313
|
+
).trim();
|
|
5314
|
+
const count = parseInt(commitCount, 10);
|
|
5315
|
+
if (count === 0) {
|
|
5316
|
+
return "WARNING: task closed with no new commits since creation. Verify work was actually produced.";
|
|
5317
|
+
}
|
|
5318
|
+
return null;
|
|
5319
|
+
} catch {
|
|
5320
|
+
return null;
|
|
5072
5321
|
}
|
|
5073
|
-
|
|
5074
|
-
|
|
5075
|
-
|
|
5076
|
-
|
|
5077
|
-
|
|
5078
|
-
|
|
5322
|
+
}
|
|
5323
|
+
async function updateTaskStatus(input) {
|
|
5324
|
+
const client = getClient();
|
|
5325
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5326
|
+
const row = await resolveTask(client, input.taskId);
|
|
5327
|
+
const taskId = String(row.id);
|
|
5328
|
+
const taskFile = String(row.task_file);
|
|
5329
|
+
if (input.status === "done" && String(row.assigned_by) === "system" && taskFile.includes("review-")) {
|
|
5330
|
+
process.stderr.write(
|
|
5331
|
+
`[updateTask] Review task "${String(row.title)}" being marked done (assigned to ${String(row.assigned_to)})
|
|
5332
|
+
`
|
|
5079
5333
|
);
|
|
5080
|
-
|
|
5081
|
-
|
|
5082
|
-
|
|
5083
|
-
|
|
5084
|
-
|
|
5085
|
-
|
|
5334
|
+
}
|
|
5335
|
+
if (input.status === "done") {
|
|
5336
|
+
const existingRow = await client.execute({
|
|
5337
|
+
sql: "SELECT context, created_at FROM tasks WHERE id = ?",
|
|
5338
|
+
args: [taskId]
|
|
5339
|
+
});
|
|
5340
|
+
if (existingRow.rows.length > 0) {
|
|
5341
|
+
const ctx = existingRow.rows[0];
|
|
5342
|
+
const warning = checkStaleCompletion(ctx.context, ctx.created_at);
|
|
5343
|
+
if (warning) {
|
|
5344
|
+
input.result = input.result ? `\u26A0\uFE0F ${warning}
|
|
5345
|
+
|
|
5346
|
+
${input.result}` : `\u26A0\uFE0F ${warning}`;
|
|
5347
|
+
process.stderr.write(`[tasks] ${warning} (task: ${taskId})
|
|
5348
|
+
`);
|
|
5349
|
+
}
|
|
5086
5350
|
}
|
|
5087
|
-
effectiveInstance = free === 0 ? void 0 : free;
|
|
5088
5351
|
}
|
|
5089
|
-
|
|
5090
|
-
|
|
5091
|
-
const
|
|
5092
|
-
|
|
5093
|
-
|
|
5352
|
+
if (input.status === "in_progress") {
|
|
5353
|
+
const tmuxSession = process.env.TMUX_PANE ?? process.env.MY_TMUX_SESSION ?? "unknown";
|
|
5354
|
+
const claim = await client.execute({
|
|
5355
|
+
sql: `UPDATE tasks
|
|
5356
|
+
SET status = 'in_progress', assigned_tmux = ?, updated_at = ?
|
|
5357
|
+
WHERE id = ? AND status = 'open'`,
|
|
5358
|
+
args: [tmuxSession, now, taskId]
|
|
5359
|
+
});
|
|
5360
|
+
if (claim.rowsAffected === 0) {
|
|
5361
|
+
const current = await client.execute({
|
|
5362
|
+
sql: "SELECT status, assigned_tmux FROM tasks WHERE id = ?",
|
|
5363
|
+
args: [taskId]
|
|
5364
|
+
});
|
|
5365
|
+
const cur = current.rows[0];
|
|
5366
|
+
const status = cur?.status ?? "unknown";
|
|
5367
|
+
const claimedBy = cur?.assigned_tmux ? ` (claimed by ${cur.assigned_tmux})` : "";
|
|
5368
|
+
throw new Error(`${TASK_ALREADY_CLAIMED_PREFIX}: task ${taskId} is ${status}${claimedBy}`);
|
|
5094
5369
|
}
|
|
5095
|
-
|
|
5096
|
-
|
|
5370
|
+
try {
|
|
5371
|
+
await writeCheckpoint({
|
|
5372
|
+
taskId,
|
|
5373
|
+
step: "claimed",
|
|
5374
|
+
contextSummary: `Task claimed by session. Transitioning open \u2192 in_progress.`
|
|
5375
|
+
});
|
|
5376
|
+
} catch {
|
|
5097
5377
|
}
|
|
5098
|
-
return {
|
|
5099
|
-
}
|
|
5100
|
-
const spawnOpts = { ...opts, instance: effectiveInstance };
|
|
5101
|
-
const result = spawnEmployee(employeeName, exeSession, projectDir, spawnOpts);
|
|
5102
|
-
if (result.error) {
|
|
5103
|
-
return { status: "failed", sessionName, error: result.error };
|
|
5378
|
+
return { row, taskFile, now, taskId };
|
|
5104
5379
|
}
|
|
5105
|
-
|
|
5106
|
-
|
|
5107
|
-
|
|
5108
|
-
|
|
5109
|
-
|
|
5110
|
-
|
|
5111
|
-
|
|
5112
|
-
|
|
5113
|
-
|
|
5114
|
-
|
|
5380
|
+
if (input.result) {
|
|
5381
|
+
await client.execute({
|
|
5382
|
+
sql: "UPDATE tasks SET status = ?, result = ?, updated_at = ? WHERE id = ?",
|
|
5383
|
+
args: [input.status, input.result, now, taskId]
|
|
5384
|
+
});
|
|
5385
|
+
} else {
|
|
5386
|
+
await client.execute({
|
|
5387
|
+
sql: "UPDATE tasks SET status = ?, updated_at = ? WHERE id = ?",
|
|
5388
|
+
args: [input.status, now, taskId]
|
|
5389
|
+
});
|
|
5115
5390
|
}
|
|
5116
|
-
transport.kill(sessionName);
|
|
5117
|
-
let cleanupSuffix = "";
|
|
5118
5391
|
try {
|
|
5119
|
-
|
|
5120
|
-
|
|
5121
|
-
|
|
5122
|
-
|
|
5123
|
-
}
|
|
5392
|
+
await writeCheckpoint({
|
|
5393
|
+
taskId,
|
|
5394
|
+
step: `status_transition:${input.status}`,
|
|
5395
|
+
contextSummary: input.result ? `Transitioned to ${input.status}. Result: ${input.result.slice(0, 500)}` : `Transitioned to ${input.status}.`
|
|
5396
|
+
});
|
|
5124
5397
|
} catch {
|
|
5125
5398
|
}
|
|
5399
|
+
return { row, taskFile, now, taskId };
|
|
5400
|
+
}
|
|
5401
|
+
async function deleteTaskCore(taskId, _baseDir) {
|
|
5402
|
+
const client = getClient();
|
|
5403
|
+
const row = await resolveTask(client, taskId);
|
|
5404
|
+
const id = String(row.id);
|
|
5405
|
+
const taskFile = String(row.task_file);
|
|
5406
|
+
const assignedTo = String(row.assigned_to);
|
|
5407
|
+
const assignedBy = String(row.assigned_by);
|
|
5408
|
+
await client.execute({ sql: "DELETE FROM tasks WHERE id = ?", args: [id] });
|
|
5409
|
+
const taskSlug = taskFile.split("/").pop()?.replace(".md", "") ?? "";
|
|
5410
|
+
return { taskFile, assignedTo, assignedBy, taskSlug };
|
|
5411
|
+
}
|
|
5412
|
+
async function ensureArchitectureDoc(baseDir, projectName) {
|
|
5413
|
+
const archPath = path12.join(baseDir, "exe", "ARCHITECTURE.md");
|
|
5126
5414
|
try {
|
|
5127
|
-
|
|
5128
|
-
|
|
5129
|
-
|
|
5130
|
-
|
|
5131
|
-
|
|
5132
|
-
|
|
5133
|
-
|
|
5134
|
-
|
|
5135
|
-
|
|
5136
|
-
|
|
5137
|
-
|
|
5138
|
-
|
|
5415
|
+
if (existsSync11(archPath)) return;
|
|
5416
|
+
const template = [
|
|
5417
|
+
`# ${projectName} \u2014 System Architecture`,
|
|
5418
|
+
"",
|
|
5419
|
+
"> Employees: read this before every task. Update it when you change system structure.",
|
|
5420
|
+
`> Last updated: ${(/* @__PURE__ */ new Date()).toISOString().split("T")[0]}`,
|
|
5421
|
+
"",
|
|
5422
|
+
"## Overview",
|
|
5423
|
+
"",
|
|
5424
|
+
"<!-- Describe what this system does, its main components, and how they connect. -->",
|
|
5425
|
+
"",
|
|
5426
|
+
"## Key Components",
|
|
5427
|
+
"",
|
|
5428
|
+
"<!-- List the major modules, services, or subsystems. -->",
|
|
5429
|
+
"",
|
|
5430
|
+
"## Data Flow",
|
|
5431
|
+
"",
|
|
5432
|
+
"<!-- How does data move through the system? What writes where? -->",
|
|
5433
|
+
"",
|
|
5434
|
+
"## Invariants",
|
|
5435
|
+
"",
|
|
5436
|
+
"<!-- Rules that must never be violated. What breaks if these are wrong? -->",
|
|
5437
|
+
"",
|
|
5438
|
+
"## Dependencies",
|
|
5439
|
+
"",
|
|
5440
|
+
"<!-- What depends on what? If I change X, what else is affected? -->",
|
|
5441
|
+
""
|
|
5442
|
+
].join("\n");
|
|
5443
|
+
await writeFile4(archPath, template, "utf-8");
|
|
5139
5444
|
} catch {
|
|
5140
5445
|
}
|
|
5446
|
+
}
|
|
5447
|
+
async function ensureGitignoreExe(baseDir) {
|
|
5448
|
+
const gitignorePath = path12.join(baseDir, ".gitignore");
|
|
5141
5449
|
try {
|
|
5142
|
-
|
|
5143
|
-
|
|
5144
|
-
|
|
5145
|
-
|
|
5146
|
-
|
|
5147
|
-
|
|
5148
|
-
settings = JSON.parse(readFileSync9(settingsPath, "utf8"));
|
|
5149
|
-
} catch {
|
|
5150
|
-
}
|
|
5151
|
-
const perms = settings.permissions ?? {};
|
|
5152
|
-
const allow = perms.allow ?? [];
|
|
5153
|
-
const toolNames = [
|
|
5154
|
-
"recall_my_memory",
|
|
5155
|
-
"store_memory",
|
|
5156
|
-
"create_task",
|
|
5157
|
-
"update_task",
|
|
5158
|
-
"list_tasks",
|
|
5159
|
-
"get_task",
|
|
5160
|
-
"ask_team_memory",
|
|
5161
|
-
"store_behavior",
|
|
5162
|
-
"get_identity",
|
|
5163
|
-
"send_message"
|
|
5164
|
-
];
|
|
5165
|
-
const requiredTools = expandDualPrefixTools(toolNames);
|
|
5166
|
-
let changed = false;
|
|
5167
|
-
for (const tool of requiredTools) {
|
|
5168
|
-
if (!allow.includes(tool)) {
|
|
5169
|
-
allow.push(tool);
|
|
5170
|
-
changed = true;
|
|
5171
|
-
}
|
|
5172
|
-
}
|
|
5173
|
-
if (changed) {
|
|
5174
|
-
perms.allow = allow;
|
|
5175
|
-
settings.permissions = perms;
|
|
5176
|
-
mkdirSync6(projSettingsDir, { recursive: true });
|
|
5177
|
-
writeFileSync4(settingsPath, JSON.stringify(settings, null, 2) + "\n");
|
|
5450
|
+
if (existsSync11(gitignorePath)) {
|
|
5451
|
+
const content = readFileSync10(gitignorePath, "utf-8");
|
|
5452
|
+
if (/^\/?exe\/?$/m.test(content)) return;
|
|
5453
|
+
await appendFile(gitignorePath, "\n# Employee task assignments (private)\n/exe/\n");
|
|
5454
|
+
} else {
|
|
5455
|
+
await writeFile4(gitignorePath, "# Employee task assignments (private)\n/exe/\n", "utf-8");
|
|
5178
5456
|
}
|
|
5179
5457
|
} catch {
|
|
5180
5458
|
}
|
|
5181
|
-
|
|
5182
|
-
|
|
5183
|
-
|
|
5184
|
-
|
|
5185
|
-
|
|
5186
|
-
|
|
5187
|
-
|
|
5188
|
-
|
|
5189
|
-
const identityPath = path11.join(
|
|
5190
|
-
os6.homedir(),
|
|
5191
|
-
".exe-os",
|
|
5192
|
-
"identity",
|
|
5193
|
-
`${employeeName}.md`
|
|
5194
|
-
);
|
|
5195
|
-
_resetCcAgentSupportCache();
|
|
5196
|
-
const hasAgentFlag = claudeSupportsAgentFlag();
|
|
5197
|
-
if (hasAgentFlag) {
|
|
5198
|
-
identityFlag = ` --agent ${employeeName}`;
|
|
5199
|
-
} else if (existsSync10(identityPath)) {
|
|
5200
|
-
identityFlag = ` --append-system-prompt-file ${identityPath}`;
|
|
5201
|
-
legacyFallbackWarned = true;
|
|
5202
|
-
}
|
|
5203
|
-
const behaviorsFile = exportBehaviorsSync(
|
|
5204
|
-
employeeName,
|
|
5205
|
-
path11.basename(spawnCwd),
|
|
5206
|
-
sessionName
|
|
5207
|
-
);
|
|
5208
|
-
if (behaviorsFile) {
|
|
5209
|
-
behaviorsFlag = ` --append-system-prompt-file ${behaviorsFile}`;
|
|
5210
|
-
}
|
|
5459
|
+
}
|
|
5460
|
+
var DELEGATION_KEYWORDS, TASK_ALREADY_CLAIMED_PREFIX;
|
|
5461
|
+
var init_tasks_crud = __esm({
|
|
5462
|
+
"src/lib/tasks-crud.ts"() {
|
|
5463
|
+
"use strict";
|
|
5464
|
+
init_database();
|
|
5465
|
+
DELEGATION_KEYWORDS = /parallel|delegate|wave|tom\d*-exe/i;
|
|
5466
|
+
TASK_ALREADY_CLAIMED_PREFIX = "TASK_ALREADY_CLAIMED";
|
|
5211
5467
|
}
|
|
5212
|
-
|
|
5468
|
+
});
|
|
5469
|
+
|
|
5470
|
+
// src/lib/tasks-review.ts
|
|
5471
|
+
import path13 from "path";
|
|
5472
|
+
import { existsSync as existsSync12, readdirSync as readdirSync3, unlinkSync as unlinkSync3 } from "fs";
|
|
5473
|
+
async function countPendingReviews() {
|
|
5474
|
+
const client = getClient();
|
|
5475
|
+
const result = await client.execute({
|
|
5476
|
+
sql: "SELECT COUNT(*) as cnt FROM tasks WHERE status = 'needs_review'",
|
|
5477
|
+
args: []
|
|
5478
|
+
});
|
|
5479
|
+
return Number(result.rows[0]?.cnt) || 0;
|
|
5480
|
+
}
|
|
5481
|
+
async function countNewPendingReviewsSince(sinceIso) {
|
|
5482
|
+
const client = getClient();
|
|
5483
|
+
const result = await client.execute({
|
|
5484
|
+
sql: `SELECT COUNT(*) as cnt FROM tasks
|
|
5485
|
+
WHERE status = 'needs_review' AND updated_at > ?`,
|
|
5486
|
+
args: [sinceIso]
|
|
5487
|
+
});
|
|
5488
|
+
return Number(result.rows[0]?.cnt) || 0;
|
|
5489
|
+
}
|
|
5490
|
+
async function listPendingReviews(limit) {
|
|
5491
|
+
const client = getClient();
|
|
5492
|
+
const result = await client.execute({
|
|
5493
|
+
sql: `SELECT title, assigned_to, project_name FROM tasks
|
|
5494
|
+
WHERE status = 'needs_review'
|
|
5495
|
+
ORDER BY priority ASC, created_at DESC LIMIT ?`,
|
|
5496
|
+
args: [limit]
|
|
5497
|
+
});
|
|
5498
|
+
return result.rows;
|
|
5499
|
+
}
|
|
5500
|
+
async function cleanupOrphanedReviews() {
|
|
5501
|
+
const client = getClient();
|
|
5502
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5503
|
+
const r1 = await client.execute({
|
|
5504
|
+
sql: `UPDATE tasks SET status = 'done', updated_at = ?
|
|
5505
|
+
WHERE status = 'needs_review'
|
|
5506
|
+
AND assigned_by = 'system'
|
|
5507
|
+
AND title LIKE 'Review:%'
|
|
5508
|
+
AND parent_task_id IN (SELECT id FROM tasks WHERE status IN ('done', 'cancelled'))`,
|
|
5509
|
+
args: [now]
|
|
5510
|
+
});
|
|
5511
|
+
const staleThreshold = new Date(Date.now() - 60 * 60 * 1e3).toISOString();
|
|
5512
|
+
const r2 = await client.execute({
|
|
5513
|
+
sql: `UPDATE tasks SET status = 'done', updated_at = ?
|
|
5514
|
+
WHERE status = 'needs_review'
|
|
5515
|
+
AND result IS NOT NULL
|
|
5516
|
+
AND updated_at < ?`,
|
|
5517
|
+
args: [now, staleThreshold]
|
|
5518
|
+
});
|
|
5519
|
+
const total = r1.rowsAffected + r2.rowsAffected;
|
|
5520
|
+
if (total > 0) {
|
|
5213
5521
|
process.stderr.write(
|
|
5214
|
-
`[
|
|
5522
|
+
`[cleanup] Closed ${total} orphaned review(s): ${r1.rowsAffected} cascade + ${r2.rowsAffected} stale
|
|
5215
5523
|
`
|
|
5216
5524
|
);
|
|
5217
5525
|
}
|
|
5218
|
-
|
|
5219
|
-
|
|
5220
|
-
|
|
5221
|
-
|
|
5222
|
-
|
|
5223
|
-
|
|
5224
|
-
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
5228
|
-
|
|
5229
|
-
|
|
5230
|
-
|
|
5231
|
-
|
|
5526
|
+
return total;
|
|
5527
|
+
}
|
|
5528
|
+
function getReviewChecklist(role, agent, taskSlug) {
|
|
5529
|
+
const roleLower = role.toLowerCase();
|
|
5530
|
+
if (roleLower.includes("engineer") || roleLower === "principal engineer") {
|
|
5531
|
+
return {
|
|
5532
|
+
lens: "Code Quality (Engineer)",
|
|
5533
|
+
checklist: [
|
|
5534
|
+
"1. Do all tests pass? Any new tests needed?",
|
|
5535
|
+
"2. Is the code clean \u2014 no dead code, no TODOs left?",
|
|
5536
|
+
"3. Does it follow existing patterns and conventions in the codebase?",
|
|
5537
|
+
"4. Any regressions in the test suite?"
|
|
5538
|
+
]
|
|
5539
|
+
};
|
|
5232
5540
|
}
|
|
5233
|
-
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
|
|
5541
|
+
if (roleLower === "cto" || roleLower.includes("architect")) {
|
|
5542
|
+
return {
|
|
5543
|
+
lens: "Architecture (CTO)",
|
|
5544
|
+
checklist: [
|
|
5545
|
+
"1. Does this fit the existing architecture? Consistent with ARCHITECTURE.md?",
|
|
5546
|
+
"2. Is it backward compatible? Any breaking changes?",
|
|
5547
|
+
"3. Does it introduce technical debt? Is that debt justified?",
|
|
5548
|
+
"4. Security implications? Any new attack surface?",
|
|
5549
|
+
"5. Does it scale? Performance considerations?",
|
|
5550
|
+
"6. Coordination: does this affect other employees' work or other projects?"
|
|
5551
|
+
]
|
|
5552
|
+
};
|
|
5553
|
+
}
|
|
5554
|
+
if (roleLower === "coo" || roleLower.includes("operations")) {
|
|
5555
|
+
return {
|
|
5556
|
+
lens: "Strategic (COO)",
|
|
5557
|
+
checklist: [
|
|
5558
|
+
"1. Does this serve the project mission?",
|
|
5559
|
+
"2. Is this the right work at the right time?",
|
|
5560
|
+
"3. Does the architectural assessment make sense for the business?",
|
|
5561
|
+
"4. Any cross-project implications?"
|
|
5562
|
+
]
|
|
5563
|
+
};
|
|
5564
|
+
}
|
|
5565
|
+
return {
|
|
5566
|
+
lens: "General",
|
|
5567
|
+
checklist: [
|
|
5568
|
+
"1. Read the original task's acceptance criteria",
|
|
5569
|
+
`2. Check git log for related commits: \`git log --oneline --author-date-order -10\``,
|
|
5570
|
+
"3. Verify code changes match requirements",
|
|
5571
|
+
"4. Check if tests were added/updated",
|
|
5572
|
+
`5. Look for output files in exe/output/${agent}-${taskSlug}*`
|
|
5573
|
+
]
|
|
5574
|
+
};
|
|
5575
|
+
}
|
|
5576
|
+
async function cleanupReviewFile(row, taskFile, _baseDir) {
|
|
5577
|
+
if (String(row.assigned_by) !== "system" || !taskFile.includes("review-")) return;
|
|
5578
|
+
try {
|
|
5579
|
+
const client = getClient();
|
|
5580
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5581
|
+
const parentId = row.parent_task_id ? String(row.parent_task_id) : null;
|
|
5582
|
+
if (parentId) {
|
|
5583
|
+
const result = await client.execute({
|
|
5584
|
+
sql: "UPDATE tasks SET status = 'done', updated_at = ? WHERE id = ? AND status = 'needs_review'",
|
|
5585
|
+
args: [now, parentId]
|
|
5586
|
+
});
|
|
5587
|
+
if (result.rowsAffected > 0) {
|
|
5588
|
+
process.stderr.write(
|
|
5589
|
+
`[review-cleanup] Cascaded original task to done via parent_task_id: ${parentId}
|
|
5590
|
+
`
|
|
5591
|
+
);
|
|
5592
|
+
}
|
|
5593
|
+
} else {
|
|
5594
|
+
const fileName = taskFile.split("/").pop() ?? "";
|
|
5595
|
+
const reviewPrefix = fileName.replace(".md", "");
|
|
5596
|
+
const parts = reviewPrefix.split("-");
|
|
5597
|
+
if (parts.length >= 3 && parts[0] === "review") {
|
|
5598
|
+
const agent = parts[1];
|
|
5599
|
+
const slug = parts.slice(2).join("-");
|
|
5600
|
+
const originalTaskFile = `exe/${agent}/${slug}.md`;
|
|
5601
|
+
const result = await client.execute({
|
|
5602
|
+
sql: "UPDATE tasks SET status = 'done', updated_at = ? WHERE task_file = ? AND status = 'needs_review'",
|
|
5603
|
+
args: [now, originalTaskFile]
|
|
5604
|
+
});
|
|
5605
|
+
if (result.rowsAffected > 0) {
|
|
5606
|
+
process.stderr.write(
|
|
5607
|
+
`[review-cleanup] Cascaded original task to done (legacy path): ${originalTaskFile}
|
|
5608
|
+
`
|
|
5609
|
+
);
|
|
5610
|
+
}
|
|
5240
5611
|
}
|
|
5241
5612
|
}
|
|
5242
|
-
}
|
|
5243
|
-
let spawnCommand;
|
|
5244
|
-
if (useExeAgent) {
|
|
5245
|
-
spawnCommand = `${envPrefix} exe-agent --employee ${employeeName} --model ${opts.model} --provider ${opts.provider}${cleanupSuffix}`;
|
|
5246
|
-
} else if (useBinSymlink) {
|
|
5247
|
-
const binName = `${employeeName}-${ccProvider}`;
|
|
5613
|
+
} catch (err) {
|
|
5248
5614
|
process.stderr.write(
|
|
5249
|
-
`[
|
|
5615
|
+
`[review-cleanup] Failed to cascade original task: ${err instanceof Error ? err.message : String(err)}
|
|
5250
5616
|
`
|
|
5251
5617
|
);
|
|
5252
|
-
spawnCommand = `${envPrefix} ${binName}${cleanupSuffix}`;
|
|
5253
|
-
} else {
|
|
5254
|
-
spawnCommand = `${envPrefix} claude --dangerously-skip-permissions${identityFlag}${behaviorsFlag}${sessionContextFlag}${cleanupSuffix}`;
|
|
5255
|
-
}
|
|
5256
|
-
const spawnResult = transport.spawn(sessionName, {
|
|
5257
|
-
cwd: spawnCwd,
|
|
5258
|
-
command: spawnCommand
|
|
5259
|
-
});
|
|
5260
|
-
if (spawnResult.error) {
|
|
5261
|
-
releaseSpawnLock2(sessionName);
|
|
5262
|
-
return { sessionName, error: `tmux new-session failed: ${spawnResult.error}` };
|
|
5263
5618
|
}
|
|
5264
|
-
transport.pipeLog(sessionName, logFile);
|
|
5265
5619
|
try {
|
|
5266
|
-
const
|
|
5267
|
-
|
|
5268
|
-
|
|
5269
|
-
|
|
5270
|
-
|
|
5271
|
-
provider: useBinSymlink ? ccProvider : useExeAgent ? opts.provider : "anthropic",
|
|
5272
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
5273
|
-
}));
|
|
5274
|
-
} catch {
|
|
5275
|
-
}
|
|
5276
|
-
let booted = false;
|
|
5277
|
-
for (let i = 0; i < 30; i++) {
|
|
5278
|
-
try {
|
|
5279
|
-
execSync4("sleep 0.5");
|
|
5280
|
-
} catch {
|
|
5281
|
-
}
|
|
5282
|
-
try {
|
|
5283
|
-
const pane = transport.capturePane(sessionName);
|
|
5284
|
-
if (useExeAgent) {
|
|
5285
|
-
if (pane.includes("[exe-agent]") || pane.includes("online")) {
|
|
5286
|
-
booted = true;
|
|
5287
|
-
break;
|
|
5288
|
-
}
|
|
5289
|
-
} else {
|
|
5290
|
-
if (pane.includes("Claude Code") || pane.includes("\u276F")) {
|
|
5291
|
-
booted = true;
|
|
5292
|
-
break;
|
|
5620
|
+
const cacheDir = path13.join(EXE_AI_DIR, "session-cache");
|
|
5621
|
+
if (existsSync12(cacheDir)) {
|
|
5622
|
+
for (const f of readdirSync3(cacheDir)) {
|
|
5623
|
+
if (f.startsWith("review-notified-")) {
|
|
5624
|
+
unlinkSync3(path13.join(cacheDir, f));
|
|
5293
5625
|
}
|
|
5294
5626
|
}
|
|
5295
|
-
} catch {
|
|
5296
|
-
}
|
|
5297
|
-
}
|
|
5298
|
-
if (!booted) {
|
|
5299
|
-
releaseSpawnLock2(sessionName);
|
|
5300
|
-
return { sessionName, error: `${useExeAgent ? "exe-agent" : "claude"} did not boot within 15s` };
|
|
5301
|
-
}
|
|
5302
|
-
if (!useExeAgent) {
|
|
5303
|
-
try {
|
|
5304
|
-
transport.sendKeys(sessionName, `/exe-call ${employeeName}`);
|
|
5305
|
-
} catch {
|
|
5306
5627
|
}
|
|
5628
|
+
} catch {
|
|
5307
5629
|
}
|
|
5308
|
-
registerSession({
|
|
5309
|
-
windowName: sessionName,
|
|
5310
|
-
agentId: employeeName,
|
|
5311
|
-
projectDir: spawnCwd,
|
|
5312
|
-
parentExe: exeSession,
|
|
5313
|
-
pid: 0,
|
|
5314
|
-
registeredAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
5315
|
-
});
|
|
5316
|
-
releaseSpawnLock2(sessionName);
|
|
5317
|
-
return { sessionName };
|
|
5318
5630
|
}
|
|
5319
|
-
var
|
|
5320
|
-
|
|
5321
|
-
"src/lib/tmux-routing.ts"() {
|
|
5631
|
+
var init_tasks_review = __esm({
|
|
5632
|
+
"src/lib/tasks-review.ts"() {
|
|
5322
5633
|
"use strict";
|
|
5323
|
-
|
|
5634
|
+
init_database();
|
|
5635
|
+
init_config();
|
|
5636
|
+
init_employees();
|
|
5637
|
+
init_notifications();
|
|
5638
|
+
init_tasks_crud();
|
|
5639
|
+
init_tmux_routing();
|
|
5324
5640
|
init_session_key();
|
|
5325
|
-
|
|
5326
|
-
init_cc_agent_support();
|
|
5327
|
-
init_mcp_prefix();
|
|
5328
|
-
init_provider_table();
|
|
5329
|
-
init_intercom_queue();
|
|
5330
|
-
init_plan_limits();
|
|
5331
|
-
SPAWN_LOCK_DIR = path11.join(os6.homedir(), ".exe-os", "spawn-locks");
|
|
5332
|
-
SESSION_CACHE = path11.join(os6.homedir(), ".exe-os", "session-cache");
|
|
5333
|
-
BEHAVIORS_EXPORT_TIMEOUT_MS = 1e4;
|
|
5334
|
-
INTERCOM_DEBOUNCE_MS = 3e4;
|
|
5335
|
-
INTERCOM_LOG2 = path11.join(os6.homedir(), ".exe-os", "intercom.log");
|
|
5336
|
-
DEBOUNCE_FILE = path11.join(SESSION_CACHE, "intercom-debounce.json");
|
|
5337
|
-
DEBOUNCE_CLEANUP_AGE_MS = 5 * 60 * 1e3;
|
|
5338
|
-
BUSY_PATTERN = /[✻✽✶✳·].*…|Running…/;
|
|
5641
|
+
init_state_bus();
|
|
5339
5642
|
}
|
|
5340
5643
|
});
|
|
5341
5644
|
|
|
5342
|
-
// src/lib/
|
|
5343
|
-
|
|
5344
|
-
|
|
5345
|
-
|
|
5346
|
-
getFailedMessages: () => getFailedMessages,
|
|
5347
|
-
getMessageStatus: () => getMessageStatus,
|
|
5348
|
-
getPendingMessages: () => getPendingMessages,
|
|
5349
|
-
getReadMessages: () => getReadMessages,
|
|
5350
|
-
getUnacknowledgedMessages: () => getUnacknowledgedMessages,
|
|
5351
|
-
markAcknowledged: () => markAcknowledged,
|
|
5352
|
-
markFailed: () => markFailed,
|
|
5353
|
-
markProcessed: () => markProcessed,
|
|
5354
|
-
markRead: () => markRead,
|
|
5355
|
-
retryPendingMessages: () => retryPendingMessages,
|
|
5356
|
-
sendMessage: () => sendMessage,
|
|
5357
|
-
setWsClientSend: () => setWsClientSend
|
|
5358
|
-
});
|
|
5359
|
-
import crypto3 from "crypto";
|
|
5360
|
-
function generateUlid() {
|
|
5361
|
-
const timestamp = Date.now().toString(36).padStart(10, "0");
|
|
5362
|
-
const random = crypto3.randomBytes(10).toString("hex").slice(0, 16);
|
|
5363
|
-
return (timestamp + random).toUpperCase();
|
|
5364
|
-
}
|
|
5365
|
-
function rowToMessage(row) {
|
|
5366
|
-
return {
|
|
5367
|
-
id: row.id,
|
|
5368
|
-
fromAgent: row.from_agent,
|
|
5369
|
-
fromDevice: row.from_device,
|
|
5370
|
-
targetAgent: row.target_agent,
|
|
5371
|
-
targetProject: row.target_project ?? null,
|
|
5372
|
-
targetDevice: row.target_device,
|
|
5373
|
-
content: row.content,
|
|
5374
|
-
priority: row.priority ?? "normal",
|
|
5375
|
-
status: row.status ?? "pending",
|
|
5376
|
-
serverSeq: row.server_seq != null ? Number(row.server_seq) : null,
|
|
5377
|
-
retryCount: Number(row.retry_count ?? 0),
|
|
5378
|
-
createdAt: row.created_at,
|
|
5379
|
-
deliveredAt: row.delivered_at ?? null,
|
|
5380
|
-
processedAt: row.processed_at ?? null,
|
|
5381
|
-
failedAt: row.failed_at ?? null,
|
|
5382
|
-
failureReason: row.failure_reason ?? null
|
|
5383
|
-
};
|
|
5384
|
-
}
|
|
5385
|
-
async function sendMessage(input) {
|
|
5645
|
+
// src/lib/tasks-chain.ts
|
|
5646
|
+
import path14 from "path";
|
|
5647
|
+
import { readFile as readFile4, writeFile as writeFile5 } from "fs/promises";
|
|
5648
|
+
async function cascadeUnblock(taskId, baseDir, now) {
|
|
5386
5649
|
const client = getClient();
|
|
5387
|
-
const
|
|
5388
|
-
|
|
5389
|
-
|
|
5390
|
-
|
|
5391
|
-
sql: `INSERT INTO messages (id, from_agent, from_device, target_agent, target_project, target_device, content, priority, status, created_at)
|
|
5392
|
-
VALUES (?, ?, 'local', ?, ?, ?, ?, ?, 'pending', ?)`,
|
|
5393
|
-
args: [
|
|
5394
|
-
id,
|
|
5395
|
-
input.fromAgent,
|
|
5396
|
-
input.targetAgent,
|
|
5397
|
-
input.targetProject ?? null,
|
|
5398
|
-
targetDevice,
|
|
5399
|
-
input.content,
|
|
5400
|
-
input.priority ?? "normal",
|
|
5401
|
-
now
|
|
5402
|
-
]
|
|
5650
|
+
const unblocked = await client.execute({
|
|
5651
|
+
sql: `UPDATE tasks SET status = 'open', blocked_by = NULL, updated_at = ?
|
|
5652
|
+
WHERE blocked_by = ? AND status = 'blocked'`,
|
|
5653
|
+
args: [now, taskId]
|
|
5403
5654
|
});
|
|
5404
|
-
|
|
5405
|
-
|
|
5406
|
-
|
|
5407
|
-
|
|
5408
|
-
|
|
5655
|
+
if (baseDir && unblocked.rowsAffected > 0) {
|
|
5656
|
+
const unblockedRows = await client.execute({
|
|
5657
|
+
sql: `SELECT task_file FROM tasks WHERE blocked_by IS NULL AND updated_at = ?`,
|
|
5658
|
+
args: [now]
|
|
5659
|
+
});
|
|
5660
|
+
for (const ur of unblockedRows.rows) {
|
|
5661
|
+
try {
|
|
5662
|
+
const ubFile = path14.join(baseDir, String(ur.task_file));
|
|
5663
|
+
let ubContent = await readFile4(ubFile, "utf-8");
|
|
5664
|
+
ubContent = ubContent.replace(/\*\*Status:\*\* blocked/, "**Status:** open");
|
|
5665
|
+
ubContent = ubContent.replace(/\n\*\*Blocked by:\*\*.*\n/, "\n");
|
|
5666
|
+
await writeFile5(ubFile, ubContent, "utf-8");
|
|
5667
|
+
} catch {
|
|
5668
|
+
}
|
|
5409
5669
|
}
|
|
5410
|
-
} catch {
|
|
5411
5670
|
}
|
|
5412
|
-
const result = await client.execute({
|
|
5413
|
-
sql: "SELECT * FROM messages WHERE id = ?",
|
|
5414
|
-
args: [id]
|
|
5415
|
-
});
|
|
5416
|
-
return rowToMessage(result.rows[0]);
|
|
5417
|
-
}
|
|
5418
|
-
function setWsClientSend(fn) {
|
|
5419
|
-
_wsClientSend = fn;
|
|
5420
5671
|
}
|
|
5421
|
-
async function
|
|
5672
|
+
async function findNextTask(assignedTo) {
|
|
5422
5673
|
const client = getClient();
|
|
5423
|
-
const
|
|
5424
|
-
sql:
|
|
5425
|
-
|
|
5426
|
-
|
|
5427
|
-
|
|
5428
|
-
|
|
5429
|
-
if (msg.status !== "pending") return false;
|
|
5430
|
-
if (!_wsClientSend) {
|
|
5431
|
-
return false;
|
|
5432
|
-
}
|
|
5433
|
-
const payload = JSON.stringify({
|
|
5434
|
-
id: msg.id,
|
|
5435
|
-
fromAgent: msg.fromAgent,
|
|
5436
|
-
targetAgent: msg.targetAgent,
|
|
5437
|
-
targetProject: msg.targetProject,
|
|
5438
|
-
content: msg.content,
|
|
5439
|
-
priority: msg.priority,
|
|
5440
|
-
createdAt: msg.createdAt
|
|
5674
|
+
const nextResult = await client.execute({
|
|
5675
|
+
sql: `SELECT title, task_file, priority FROM tasks
|
|
5676
|
+
WHERE assigned_to = ? AND status = 'open'
|
|
5677
|
+
ORDER BY priority ASC, created_at ASC
|
|
5678
|
+
LIMIT 1`,
|
|
5679
|
+
args: [assignedTo]
|
|
5441
5680
|
});
|
|
5442
|
-
|
|
5443
|
-
|
|
5444
|
-
|
|
5445
|
-
|
|
5446
|
-
|
|
5447
|
-
|
|
5448
|
-
|
|
5681
|
+
if (nextResult.rows.length === 1) {
|
|
5682
|
+
const nr = nextResult.rows[0];
|
|
5683
|
+
return {
|
|
5684
|
+
title: String(nr.title),
|
|
5685
|
+
priority: String(nr.priority),
|
|
5686
|
+
taskFile: String(nr.task_file)
|
|
5687
|
+
};
|
|
5449
5688
|
}
|
|
5450
|
-
return
|
|
5689
|
+
return void 0;
|
|
5451
5690
|
}
|
|
5452
|
-
async function
|
|
5691
|
+
async function checkSubtaskCompletion(parentTaskId, projectName) {
|
|
5453
5692
|
const client = getClient();
|
|
5454
|
-
const
|
|
5455
|
-
sql:
|
|
5456
|
-
|
|
5693
|
+
const remaining = await client.execute({
|
|
5694
|
+
sql: `SELECT COUNT(*) as cnt FROM tasks
|
|
5695
|
+
WHERE parent_task_id = ? AND status NOT IN ('done', 'cancelled')`,
|
|
5696
|
+
args: [parentTaskId]
|
|
5457
5697
|
});
|
|
5458
|
-
|
|
5459
|
-
|
|
5460
|
-
|
|
5461
|
-
|
|
5462
|
-
|
|
5463
|
-
try {
|
|
5464
|
-
const exeSession = resolveExeSession();
|
|
5465
|
-
if (!exeSession) {
|
|
5466
|
-
throw new Error("No exe session found");
|
|
5467
|
-
}
|
|
5468
|
-
const ensureResult = ensureEmployee(targetAgent, exeSession, process.cwd());
|
|
5469
|
-
if (ensureResult.status === "failed") {
|
|
5470
|
-
throw new Error(ensureResult.error ?? "ensureEmployee failed");
|
|
5471
|
-
}
|
|
5472
|
-
await client.execute({
|
|
5473
|
-
sql: "UPDATE messages SET status = 'delivered', delivered_at = ? WHERE id = ?",
|
|
5474
|
-
args: [now, messageId]
|
|
5698
|
+
const cnt = Number(remaining.rows[0]?.cnt ?? 1);
|
|
5699
|
+
if (cnt === 0) {
|
|
5700
|
+
const parentRow = await client.execute({
|
|
5701
|
+
sql: `SELECT assigned_to, title, task_file, project_name FROM tasks WHERE id = ?`,
|
|
5702
|
+
args: [parentTaskId]
|
|
5475
5703
|
});
|
|
5476
|
-
|
|
5477
|
-
|
|
5478
|
-
|
|
5479
|
-
|
|
5480
|
-
|
|
5481
|
-
|
|
5482
|
-
|
|
5483
|
-
|
|
5484
|
-
|
|
5704
|
+
if (parentRow.rows.length === 1) {
|
|
5705
|
+
const pr = parentRow.rows[0];
|
|
5706
|
+
const parentProject = pr.project_name == null ? projectName : String(pr.project_name);
|
|
5707
|
+
await writeNotification({
|
|
5708
|
+
agentId: String(pr.assigned_to),
|
|
5709
|
+
agentRole: "system",
|
|
5710
|
+
event: "subtasks_complete",
|
|
5711
|
+
project: parentProject,
|
|
5712
|
+
summary: `All subtasks complete for "${String(pr.title)}" \u2014 ready for rollup review`,
|
|
5713
|
+
taskFile: String(pr.task_file)
|
|
5485
5714
|
});
|
|
5486
5715
|
}
|
|
5487
|
-
return false;
|
|
5488
5716
|
}
|
|
5489
5717
|
}
|
|
5490
|
-
|
|
5491
|
-
|
|
5492
|
-
|
|
5493
|
-
|
|
5494
|
-
|
|
5495
|
-
|
|
5496
|
-
|
|
5497
|
-
|
|
5498
|
-
|
|
5499
|
-
}
|
|
5500
|
-
|
|
5501
|
-
|
|
5502
|
-
|
|
5503
|
-
|
|
5504
|
-
|
|
5505
|
-
|
|
5506
|
-
|
|
5507
|
-
|
|
5508
|
-
|
|
5509
|
-
|
|
5510
|
-
|
|
5511
|
-
|
|
5512
|
-
|
|
5718
|
+
var init_tasks_chain = __esm({
|
|
5719
|
+
"src/lib/tasks-chain.ts"() {
|
|
5720
|
+
"use strict";
|
|
5721
|
+
init_database();
|
|
5722
|
+
init_notifications();
|
|
5723
|
+
}
|
|
5724
|
+
});
|
|
5725
|
+
|
|
5726
|
+
// src/lib/project-name.ts
|
|
5727
|
+
import { execSync as execSync5 } from "child_process";
|
|
5728
|
+
import path15 from "path";
|
|
5729
|
+
function getProjectName(cwd) {
|
|
5730
|
+
const dir = cwd ?? process.cwd();
|
|
5731
|
+
if (_cached2 && _cachedCwd === dir) return _cached2;
|
|
5732
|
+
try {
|
|
5733
|
+
let repoRoot;
|
|
5734
|
+
try {
|
|
5735
|
+
const gitCommonDir = execSync5("git rev-parse --path-format=absolute --git-common-dir", {
|
|
5736
|
+
cwd: dir,
|
|
5737
|
+
encoding: "utf8",
|
|
5738
|
+
timeout: 2e3,
|
|
5739
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
5740
|
+
}).trim();
|
|
5741
|
+
repoRoot = path15.dirname(gitCommonDir);
|
|
5742
|
+
} catch {
|
|
5743
|
+
repoRoot = execSync5("git rev-parse --show-toplevel", {
|
|
5744
|
+
cwd: dir,
|
|
5745
|
+
encoding: "utf8",
|
|
5746
|
+
timeout: 2e3,
|
|
5747
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
5748
|
+
}).trim();
|
|
5749
|
+
}
|
|
5750
|
+
_cached2 = path15.basename(repoRoot);
|
|
5751
|
+
_cachedCwd = dir;
|
|
5752
|
+
return _cached2;
|
|
5753
|
+
} catch {
|
|
5754
|
+
_cached2 = path15.basename(dir);
|
|
5755
|
+
_cachedCwd = dir;
|
|
5756
|
+
return _cached2;
|
|
5757
|
+
}
|
|
5513
5758
|
}
|
|
5514
|
-
|
|
5515
|
-
|
|
5516
|
-
|
|
5517
|
-
|
|
5518
|
-
|
|
5519
|
-
|
|
5520
|
-
}
|
|
5521
|
-
|
|
5522
|
-
|
|
5523
|
-
|
|
5524
|
-
|
|
5525
|
-
|
|
5526
|
-
|
|
5527
|
-
|
|
5528
|
-
|
|
5529
|
-
|
|
5530
|
-
|
|
5531
|
-
const
|
|
5532
|
-
|
|
5533
|
-
|
|
5534
|
-
|
|
5535
|
-
|
|
5536
|
-
});
|
|
5537
|
-
return result.rows.map((row) => rowToMessage(row));
|
|
5538
|
-
}
|
|
5539
|
-
async function getReadMessages(targetAgent) {
|
|
5540
|
-
const client = getClient();
|
|
5541
|
-
const result = await client.execute({
|
|
5542
|
-
sql: "SELECT * FROM messages WHERE target_agent = ? AND status = 'read' ORDER BY id",
|
|
5543
|
-
args: [targetAgent]
|
|
5544
|
-
});
|
|
5545
|
-
return result.rows.map((row) => rowToMessage(row));
|
|
5546
|
-
}
|
|
5547
|
-
async function markFailed(messageId, reason) {
|
|
5548
|
-
const client = getClient();
|
|
5549
|
-
await client.execute({
|
|
5550
|
-
sql: "UPDATE messages SET status = 'failed', failed_at = ?, failure_reason = ? WHERE id = ?",
|
|
5551
|
-
args: [(/* @__PURE__ */ new Date()).toISOString(), reason, messageId]
|
|
5552
|
-
});
|
|
5759
|
+
var _cached2, _cachedCwd;
|
|
5760
|
+
var init_project_name = __esm({
|
|
5761
|
+
"src/lib/project-name.ts"() {
|
|
5762
|
+
"use strict";
|
|
5763
|
+
_cached2 = null;
|
|
5764
|
+
_cachedCwd = null;
|
|
5765
|
+
}
|
|
5766
|
+
});
|
|
5767
|
+
|
|
5768
|
+
// src/lib/session-scope.ts
|
|
5769
|
+
var session_scope_exports = {};
|
|
5770
|
+
__export(session_scope_exports, {
|
|
5771
|
+
assertSessionScope: () => assertSessionScope,
|
|
5772
|
+
findSessionForProject: () => findSessionForProject,
|
|
5773
|
+
getSessionProject: () => getSessionProject
|
|
5774
|
+
});
|
|
5775
|
+
function getSessionProject(sessionName) {
|
|
5776
|
+
const sessions = listSessions();
|
|
5777
|
+
const entry = sessions.find((s) => s.windowName === sessionName);
|
|
5778
|
+
if (!entry) return null;
|
|
5779
|
+
const parts = entry.projectDir.split("/").filter(Boolean);
|
|
5780
|
+
return parts[parts.length - 1] ?? null;
|
|
5553
5781
|
}
|
|
5554
|
-
|
|
5555
|
-
const
|
|
5556
|
-
const
|
|
5557
|
-
|
|
5558
|
-
|
|
5559
|
-
}
|
|
5560
|
-
return
|
|
5782
|
+
function findSessionForProject(projectName) {
|
|
5783
|
+
const sessions = listSessions();
|
|
5784
|
+
for (const s of sessions) {
|
|
5785
|
+
const proj = s.projectDir.split("/").filter(Boolean).pop();
|
|
5786
|
+
if (proj === projectName && s.agentId === "exe") return s;
|
|
5787
|
+
}
|
|
5788
|
+
return null;
|
|
5561
5789
|
}
|
|
5562
|
-
|
|
5563
|
-
|
|
5564
|
-
|
|
5565
|
-
|
|
5566
|
-
|
|
5567
|
-
|
|
5568
|
-
|
|
5569
|
-
|
|
5570
|
-
|
|
5571
|
-
|
|
5572
|
-
|
|
5573
|
-
|
|
5574
|
-
|
|
5575
|
-
|
|
5576
|
-
} catch {
|
|
5790
|
+
function assertSessionScope(actionType, targetProject) {
|
|
5791
|
+
try {
|
|
5792
|
+
const currentProject = getProjectName();
|
|
5793
|
+
const exeSession = resolveExeSession();
|
|
5794
|
+
if (!exeSession) {
|
|
5795
|
+
return { allowed: true, reason: "no_session" };
|
|
5796
|
+
}
|
|
5797
|
+
if (currentProject === targetProject) {
|
|
5798
|
+
return {
|
|
5799
|
+
allowed: true,
|
|
5800
|
+
reason: "same_session",
|
|
5801
|
+
currentProject,
|
|
5802
|
+
targetProject
|
|
5803
|
+
};
|
|
5577
5804
|
}
|
|
5805
|
+
process.stderr.write(
|
|
5806
|
+
`[session-scope] BLOCKED cross-project ${actionType}: session project="${currentProject}" \u2260 target project="${targetProject}"
|
|
5807
|
+
`
|
|
5808
|
+
);
|
|
5809
|
+
return {
|
|
5810
|
+
allowed: false,
|
|
5811
|
+
reason: "cross_session_denied",
|
|
5812
|
+
currentProject,
|
|
5813
|
+
targetProject,
|
|
5814
|
+
targetSession: findSessionForProject(targetProject)?.windowName
|
|
5815
|
+
};
|
|
5816
|
+
} catch {
|
|
5817
|
+
return { allowed: true, reason: "no_session" };
|
|
5578
5818
|
}
|
|
5579
|
-
return delivered;
|
|
5580
5819
|
}
|
|
5581
|
-
var
|
|
5582
|
-
|
|
5583
|
-
"src/lib/messaging.ts"() {
|
|
5820
|
+
var init_session_scope = __esm({
|
|
5821
|
+
"src/lib/session-scope.ts"() {
|
|
5584
5822
|
"use strict";
|
|
5585
|
-
|
|
5823
|
+
init_session_registry();
|
|
5824
|
+
init_project_name();
|
|
5586
5825
|
init_tmux_routing();
|
|
5587
|
-
MAX_RETRIES2 = 10;
|
|
5588
|
-
_wsClientSend = null;
|
|
5589
5826
|
}
|
|
5590
5827
|
});
|
|
5591
5828
|
|
|
5592
|
-
// src/lib/
|
|
5593
|
-
|
|
5594
|
-
|
|
5595
|
-
|
|
5596
|
-
|
|
5597
|
-
|
|
5598
|
-
|
|
5599
|
-
|
|
5600
|
-
|
|
5601
|
-
|
|
5602
|
-
|
|
5603
|
-
|
|
5829
|
+
// src/lib/tasks-notify.ts
|
|
5830
|
+
async function dispatchTaskToEmployee(input) {
|
|
5831
|
+
if (input.assignedTo === "exe") return { dispatched: "skipped" };
|
|
5832
|
+
let crossProject = false;
|
|
5833
|
+
if (input.projectName) {
|
|
5834
|
+
try {
|
|
5835
|
+
const { assertSessionScope: assertSessionScope2 } = (init_session_scope(), __toCommonJS(session_scope_exports));
|
|
5836
|
+
const check = assertSessionScope2("dispatch_task", input.projectName);
|
|
5837
|
+
if (check.reason === "cross_session_denied") {
|
|
5838
|
+
crossProject = true;
|
|
5839
|
+
return { dispatched: "skipped", crossProject: true };
|
|
5840
|
+
}
|
|
5841
|
+
} catch {
|
|
5842
|
+
}
|
|
5843
|
+
}
|
|
5604
5844
|
try {
|
|
5605
|
-
const
|
|
5606
|
-
const
|
|
5607
|
-
|
|
5608
|
-
|
|
5609
|
-
|
|
5610
|
-
|
|
5611
|
-
|
|
5612
|
-
|
|
5613
|
-
|
|
5614
|
-
|
|
5615
|
-
|
|
5616
|
-
|
|
5617
|
-
|
|
5618
|
-
|
|
5619
|
-
|
|
5620
|
-
|
|
5621
|
-
|
|
5622
|
-
|
|
5623
|
-
|
|
5624
|
-
|
|
5845
|
+
const transport = getTransport();
|
|
5846
|
+
const exeSession = resolveExeSession();
|
|
5847
|
+
if (!exeSession) return { dispatched: "session_missing" };
|
|
5848
|
+
const sessionName = employeeSessionName(input.assignedTo, exeSession);
|
|
5849
|
+
if (transport.isAlive(sessionName)) {
|
|
5850
|
+
const result = sendIntercom(sessionName);
|
|
5851
|
+
const dispatched = result === "acknowledged" || result === "debounced" || result === "queued" ? "verified" : result === "delivered" ? "sent_unverified" : "session_dead";
|
|
5852
|
+
return { dispatched, session: sessionName, crossProject };
|
|
5853
|
+
} else {
|
|
5854
|
+
const projectDir = input.projectDir ?? process.cwd();
|
|
5855
|
+
const result = ensureEmployee(input.assignedTo, exeSession, projectDir, {
|
|
5856
|
+
autoInstance: isMultiInstance(input.assignedTo)
|
|
5857
|
+
});
|
|
5858
|
+
if (result.status === "failed") {
|
|
5859
|
+
process.stderr.write(
|
|
5860
|
+
`[dispatch] Failed to spawn ${input.assignedTo}: ${result.error}
|
|
5861
|
+
`
|
|
5862
|
+
);
|
|
5863
|
+
return { dispatched: "session_missing" };
|
|
5864
|
+
}
|
|
5865
|
+
return { dispatched: "spawned", session: result.sessionName, crossProject };
|
|
5866
|
+
}
|
|
5867
|
+
} catch {
|
|
5868
|
+
return { dispatched: "session_missing" };
|
|
5625
5869
|
}
|
|
5626
5870
|
}
|
|
5627
|
-
|
|
5871
|
+
function notifyTaskDone() {
|
|
5628
5872
|
try {
|
|
5629
|
-
const
|
|
5630
|
-
|
|
5631
|
-
sql: "UPDATE notifications SET read = 1 WHERE task_file = ? AND read = 0",
|
|
5632
|
-
args: [taskFile]
|
|
5633
|
-
});
|
|
5873
|
+
const key = getSessionKey();
|
|
5874
|
+
if (key && !process.env.VITEST) notifyParentExe(key);
|
|
5634
5875
|
} catch {
|
|
5635
5876
|
}
|
|
5636
5877
|
}
|
|
5637
|
-
|
|
5638
|
-
|
|
5878
|
+
async function markTaskNotificationsRead(taskFile) {
|
|
5879
|
+
try {
|
|
5880
|
+
await markAsReadByTaskFile(taskFile);
|
|
5881
|
+
} catch {
|
|
5882
|
+
}
|
|
5883
|
+
}
|
|
5884
|
+
var init_tasks_notify = __esm({
|
|
5885
|
+
"src/lib/tasks-notify.ts"() {
|
|
5639
5886
|
"use strict";
|
|
5640
|
-
|
|
5887
|
+
init_tmux_routing();
|
|
5888
|
+
init_session_key();
|
|
5889
|
+
init_notifications();
|
|
5890
|
+
init_transport();
|
|
5891
|
+
init_employees();
|
|
5641
5892
|
}
|
|
5642
5893
|
});
|
|
5643
5894
|
|
|
5644
|
-
// src/lib/
|
|
5645
|
-
import
|
|
5646
|
-
|
|
5647
|
-
import { execSync as execSync5 } from "child_process";
|
|
5648
|
-
import { mkdir as mkdir4, writeFile as writeFile4, appendFile } from "fs/promises";
|
|
5649
|
-
import { existsSync as existsSync12, readFileSync as readFileSync11 } from "fs";
|
|
5650
|
-
async function writeCheckpoint(input) {
|
|
5895
|
+
// src/lib/behaviors.ts
|
|
5896
|
+
import crypto6 from "crypto";
|
|
5897
|
+
async function storeBehavior(opts) {
|
|
5651
5898
|
const client = getClient();
|
|
5652
|
-
const
|
|
5653
|
-
const taskId = String(row.id);
|
|
5899
|
+
const id = crypto6.randomUUID();
|
|
5654
5900
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5655
|
-
|
|
5656
|
-
|
|
5657
|
-
|
|
5658
|
-
|
|
5659
|
-
const checkpoint = {
|
|
5660
|
-
step: input.step,
|
|
5661
|
-
context_summary: input.contextSummary,
|
|
5662
|
-
files_touched: input.filesTouched ?? [],
|
|
5663
|
-
blocked_by_ids: blockedByIds,
|
|
5664
|
-
last_checkpoint_at: now
|
|
5665
|
-
};
|
|
5666
|
-
const result = await client.execute({
|
|
5667
|
-
sql: `UPDATE tasks SET checkpoint = ?, checkpoint_count = checkpoint_count + 1, updated_at = ? WHERE id = ?`,
|
|
5668
|
-
args: [JSON.stringify(checkpoint), now, taskId]
|
|
5669
|
-
});
|
|
5670
|
-
if (result.rowsAffected === 0) {
|
|
5671
|
-
throw new Error(`Checkpoint write failed: task ${taskId} not found`);
|
|
5672
|
-
}
|
|
5673
|
-
const countResult = await client.execute({
|
|
5674
|
-
sql: "SELECT checkpoint_count FROM tasks WHERE id = ?",
|
|
5675
|
-
args: [taskId]
|
|
5901
|
+
await client.execute({
|
|
5902
|
+
sql: `INSERT INTO behaviors (id, agent_id, project_name, domain, priority, content, active, created_at, updated_at)
|
|
5903
|
+
VALUES (?, ?, ?, ?, ?, ?, 1, ?, ?)`,
|
|
5904
|
+
args: [id, opts.agentId, opts.projectName ?? null, opts.domain ?? null, opts.priority ?? "p1", opts.content, now, now]
|
|
5676
5905
|
});
|
|
5677
|
-
|
|
5678
|
-
return { checkpointCount };
|
|
5906
|
+
return id;
|
|
5679
5907
|
}
|
|
5680
|
-
|
|
5681
|
-
|
|
5682
|
-
|
|
5683
|
-
|
|
5684
|
-
);
|
|
5685
|
-
return match ? match[1].toLowerCase() : null;
|
|
5686
|
-
}
|
|
5687
|
-
function slugify(title) {
|
|
5688
|
-
return title.toLowerCase().replace(/[^a-z0-9-]/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "");
|
|
5689
|
-
}
|
|
5690
|
-
async function resolveTask(client, identifier) {
|
|
5691
|
-
let result = await client.execute({
|
|
5692
|
-
sql: "SELECT * FROM tasks WHERE id = ?",
|
|
5693
|
-
args: [identifier]
|
|
5694
|
-
});
|
|
5695
|
-
if (result.rows.length === 1) return result.rows[0];
|
|
5696
|
-
result = await client.execute({
|
|
5697
|
-
sql: "SELECT * FROM tasks WHERE task_file LIKE ?",
|
|
5698
|
-
args: [`%${identifier}%`]
|
|
5699
|
-
});
|
|
5700
|
-
if (result.rows.length === 1) return result.rows[0];
|
|
5701
|
-
if (result.rows.length > 1) {
|
|
5702
|
-
const exact = result.rows.filter(
|
|
5703
|
-
(r) => String(r.task_file).endsWith(`/${identifier}.md`)
|
|
5704
|
-
);
|
|
5705
|
-
if (exact.length === 1) return exact[0];
|
|
5706
|
-
const candidates = exact.length > 1 ? exact : result.rows;
|
|
5707
|
-
const active = candidates.filter(
|
|
5708
|
-
(r) => !["done", "cancelled"].includes(String(r.status))
|
|
5709
|
-
);
|
|
5710
|
-
if (active.length === 1) return active[0];
|
|
5711
|
-
const matches = (active.length > 1 ? active : candidates).map((r) => `${String(r.task_file)} (${String(r.status)}, ${String(r.id)})`).join(", ");
|
|
5712
|
-
throw new Error(
|
|
5713
|
-
`Multiple tasks match "${identifier}": ${matches}. Use a UUID to disambiguate.`
|
|
5714
|
-
);
|
|
5715
|
-
}
|
|
5716
|
-
result = await client.execute({
|
|
5717
|
-
sql: "SELECT * FROM tasks WHERE title LIKE ?",
|
|
5718
|
-
args: [`%${identifier}%`]
|
|
5719
|
-
});
|
|
5720
|
-
if (result.rows.length === 1) return result.rows[0];
|
|
5721
|
-
if (result.rows.length > 1) {
|
|
5722
|
-
const active = result.rows.filter(
|
|
5723
|
-
(r) => !["done", "cancelled"].includes(String(r.status))
|
|
5724
|
-
);
|
|
5725
|
-
if (active.length === 1) return active[0];
|
|
5726
|
-
const matches = (active.length > 1 ? active : result.rows).map((r) => `"${String(r.title)}" (${String(r.status)}, ${String(r.id)})`).join(", ");
|
|
5727
|
-
throw new Error(
|
|
5728
|
-
`Multiple tasks match "${identifier}": ${matches}. Use a UUID to disambiguate.`
|
|
5729
|
-
);
|
|
5908
|
+
var init_behaviors = __esm({
|
|
5909
|
+
"src/lib/behaviors.ts"() {
|
|
5910
|
+
"use strict";
|
|
5911
|
+
init_database();
|
|
5730
5912
|
}
|
|
5731
|
-
|
|
5732
|
-
|
|
5733
|
-
|
|
5913
|
+
});
|
|
5914
|
+
|
|
5915
|
+
// src/lib/skill-learning.ts
|
|
5916
|
+
var skill_learning_exports = {};
|
|
5917
|
+
__export(skill_learning_exports, {
|
|
5918
|
+
captureAndLearn: () => captureAndLearn,
|
|
5919
|
+
captureTrajectory: () => captureTrajectory,
|
|
5920
|
+
editDistance: () => editDistance,
|
|
5921
|
+
extractSkill: () => extractSkill,
|
|
5922
|
+
extractTrajectory: () => extractTrajectory,
|
|
5923
|
+
findSimilarTrajectories: () => findSimilarTrajectories,
|
|
5924
|
+
hashSignature: () => hashSignature,
|
|
5925
|
+
storeTrajectory: () => storeTrajectory,
|
|
5926
|
+
sweepTrajectories: () => sweepTrajectories
|
|
5927
|
+
});
|
|
5928
|
+
import crypto7 from "crypto";
|
|
5929
|
+
async function extractTrajectory(taskId, agentId) {
|
|
5734
5930
|
const client = getClient();
|
|
5735
|
-
const
|
|
5736
|
-
|
|
5737
|
-
|
|
5738
|
-
|
|
5739
|
-
|
|
5740
|
-
|
|
5741
|
-
|
|
5742
|
-
|
|
5743
|
-
|
|
5744
|
-
|
|
5745
|
-
|
|
5746
|
-
|
|
5747
|
-
|
|
5748
|
-
|
|
5749
|
-
if (extracted) {
|
|
5750
|
-
parentRef = extracted;
|
|
5751
|
-
process.stderr.write(
|
|
5752
|
-
"[create_task] auto-populated parent_task_id from context body \u2014 dispatchers should pass parent_task_id explicitly\n"
|
|
5753
|
-
);
|
|
5754
|
-
}
|
|
5755
|
-
}
|
|
5756
|
-
if (parentRef) {
|
|
5757
|
-
try {
|
|
5758
|
-
const parent = await resolveTask(client, parentRef);
|
|
5759
|
-
parentTaskId = String(parent.id);
|
|
5760
|
-
} catch (err) {
|
|
5761
|
-
if (!input.parentTaskId) {
|
|
5762
|
-
throw new Error(
|
|
5763
|
-
`create_task: parent reference "${parentRef}" in context body does not resolve to an existing task`
|
|
5764
|
-
);
|
|
5765
|
-
}
|
|
5766
|
-
throw err;
|
|
5931
|
+
const result = await client.execute({
|
|
5932
|
+
sql: `SELECT tool_name, raw_text
|
|
5933
|
+
FROM memories
|
|
5934
|
+
WHERE task_id = ? AND agent_id = ?
|
|
5935
|
+
ORDER BY timestamp ASC`,
|
|
5936
|
+
args: [taskId, agentId]
|
|
5937
|
+
});
|
|
5938
|
+
if (result.rows.length === 0) return [];
|
|
5939
|
+
const rawTools = result.rows.map((r) => {
|
|
5940
|
+
const toolName = String(r.tool_name);
|
|
5941
|
+
if (toolName === "Bash") {
|
|
5942
|
+
const text = String(r.raw_text);
|
|
5943
|
+
const cmdMatch = text.match(/(?:command|Command).*?[:\s]+"?(\w+)/);
|
|
5944
|
+
return cmdMatch ? `Bash:${cmdMatch[1]}` : "Bash";
|
|
5767
5945
|
}
|
|
5768
|
-
|
|
5769
|
-
let warning;
|
|
5770
|
-
const dupCheck = await client.execute({
|
|
5771
|
-
sql: "SELECT id FROM tasks WHERE title = ? AND assigned_to = ? AND status IN ('open', 'in_progress', 'blocked')",
|
|
5772
|
-
args: [input.title, input.assignedTo]
|
|
5946
|
+
return toolName;
|
|
5773
5947
|
});
|
|
5774
|
-
|
|
5775
|
-
|
|
5776
|
-
|
|
5777
|
-
|
|
5778
|
-
try {
|
|
5779
|
-
await mkdir4(path13.join(input.baseDir, "exe", "output"), { recursive: true });
|
|
5780
|
-
await mkdir4(path13.join(input.baseDir, "exe", "research"), { recursive: true });
|
|
5781
|
-
await ensureArchitectureDoc(input.baseDir, input.projectName);
|
|
5782
|
-
await ensureGitignoreExe(input.baseDir);
|
|
5783
|
-
} catch {
|
|
5948
|
+
const signature = [];
|
|
5949
|
+
for (const tool of rawTools) {
|
|
5950
|
+
if (signature.length === 0 || signature[signature.length - 1] !== tool) {
|
|
5951
|
+
signature.push(tool);
|
|
5784
5952
|
}
|
|
5785
5953
|
}
|
|
5786
|
-
|
|
5954
|
+
return signature;
|
|
5955
|
+
}
|
|
5956
|
+
function hashSignature(signature) {
|
|
5957
|
+
return crypto7.createHash("sha256").update(signature.join("|")).digest("hex").slice(0, 16);
|
|
5958
|
+
}
|
|
5959
|
+
async function storeTrajectory(opts) {
|
|
5960
|
+
const client = getClient();
|
|
5961
|
+
const id = crypto7.randomUUID();
|
|
5962
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5963
|
+
const signatureHash = hashSignature(opts.signature);
|
|
5787
5964
|
await client.execute({
|
|
5788
|
-
sql: `INSERT INTO
|
|
5789
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?,
|
|
5965
|
+
sql: `INSERT INTO trajectories (id, task_id, agent_id, project_name, task_title, signature, signature_hash, tool_count, created_at)
|
|
5966
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
5790
5967
|
args: [
|
|
5791
5968
|
id,
|
|
5792
|
-
|
|
5793
|
-
|
|
5794
|
-
|
|
5795
|
-
|
|
5796
|
-
|
|
5797
|
-
|
|
5798
|
-
|
|
5799
|
-
blockedById,
|
|
5800
|
-
parentTaskId,
|
|
5801
|
-
input.reviewer ?? null,
|
|
5802
|
-
input.context,
|
|
5803
|
-
complexity,
|
|
5804
|
-
input.budgetTokens ?? null,
|
|
5805
|
-
input.budgetFallbackModel ?? null,
|
|
5806
|
-
0,
|
|
5807
|
-
null,
|
|
5808
|
-
now,
|
|
5969
|
+
opts.taskId,
|
|
5970
|
+
opts.agentId,
|
|
5971
|
+
opts.projectName,
|
|
5972
|
+
opts.taskTitle,
|
|
5973
|
+
JSON.stringify(opts.signature),
|
|
5974
|
+
signatureHash,
|
|
5975
|
+
opts.signature.length,
|
|
5809
5976
|
now
|
|
5810
5977
|
]
|
|
5811
5978
|
});
|
|
5812
|
-
return
|
|
5813
|
-
id,
|
|
5814
|
-
title: input.title,
|
|
5815
|
-
assignedTo: input.assignedTo,
|
|
5816
|
-
assignedBy: input.assignedBy,
|
|
5817
|
-
projectName: input.projectName,
|
|
5818
|
-
priority: input.priority,
|
|
5819
|
-
status: initialStatus,
|
|
5820
|
-
taskFile,
|
|
5821
|
-
createdAt: now,
|
|
5822
|
-
updatedAt: now,
|
|
5823
|
-
warning,
|
|
5824
|
-
budgetTokens: input.budgetTokens ?? null,
|
|
5825
|
-
budgetFallbackModel: input.budgetFallbackModel ?? null,
|
|
5826
|
-
tokensUsed: 0,
|
|
5827
|
-
tokensWarnedAt: null
|
|
5828
|
-
};
|
|
5979
|
+
return id;
|
|
5829
5980
|
}
|
|
5830
|
-
async function
|
|
5981
|
+
async function findSimilarTrajectories(signature, threshold = DEFAULT_SKILL_THRESHOLD) {
|
|
5831
5982
|
const client = getClient();
|
|
5832
|
-
const
|
|
5833
|
-
const args = [];
|
|
5834
|
-
if (input.assignedTo) {
|
|
5835
|
-
conditions.push("assigned_to = ?");
|
|
5836
|
-
args.push(input.assignedTo);
|
|
5837
|
-
}
|
|
5838
|
-
if (input.status) {
|
|
5839
|
-
conditions.push("status = ?");
|
|
5840
|
-
args.push(input.status);
|
|
5841
|
-
} else {
|
|
5842
|
-
conditions.push("status IN ('open', 'in_progress', 'blocked')");
|
|
5843
|
-
}
|
|
5844
|
-
if (input.projectName) {
|
|
5845
|
-
conditions.push("project_name = ?");
|
|
5846
|
-
args.push(input.projectName);
|
|
5847
|
-
}
|
|
5848
|
-
if (input.priority) {
|
|
5849
|
-
conditions.push("priority = ?");
|
|
5850
|
-
args.push(input.priority);
|
|
5851
|
-
}
|
|
5852
|
-
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
5983
|
+
const hash = hashSignature(signature);
|
|
5853
5984
|
const result = await client.execute({
|
|
5854
|
-
sql: `SELECT
|
|
5855
|
-
|
|
5985
|
+
sql: `SELECT id, task_id, agent_id, project_name, task_title, signature, signature_hash, tool_count, skill_id, created_at
|
|
5986
|
+
FROM trajectories
|
|
5987
|
+
WHERE signature_hash = ?
|
|
5988
|
+
ORDER BY created_at DESC
|
|
5989
|
+
LIMIT 20`,
|
|
5990
|
+
args: [hash]
|
|
5856
5991
|
});
|
|
5857
|
-
|
|
5992
|
+
const mapRow = (r) => ({
|
|
5858
5993
|
id: String(r.id),
|
|
5859
|
-
|
|
5860
|
-
|
|
5861
|
-
assignedBy: String(r.assigned_by),
|
|
5994
|
+
taskId: String(r.task_id),
|
|
5995
|
+
agentId: String(r.agent_id),
|
|
5862
5996
|
projectName: String(r.project_name),
|
|
5863
|
-
|
|
5864
|
-
|
|
5865
|
-
|
|
5866
|
-
|
|
5867
|
-
|
|
5868
|
-
|
|
5869
|
-
|
|
5870
|
-
|
|
5871
|
-
|
|
5872
|
-
|
|
5873
|
-
|
|
5874
|
-
|
|
5875
|
-
|
|
5876
|
-
|
|
5877
|
-
|
|
5878
|
-
|
|
5879
|
-
|
|
5880
|
-
|
|
5881
|
-
|
|
5882
|
-
|
|
5883
|
-
|
|
5884
|
-
|
|
5885
|
-
|
|
5886
|
-
|
|
5887
|
-
|
|
5888
|
-
|
|
5889
|
-
|
|
5890
|
-
if (count === 0) {
|
|
5891
|
-
return "WARNING: task closed with no new commits since creation. Verify work was actually produced.";
|
|
5892
|
-
}
|
|
5893
|
-
return null;
|
|
5894
|
-
} catch {
|
|
5895
|
-
return null;
|
|
5997
|
+
taskTitle: String(r.task_title),
|
|
5998
|
+
signature: JSON.parse(String(r.signature)),
|
|
5999
|
+
signatureHash: String(r.signature_hash),
|
|
6000
|
+
toolCount: Number(r.tool_count),
|
|
6001
|
+
skillId: r.skill_id ? String(r.skill_id) : null,
|
|
6002
|
+
createdAt: String(r.created_at)
|
|
6003
|
+
});
|
|
6004
|
+
const matches = result.rows.map(mapRow);
|
|
6005
|
+
if (matches.length >= threshold) return matches;
|
|
6006
|
+
const nearResult = await client.execute({
|
|
6007
|
+
sql: `SELECT id, task_id, agent_id, project_name, task_title, signature, signature_hash, tool_count, skill_id, created_at
|
|
6008
|
+
FROM trajectories
|
|
6009
|
+
WHERE tool_count BETWEEN ? AND ?
|
|
6010
|
+
AND signature_hash != ?
|
|
6011
|
+
ORDER BY created_at DESC
|
|
6012
|
+
LIMIT 50`,
|
|
6013
|
+
args: [
|
|
6014
|
+
Math.max(1, signature.length - 3),
|
|
6015
|
+
signature.length + 3,
|
|
6016
|
+
hash
|
|
6017
|
+
]
|
|
6018
|
+
});
|
|
6019
|
+
for (const r of nearResult.rows) {
|
|
6020
|
+
const candidateSig = JSON.parse(String(r.signature));
|
|
6021
|
+
if (editDistance(signature, candidateSig) <= 2) {
|
|
6022
|
+
matches.push(mapRow(r));
|
|
6023
|
+
}
|
|
5896
6024
|
}
|
|
6025
|
+
return matches;
|
|
5897
6026
|
}
|
|
5898
|
-
async function
|
|
5899
|
-
const
|
|
5900
|
-
|
|
5901
|
-
|
|
5902
|
-
const taskId = String(row.id);
|
|
5903
|
-
const taskFile = String(row.task_file);
|
|
5904
|
-
if (input.status === "done" && String(row.assigned_by) === "system" && taskFile.includes("review-")) {
|
|
5905
|
-
process.stderr.write(
|
|
5906
|
-
`[updateTask] Review task "${String(row.title)}" being marked done (assigned to ${String(row.assigned_to)})
|
|
5907
|
-
`
|
|
5908
|
-
);
|
|
6027
|
+
async function captureTrajectory(opts) {
|
|
6028
|
+
const signature = await extractTrajectory(opts.taskId, opts.agentId);
|
|
6029
|
+
if (signature.length < 3) {
|
|
6030
|
+
return { trajectoryId: "", similarCount: 0, similar: [] };
|
|
5909
6031
|
}
|
|
5910
|
-
|
|
5911
|
-
|
|
5912
|
-
|
|
5913
|
-
|
|
5914
|
-
|
|
5915
|
-
|
|
5916
|
-
|
|
5917
|
-
|
|
5918
|
-
|
|
5919
|
-
|
|
6032
|
+
const trajectoryId = await storeTrajectory({
|
|
6033
|
+
taskId: opts.taskId,
|
|
6034
|
+
agentId: opts.agentId,
|
|
6035
|
+
projectName: opts.projectName,
|
|
6036
|
+
taskTitle: opts.taskTitle,
|
|
6037
|
+
signature
|
|
6038
|
+
});
|
|
6039
|
+
const similar = await findSimilarTrajectories(
|
|
6040
|
+
signature,
|
|
6041
|
+
opts.skillThreshold ?? DEFAULT_SKILL_THRESHOLD
|
|
6042
|
+
);
|
|
6043
|
+
return { trajectoryId, similarCount: similar.length, similar };
|
|
6044
|
+
}
|
|
6045
|
+
function buildExtractionPrompt(trajectories) {
|
|
6046
|
+
const items = trajectories.map((t, i) => {
|
|
6047
|
+
const sig = t.signature.join(" \u2192 ");
|
|
6048
|
+
return `Task ${i + 1}: "${t.taskTitle}" (${t.agentId}, ${t.projectName}) \u2014 ${t.toolCount} tool calls
|
|
6049
|
+
Signature: ${sig}`;
|
|
6050
|
+
}).join("\n\n");
|
|
6051
|
+
return `You are analyzing ${trajectories.length} completed tasks that followed similar procedures:
|
|
5920
6052
|
|
|
5921
|
-
${
|
|
5922
|
-
|
|
5923
|
-
|
|
5924
|
-
|
|
5925
|
-
|
|
5926
|
-
|
|
5927
|
-
|
|
5928
|
-
|
|
5929
|
-
|
|
5930
|
-
|
|
5931
|
-
|
|
5932
|
-
|
|
5933
|
-
|
|
5934
|
-
|
|
5935
|
-
|
|
5936
|
-
|
|
5937
|
-
|
|
5938
|
-
|
|
5939
|
-
|
|
5940
|
-
|
|
5941
|
-
|
|
5942
|
-
|
|
5943
|
-
|
|
5944
|
-
}
|
|
5945
|
-
|
|
5946
|
-
|
|
5947
|
-
|
|
5948
|
-
|
|
5949
|
-
|
|
5950
|
-
|
|
5951
|
-
|
|
5952
|
-
|
|
5953
|
-
|
|
5954
|
-
|
|
5955
|
-
|
|
5956
|
-
|
|
5957
|
-
|
|
5958
|
-
|
|
5959
|
-
|
|
5960
|
-
|
|
5961
|
-
|
|
5962
|
-
sql: "UPDATE tasks SET status = ?, updated_at = ? WHERE id = ?",
|
|
5963
|
-
args: [input.status, now, taskId]
|
|
6053
|
+
${items}
|
|
6054
|
+
|
|
6055
|
+
Extract the reusable procedure. Format your response EXACTLY like this:
|
|
6056
|
+
|
|
6057
|
+
SKILL: {name \u2014 short, descriptive}
|
|
6058
|
+
TRIGGER: {when to use this \u2014 one sentence}
|
|
6059
|
+
STEPS:
|
|
6060
|
+
1. ...
|
|
6061
|
+
2. ...
|
|
6062
|
+
PITFALLS: {common mistakes to avoid}
|
|
6063
|
+
|
|
6064
|
+
Be specific and actionable. Include tool names, file patterns, and concrete commands where applicable.`;
|
|
6065
|
+
}
|
|
6066
|
+
async function extractSkill(trajectories, model) {
|
|
6067
|
+
if (trajectories.length === 0) return null;
|
|
6068
|
+
const config2 = await loadConfig();
|
|
6069
|
+
const skillModel = model ?? config2.skillModel;
|
|
6070
|
+
const Anthropic2 = (await import("@anthropic-ai/sdk")).default;
|
|
6071
|
+
const client = new Anthropic2();
|
|
6072
|
+
const prompt = buildExtractionPrompt(trajectories);
|
|
6073
|
+
const response = await client.messages.create({
|
|
6074
|
+
model: skillModel,
|
|
6075
|
+
max_tokens: 500,
|
|
6076
|
+
messages: [{ role: "user", content: prompt }]
|
|
6077
|
+
});
|
|
6078
|
+
const textBlock = response.content.find((b) => b.type === "text");
|
|
6079
|
+
const skillText = textBlock?.text;
|
|
6080
|
+
if (!skillText) return null;
|
|
6081
|
+
const agentId = trajectories[0].agentId;
|
|
6082
|
+
const projectName = trajectories[0].projectName;
|
|
6083
|
+
const skillId = await storeBehavior({
|
|
6084
|
+
agentId,
|
|
6085
|
+
content: skillText,
|
|
6086
|
+
domain: "skill",
|
|
6087
|
+
projectName
|
|
6088
|
+
});
|
|
6089
|
+
const dbClient = getClient();
|
|
6090
|
+
for (const t of trajectories) {
|
|
6091
|
+
await dbClient.execute({
|
|
6092
|
+
sql: "UPDATE trajectories SET skill_id = ? WHERE id = ?",
|
|
6093
|
+
args: [skillId, t.id]
|
|
5964
6094
|
});
|
|
5965
6095
|
}
|
|
6096
|
+
process.stderr.write(
|
|
6097
|
+
`[skill-learning] Skill extracted from ${trajectories.length} trajectories \u2192 behavior ${skillId}
|
|
6098
|
+
`
|
|
6099
|
+
);
|
|
6100
|
+
return skillId;
|
|
6101
|
+
}
|
|
6102
|
+
async function captureAndLearn(opts) {
|
|
5966
6103
|
try {
|
|
5967
|
-
await
|
|
5968
|
-
|
|
5969
|
-
|
|
5970
|
-
|
|
6104
|
+
const config2 = await loadConfig();
|
|
6105
|
+
if (!config2.skillLearning) return;
|
|
6106
|
+
const { trajectoryId, similarCount, similar } = await captureTrajectory({
|
|
6107
|
+
...opts,
|
|
6108
|
+
skillThreshold: config2.skillThreshold
|
|
5971
6109
|
});
|
|
5972
|
-
|
|
6110
|
+
if (!trajectoryId) return;
|
|
6111
|
+
if (similarCount >= config2.skillThreshold) {
|
|
6112
|
+
const unprocessed = similar.filter((t) => !t.skillId);
|
|
6113
|
+
if (unprocessed.length >= config2.skillThreshold) {
|
|
6114
|
+
extractSkill(unprocessed, config2.skillModel).catch((err) => {
|
|
6115
|
+
process.stderr.write(
|
|
6116
|
+
`[skill-learning] Extraction failed: ${err instanceof Error ? err.message : String(err)}
|
|
6117
|
+
`
|
|
6118
|
+
);
|
|
6119
|
+
});
|
|
6120
|
+
}
|
|
6121
|
+
}
|
|
6122
|
+
} catch (err) {
|
|
6123
|
+
process.stderr.write(
|
|
6124
|
+
`[skill-learning] captureAndLearn failed: ${err instanceof Error ? err.message : String(err)}
|
|
6125
|
+
`
|
|
6126
|
+
);
|
|
5973
6127
|
}
|
|
5974
|
-
return { row, taskFile, now, taskId };
|
|
5975
6128
|
}
|
|
5976
|
-
async function
|
|
6129
|
+
async function sweepTrajectories(threshold, model) {
|
|
6130
|
+
const config2 = await loadConfig();
|
|
6131
|
+
if (!config2.skillLearning) return { clustersProcessed: 0, skillsExtracted: 0 };
|
|
6132
|
+
const t = threshold ?? config2.skillThreshold;
|
|
5977
6133
|
const client = getClient();
|
|
5978
|
-
const
|
|
5979
|
-
|
|
5980
|
-
|
|
5981
|
-
|
|
5982
|
-
|
|
5983
|
-
|
|
5984
|
-
|
|
5985
|
-
|
|
5986
|
-
|
|
5987
|
-
|
|
5988
|
-
|
|
5989
|
-
|
|
5990
|
-
|
|
5991
|
-
const
|
|
5992
|
-
|
|
5993
|
-
|
|
5994
|
-
|
|
5995
|
-
|
|
5996
|
-
|
|
5997
|
-
|
|
5998
|
-
|
|
5999
|
-
|
|
6000
|
-
|
|
6001
|
-
|
|
6002
|
-
|
|
6003
|
-
|
|
6004
|
-
|
|
6005
|
-
|
|
6006
|
-
|
|
6007
|
-
|
|
6008
|
-
|
|
6009
|
-
|
|
6010
|
-
|
|
6011
|
-
|
|
6012
|
-
|
|
6013
|
-
|
|
6014
|
-
|
|
6015
|
-
|
|
6016
|
-
|
|
6017
|
-
].join("\n");
|
|
6018
|
-
await writeFile4(archPath, template, "utf-8");
|
|
6019
|
-
} catch {
|
|
6134
|
+
const result = await client.execute({
|
|
6135
|
+
sql: `SELECT signature_hash, COUNT(*) as cnt
|
|
6136
|
+
FROM trajectories
|
|
6137
|
+
WHERE skill_id IS NULL
|
|
6138
|
+
GROUP BY signature_hash
|
|
6139
|
+
HAVING cnt >= ?
|
|
6140
|
+
ORDER BY cnt DESC
|
|
6141
|
+
LIMIT 10`,
|
|
6142
|
+
args: [t]
|
|
6143
|
+
});
|
|
6144
|
+
let clustersProcessed = 0;
|
|
6145
|
+
let skillsExtracted = 0;
|
|
6146
|
+
for (const row of result.rows) {
|
|
6147
|
+
const hash = String(row.signature_hash);
|
|
6148
|
+
const trajResult = await client.execute({
|
|
6149
|
+
sql: `SELECT id, task_id, agent_id, project_name, task_title, signature, signature_hash, tool_count, created_at
|
|
6150
|
+
FROM trajectories
|
|
6151
|
+
WHERE signature_hash = ? AND skill_id IS NULL
|
|
6152
|
+
ORDER BY created_at DESC
|
|
6153
|
+
LIMIT 10`,
|
|
6154
|
+
args: [hash]
|
|
6155
|
+
});
|
|
6156
|
+
const trajectories = trajResult.rows.map((r) => ({
|
|
6157
|
+
id: String(r.id),
|
|
6158
|
+
taskId: String(r.task_id),
|
|
6159
|
+
agentId: String(r.agent_id),
|
|
6160
|
+
projectName: String(r.project_name),
|
|
6161
|
+
taskTitle: String(r.task_title),
|
|
6162
|
+
signature: JSON.parse(String(r.signature)),
|
|
6163
|
+
signatureHash: String(r.signature_hash),
|
|
6164
|
+
toolCount: Number(r.tool_count),
|
|
6165
|
+
skillId: null,
|
|
6166
|
+
createdAt: String(r.created_at)
|
|
6167
|
+
}));
|
|
6168
|
+
if (trajectories.length >= t) {
|
|
6169
|
+
clustersProcessed++;
|
|
6170
|
+
const skillId = await extractSkill(trajectories, model ?? config2.skillModel);
|
|
6171
|
+
if (skillId) skillsExtracted++;
|
|
6172
|
+
}
|
|
6020
6173
|
}
|
|
6174
|
+
return { clustersProcessed, skillsExtracted };
|
|
6021
6175
|
}
|
|
6022
|
-
|
|
6023
|
-
const
|
|
6024
|
-
|
|
6025
|
-
|
|
6026
|
-
|
|
6027
|
-
|
|
6028
|
-
|
|
6029
|
-
|
|
6030
|
-
|
|
6176
|
+
function editDistance(a, b) {
|
|
6177
|
+
const m = a.length;
|
|
6178
|
+
const n = b.length;
|
|
6179
|
+
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
|
|
6180
|
+
for (let i = 0; i <= m; i++) dp[i][0] = i;
|
|
6181
|
+
for (let j = 0; j <= n; j++) dp[0][j] = j;
|
|
6182
|
+
for (let i = 1; i <= m; i++) {
|
|
6183
|
+
for (let j = 1; j <= n; j++) {
|
|
6184
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
6185
|
+
dp[i][j] = Math.min(
|
|
6186
|
+
dp[i - 1][j] + 1,
|
|
6187
|
+
dp[i][j - 1] + 1,
|
|
6188
|
+
dp[i - 1][j - 1] + cost
|
|
6189
|
+
);
|
|
6031
6190
|
}
|
|
6032
|
-
} catch {
|
|
6033
6191
|
}
|
|
6192
|
+
return dp[m][n];
|
|
6034
6193
|
}
|
|
6035
|
-
var
|
|
6036
|
-
var
|
|
6037
|
-
"src/lib/
|
|
6194
|
+
var DEFAULT_SKILL_THRESHOLD;
|
|
6195
|
+
var init_skill_learning = __esm({
|
|
6196
|
+
"src/lib/skill-learning.ts"() {
|
|
6038
6197
|
"use strict";
|
|
6039
6198
|
init_database();
|
|
6040
|
-
|
|
6041
|
-
|
|
6199
|
+
init_behaviors();
|
|
6200
|
+
init_config();
|
|
6201
|
+
DEFAULT_SKILL_THRESHOLD = 3;
|
|
6042
6202
|
}
|
|
6043
6203
|
});
|
|
6044
6204
|
|
|
6045
|
-
// src/lib/tasks
|
|
6046
|
-
|
|
6047
|
-
|
|
6048
|
-
|
|
6049
|
-
|
|
6050
|
-
|
|
6051
|
-
|
|
6052
|
-
|
|
6053
|
-
|
|
6054
|
-
|
|
6055
|
-
|
|
6056
|
-
|
|
6057
|
-
|
|
6058
|
-
|
|
6059
|
-
|
|
6060
|
-
|
|
6061
|
-
|
|
6062
|
-
|
|
6063
|
-
|
|
6064
|
-
|
|
6065
|
-
|
|
6066
|
-
|
|
6067
|
-
|
|
6068
|
-
|
|
6069
|
-
|
|
6070
|
-
|
|
6071
|
-
|
|
6072
|
-
|
|
6073
|
-
|
|
6074
|
-
|
|
6075
|
-
|
|
6076
|
-
|
|
6077
|
-
|
|
6078
|
-
|
|
6079
|
-
|
|
6080
|
-
|
|
6081
|
-
AND assigned_by = 'system'
|
|
6082
|
-
AND title LIKE 'Review:%'
|
|
6083
|
-
AND parent_task_id IN (SELECT id FROM tasks WHERE status IN ('done', 'cancelled'))`,
|
|
6084
|
-
args: [now]
|
|
6085
|
-
});
|
|
6086
|
-
const staleThreshold = new Date(Date.now() - 60 * 60 * 1e3).toISOString();
|
|
6087
|
-
const r2 = await client.execute({
|
|
6088
|
-
sql: `UPDATE tasks SET status = 'done', updated_at = ?
|
|
6089
|
-
WHERE status = 'needs_review'
|
|
6090
|
-
AND result IS NOT NULL
|
|
6091
|
-
AND updated_at < ?`,
|
|
6092
|
-
args: [now, staleThreshold]
|
|
6093
|
-
});
|
|
6094
|
-
const total = r1.rowsAffected + r2.rowsAffected;
|
|
6095
|
-
if (total > 0) {
|
|
6096
|
-
process.stderr.write(
|
|
6097
|
-
`[cleanup] Closed ${total} orphaned review(s): ${r1.rowsAffected} cascade + ${r2.rowsAffected} stale
|
|
6098
|
-
`
|
|
6099
|
-
);
|
|
6100
|
-
}
|
|
6101
|
-
return total;
|
|
6102
|
-
}
|
|
6103
|
-
function getReviewChecklist(role, agent, taskSlug) {
|
|
6104
|
-
const roleLower = role.toLowerCase();
|
|
6105
|
-
if (roleLower.includes("engineer") || roleLower === "principal engineer") {
|
|
6106
|
-
return {
|
|
6107
|
-
lens: "Code Quality (Engineer)",
|
|
6108
|
-
checklist: [
|
|
6109
|
-
"1. Do all tests pass? Any new tests needed?",
|
|
6110
|
-
"2. Is the code clean \u2014 no dead code, no TODOs left?",
|
|
6111
|
-
"3. Does it follow existing patterns and conventions in the codebase?",
|
|
6112
|
-
"4. Any regressions in the test suite?"
|
|
6113
|
-
]
|
|
6114
|
-
};
|
|
6115
|
-
}
|
|
6116
|
-
if (roleLower === "cto" || roleLower.includes("architect")) {
|
|
6117
|
-
return {
|
|
6118
|
-
lens: "Architecture (CTO)",
|
|
6119
|
-
checklist: [
|
|
6120
|
-
"1. Does this fit the existing architecture? Consistent with ARCHITECTURE.md?",
|
|
6121
|
-
"2. Is it backward compatible? Any breaking changes?",
|
|
6122
|
-
"3. Does it introduce technical debt? Is that debt justified?",
|
|
6123
|
-
"4. Security implications? Any new attack surface?",
|
|
6124
|
-
"5. Does it scale? Performance considerations?",
|
|
6125
|
-
"6. Coordination: does this affect other employees' work or other projects?"
|
|
6126
|
-
]
|
|
6127
|
-
};
|
|
6128
|
-
}
|
|
6129
|
-
if (roleLower === "coo" || roleLower.includes("operations")) {
|
|
6130
|
-
return {
|
|
6131
|
-
lens: "Strategic (COO)",
|
|
6132
|
-
checklist: [
|
|
6133
|
-
"1. Does this serve the project mission?",
|
|
6134
|
-
"2. Is this the right work at the right time?",
|
|
6135
|
-
"3. Does the architectural assessment make sense for the business?",
|
|
6136
|
-
"4. Any cross-project implications?"
|
|
6137
|
-
]
|
|
6138
|
-
};
|
|
6139
|
-
}
|
|
6140
|
-
return {
|
|
6141
|
-
lens: "General",
|
|
6142
|
-
checklist: [
|
|
6143
|
-
"1. Read the original task's acceptance criteria",
|
|
6144
|
-
`2. Check git log for related commits: \`git log --oneline --author-date-order -10\``,
|
|
6145
|
-
"3. Verify code changes match requirements",
|
|
6146
|
-
"4. Check if tests were added/updated",
|
|
6147
|
-
`5. Look for output files in exe/output/${agent}-${taskSlug}*`
|
|
6148
|
-
]
|
|
6149
|
-
};
|
|
6205
|
+
// src/lib/tasks.ts
|
|
6206
|
+
var tasks_exports = {};
|
|
6207
|
+
__export(tasks_exports, {
|
|
6208
|
+
cleanupOrphanedReviews: () => cleanupOrphanedReviews,
|
|
6209
|
+
countNewPendingReviewsSince: () => countNewPendingReviewsSince,
|
|
6210
|
+
countPendingReviews: () => countPendingReviews,
|
|
6211
|
+
createTask: () => createTask,
|
|
6212
|
+
createTaskCore: () => createTaskCore,
|
|
6213
|
+
deleteTask: () => deleteTask,
|
|
6214
|
+
deleteTaskCore: () => deleteTaskCore,
|
|
6215
|
+
ensureArchitectureDoc: () => ensureArchitectureDoc,
|
|
6216
|
+
ensureGitignoreExe: () => ensureGitignoreExe,
|
|
6217
|
+
getReviewChecklist: () => getReviewChecklist,
|
|
6218
|
+
listPendingReviews: () => listPendingReviews,
|
|
6219
|
+
listTasks: () => listTasks,
|
|
6220
|
+
resolveTask: () => resolveTask,
|
|
6221
|
+
slugify: () => slugify,
|
|
6222
|
+
updateTask: () => updateTask,
|
|
6223
|
+
updateTaskStatus: () => updateTaskStatus,
|
|
6224
|
+
writeCheckpoint: () => writeCheckpoint
|
|
6225
|
+
});
|
|
6226
|
+
import path16 from "path";
|
|
6227
|
+
import { writeFileSync as writeFileSync4, mkdirSync as mkdirSync6, unlinkSync as unlinkSync4 } from "fs";
|
|
6228
|
+
async function createTask(input) {
|
|
6229
|
+
const result = await createTaskCore(input);
|
|
6230
|
+
if (!input.skipDispatch && result.status !== "blocked" && !process.env.VITEST) {
|
|
6231
|
+
dispatchTaskToEmployee({
|
|
6232
|
+
assignedTo: input.assignedTo,
|
|
6233
|
+
title: input.title,
|
|
6234
|
+
priority: input.priority,
|
|
6235
|
+
taskFile: result.taskFile,
|
|
6236
|
+
initialStatus: result.status,
|
|
6237
|
+
projectName: input.projectName
|
|
6238
|
+
});
|
|
6239
|
+
}
|
|
6240
|
+
return result;
|
|
6150
6241
|
}
|
|
6151
|
-
async function
|
|
6152
|
-
|
|
6242
|
+
async function updateTask(input) {
|
|
6243
|
+
const { row, taskFile, now, taskId } = await updateTaskStatus(input);
|
|
6153
6244
|
try {
|
|
6154
|
-
const
|
|
6155
|
-
const
|
|
6156
|
-
const
|
|
6157
|
-
if (
|
|
6158
|
-
|
|
6159
|
-
|
|
6160
|
-
|
|
6245
|
+
const agent = String(row.assigned_to);
|
|
6246
|
+
const cacheDir = path16.join(EXE_AI_DIR, "session-cache");
|
|
6247
|
+
const cachePath = path16.join(cacheDir, `current-task-${agent}.json`);
|
|
6248
|
+
if (input.status === "in_progress") {
|
|
6249
|
+
mkdirSync6(cacheDir, { recursive: true });
|
|
6250
|
+
writeFileSync4(cachePath, JSON.stringify({ taskId, title: String(row.title) }));
|
|
6251
|
+
} else if (input.status === "done" || input.status === "blocked" || input.status === "cancelled") {
|
|
6252
|
+
try {
|
|
6253
|
+
unlinkSync4(cachePath);
|
|
6254
|
+
} catch {
|
|
6255
|
+
}
|
|
6256
|
+
}
|
|
6257
|
+
} catch {
|
|
6258
|
+
}
|
|
6259
|
+
if (input.status === "done") {
|
|
6260
|
+
await cleanupReviewFile(row, taskFile, input.baseDir);
|
|
6261
|
+
}
|
|
6262
|
+
if (input.status === "done" || input.status === "cancelled") {
|
|
6263
|
+
try {
|
|
6264
|
+
const client = getClient();
|
|
6265
|
+
const taskTitle = String(row.title);
|
|
6266
|
+
const escaped = taskTitle.replace(/%/g, "\\%").replace(/_/g, "\\_");
|
|
6267
|
+
await client.execute({
|
|
6268
|
+
sql: `UPDATE tasks SET status = 'cancelled', updated_at = ?
|
|
6269
|
+
WHERE title LIKE ? ESCAPE '\\' AND status IN ('open', 'in_progress')`,
|
|
6270
|
+
args: [now, `%left '${escaped}' as in\\_progress%`]
|
|
6161
6271
|
});
|
|
6162
|
-
|
|
6272
|
+
} catch {
|
|
6273
|
+
}
|
|
6274
|
+
try {
|
|
6275
|
+
const client = getClient();
|
|
6276
|
+
const cascaded = await client.execute({
|
|
6277
|
+
sql: `UPDATE tasks SET status = 'done', updated_at = ?
|
|
6278
|
+
WHERE parent_task_id = ? AND status = 'needs_review'`,
|
|
6279
|
+
args: [now, taskId]
|
|
6280
|
+
});
|
|
6281
|
+
if (cascaded.rowsAffected > 0) {
|
|
6163
6282
|
process.stderr.write(
|
|
6164
|
-
`[
|
|
6283
|
+
`[cascade] Closed ${cascaded.rowsAffected} orphaned review task(s) for parent ${taskId}
|
|
6165
6284
|
`
|
|
6166
6285
|
);
|
|
6167
6286
|
}
|
|
6168
|
-
}
|
|
6169
|
-
|
|
6170
|
-
|
|
6171
|
-
|
|
6172
|
-
|
|
6173
|
-
|
|
6174
|
-
|
|
6175
|
-
|
|
6176
|
-
|
|
6177
|
-
|
|
6178
|
-
|
|
6179
|
-
|
|
6180
|
-
|
|
6181
|
-
|
|
6182
|
-
|
|
6183
|
-
|
|
6184
|
-
|
|
6287
|
+
} catch {
|
|
6288
|
+
}
|
|
6289
|
+
}
|
|
6290
|
+
const isTerminal = input.status === "done" || input.status === "needs_review";
|
|
6291
|
+
if (isTerminal) {
|
|
6292
|
+
const isExe = String(row.assigned_to) === "exe";
|
|
6293
|
+
if (!isExe) {
|
|
6294
|
+
notifyTaskDone();
|
|
6295
|
+
}
|
|
6296
|
+
await markTaskNotificationsRead(taskFile);
|
|
6297
|
+
if (input.status === "done") {
|
|
6298
|
+
try {
|
|
6299
|
+
await cascadeUnblock(taskId, input.baseDir, now);
|
|
6300
|
+
} catch {
|
|
6301
|
+
}
|
|
6302
|
+
orgBus.emit({
|
|
6303
|
+
type: "task_completed",
|
|
6304
|
+
taskId,
|
|
6305
|
+
employee: String(row.assigned_to),
|
|
6306
|
+
result: input.result ?? "",
|
|
6307
|
+
timestamp: now
|
|
6308
|
+
});
|
|
6309
|
+
if (row.parent_task_id) {
|
|
6310
|
+
try {
|
|
6311
|
+
await checkSubtaskCompletion(String(row.parent_task_id), String(row.project_name));
|
|
6312
|
+
} catch {
|
|
6185
6313
|
}
|
|
6186
6314
|
}
|
|
6187
6315
|
}
|
|
6188
|
-
}
|
|
6189
|
-
|
|
6190
|
-
|
|
6316
|
+
}
|
|
6317
|
+
if (input.status === "done" && String(row.assigned_to) !== "exe" && !process.env.VITEST) {
|
|
6318
|
+
Promise.resolve().then(() => (init_skill_learning(), skill_learning_exports)).then(
|
|
6319
|
+
({ captureAndLearn: captureAndLearn2 }) => captureAndLearn2({
|
|
6320
|
+
taskId,
|
|
6321
|
+
agentId: String(row.assigned_to),
|
|
6322
|
+
projectName: String(row.project_name),
|
|
6323
|
+
taskTitle: String(row.title)
|
|
6324
|
+
})
|
|
6325
|
+
).catch((err) => {
|
|
6326
|
+
process.stderr.write(
|
|
6327
|
+
`[updateTask] skill learning failed: ${err instanceof Error ? err.message : String(err)}
|
|
6191
6328
|
`
|
|
6192
|
-
|
|
6329
|
+
);
|
|
6330
|
+
});
|
|
6193
6331
|
}
|
|
6194
|
-
|
|
6195
|
-
|
|
6196
|
-
|
|
6197
|
-
|
|
6198
|
-
|
|
6199
|
-
unlinkSync4(path14.join(cacheDir, f));
|
|
6200
|
-
}
|
|
6201
|
-
}
|
|
6332
|
+
let nextTask;
|
|
6333
|
+
if (isTerminal && String(row.assigned_to) !== "exe") {
|
|
6334
|
+
try {
|
|
6335
|
+
nextTask = await findNextTask(String(row.assigned_to));
|
|
6336
|
+
} catch {
|
|
6202
6337
|
}
|
|
6203
|
-
} catch {
|
|
6204
6338
|
}
|
|
6339
|
+
return {
|
|
6340
|
+
id: String(row.id),
|
|
6341
|
+
title: String(row.title),
|
|
6342
|
+
assignedTo: String(row.assigned_to),
|
|
6343
|
+
assignedBy: String(row.assigned_by),
|
|
6344
|
+
projectName: String(row.project_name),
|
|
6345
|
+
priority: String(row.priority),
|
|
6346
|
+
status: input.status,
|
|
6347
|
+
taskFile,
|
|
6348
|
+
createdAt: String(row.created_at),
|
|
6349
|
+
updatedAt: now,
|
|
6350
|
+
budgetTokens: row.budget_tokens !== void 0 && row.budget_tokens !== null ? Number(row.budget_tokens) : null,
|
|
6351
|
+
budgetFallbackModel: row.budget_fallback_model !== void 0 && row.budget_fallback_model !== null ? String(row.budget_fallback_model) : null,
|
|
6352
|
+
tokensUsed: Number(row.tokens_used ?? 0),
|
|
6353
|
+
tokensWarnedAt: row.tokens_warned_at !== void 0 && row.tokens_warned_at !== null ? Number(row.tokens_warned_at) : null,
|
|
6354
|
+
nextTask
|
|
6355
|
+
};
|
|
6205
6356
|
}
|
|
6206
|
-
|
|
6207
|
-
|
|
6357
|
+
async function deleteTask(taskId, baseDir) {
|
|
6358
|
+
const client = getClient();
|
|
6359
|
+
const { taskFile, assignedTo, assignedBy, taskSlug } = await deleteTaskCore(taskId, baseDir);
|
|
6360
|
+
const reviewer = assignedBy || "exe";
|
|
6361
|
+
const reviewSlug = `review-${assignedTo}-${taskSlug}`;
|
|
6362
|
+
const reviewFile = `exe/${reviewer}/${reviewSlug}.md`;
|
|
6363
|
+
await client.execute({
|
|
6364
|
+
sql: "DELETE FROM tasks WHERE task_file = ? OR task_file = ?",
|
|
6365
|
+
args: [reviewFile, `exe/exe/${reviewSlug}.md`]
|
|
6366
|
+
});
|
|
6367
|
+
await markAsReadByTaskFile(taskFile);
|
|
6368
|
+
await markAsReadByTaskFile(reviewFile);
|
|
6369
|
+
}
|
|
6370
|
+
var init_tasks = __esm({
|
|
6371
|
+
"src/lib/tasks.ts"() {
|
|
6208
6372
|
"use strict";
|
|
6209
6373
|
init_database();
|
|
6210
6374
|
init_config();
|
|
6211
|
-
init_employees();
|
|
6212
6375
|
init_notifications();
|
|
6376
|
+
init_state_bus();
|
|
6213
6377
|
init_tasks_crud();
|
|
6214
|
-
|
|
6215
|
-
|
|
6378
|
+
init_tasks_review();
|
|
6379
|
+
init_tasks_crud();
|
|
6380
|
+
init_tasks_chain();
|
|
6381
|
+
init_tasks_review();
|
|
6382
|
+
init_tasks_notify();
|
|
6216
6383
|
}
|
|
6217
6384
|
});
|
|
6218
6385
|
|
|
6219
|
-
// src/lib/
|
|
6220
|
-
|
|
6221
|
-
|
|
6222
|
-
|
|
6223
|
-
|
|
6224
|
-
|
|
6225
|
-
|
|
6226
|
-
|
|
6227
|
-
|
|
6228
|
-
|
|
6229
|
-
|
|
6230
|
-
|
|
6231
|
-
|
|
6232
|
-
|
|
6233
|
-
|
|
6234
|
-
|
|
6235
|
-
|
|
6236
|
-
|
|
6237
|
-
|
|
6238
|
-
|
|
6239
|
-
|
|
6240
|
-
|
|
6241
|
-
|
|
6242
|
-
|
|
6386
|
+
// src/lib/capacity-monitor.ts
|
|
6387
|
+
var capacity_monitor_exports = {};
|
|
6388
|
+
__export(capacity_monitor_exports, {
|
|
6389
|
+
CTX_FLOOR_PERCENT: () => CTX_FLOOR_PERCENT,
|
|
6390
|
+
_resetLastRelaunchCache: () => _resetLastRelaunchCache,
|
|
6391
|
+
_resetPendingCapacityKills: () => _resetPendingCapacityKills,
|
|
6392
|
+
confirmCapacityKill: () => confirmCapacityKill,
|
|
6393
|
+
createOrRefreshResumeTask: () => createOrRefreshResumeTask,
|
|
6394
|
+
extractContextPercent: () => extractContextPercent,
|
|
6395
|
+
isAtCapacity: () => isAtCapacity,
|
|
6396
|
+
isWithinRelaunchCooldown: () => isWithinRelaunchCooldown,
|
|
6397
|
+
pollCapacityDead: () => pollCapacityDead
|
|
6398
|
+
});
|
|
6399
|
+
function resumeTaskTitle(agentId) {
|
|
6400
|
+
return `${RESUME_TITLE_PREFIX} ${agentId} hit context capacity \u2014 continue open tasks`;
|
|
6401
|
+
}
|
|
6402
|
+
function buildResumeContext(agentId, openTasks) {
|
|
6403
|
+
const taskList = openTasks.map(
|
|
6404
|
+
(r, i) => `${i + 1}. [${String(r.priority).toUpperCase()}] ${String(r.title)} (${String(r.task_file)})`
|
|
6405
|
+
).join("\n");
|
|
6406
|
+
return [
|
|
6407
|
+
"## Context",
|
|
6408
|
+
"",
|
|
6409
|
+
`${agentId} hit context capacity and was auto-relaunched by the capacity monitor.`,
|
|
6410
|
+
"Call recall_my_memory first \u2014 search for 'CONTEXT CHECKPOINT'. Pick up where the previous session stopped.",
|
|
6411
|
+
"",
|
|
6412
|
+
`You have ${openTasks.length} open task(s). Work through them in priority order:`,
|
|
6413
|
+
"",
|
|
6414
|
+
taskList,
|
|
6415
|
+
"",
|
|
6416
|
+
"Read each task file and chain through them. Build and commit after each one."
|
|
6417
|
+
].join("\n");
|
|
6418
|
+
}
|
|
6419
|
+
function filterPaneContent(paneOutput) {
|
|
6420
|
+
return paneOutput.split("\n").filter((line) => {
|
|
6421
|
+
if (CONTENT_LINE_PREFIX.test(line)) return false;
|
|
6422
|
+
for (const marker of CONTENT_LINE_MARKERS) {
|
|
6423
|
+
if (line.includes(marker)) return false;
|
|
6424
|
+
}
|
|
6425
|
+
for (const re of SOURCE_CODE_MARKERS) {
|
|
6426
|
+
if (re.test(line)) return false;
|
|
6243
6427
|
}
|
|
6428
|
+
return true;
|
|
6429
|
+
}).join("\n");
|
|
6430
|
+
}
|
|
6431
|
+
function extractContextPercent(paneOutput) {
|
|
6432
|
+
const match = paneOutput.match(CC_CONTEXT_BAR_RE);
|
|
6433
|
+
if (!match) return null;
|
|
6434
|
+
const parsed = Number.parseInt(match[2], 10);
|
|
6435
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
6436
|
+
}
|
|
6437
|
+
function isAtCapacity(paneOutput) {
|
|
6438
|
+
const filtered = filterPaneContent(paneOutput);
|
|
6439
|
+
return CAPACITY_PATTERNS.some((p) => p.test(filtered));
|
|
6440
|
+
}
|
|
6441
|
+
function confirmCapacityKill(agentId, now = Date.now()) {
|
|
6442
|
+
const pendingSince = _pendingCapacityKill.get(agentId);
|
|
6443
|
+
if (pendingSince === void 0) {
|
|
6444
|
+
_pendingCapacityKill.set(agentId, now);
|
|
6445
|
+
return false;
|
|
6446
|
+
}
|
|
6447
|
+
if (now - pendingSince > CONFIRMATION_WINDOW_MS) {
|
|
6448
|
+
_pendingCapacityKill.set(agentId, now);
|
|
6449
|
+
return false;
|
|
6244
6450
|
}
|
|
6451
|
+
_pendingCapacityKill.delete(agentId);
|
|
6452
|
+
return true;
|
|
6245
6453
|
}
|
|
6246
|
-
|
|
6454
|
+
function _resetPendingCapacityKills() {
|
|
6455
|
+
_pendingCapacityKill.clear();
|
|
6456
|
+
}
|
|
6457
|
+
function _resetLastRelaunchCache() {
|
|
6458
|
+
_lastRelaunch.clear();
|
|
6459
|
+
}
|
|
6460
|
+
async function lastResumeCreatedAtMs(agentId) {
|
|
6247
6461
|
const client = getClient();
|
|
6248
|
-
const
|
|
6249
|
-
sql: `SELECT
|
|
6250
|
-
|
|
6251
|
-
|
|
6252
|
-
|
|
6253
|
-
args: [assignedTo]
|
|
6462
|
+
const result = await client.execute({
|
|
6463
|
+
sql: `SELECT MAX(created_at) AS last_created_at
|
|
6464
|
+
FROM tasks
|
|
6465
|
+
WHERE assigned_to = ? AND title LIKE ?`,
|
|
6466
|
+
args: [agentId, `${RESUME_TITLE_PREFIX} %`]
|
|
6254
6467
|
});
|
|
6255
|
-
|
|
6256
|
-
|
|
6257
|
-
|
|
6258
|
-
|
|
6259
|
-
|
|
6260
|
-
|
|
6261
|
-
|
|
6262
|
-
|
|
6263
|
-
|
|
6468
|
+
const raw = result.rows[0]?.last_created_at;
|
|
6469
|
+
if (raw === null || raw === void 0) return null;
|
|
6470
|
+
const parsed = Date.parse(String(raw));
|
|
6471
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
6472
|
+
}
|
|
6473
|
+
async function isWithinRelaunchCooldown(agentId, now = Date.now()) {
|
|
6474
|
+
const cached = _lastRelaunch.get(agentId);
|
|
6475
|
+
if (cached !== void 0) return now - cached < RELAUNCH_COOLDOWN_MS;
|
|
6476
|
+
const persisted = await lastResumeCreatedAtMs(agentId);
|
|
6477
|
+
if (persisted === null) return false;
|
|
6478
|
+
if (now - persisted >= RELAUNCH_COOLDOWN_MS) return false;
|
|
6479
|
+
_lastRelaunch.set(agentId, persisted);
|
|
6480
|
+
return true;
|
|
6264
6481
|
}
|
|
6265
|
-
async function
|
|
6482
|
+
async function createOrRefreshResumeTask(agentId, projectDir, openTasks) {
|
|
6266
6483
|
const client = getClient();
|
|
6267
|
-
const
|
|
6268
|
-
|
|
6269
|
-
|
|
6270
|
-
|
|
6484
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
6485
|
+
const context = buildResumeContext(agentId, openTasks);
|
|
6486
|
+
const existing = await client.execute({
|
|
6487
|
+
sql: `SELECT id FROM tasks
|
|
6488
|
+
WHERE assigned_to = ?
|
|
6489
|
+
AND title LIKE ?
|
|
6490
|
+
AND status IN (${RESUME_ACTIVE_STATUSES.map(() => "?").join(", ")})
|
|
6491
|
+
ORDER BY created_at DESC
|
|
6492
|
+
LIMIT 1`,
|
|
6493
|
+
args: [agentId, RESUME_TITLE_LIKE_PATTERN, ...RESUME_ACTIVE_STATUSES]
|
|
6271
6494
|
});
|
|
6272
|
-
|
|
6273
|
-
|
|
6274
|
-
|
|
6275
|
-
sql: `
|
|
6276
|
-
args: [
|
|
6495
|
+
if (existing.rows.length > 0) {
|
|
6496
|
+
const taskId = String(existing.rows[0].id);
|
|
6497
|
+
await client.execute({
|
|
6498
|
+
sql: `UPDATE tasks SET context = ?, updated_at = ? WHERE id = ?`,
|
|
6499
|
+
args: [context, now, taskId]
|
|
6277
6500
|
});
|
|
6278
|
-
|
|
6279
|
-
|
|
6280
|
-
|
|
6281
|
-
|
|
6282
|
-
|
|
6283
|
-
|
|
6284
|
-
|
|
6285
|
-
|
|
6286
|
-
|
|
6287
|
-
|
|
6501
|
+
return { created: false, taskId };
|
|
6502
|
+
}
|
|
6503
|
+
const { createTask: createTask2 } = await Promise.resolve().then(() => (init_tasks(), tasks_exports));
|
|
6504
|
+
const task = await createTask2({
|
|
6505
|
+
title: resumeTaskTitle(agentId),
|
|
6506
|
+
assignedTo: agentId,
|
|
6507
|
+
assignedBy: "system",
|
|
6508
|
+
projectName: projectDir.split("/").pop() ?? "unknown",
|
|
6509
|
+
priority: "p0",
|
|
6510
|
+
context,
|
|
6511
|
+
baseDir: projectDir
|
|
6512
|
+
});
|
|
6513
|
+
return { created: true, taskId: task.id };
|
|
6514
|
+
}
|
|
6515
|
+
async function pollCapacityDead() {
|
|
6516
|
+
const transport = getTransport();
|
|
6517
|
+
const relaunched = [];
|
|
6518
|
+
const registered = listSessions().filter(
|
|
6519
|
+
(s) => s.agentId !== "exe"
|
|
6520
|
+
);
|
|
6521
|
+
if (registered.length === 0) return [];
|
|
6522
|
+
let liveSessions;
|
|
6523
|
+
try {
|
|
6524
|
+
liveSessions = transport.listSessions();
|
|
6525
|
+
} catch {
|
|
6526
|
+
return [];
|
|
6527
|
+
}
|
|
6528
|
+
for (const entry of registered) {
|
|
6529
|
+
const { windowName, agentId, projectDir } = entry;
|
|
6530
|
+
if (!liveSessions.includes(windowName)) continue;
|
|
6531
|
+
if (await isWithinRelaunchCooldown(agentId)) continue;
|
|
6532
|
+
let pane;
|
|
6533
|
+
try {
|
|
6534
|
+
pane = transport.capturePane(windowName, 15);
|
|
6535
|
+
} catch {
|
|
6536
|
+
continue;
|
|
6537
|
+
}
|
|
6538
|
+
if (!isAtCapacity(pane)) continue;
|
|
6539
|
+
const ctxPct = extractContextPercent(pane);
|
|
6540
|
+
if (ctxPct !== null && ctxPct < CTX_FLOOR_PERCENT) {
|
|
6541
|
+
process.stderr.write(
|
|
6542
|
+
`[capacity-monitor] ctx-floor: ${agentId} at ${ctxPct}% in ${windowName} \u2014 below ${CTX_FLOOR_PERCENT}%. Skipping capacity kill (likely self-referential content or false positive).
|
|
6543
|
+
`
|
|
6544
|
+
);
|
|
6545
|
+
continue;
|
|
6546
|
+
}
|
|
6547
|
+
if (!confirmCapacityKill(agentId)) {
|
|
6548
|
+
process.stderr.write(
|
|
6549
|
+
`[capacity-monitor] ${agentId} matched capacity pattern once in ${windowName}. Awaiting confirmation on next tick.
|
|
6550
|
+
`
|
|
6551
|
+
);
|
|
6552
|
+
continue;
|
|
6553
|
+
}
|
|
6554
|
+
const verify = await verifyPaneAtCapacity(windowName);
|
|
6555
|
+
if (!verify.atCapacity) {
|
|
6556
|
+
process.stderr.write(
|
|
6557
|
+
`[capacity-monitor] verifyPaneAtCapacity rejected kill for ${agentId} in ${windowName} (reason: ${verify.reason}). Skipping.
|
|
6558
|
+
`
|
|
6559
|
+
);
|
|
6560
|
+
void recordSessionKill({
|
|
6561
|
+
sessionName: windowName,
|
|
6562
|
+
agentId,
|
|
6563
|
+
reason: "capacity_false_positive_blocked"
|
|
6564
|
+
});
|
|
6565
|
+
continue;
|
|
6566
|
+
}
|
|
6567
|
+
process.stderr.write(
|
|
6568
|
+
`[capacity-monitor] Detected ${agentId} at capacity in session ${windowName} (confirmed). Auto-relaunching.
|
|
6569
|
+
`
|
|
6570
|
+
);
|
|
6571
|
+
try {
|
|
6572
|
+
transport.kill(windowName);
|
|
6573
|
+
void recordSessionKill({
|
|
6574
|
+
sessionName: windowName,
|
|
6575
|
+
agentId,
|
|
6576
|
+
reason: "capacity"
|
|
6577
|
+
});
|
|
6578
|
+
const client = getClient();
|
|
6579
|
+
const openTasks = await client.execute({
|
|
6580
|
+
sql: `SELECT id, title, priority, task_file, status
|
|
6581
|
+
FROM tasks
|
|
6582
|
+
WHERE assigned_to = ? AND status IN ('open', 'in_progress')
|
|
6583
|
+
ORDER BY
|
|
6584
|
+
CASE priority WHEN 'p0' THEN 0 WHEN 'p1' THEN 1 WHEN 'p2' THEN 2 ELSE 3 END,
|
|
6585
|
+
created_at ASC
|
|
6586
|
+
LIMIT 10`,
|
|
6587
|
+
args: [agentId]
|
|
6288
6588
|
});
|
|
6589
|
+
if (openTasks.rows.length === 0) {
|
|
6590
|
+
process.stderr.write(
|
|
6591
|
+
`[capacity-monitor] ${agentId} has no open tasks \u2014 skipping relaunch.
|
|
6592
|
+
`
|
|
6593
|
+
);
|
|
6594
|
+
continue;
|
|
6595
|
+
}
|
|
6596
|
+
const { created } = await createOrRefreshResumeTask(
|
|
6597
|
+
agentId,
|
|
6598
|
+
projectDir,
|
|
6599
|
+
openTasks.rows
|
|
6600
|
+
);
|
|
6601
|
+
if (created) {
|
|
6602
|
+
await writeNotification({
|
|
6603
|
+
agentId: "system",
|
|
6604
|
+
agentRole: "daemon",
|
|
6605
|
+
event: "capacity_relaunch",
|
|
6606
|
+
project: projectDir.split("/").pop() ?? "unknown",
|
|
6607
|
+
summary: `${agentId} hit context capacity. Auto-relaunched with ${openTasks.rows.length} open task(s).`
|
|
6608
|
+
});
|
|
6609
|
+
}
|
|
6610
|
+
_lastRelaunch.set(agentId, Date.now());
|
|
6611
|
+
if (created) relaunched.push(agentId);
|
|
6612
|
+
} catch (err) {
|
|
6613
|
+
process.stderr.write(
|
|
6614
|
+
`[capacity-monitor] Failed to relaunch ${agentId}: ${err instanceof Error ? err.message : String(err)}
|
|
6615
|
+
`
|
|
6616
|
+
);
|
|
6289
6617
|
}
|
|
6290
6618
|
}
|
|
6619
|
+
return relaunched;
|
|
6291
6620
|
}
|
|
6292
|
-
var
|
|
6293
|
-
|
|
6621
|
+
var CAPACITY_PATTERNS, CONTENT_LINE_PREFIX, CONTENT_LINE_MARKERS, SOURCE_CODE_MARKERS, RELAUNCH_COOLDOWN_MS, _lastRelaunch, RESUME_TITLE_PREFIX, RESUME_TITLE_LIKE_PATTERN, RESUME_ACTIVE_STATUSES, CONFIRMATION_WINDOW_MS, _pendingCapacityKill, CC_CONTEXT_BAR_RE, CTX_FLOOR_PERCENT;
|
|
6622
|
+
var init_capacity_monitor = __esm({
|
|
6623
|
+
"src/lib/capacity-monitor.ts"() {
|
|
6294
6624
|
"use strict";
|
|
6295
|
-
|
|
6625
|
+
init_session_registry();
|
|
6626
|
+
init_transport();
|
|
6296
6627
|
init_notifications();
|
|
6628
|
+
init_database();
|
|
6629
|
+
init_session_kill_telemetry();
|
|
6630
|
+
init_tmux_routing();
|
|
6631
|
+
CAPACITY_PATTERNS = [
|
|
6632
|
+
/conversation is too long/i,
|
|
6633
|
+
/maximum context length/i,
|
|
6634
|
+
/context window.*(?:limit|exceed|full)/i,
|
|
6635
|
+
/reached.*(?:token|context).*limit/i
|
|
6636
|
+
];
|
|
6637
|
+
CONTENT_LINE_PREFIX = /^[\s>#\-*[]/;
|
|
6638
|
+
CONTENT_LINE_MARKERS = [
|
|
6639
|
+
"RESUME:",
|
|
6640
|
+
"intercom",
|
|
6641
|
+
"capacity-monitor",
|
|
6642
|
+
"CAPACITY_PATTERNS",
|
|
6643
|
+
"isAtCapacity",
|
|
6644
|
+
"CONTENT_LINE_MARKERS",
|
|
6645
|
+
"pollCapacityDead",
|
|
6646
|
+
"confirmCapacityKill",
|
|
6647
|
+
"session_kills",
|
|
6648
|
+
"capacity-monitor.test"
|
|
6649
|
+
];
|
|
6650
|
+
SOURCE_CODE_MARKERS = [
|
|
6651
|
+
/["'`/].*(?:maximum context length|conversation is too long)/i,
|
|
6652
|
+
/(?:maximum context length|conversation is too long).*["'`/]/i
|
|
6653
|
+
];
|
|
6654
|
+
RELAUNCH_COOLDOWN_MS = 5 * 60 * 1e3;
|
|
6655
|
+
_lastRelaunch = /* @__PURE__ */ new Map();
|
|
6656
|
+
RESUME_TITLE_PREFIX = "RESUME:";
|
|
6657
|
+
RESUME_TITLE_LIKE_PATTERN = `${RESUME_TITLE_PREFIX} % hit context capacity%`;
|
|
6658
|
+
RESUME_ACTIVE_STATUSES = ["open", "in_progress"];
|
|
6659
|
+
CONFIRMATION_WINDOW_MS = 3 * 60 * 1e3;
|
|
6660
|
+
_pendingCapacityKill = /* @__PURE__ */ new Map();
|
|
6661
|
+
CC_CONTEXT_BAR_RE = /([\u2588\u2591\u2592\u2593]{10})\s+(\d+)%/;
|
|
6662
|
+
CTX_FLOOR_PERCENT = 50;
|
|
6297
6663
|
}
|
|
6298
6664
|
});
|
|
6299
6665
|
|
|
6300
|
-
// src/lib/
|
|
6301
|
-
|
|
6302
|
-
|
|
6303
|
-
|
|
6304
|
-
|
|
6305
|
-
|
|
6666
|
+
// src/lib/tmux-routing.ts
|
|
6667
|
+
var tmux_routing_exports = {};
|
|
6668
|
+
__export(tmux_routing_exports, {
|
|
6669
|
+
acquireSpawnLock: () => acquireSpawnLock2,
|
|
6670
|
+
employeeSessionName: () => employeeSessionName,
|
|
6671
|
+
ensureEmployee: () => ensureEmployee,
|
|
6672
|
+
extractRootExe: () => extractRootExe,
|
|
6673
|
+
findFreeInstance: () => findFreeInstance,
|
|
6674
|
+
getDispatchedBy: () => getDispatchedBy,
|
|
6675
|
+
getMySession: () => getMySession,
|
|
6676
|
+
getParentExe: () => getParentExe,
|
|
6677
|
+
getSessionState: () => getSessionState,
|
|
6678
|
+
isEmployeeAlive: () => isEmployeeAlive,
|
|
6679
|
+
isExeSession: () => isExeSession,
|
|
6680
|
+
isSessionBusy: () => isSessionBusy,
|
|
6681
|
+
notifyParentExe: () => notifyParentExe,
|
|
6682
|
+
parseParentExe: () => parseParentExe,
|
|
6683
|
+
registerParentExe: () => registerParentExe,
|
|
6684
|
+
releaseSpawnLock: () => releaseSpawnLock2,
|
|
6685
|
+
resolveExeSession: () => resolveExeSession,
|
|
6686
|
+
sendIntercom: () => sendIntercom,
|
|
6687
|
+
spawnEmployee: () => spawnEmployee,
|
|
6688
|
+
verifyPaneAtCapacity: () => verifyPaneAtCapacity
|
|
6689
|
+
});
|
|
6690
|
+
import { execFileSync as execFileSync2, execSync as execSync6 } from "child_process";
|
|
6691
|
+
import { readFileSync as readFileSync11, writeFileSync as writeFileSync5, mkdirSync as mkdirSync7, existsSync as existsSync13, appendFileSync } from "fs";
|
|
6692
|
+
import path17 from "path";
|
|
6693
|
+
import os7 from "os";
|
|
6694
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
6695
|
+
import { unlinkSync as unlinkSync5 } from "fs";
|
|
6696
|
+
function spawnLockPath(sessionName) {
|
|
6697
|
+
return path17.join(SPAWN_LOCK_DIR, `${sessionName}.lock`);
|
|
6698
|
+
}
|
|
6699
|
+
function isProcessAlive(pid) {
|
|
6306
6700
|
try {
|
|
6307
|
-
|
|
6701
|
+
process.kill(pid, 0);
|
|
6702
|
+
return true;
|
|
6703
|
+
} catch {
|
|
6704
|
+
return false;
|
|
6705
|
+
}
|
|
6706
|
+
}
|
|
6707
|
+
function acquireSpawnLock2(sessionName) {
|
|
6708
|
+
if (!existsSync13(SPAWN_LOCK_DIR)) {
|
|
6709
|
+
mkdirSync7(SPAWN_LOCK_DIR, { recursive: true });
|
|
6710
|
+
}
|
|
6711
|
+
const lockFile = spawnLockPath(sessionName);
|
|
6712
|
+
if (existsSync13(lockFile)) {
|
|
6308
6713
|
try {
|
|
6309
|
-
const
|
|
6310
|
-
|
|
6311
|
-
|
|
6312
|
-
|
|
6313
|
-
|
|
6314
|
-
}).trim();
|
|
6315
|
-
repoRoot = path16.dirname(gitCommonDir);
|
|
6714
|
+
const lock = JSON.parse(readFileSync11(lockFile, "utf8"));
|
|
6715
|
+
const age = Date.now() - lock.timestamp;
|
|
6716
|
+
if (isProcessAlive(lock.pid) && age < 6e4) {
|
|
6717
|
+
return false;
|
|
6718
|
+
}
|
|
6316
6719
|
} catch {
|
|
6317
|
-
repoRoot = execSync6("git rev-parse --show-toplevel", {
|
|
6318
|
-
cwd: dir,
|
|
6319
|
-
encoding: "utf8",
|
|
6320
|
-
timeout: 2e3,
|
|
6321
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
6322
|
-
}).trim();
|
|
6323
6720
|
}
|
|
6324
|
-
_cached2 = path16.basename(repoRoot);
|
|
6325
|
-
_cachedCwd = dir;
|
|
6326
|
-
return _cached2;
|
|
6327
|
-
} catch {
|
|
6328
|
-
_cached2 = path16.basename(dir);
|
|
6329
|
-
_cachedCwd = dir;
|
|
6330
|
-
return _cached2;
|
|
6331
6721
|
}
|
|
6722
|
+
writeFileSync5(lockFile, JSON.stringify({ pid: process.pid, timestamp: Date.now() }));
|
|
6723
|
+
return true;
|
|
6332
6724
|
}
|
|
6333
|
-
|
|
6334
|
-
|
|
6335
|
-
|
|
6336
|
-
|
|
6337
|
-
_cached2 = null;
|
|
6338
|
-
_cachedCwd = null;
|
|
6725
|
+
function releaseSpawnLock2(sessionName) {
|
|
6726
|
+
try {
|
|
6727
|
+
unlinkSync5(spawnLockPath(sessionName));
|
|
6728
|
+
} catch {
|
|
6339
6729
|
}
|
|
6340
|
-
});
|
|
6341
|
-
|
|
6342
|
-
// src/lib/session-scope.ts
|
|
6343
|
-
var session_scope_exports = {};
|
|
6344
|
-
__export(session_scope_exports, {
|
|
6345
|
-
assertSessionScope: () => assertSessionScope,
|
|
6346
|
-
findSessionForProject: () => findSessionForProject,
|
|
6347
|
-
getSessionProject: () => getSessionProject
|
|
6348
|
-
});
|
|
6349
|
-
function getSessionProject(sessionName) {
|
|
6350
|
-
const sessions = listSessions();
|
|
6351
|
-
const entry = sessions.find((s) => s.windowName === sessionName);
|
|
6352
|
-
if (!entry) return null;
|
|
6353
|
-
const parts = entry.projectDir.split("/").filter(Boolean);
|
|
6354
|
-
return parts[parts.length - 1] ?? null;
|
|
6355
6730
|
}
|
|
6356
|
-
function
|
|
6357
|
-
|
|
6358
|
-
|
|
6359
|
-
const
|
|
6360
|
-
|
|
6731
|
+
function resolveBehaviorsExporterScript() {
|
|
6732
|
+
try {
|
|
6733
|
+
const thisFile = fileURLToPath2(import.meta.url);
|
|
6734
|
+
const scriptPath = path17.join(
|
|
6735
|
+
path17.dirname(thisFile),
|
|
6736
|
+
"..",
|
|
6737
|
+
"bin",
|
|
6738
|
+
"exe-export-behaviors.js"
|
|
6739
|
+
);
|
|
6740
|
+
return existsSync13(scriptPath) ? scriptPath : null;
|
|
6741
|
+
} catch {
|
|
6742
|
+
return null;
|
|
6361
6743
|
}
|
|
6362
|
-
return null;
|
|
6363
6744
|
}
|
|
6364
|
-
function
|
|
6745
|
+
function exportBehaviorsSync(agentId, projectName, sessionKey) {
|
|
6746
|
+
const script = resolveBehaviorsExporterScript();
|
|
6747
|
+
if (!script) return null;
|
|
6365
6748
|
try {
|
|
6366
|
-
const
|
|
6367
|
-
|
|
6368
|
-
|
|
6369
|
-
|
|
6370
|
-
|
|
6371
|
-
|
|
6372
|
-
|
|
6373
|
-
allowed: true,
|
|
6374
|
-
reason: "same_session",
|
|
6375
|
-
currentProject,
|
|
6376
|
-
targetProject
|
|
6377
|
-
};
|
|
6378
|
-
}
|
|
6749
|
+
const output = execFileSync2(
|
|
6750
|
+
process.execPath,
|
|
6751
|
+
[script, agentId, projectName, sessionKey],
|
|
6752
|
+
{ encoding: "utf-8", timeout: BEHAVIORS_EXPORT_TIMEOUT_MS }
|
|
6753
|
+
).trim();
|
|
6754
|
+
return output.length > 0 ? output : null;
|
|
6755
|
+
} catch (err) {
|
|
6379
6756
|
process.stderr.write(
|
|
6380
|
-
`[
|
|
6757
|
+
`[tmux-routing] behaviors export failed for ${agentId}: ${err instanceof Error ? err.message : String(err)}
|
|
6381
6758
|
`
|
|
6382
6759
|
);
|
|
6383
|
-
return
|
|
6384
|
-
|
|
6385
|
-
|
|
6386
|
-
|
|
6387
|
-
|
|
6388
|
-
|
|
6389
|
-
|
|
6390
|
-
|
|
6760
|
+
return null;
|
|
6761
|
+
}
|
|
6762
|
+
}
|
|
6763
|
+
function getMySession() {
|
|
6764
|
+
return getTransport().getMySession();
|
|
6765
|
+
}
|
|
6766
|
+
function employeeSessionName(employee, exeSession, instance) {
|
|
6767
|
+
if (!/^exe\d+$/.test(exeSession)) {
|
|
6768
|
+
const root = extractRootExe(exeSession);
|
|
6769
|
+
if (root) {
|
|
6770
|
+
process.stderr.write(
|
|
6771
|
+
`[tmux-routing] WARN: exeSession="${exeSession}" is not a root exe session, using "${root}" instead
|
|
6772
|
+
`
|
|
6773
|
+
);
|
|
6774
|
+
exeSession = root;
|
|
6775
|
+
} else {
|
|
6776
|
+
throw new Error(
|
|
6777
|
+
`Invalid exeSession "${exeSession}" \u2014 must be a root exe session (e.g., "exe1"), not an agent session`
|
|
6778
|
+
);
|
|
6779
|
+
}
|
|
6780
|
+
}
|
|
6781
|
+
const suffix = instance != null && instance > 0 ? String(instance) : "";
|
|
6782
|
+
const name = `${employee}${suffix}-${exeSession}`;
|
|
6783
|
+
if (!VALID_SESSION_NAME.test(name)) {
|
|
6784
|
+
throw new Error(
|
|
6785
|
+
`Invalid session name "${name}" \u2014 must match {agent}-exe{N} or {agent}{instance}-exe{N}`
|
|
6786
|
+
);
|
|
6787
|
+
}
|
|
6788
|
+
return name;
|
|
6789
|
+
}
|
|
6790
|
+
function parseParentExe(sessionName, agentId) {
|
|
6791
|
+
const escaped = agentId.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
6792
|
+
const regex = new RegExp(`^${escaped}\\d*-(.+)$`);
|
|
6793
|
+
const match = sessionName.match(regex);
|
|
6794
|
+
return match?.[1] ?? null;
|
|
6795
|
+
}
|
|
6796
|
+
function extractRootExe(name) {
|
|
6797
|
+
const match = name.match(/(exe\d+)$/);
|
|
6798
|
+
return match?.[1] ?? null;
|
|
6799
|
+
}
|
|
6800
|
+
function registerParentExe(sessionKey, parentExe, dispatchedBy) {
|
|
6801
|
+
if (!existsSync13(SESSION_CACHE)) {
|
|
6802
|
+
mkdirSync7(SESSION_CACHE, { recursive: true });
|
|
6803
|
+
}
|
|
6804
|
+
const rootExe = extractRootExe(parentExe) ?? parentExe;
|
|
6805
|
+
const filePath = path17.join(SESSION_CACHE, `parent-exe-${sessionKey}.json`);
|
|
6806
|
+
writeFileSync5(filePath, JSON.stringify({
|
|
6807
|
+
parentExe: rootExe,
|
|
6808
|
+
dispatchedBy: dispatchedBy || rootExe,
|
|
6809
|
+
registeredAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
6810
|
+
}));
|
|
6811
|
+
}
|
|
6812
|
+
function getParentExe(sessionKey) {
|
|
6813
|
+
try {
|
|
6814
|
+
const data = JSON.parse(readFileSync11(path17.join(SESSION_CACHE, `parent-exe-${sessionKey}.json`), "utf8"));
|
|
6815
|
+
return data.parentExe || null;
|
|
6391
6816
|
} catch {
|
|
6392
|
-
return
|
|
6817
|
+
return null;
|
|
6393
6818
|
}
|
|
6394
6819
|
}
|
|
6395
|
-
|
|
6396
|
-
|
|
6397
|
-
|
|
6398
|
-
|
|
6399
|
-
|
|
6400
|
-
|
|
6820
|
+
function getDispatchedBy(sessionKey) {
|
|
6821
|
+
try {
|
|
6822
|
+
const data = JSON.parse(readFileSync11(
|
|
6823
|
+
path17.join(SESSION_CACHE, `parent-exe-${sessionKey}.json`),
|
|
6824
|
+
"utf8"
|
|
6825
|
+
));
|
|
6826
|
+
return data.dispatchedBy ?? data.parentExe ?? null;
|
|
6827
|
+
} catch {
|
|
6828
|
+
return null;
|
|
6401
6829
|
}
|
|
6402
|
-
}
|
|
6403
|
-
|
|
6404
|
-
|
|
6405
|
-
|
|
6406
|
-
|
|
6407
|
-
|
|
6408
|
-
|
|
6409
|
-
|
|
6410
|
-
|
|
6411
|
-
const check = assertSessionScope2("dispatch_task", input.projectName);
|
|
6412
|
-
if (check.reason === "cross_session_granted") {
|
|
6413
|
-
crossProject = true;
|
|
6414
|
-
}
|
|
6415
|
-
} catch {
|
|
6830
|
+
}
|
|
6831
|
+
function resolveExeSession() {
|
|
6832
|
+
const mySession = getMySession();
|
|
6833
|
+
if (!mySession) return null;
|
|
6834
|
+
try {
|
|
6835
|
+
const key = getSessionKey();
|
|
6836
|
+
const parentExe = getParentExe(key);
|
|
6837
|
+
if (parentExe) {
|
|
6838
|
+
return extractRootExe(parentExe) ?? parentExe;
|
|
6416
6839
|
}
|
|
6840
|
+
} catch {
|
|
6841
|
+
}
|
|
6842
|
+
return extractRootExe(mySession) ?? mySession;
|
|
6843
|
+
}
|
|
6844
|
+
function isEmployeeAlive(sessionName) {
|
|
6845
|
+
return getTransport().isAlive(sessionName);
|
|
6846
|
+
}
|
|
6847
|
+
function findFreeInstance(employeeName, exeSession, maxInstances = 10, isAlive = isEmployeeAlive) {
|
|
6848
|
+
const base = employeeSessionName(employeeName, exeSession);
|
|
6849
|
+
if (!isAlive(base) && acquireSpawnLock2(base)) return 0;
|
|
6850
|
+
for (let i = 2; i <= maxInstances; i++) {
|
|
6851
|
+
const candidate = employeeSessionName(employeeName, exeSession, i);
|
|
6852
|
+
if (!isAlive(candidate) && acquireSpawnLock2(candidate)) return i;
|
|
6853
|
+
}
|
|
6854
|
+
return null;
|
|
6855
|
+
}
|
|
6856
|
+
async function verifyPaneAtCapacity(sessionName) {
|
|
6857
|
+
const transport = getTransport();
|
|
6858
|
+
if (!transport.isAlive(sessionName)) {
|
|
6859
|
+
return { atCapacity: false, reason: `session ${sessionName} is not alive` };
|
|
6417
6860
|
}
|
|
6861
|
+
let pane;
|
|
6418
6862
|
try {
|
|
6419
|
-
|
|
6420
|
-
|
|
6421
|
-
|
|
6422
|
-
|
|
6423
|
-
|
|
6424
|
-
|
|
6425
|
-
|
|
6426
|
-
|
|
6427
|
-
|
|
6428
|
-
|
|
6429
|
-
|
|
6430
|
-
|
|
6431
|
-
|
|
6432
|
-
|
|
6433
|
-
|
|
6434
|
-
|
|
6435
|
-
|
|
6436
|
-
|
|
6437
|
-
|
|
6438
|
-
|
|
6439
|
-
|
|
6440
|
-
}
|
|
6863
|
+
pane = transport.capturePane(sessionName, VERIFY_PANE_LINES);
|
|
6864
|
+
} catch (err) {
|
|
6865
|
+
return {
|
|
6866
|
+
atCapacity: false,
|
|
6867
|
+
reason: `capture-pane failed: ${err instanceof Error ? err.message : String(err)}`
|
|
6868
|
+
};
|
|
6869
|
+
}
|
|
6870
|
+
const { isAtCapacity: isAtCapacity2 } = await Promise.resolve().then(() => (init_capacity_monitor(), capacity_monitor_exports));
|
|
6871
|
+
if (!isAtCapacity2(pane)) {
|
|
6872
|
+
return {
|
|
6873
|
+
atCapacity: false,
|
|
6874
|
+
reason: `last ${VERIFY_PANE_LINES} lines show normal work, no capacity banner`
|
|
6875
|
+
};
|
|
6876
|
+
}
|
|
6877
|
+
return {
|
|
6878
|
+
atCapacity: true,
|
|
6879
|
+
reason: "capacity banner matched in recent pane output"
|
|
6880
|
+
};
|
|
6881
|
+
}
|
|
6882
|
+
function readDebounceState() {
|
|
6883
|
+
try {
|
|
6884
|
+
if (!existsSync13(DEBOUNCE_FILE)) return {};
|
|
6885
|
+
return JSON.parse(readFileSync11(DEBOUNCE_FILE, "utf8"));
|
|
6441
6886
|
} catch {
|
|
6442
|
-
return {
|
|
6887
|
+
return {};
|
|
6443
6888
|
}
|
|
6444
6889
|
}
|
|
6445
|
-
function
|
|
6890
|
+
function writeDebounceState(state) {
|
|
6446
6891
|
try {
|
|
6447
|
-
|
|
6448
|
-
|
|
6892
|
+
if (!existsSync13(SESSION_CACHE)) mkdirSync7(SESSION_CACHE, { recursive: true });
|
|
6893
|
+
writeFileSync5(DEBOUNCE_FILE, JSON.stringify(state));
|
|
6449
6894
|
} catch {
|
|
6450
6895
|
}
|
|
6451
6896
|
}
|
|
6452
|
-
|
|
6897
|
+
function isDebounced(targetSession) {
|
|
6898
|
+
const state = readDebounceState();
|
|
6899
|
+
const lastSent = state[targetSession] ?? 0;
|
|
6900
|
+
return Date.now() - lastSent < INTERCOM_DEBOUNCE_MS;
|
|
6901
|
+
}
|
|
6902
|
+
function recordDebounce(targetSession) {
|
|
6903
|
+
const state = readDebounceState();
|
|
6904
|
+
state[targetSession] = Date.now();
|
|
6905
|
+
const cutoff = Date.now() - DEBOUNCE_CLEANUP_AGE_MS;
|
|
6906
|
+
for (const key of Object.keys(state)) {
|
|
6907
|
+
if ((state[key] ?? 0) < cutoff) delete state[key];
|
|
6908
|
+
}
|
|
6909
|
+
writeDebounceState(state);
|
|
6910
|
+
}
|
|
6911
|
+
function logIntercom(msg) {
|
|
6912
|
+
const line = `[${(/* @__PURE__ */ new Date()).toISOString()}] ${msg}
|
|
6913
|
+
`;
|
|
6914
|
+
process.stderr.write(`[intercom] ${msg}
|
|
6915
|
+
`);
|
|
6453
6916
|
try {
|
|
6454
|
-
|
|
6917
|
+
appendFileSync(INTERCOM_LOG2, line);
|
|
6455
6918
|
} catch {
|
|
6456
6919
|
}
|
|
6457
6920
|
}
|
|
6458
|
-
|
|
6459
|
-
|
|
6460
|
-
|
|
6461
|
-
|
|
6462
|
-
|
|
6463
|
-
|
|
6464
|
-
|
|
6465
|
-
|
|
6921
|
+
function getSessionState(sessionName) {
|
|
6922
|
+
const transport = getTransport();
|
|
6923
|
+
if (!transport.isAlive(sessionName)) return "offline";
|
|
6924
|
+
try {
|
|
6925
|
+
const pane = transport.capturePane(sessionName, 5);
|
|
6926
|
+
if (!pane.includes("\u276F") && !pane.includes("Claude Code") && !BUSY_PATTERN.test(pane) && !/Running…/.test(pane)) {
|
|
6927
|
+
if (/\$\s*$/.test(pane) || /% $/.test(pane.trimEnd())) {
|
|
6928
|
+
return "no_claude";
|
|
6929
|
+
}
|
|
6930
|
+
}
|
|
6931
|
+
if (/Running…/.test(pane)) return "tool";
|
|
6932
|
+
if (BUSY_PATTERN.test(pane)) return "thinking";
|
|
6933
|
+
return "idle";
|
|
6934
|
+
} catch {
|
|
6935
|
+
return "offline";
|
|
6466
6936
|
}
|
|
6467
|
-
});
|
|
6468
|
-
|
|
6469
|
-
// src/lib/behaviors.ts
|
|
6470
|
-
import crypto6 from "crypto";
|
|
6471
|
-
async function storeBehavior(opts) {
|
|
6472
|
-
const client = getClient();
|
|
6473
|
-
const id = crypto6.randomUUID();
|
|
6474
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
6475
|
-
await client.execute({
|
|
6476
|
-
sql: `INSERT INTO behaviors (id, agent_id, project_name, domain, priority, content, active, created_at, updated_at)
|
|
6477
|
-
VALUES (?, ?, ?, ?, ?, ?, 1, ?, ?)`,
|
|
6478
|
-
args: [id, opts.agentId, opts.projectName ?? null, opts.domain ?? null, opts.priority ?? "p1", opts.content, now, now]
|
|
6479
|
-
});
|
|
6480
|
-
return id;
|
|
6481
6937
|
}
|
|
6482
|
-
|
|
6483
|
-
|
|
6484
|
-
|
|
6485
|
-
|
|
6938
|
+
function isSessionBusy(sessionName) {
|
|
6939
|
+
const state = getSessionState(sessionName);
|
|
6940
|
+
return state === "thinking" || state === "tool";
|
|
6941
|
+
}
|
|
6942
|
+
function isExeSession(sessionName) {
|
|
6943
|
+
return /^exe\d*$/.test(sessionName);
|
|
6944
|
+
}
|
|
6945
|
+
function sendIntercom(targetSession) {
|
|
6946
|
+
const transport = getTransport();
|
|
6947
|
+
if (isExeSession(targetSession)) {
|
|
6948
|
+
logIntercom(`SKIP_EXE \u2192 ${targetSession} (exe sessions use prompt-submit hook)`);
|
|
6949
|
+
return "skipped_exe";
|
|
6486
6950
|
}
|
|
6487
|
-
|
|
6488
|
-
|
|
6489
|
-
|
|
6490
|
-
|
|
6491
|
-
|
|
6492
|
-
|
|
6493
|
-
|
|
6494
|
-
|
|
6495
|
-
|
|
6496
|
-
extractTrajectory: () => extractTrajectory,
|
|
6497
|
-
findSimilarTrajectories: () => findSimilarTrajectories,
|
|
6498
|
-
hashSignature: () => hashSignature,
|
|
6499
|
-
storeTrajectory: () => storeTrajectory,
|
|
6500
|
-
sweepTrajectories: () => sweepTrajectories
|
|
6501
|
-
});
|
|
6502
|
-
import crypto7 from "crypto";
|
|
6503
|
-
async function extractTrajectory(taskId, agentId) {
|
|
6504
|
-
const client = getClient();
|
|
6505
|
-
const result = await client.execute({
|
|
6506
|
-
sql: `SELECT tool_name, raw_text
|
|
6507
|
-
FROM memories
|
|
6508
|
-
WHERE task_id = ? AND agent_id = ?
|
|
6509
|
-
ORDER BY timestamp ASC`,
|
|
6510
|
-
args: [taskId, agentId]
|
|
6511
|
-
});
|
|
6512
|
-
if (result.rows.length === 0) return [];
|
|
6513
|
-
const rawTools = result.rows.map((r) => {
|
|
6514
|
-
const toolName = String(r.tool_name);
|
|
6515
|
-
if (toolName === "Bash") {
|
|
6516
|
-
const text = String(r.raw_text);
|
|
6517
|
-
const cmdMatch = text.match(/(?:command|Command).*?[:\s]+"?(\w+)/);
|
|
6518
|
-
return cmdMatch ? `Bash:${cmdMatch[1]}` : "Bash";
|
|
6951
|
+
if (isDebounced(targetSession)) {
|
|
6952
|
+
logIntercom(`DEBOUNCE \u2192 ${targetSession} (cross-process file debounce)`);
|
|
6953
|
+
return "debounced";
|
|
6954
|
+
}
|
|
6955
|
+
try {
|
|
6956
|
+
const sessions = transport.listSessions();
|
|
6957
|
+
if (!sessions.includes(targetSession)) {
|
|
6958
|
+
logIntercom(`SKIP \u2192 ${targetSession} (session not found)`);
|
|
6959
|
+
return "failed";
|
|
6519
6960
|
}
|
|
6520
|
-
|
|
6521
|
-
|
|
6522
|
-
|
|
6523
|
-
|
|
6524
|
-
|
|
6525
|
-
|
|
6961
|
+
const sessionState = getSessionState(targetSession);
|
|
6962
|
+
if (sessionState === "no_claude") {
|
|
6963
|
+
queueIntercom(targetSession, "claude not running in session");
|
|
6964
|
+
recordDebounce(targetSession);
|
|
6965
|
+
logIntercom(`QUEUED \u2192 ${targetSession} (no claude process \u2014 raw shell detected)`);
|
|
6966
|
+
return "queued";
|
|
6967
|
+
}
|
|
6968
|
+
if (sessionState === "thinking" || sessionState === "tool") {
|
|
6969
|
+
queueIntercom(targetSession, "session busy at send time");
|
|
6970
|
+
recordDebounce(targetSession);
|
|
6971
|
+
logIntercom(`QUEUED \u2192 ${targetSession} (session busy, will retry from queue)`);
|
|
6972
|
+
return "queued";
|
|
6973
|
+
}
|
|
6974
|
+
if (transport.isPaneInCopyMode(targetSession)) {
|
|
6975
|
+
logIntercom(`COPY_MODE \u2192 ${targetSession} (exiting copy mode first)`);
|
|
6976
|
+
transport.sendKeys(targetSession, "q");
|
|
6526
6977
|
}
|
|
6978
|
+
transport.sendKeys(targetSession, "/exe-intercom");
|
|
6979
|
+
recordDebounce(targetSession);
|
|
6980
|
+
logIntercom(`DELIVERED \u2192 ${targetSession} (fire-and-forget)`);
|
|
6981
|
+
return "delivered";
|
|
6982
|
+
} catch {
|
|
6983
|
+
logIntercom(`FAIL \u2192 ${targetSession}`);
|
|
6984
|
+
return "failed";
|
|
6527
6985
|
}
|
|
6528
|
-
return signature;
|
|
6529
6986
|
}
|
|
6530
|
-
function
|
|
6531
|
-
|
|
6532
|
-
|
|
6533
|
-
|
|
6534
|
-
|
|
6535
|
-
|
|
6536
|
-
|
|
6537
|
-
|
|
6538
|
-
|
|
6539
|
-
|
|
6540
|
-
|
|
6541
|
-
|
|
6542
|
-
|
|
6543
|
-
|
|
6544
|
-
|
|
6545
|
-
|
|
6546
|
-
|
|
6547
|
-
|
|
6548
|
-
|
|
6549
|
-
|
|
6550
|
-
|
|
6551
|
-
]
|
|
6552
|
-
});
|
|
6553
|
-
return id;
|
|
6987
|
+
function notifyParentExe(sessionKey) {
|
|
6988
|
+
const target = getDispatchedBy(sessionKey);
|
|
6989
|
+
if (!target) {
|
|
6990
|
+
process.stderr.write(`[intercom] notifyParentExe: no dispatcher found for key ${sessionKey}
|
|
6991
|
+
`);
|
|
6992
|
+
return false;
|
|
6993
|
+
}
|
|
6994
|
+
process.stderr.write(`[intercom] notifyParentExe \u2192 ${target}
|
|
6995
|
+
`);
|
|
6996
|
+
const result = sendIntercom(target);
|
|
6997
|
+
if (result === "failed") {
|
|
6998
|
+
const rootExe = resolveExeSession();
|
|
6999
|
+
if (rootExe && rootExe !== target) {
|
|
7000
|
+
process.stderr.write(`[intercom] notifyParentExe: dispatcher ${target} dead, falling back to root exe ${rootExe}
|
|
7001
|
+
`);
|
|
7002
|
+
const fallback = sendIntercom(rootExe);
|
|
7003
|
+
return fallback !== "failed";
|
|
7004
|
+
}
|
|
7005
|
+
return false;
|
|
7006
|
+
}
|
|
7007
|
+
return true;
|
|
6554
7008
|
}
|
|
6555
|
-
|
|
6556
|
-
|
|
6557
|
-
|
|
6558
|
-
|
|
6559
|
-
|
|
6560
|
-
|
|
6561
|
-
|
|
6562
|
-
|
|
6563
|
-
|
|
6564
|
-
args: [hash]
|
|
6565
|
-
});
|
|
6566
|
-
const mapRow = (r) => ({
|
|
6567
|
-
id: String(r.id),
|
|
6568
|
-
taskId: String(r.task_id),
|
|
6569
|
-
agentId: String(r.agent_id),
|
|
6570
|
-
projectName: String(r.project_name),
|
|
6571
|
-
taskTitle: String(r.task_title),
|
|
6572
|
-
signature: JSON.parse(String(r.signature)),
|
|
6573
|
-
signatureHash: String(r.signature_hash),
|
|
6574
|
-
toolCount: Number(r.tool_count),
|
|
6575
|
-
skillId: r.skill_id ? String(r.skill_id) : null,
|
|
6576
|
-
createdAt: String(r.created_at)
|
|
6577
|
-
});
|
|
6578
|
-
const matches = result.rows.map(mapRow);
|
|
6579
|
-
if (matches.length >= threshold) return matches;
|
|
6580
|
-
const nearResult = await client.execute({
|
|
6581
|
-
sql: `SELECT id, task_id, agent_id, project_name, task_title, signature, signature_hash, tool_count, skill_id, created_at
|
|
6582
|
-
FROM trajectories
|
|
6583
|
-
WHERE tool_count BETWEEN ? AND ?
|
|
6584
|
-
AND signature_hash != ?
|
|
6585
|
-
ORDER BY created_at DESC
|
|
6586
|
-
LIMIT 50`,
|
|
6587
|
-
args: [
|
|
6588
|
-
Math.max(1, signature.length - 3),
|
|
6589
|
-
signature.length + 3,
|
|
6590
|
-
hash
|
|
6591
|
-
]
|
|
6592
|
-
});
|
|
6593
|
-
for (const r of nearResult.rows) {
|
|
6594
|
-
const candidateSig = JSON.parse(String(r.signature));
|
|
6595
|
-
if (editDistance(signature, candidateSig) <= 2) {
|
|
6596
|
-
matches.push(mapRow(r));
|
|
7009
|
+
function ensureEmployee(employeeName, exeSession, projectDir, opts) {
|
|
7010
|
+
if (employeeName === "exe") {
|
|
7011
|
+
return { status: "failed", sessionName: "", error: "exe is the COO, not a dispatchable employee" };
|
|
7012
|
+
}
|
|
7013
|
+
try {
|
|
7014
|
+
assertEmployeeLimitSync();
|
|
7015
|
+
} catch (err) {
|
|
7016
|
+
if (err instanceof PlanLimitError) {
|
|
7017
|
+
return { status: "failed", sessionName: "", error: err.message };
|
|
6597
7018
|
}
|
|
6598
7019
|
}
|
|
6599
|
-
|
|
6600
|
-
|
|
6601
|
-
|
|
6602
|
-
|
|
6603
|
-
|
|
6604
|
-
|
|
6605
|
-
|
|
6606
|
-
const trajectoryId = await storeTrajectory({
|
|
6607
|
-
taskId: opts.taskId,
|
|
6608
|
-
agentId: opts.agentId,
|
|
6609
|
-
projectName: opts.projectName,
|
|
6610
|
-
taskTitle: opts.taskTitle,
|
|
6611
|
-
signature
|
|
6612
|
-
});
|
|
6613
|
-
const similar = await findSimilarTrajectories(
|
|
6614
|
-
signature,
|
|
6615
|
-
opts.skillThreshold ?? DEFAULT_SKILL_THRESHOLD
|
|
6616
|
-
);
|
|
6617
|
-
return { trajectoryId, similarCount: similar.length, similar };
|
|
6618
|
-
}
|
|
6619
|
-
function buildExtractionPrompt(trajectories) {
|
|
6620
|
-
const items = trajectories.map((t, i) => {
|
|
6621
|
-
const sig = t.signature.join(" \u2192 ");
|
|
6622
|
-
return `Task ${i + 1}: "${t.taskTitle}" (${t.agentId}, ${t.projectName}) \u2014 ${t.toolCount} tool calls
|
|
6623
|
-
Signature: ${sig}`;
|
|
6624
|
-
}).join("\n\n");
|
|
6625
|
-
return `You are analyzing ${trajectories.length} completed tasks that followed similar procedures:
|
|
6626
|
-
|
|
6627
|
-
${items}
|
|
6628
|
-
|
|
6629
|
-
Extract the reusable procedure. Format your response EXACTLY like this:
|
|
6630
|
-
|
|
6631
|
-
SKILL: {name \u2014 short, descriptive}
|
|
6632
|
-
TRIGGER: {when to use this \u2014 one sentence}
|
|
6633
|
-
STEPS:
|
|
6634
|
-
1. ...
|
|
6635
|
-
2. ...
|
|
6636
|
-
PITFALLS: {common mistakes to avoid}
|
|
6637
|
-
|
|
6638
|
-
Be specific and actionable. Include tool names, file patterns, and concrete commands where applicable.`;
|
|
6639
|
-
}
|
|
6640
|
-
async function extractSkill(trajectories, model) {
|
|
6641
|
-
if (trajectories.length === 0) return null;
|
|
6642
|
-
const config2 = await loadConfig();
|
|
6643
|
-
const skillModel = model ?? config2.skillModel;
|
|
6644
|
-
const Anthropic2 = (await import("@anthropic-ai/sdk")).default;
|
|
6645
|
-
const client = new Anthropic2();
|
|
6646
|
-
const prompt = buildExtractionPrompt(trajectories);
|
|
6647
|
-
const response = await client.messages.create({
|
|
6648
|
-
model: skillModel,
|
|
6649
|
-
max_tokens: 500,
|
|
6650
|
-
messages: [{ role: "user", content: prompt }]
|
|
6651
|
-
});
|
|
6652
|
-
const textBlock = response.content.find((b) => b.type === "text");
|
|
6653
|
-
const skillText = textBlock?.text;
|
|
6654
|
-
if (!skillText) return null;
|
|
6655
|
-
const agentId = trajectories[0].agentId;
|
|
6656
|
-
const projectName = trajectories[0].projectName;
|
|
6657
|
-
const skillId = await storeBehavior({
|
|
6658
|
-
agentId,
|
|
6659
|
-
content: skillText,
|
|
6660
|
-
domain: "skill",
|
|
6661
|
-
projectName
|
|
6662
|
-
});
|
|
6663
|
-
const dbClient = getClient();
|
|
6664
|
-
for (const t of trajectories) {
|
|
6665
|
-
await dbClient.execute({
|
|
6666
|
-
sql: "UPDATE trajectories SET skill_id = ? WHERE id = ?",
|
|
6667
|
-
args: [skillId, t.id]
|
|
6668
|
-
});
|
|
7020
|
+
if (/-exe\d*$/.test(employeeName)) {
|
|
7021
|
+
const bare = employeeName.replace(/-exe\d*$/, "").replace(/\d+$/, "");
|
|
7022
|
+
return {
|
|
7023
|
+
status: "failed",
|
|
7024
|
+
sessionName: "",
|
|
7025
|
+
error: `Error: pass employee name ('${bare}'), not session name ('${employeeName}')`
|
|
7026
|
+
};
|
|
6669
7027
|
}
|
|
6670
|
-
|
|
6671
|
-
|
|
7028
|
+
if (!/^exe\d+$/.test(exeSession)) {
|
|
7029
|
+
const root = extractRootExe(exeSession);
|
|
7030
|
+
if (root) {
|
|
7031
|
+
process.stderr.write(
|
|
7032
|
+
`[ensureEmployee] WARN: caller passed exeSession="${exeSession}" (not a root exe). Auto-correcting to "${root}".
|
|
6672
7033
|
`
|
|
6673
|
-
|
|
6674
|
-
|
|
7034
|
+
);
|
|
7035
|
+
exeSession = root;
|
|
7036
|
+
} else {
|
|
7037
|
+
return {
|
|
7038
|
+
status: "failed",
|
|
7039
|
+
sessionName: "",
|
|
7040
|
+
error: `Invalid exeSession "${exeSession}" \u2014 must be a root exe session (e.g., "exe1")`
|
|
7041
|
+
};
|
|
7042
|
+
}
|
|
7043
|
+
}
|
|
7044
|
+
let effectiveInstance = opts?.instance;
|
|
7045
|
+
if (effectiveInstance === void 0 && opts?.autoInstance) {
|
|
7046
|
+
const free = findFreeInstance(
|
|
7047
|
+
employeeName,
|
|
7048
|
+
exeSession,
|
|
7049
|
+
opts.maxAutoInstances ?? 10
|
|
7050
|
+
);
|
|
7051
|
+
if (free === null) {
|
|
7052
|
+
return {
|
|
7053
|
+
status: "failed",
|
|
7054
|
+
sessionName: employeeSessionName(employeeName, exeSession),
|
|
7055
|
+
error: `All ${opts.maxAutoInstances ?? 10} instances of ${employeeName} are alive \u2014 cap reached`
|
|
7056
|
+
};
|
|
7057
|
+
}
|
|
7058
|
+
effectiveInstance = free === 0 ? void 0 : free;
|
|
7059
|
+
}
|
|
7060
|
+
const sessionName = employeeSessionName(employeeName, exeSession, effectiveInstance);
|
|
7061
|
+
if (isEmployeeAlive(sessionName)) {
|
|
7062
|
+
const result2 = sendIntercom(sessionName);
|
|
7063
|
+
if (result2 === "acknowledged" || result2 === "skipped_exe" || result2 === "debounced" || result2 === "queued") {
|
|
7064
|
+
return { status: "intercom_sent", sessionName };
|
|
7065
|
+
}
|
|
7066
|
+
if (result2 === "delivered") {
|
|
7067
|
+
return { status: "intercom_unprocessed", sessionName };
|
|
7068
|
+
}
|
|
7069
|
+
return { status: "failed", sessionName, error: "intercom delivery failed" };
|
|
7070
|
+
}
|
|
7071
|
+
const spawnOpts = { ...opts, instance: effectiveInstance };
|
|
7072
|
+
const result = spawnEmployee(employeeName, exeSession, projectDir, spawnOpts);
|
|
7073
|
+
if (result.error) {
|
|
7074
|
+
return { status: "failed", sessionName, error: result.error };
|
|
7075
|
+
}
|
|
7076
|
+
return { status: "spawned", sessionName };
|
|
6675
7077
|
}
|
|
6676
|
-
|
|
7078
|
+
function spawnEmployee(employeeName, exeSession, projectDir, opts) {
|
|
7079
|
+
const transport = getTransport();
|
|
7080
|
+
const sessionName = employeeSessionName(employeeName, exeSession, opts?.instance);
|
|
7081
|
+
const instanceLabel = opts?.instance != null && opts.instance > 0 ? `${employeeName}${opts.instance}` : employeeName;
|
|
7082
|
+
const logDir = path17.join(os7.homedir(), ".exe-os", "session-logs");
|
|
7083
|
+
const logFile = path17.join(logDir, `${instanceLabel}-${Date.now()}.log`);
|
|
7084
|
+
if (!existsSync13(logDir)) {
|
|
7085
|
+
mkdirSync7(logDir, { recursive: true });
|
|
7086
|
+
}
|
|
7087
|
+
transport.kill(sessionName);
|
|
7088
|
+
let cleanupSuffix = "";
|
|
6677
7089
|
try {
|
|
6678
|
-
const
|
|
6679
|
-
|
|
6680
|
-
|
|
6681
|
-
|
|
6682
|
-
|
|
6683
|
-
|
|
6684
|
-
|
|
6685
|
-
|
|
6686
|
-
|
|
6687
|
-
|
|
6688
|
-
|
|
6689
|
-
|
|
6690
|
-
|
|
7090
|
+
const thisFile = fileURLToPath2(import.meta.url);
|
|
7091
|
+
const cleanupScript = path17.join(path17.dirname(thisFile), "..", "bin", "exe-session-cleanup.js");
|
|
7092
|
+
if (existsSync13(cleanupScript)) {
|
|
7093
|
+
cleanupSuffix = `; ${process.execPath} "${cleanupScript}" "${employeeName}" "${exeSession}"`;
|
|
7094
|
+
}
|
|
7095
|
+
} catch {
|
|
7096
|
+
}
|
|
7097
|
+
try {
|
|
7098
|
+
const claudeJsonPath = path17.join(os7.homedir(), ".claude.json");
|
|
7099
|
+
let claudeJson = {};
|
|
7100
|
+
try {
|
|
7101
|
+
claudeJson = JSON.parse(readFileSync11(claudeJsonPath, "utf8"));
|
|
7102
|
+
} catch {
|
|
7103
|
+
}
|
|
7104
|
+
if (!claudeJson.projects) claudeJson.projects = {};
|
|
7105
|
+
const projects = claudeJson.projects;
|
|
7106
|
+
const trustDir = opts?.cwd ?? projectDir;
|
|
7107
|
+
if (!projects[trustDir]) projects[trustDir] = {};
|
|
7108
|
+
projects[trustDir].hasTrustDialogAccepted = true;
|
|
7109
|
+
writeFileSync5(claudeJsonPath, JSON.stringify(claudeJson, null, 2) + "\n");
|
|
7110
|
+
} catch {
|
|
7111
|
+
}
|
|
7112
|
+
try {
|
|
7113
|
+
const settingsDir = path17.join(os7.homedir(), ".claude", "projects");
|
|
7114
|
+
const normalizedKey = (opts?.cwd ?? projectDir).replace(/\//g, "-").replace(/^-/, "");
|
|
7115
|
+
const projSettingsDir = path17.join(settingsDir, normalizedKey);
|
|
7116
|
+
const settingsPath = path17.join(projSettingsDir, "settings.json");
|
|
7117
|
+
let settings = {};
|
|
7118
|
+
try {
|
|
7119
|
+
settings = JSON.parse(readFileSync11(settingsPath, "utf8"));
|
|
7120
|
+
} catch {
|
|
7121
|
+
}
|
|
7122
|
+
const perms = settings.permissions ?? {};
|
|
7123
|
+
const allow = perms.allow ?? [];
|
|
7124
|
+
const toolNames = [
|
|
7125
|
+
"recall_my_memory",
|
|
7126
|
+
"store_memory",
|
|
7127
|
+
"create_task",
|
|
7128
|
+
"update_task",
|
|
7129
|
+
"list_tasks",
|
|
7130
|
+
"get_task",
|
|
7131
|
+
"ask_team_memory",
|
|
7132
|
+
"store_behavior",
|
|
7133
|
+
"get_identity",
|
|
7134
|
+
"send_message"
|
|
7135
|
+
];
|
|
7136
|
+
const requiredTools = expandDualPrefixTools(toolNames);
|
|
7137
|
+
let changed = false;
|
|
7138
|
+
for (const tool of requiredTools) {
|
|
7139
|
+
if (!allow.includes(tool)) {
|
|
7140
|
+
allow.push(tool);
|
|
7141
|
+
changed = true;
|
|
7142
|
+
}
|
|
7143
|
+
}
|
|
7144
|
+
if (changed) {
|
|
7145
|
+
perms.allow = allow;
|
|
7146
|
+
settings.permissions = perms;
|
|
7147
|
+
mkdirSync7(projSettingsDir, { recursive: true });
|
|
7148
|
+
writeFileSync5(settingsPath, JSON.stringify(settings, null, 2) + "\n");
|
|
7149
|
+
}
|
|
7150
|
+
} catch {
|
|
7151
|
+
}
|
|
7152
|
+
const spawnCwd = opts?.cwd ?? projectDir;
|
|
7153
|
+
const useExeAgent = !!(opts?.model && opts?.provider);
|
|
7154
|
+
const ccProvider = useExeAgent ? DEFAULT_PROVIDER : detectActiveProvider();
|
|
7155
|
+
const useBinSymlink = ccProvider !== DEFAULT_PROVIDER;
|
|
7156
|
+
let identityFlag = "";
|
|
7157
|
+
let behaviorsFlag = "";
|
|
7158
|
+
let legacyFallbackWarned = false;
|
|
7159
|
+
if (!useExeAgent && !useBinSymlink) {
|
|
7160
|
+
const identityPath = path17.join(
|
|
7161
|
+
os7.homedir(),
|
|
7162
|
+
".exe-os",
|
|
7163
|
+
"identity",
|
|
7164
|
+
`${employeeName}.md`
|
|
7165
|
+
);
|
|
7166
|
+
_resetCcAgentSupportCache();
|
|
7167
|
+
const hasAgentFlag = claudeSupportsAgentFlag();
|
|
7168
|
+
if (hasAgentFlag) {
|
|
7169
|
+
identityFlag = ` --agent ${employeeName}`;
|
|
7170
|
+
} else if (existsSync13(identityPath)) {
|
|
7171
|
+
identityFlag = ` --append-system-prompt-file ${identityPath}`;
|
|
7172
|
+
legacyFallbackWarned = true;
|
|
7173
|
+
}
|
|
7174
|
+
const behaviorsFile = exportBehaviorsSync(
|
|
7175
|
+
employeeName,
|
|
7176
|
+
path17.basename(spawnCwd),
|
|
7177
|
+
sessionName
|
|
7178
|
+
);
|
|
7179
|
+
if (behaviorsFile) {
|
|
7180
|
+
behaviorsFlag = ` --append-system-prompt-file ${behaviorsFile}`;
|
|
7181
|
+
}
|
|
7182
|
+
}
|
|
7183
|
+
if (legacyFallbackWarned) {
|
|
7184
|
+
process.stderr.write(
|
|
7185
|
+
`[tmux-routing] claude --agent not supported by installed CC. Falling back to --append-system-prompt-file for ${employeeName}. Upgrade Claude Code to enable native --agent launch.
|
|
6691
7186
|
`
|
|
6692
|
-
|
|
6693
|
-
|
|
7187
|
+
);
|
|
7188
|
+
}
|
|
7189
|
+
let sessionContextFlag = "";
|
|
7190
|
+
try {
|
|
7191
|
+
const ctxDir = path17.join(os7.homedir(), ".exe-os", "session-cache");
|
|
7192
|
+
mkdirSync7(ctxDir, { recursive: true });
|
|
7193
|
+
const ctxFile = path17.join(ctxDir, `session-context-${sessionName}.md`);
|
|
7194
|
+
const ctxContent = [
|
|
7195
|
+
`## Session Context`,
|
|
7196
|
+
`You are running in tmux session: ${sessionName}.`,
|
|
7197
|
+
`Your parent exe session is ${exeSession}.`,
|
|
7198
|
+
`Your employees (if any) use the -${exeSession} suffix (e.g., tom-${exeSession}).`
|
|
7199
|
+
].join("\n");
|
|
7200
|
+
writeFileSync5(ctxFile, ctxContent);
|
|
7201
|
+
sessionContextFlag = ` --append-system-prompt-file ${ctxFile}`;
|
|
7202
|
+
} catch {
|
|
7203
|
+
}
|
|
7204
|
+
let envPrefix = `EXE_SESSION=${exeSession} EXE_SESSION_NAME=${sessionName}`;
|
|
7205
|
+
if (ccProvider !== DEFAULT_PROVIDER) {
|
|
7206
|
+
const cfg = PROVIDER_TABLE[ccProvider];
|
|
7207
|
+
if (cfg?.apiKeyEnv) {
|
|
7208
|
+
const keyVal = process.env[cfg.apiKeyEnv];
|
|
7209
|
+
if (keyVal) {
|
|
7210
|
+
envPrefix = `${envPrefix} ${cfg.apiKeyEnv}=${keyVal}`;
|
|
6694
7211
|
}
|
|
6695
7212
|
}
|
|
6696
|
-
}
|
|
7213
|
+
}
|
|
7214
|
+
let spawnCommand;
|
|
7215
|
+
if (useExeAgent) {
|
|
7216
|
+
spawnCommand = `${envPrefix} exe-agent --employee ${employeeName} --model ${opts.model} --provider ${opts.provider}${cleanupSuffix}`;
|
|
7217
|
+
} else if (useBinSymlink) {
|
|
7218
|
+
const binName = `${employeeName}-${ccProvider}`;
|
|
6697
7219
|
process.stderr.write(
|
|
6698
|
-
`[
|
|
7220
|
+
`[tmux-routing] provider cascade: ${ccProvider} \u2192 spawning ${binName}
|
|
6699
7221
|
`
|
|
6700
7222
|
);
|
|
7223
|
+
spawnCommand = `${envPrefix} ${binName}${cleanupSuffix}`;
|
|
7224
|
+
} else {
|
|
7225
|
+
spawnCommand = `${envPrefix} claude --dangerously-skip-permissions${identityFlag}${behaviorsFlag}${sessionContextFlag}${cleanupSuffix}`;
|
|
6701
7226
|
}
|
|
6702
|
-
|
|
6703
|
-
|
|
6704
|
-
|
|
6705
|
-
if (!config2.skillLearning) return { clustersProcessed: 0, skillsExtracted: 0 };
|
|
6706
|
-
const t = threshold ?? config2.skillThreshold;
|
|
6707
|
-
const client = getClient();
|
|
6708
|
-
const result = await client.execute({
|
|
6709
|
-
sql: `SELECT signature_hash, COUNT(*) as cnt
|
|
6710
|
-
FROM trajectories
|
|
6711
|
-
WHERE skill_id IS NULL
|
|
6712
|
-
GROUP BY signature_hash
|
|
6713
|
-
HAVING cnt >= ?
|
|
6714
|
-
ORDER BY cnt DESC
|
|
6715
|
-
LIMIT 10`,
|
|
6716
|
-
args: [t]
|
|
7227
|
+
const spawnResult = transport.spawn(sessionName, {
|
|
7228
|
+
cwd: spawnCwd,
|
|
7229
|
+
command: spawnCommand
|
|
6717
7230
|
});
|
|
6718
|
-
|
|
6719
|
-
|
|
6720
|
-
|
|
6721
|
-
|
|
6722
|
-
|
|
6723
|
-
|
|
6724
|
-
|
|
6725
|
-
|
|
6726
|
-
|
|
6727
|
-
|
|
6728
|
-
|
|
6729
|
-
|
|
6730
|
-
|
|
6731
|
-
id: String(r.id),
|
|
6732
|
-
taskId: String(r.task_id),
|
|
6733
|
-
agentId: String(r.agent_id),
|
|
6734
|
-
projectName: String(r.project_name),
|
|
6735
|
-
taskTitle: String(r.task_title),
|
|
6736
|
-
signature: JSON.parse(String(r.signature)),
|
|
6737
|
-
signatureHash: String(r.signature_hash),
|
|
6738
|
-
toolCount: Number(r.tool_count),
|
|
6739
|
-
skillId: null,
|
|
6740
|
-
createdAt: String(r.created_at)
|
|
7231
|
+
if (spawnResult.error) {
|
|
7232
|
+
releaseSpawnLock2(sessionName);
|
|
7233
|
+
return { sessionName, error: `tmux new-session failed: ${spawnResult.error}` };
|
|
7234
|
+
}
|
|
7235
|
+
transport.pipeLog(sessionName, logFile);
|
|
7236
|
+
try {
|
|
7237
|
+
const mySession = getMySession();
|
|
7238
|
+
const dispatchInfo = path17.join(SESSION_CACHE, `dispatch-info-${sessionName}.json`);
|
|
7239
|
+
writeFileSync5(dispatchInfo, JSON.stringify({
|
|
7240
|
+
dispatchedBy: mySession,
|
|
7241
|
+
rootExe: exeSession,
|
|
7242
|
+
provider: useBinSymlink ? ccProvider : useExeAgent ? opts.provider : "anthropic",
|
|
7243
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
6741
7244
|
}));
|
|
6742
|
-
|
|
6743
|
-
|
|
6744
|
-
|
|
6745
|
-
|
|
7245
|
+
} catch {
|
|
7246
|
+
}
|
|
7247
|
+
let booted = false;
|
|
7248
|
+
for (let i = 0; i < 30; i++) {
|
|
7249
|
+
try {
|
|
7250
|
+
execSync6("sleep 0.5");
|
|
7251
|
+
} catch {
|
|
7252
|
+
}
|
|
7253
|
+
try {
|
|
7254
|
+
const pane = transport.capturePane(sessionName);
|
|
7255
|
+
if (useExeAgent) {
|
|
7256
|
+
if (pane.includes("[exe-agent]") || pane.includes("online")) {
|
|
7257
|
+
booted = true;
|
|
7258
|
+
break;
|
|
7259
|
+
}
|
|
7260
|
+
} else {
|
|
7261
|
+
if (pane.includes("Claude Code") || pane.includes("\u276F")) {
|
|
7262
|
+
booted = true;
|
|
7263
|
+
break;
|
|
7264
|
+
}
|
|
7265
|
+
}
|
|
7266
|
+
} catch {
|
|
6746
7267
|
}
|
|
6747
7268
|
}
|
|
6748
|
-
|
|
6749
|
-
|
|
6750
|
-
|
|
6751
|
-
|
|
6752
|
-
|
|
6753
|
-
|
|
6754
|
-
|
|
6755
|
-
|
|
6756
|
-
for (let i = 1; i <= m; i++) {
|
|
6757
|
-
for (let j = 1; j <= n; j++) {
|
|
6758
|
-
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
6759
|
-
dp[i][j] = Math.min(
|
|
6760
|
-
dp[i - 1][j] + 1,
|
|
6761
|
-
dp[i][j - 1] + 1,
|
|
6762
|
-
dp[i - 1][j - 1] + cost
|
|
6763
|
-
);
|
|
7269
|
+
if (!booted) {
|
|
7270
|
+
releaseSpawnLock2(sessionName);
|
|
7271
|
+
return { sessionName, error: `${useExeAgent ? "exe-agent" : "claude"} did not boot within 15s` };
|
|
7272
|
+
}
|
|
7273
|
+
if (!useExeAgent) {
|
|
7274
|
+
try {
|
|
7275
|
+
transport.sendKeys(sessionName, `/exe-call ${employeeName}`);
|
|
7276
|
+
} catch {
|
|
6764
7277
|
}
|
|
6765
7278
|
}
|
|
6766
|
-
|
|
7279
|
+
registerSession({
|
|
7280
|
+
windowName: sessionName,
|
|
7281
|
+
agentId: employeeName,
|
|
7282
|
+
projectDir: spawnCwd,
|
|
7283
|
+
parentExe: exeSession,
|
|
7284
|
+
pid: 0,
|
|
7285
|
+
registeredAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
7286
|
+
});
|
|
7287
|
+
releaseSpawnLock2(sessionName);
|
|
7288
|
+
return { sessionName };
|
|
6767
7289
|
}
|
|
6768
|
-
var
|
|
6769
|
-
var
|
|
6770
|
-
"src/lib/
|
|
7290
|
+
var SPAWN_LOCK_DIR, SESSION_CACHE, BEHAVIORS_EXPORT_TIMEOUT_MS, VALID_SESSION_NAME, VERIFY_PANE_LINES, INTERCOM_DEBOUNCE_MS, INTERCOM_LOG2, DEBOUNCE_FILE, DEBOUNCE_CLEANUP_AGE_MS, BUSY_PATTERN;
|
|
7291
|
+
var init_tmux_routing = __esm({
|
|
7292
|
+
"src/lib/tmux-routing.ts"() {
|
|
6771
7293
|
"use strict";
|
|
6772
|
-
|
|
6773
|
-
|
|
6774
|
-
|
|
6775
|
-
|
|
7294
|
+
init_session_registry();
|
|
7295
|
+
init_session_key();
|
|
7296
|
+
init_transport();
|
|
7297
|
+
init_cc_agent_support();
|
|
7298
|
+
init_mcp_prefix();
|
|
7299
|
+
init_provider_table();
|
|
7300
|
+
init_intercom_queue();
|
|
7301
|
+
init_plan_limits();
|
|
7302
|
+
SPAWN_LOCK_DIR = path17.join(os7.homedir(), ".exe-os", "spawn-locks");
|
|
7303
|
+
SESSION_CACHE = path17.join(os7.homedir(), ".exe-os", "session-cache");
|
|
7304
|
+
BEHAVIORS_EXPORT_TIMEOUT_MS = 1e4;
|
|
7305
|
+
VALID_SESSION_NAME = /^[a-z]+-exe\d+$|^[a-z]+\d+-exe\d+$/;
|
|
7306
|
+
VERIFY_PANE_LINES = 200;
|
|
7307
|
+
INTERCOM_DEBOUNCE_MS = 3e4;
|
|
7308
|
+
INTERCOM_LOG2 = path17.join(os7.homedir(), ".exe-os", "intercom.log");
|
|
7309
|
+
DEBOUNCE_FILE = path17.join(SESSION_CACHE, "intercom-debounce.json");
|
|
7310
|
+
DEBOUNCE_CLEANUP_AGE_MS = 5 * 60 * 1e3;
|
|
7311
|
+
BUSY_PATTERN = /[✻✽✶✳·].*…|Running…/;
|
|
6776
7312
|
}
|
|
6777
7313
|
});
|
|
6778
7314
|
|
|
6779
|
-
// src/lib/
|
|
6780
|
-
var
|
|
6781
|
-
__export(
|
|
6782
|
-
|
|
6783
|
-
|
|
6784
|
-
|
|
6785
|
-
|
|
6786
|
-
|
|
6787
|
-
|
|
6788
|
-
|
|
6789
|
-
|
|
6790
|
-
|
|
6791
|
-
|
|
6792
|
-
|
|
6793
|
-
|
|
6794
|
-
|
|
6795
|
-
slugify: () => slugify,
|
|
6796
|
-
updateTask: () => updateTask,
|
|
6797
|
-
updateTaskStatus: () => updateTaskStatus,
|
|
6798
|
-
writeCheckpoint: () => writeCheckpoint
|
|
7315
|
+
// src/lib/messaging.ts
|
|
7316
|
+
var messaging_exports = {};
|
|
7317
|
+
__export(messaging_exports, {
|
|
7318
|
+
deliverLocalMessage: () => deliverLocalMessage,
|
|
7319
|
+
getFailedMessages: () => getFailedMessages,
|
|
7320
|
+
getMessageStatus: () => getMessageStatus,
|
|
7321
|
+
getPendingMessages: () => getPendingMessages,
|
|
7322
|
+
getReadMessages: () => getReadMessages,
|
|
7323
|
+
getUnacknowledgedMessages: () => getUnacknowledgedMessages,
|
|
7324
|
+
markAcknowledged: () => markAcknowledged,
|
|
7325
|
+
markFailed: () => markFailed,
|
|
7326
|
+
markProcessed: () => markProcessed,
|
|
7327
|
+
markRead: () => markRead,
|
|
7328
|
+
retryPendingMessages: () => retryPendingMessages,
|
|
7329
|
+
sendMessage: () => sendMessage,
|
|
7330
|
+
setWsClientSend: () => setWsClientSend
|
|
6799
7331
|
});
|
|
6800
|
-
import
|
|
6801
|
-
|
|
6802
|
-
|
|
6803
|
-
const
|
|
6804
|
-
|
|
6805
|
-
|
|
6806
|
-
|
|
6807
|
-
|
|
6808
|
-
|
|
6809
|
-
|
|
6810
|
-
|
|
6811
|
-
|
|
7332
|
+
import crypto8 from "crypto";
|
|
7333
|
+
function generateUlid() {
|
|
7334
|
+
const timestamp = Date.now().toString(36).padStart(10, "0");
|
|
7335
|
+
const random = crypto8.randomBytes(10).toString("hex").slice(0, 16);
|
|
7336
|
+
return (timestamp + random).toUpperCase();
|
|
7337
|
+
}
|
|
7338
|
+
function rowToMessage(row) {
|
|
7339
|
+
return {
|
|
7340
|
+
id: row.id,
|
|
7341
|
+
fromAgent: row.from_agent,
|
|
7342
|
+
fromDevice: row.from_device,
|
|
7343
|
+
targetAgent: row.target_agent,
|
|
7344
|
+
targetProject: row.target_project ?? null,
|
|
7345
|
+
targetDevice: row.target_device,
|
|
7346
|
+
content: row.content,
|
|
7347
|
+
priority: row.priority ?? "normal",
|
|
7348
|
+
status: row.status ?? "pending",
|
|
7349
|
+
serverSeq: row.server_seq != null ? Number(row.server_seq) : null,
|
|
7350
|
+
retryCount: Number(row.retry_count ?? 0),
|
|
7351
|
+
createdAt: row.created_at,
|
|
7352
|
+
deliveredAt: row.delivered_at ?? null,
|
|
7353
|
+
processedAt: row.processed_at ?? null,
|
|
7354
|
+
failedAt: row.failed_at ?? null,
|
|
7355
|
+
failureReason: row.failure_reason ?? null
|
|
7356
|
+
};
|
|
7357
|
+
}
|
|
7358
|
+
async function sendMessage(input) {
|
|
7359
|
+
const client = getClient();
|
|
7360
|
+
const id = generateUlid();
|
|
7361
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
7362
|
+
const targetDevice = input.targetDevice ?? "local";
|
|
7363
|
+
await client.execute({
|
|
7364
|
+
sql: `INSERT INTO messages (id, from_agent, from_device, target_agent, target_project, target_device, content, priority, status, created_at)
|
|
7365
|
+
VALUES (?, ?, 'local', ?, ?, ?, ?, ?, 'pending', ?)`,
|
|
7366
|
+
args: [
|
|
7367
|
+
id,
|
|
7368
|
+
input.fromAgent,
|
|
7369
|
+
input.targetAgent,
|
|
7370
|
+
input.targetProject ?? null,
|
|
7371
|
+
targetDevice,
|
|
7372
|
+
input.content,
|
|
7373
|
+
input.priority ?? "normal",
|
|
7374
|
+
now
|
|
7375
|
+
]
|
|
7376
|
+
});
|
|
7377
|
+
try {
|
|
7378
|
+
if (targetDevice !== "local") {
|
|
7379
|
+
await deliverCrossMachineMessage(id, targetDevice);
|
|
7380
|
+
} else {
|
|
7381
|
+
await deliverLocalMessage(id);
|
|
7382
|
+
}
|
|
7383
|
+
} catch {
|
|
7384
|
+
}
|
|
7385
|
+
const result = await client.execute({
|
|
7386
|
+
sql: "SELECT * FROM messages WHERE id = ?",
|
|
7387
|
+
args: [id]
|
|
7388
|
+
});
|
|
7389
|
+
return rowToMessage(result.rows[0]);
|
|
7390
|
+
}
|
|
7391
|
+
function setWsClientSend(fn) {
|
|
7392
|
+
_wsClientSend = fn;
|
|
7393
|
+
}
|
|
7394
|
+
async function deliverCrossMachineMessage(messageId, targetDevice) {
|
|
7395
|
+
const client = getClient();
|
|
7396
|
+
const result = await client.execute({
|
|
7397
|
+
sql: "SELECT * FROM messages WHERE id = ?",
|
|
7398
|
+
args: [messageId]
|
|
7399
|
+
});
|
|
7400
|
+
if (result.rows.length === 0) return false;
|
|
7401
|
+
const msg = rowToMessage(result.rows[0]);
|
|
7402
|
+
if (msg.status !== "pending") return false;
|
|
7403
|
+
if (!_wsClientSend) {
|
|
7404
|
+
return false;
|
|
7405
|
+
}
|
|
7406
|
+
const payload = JSON.stringify({
|
|
7407
|
+
id: msg.id,
|
|
7408
|
+
fromAgent: msg.fromAgent,
|
|
7409
|
+
targetAgent: msg.targetAgent,
|
|
7410
|
+
targetProject: msg.targetProject,
|
|
7411
|
+
content: msg.content,
|
|
7412
|
+
priority: msg.priority,
|
|
7413
|
+
createdAt: msg.createdAt
|
|
7414
|
+
});
|
|
7415
|
+
const sent = _wsClientSend(targetDevice, payload);
|
|
7416
|
+
if (sent) {
|
|
7417
|
+
await client.execute({
|
|
7418
|
+
sql: "UPDATE messages SET status = 'synced' WHERE id = ?",
|
|
7419
|
+
args: [messageId]
|
|
6812
7420
|
});
|
|
7421
|
+
return true;
|
|
6813
7422
|
}
|
|
6814
|
-
return
|
|
7423
|
+
return false;
|
|
6815
7424
|
}
|
|
6816
|
-
async function
|
|
6817
|
-
const
|
|
7425
|
+
async function deliverLocalMessage(messageId) {
|
|
7426
|
+
const client = getClient();
|
|
7427
|
+
const result = await client.execute({
|
|
7428
|
+
sql: "SELECT * FROM messages WHERE id = ?",
|
|
7429
|
+
args: [messageId]
|
|
7430
|
+
});
|
|
7431
|
+
if (result.rows.length === 0) return false;
|
|
7432
|
+
const msg = rowToMessage(result.rows[0]);
|
|
7433
|
+
if (msg.status !== "pending") return false;
|
|
7434
|
+
const targetAgent = msg.targetAgent;
|
|
7435
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
6818
7436
|
try {
|
|
6819
|
-
const
|
|
6820
|
-
|
|
6821
|
-
|
|
6822
|
-
|
|
6823
|
-
|
|
6824
|
-
|
|
6825
|
-
|
|
6826
|
-
try {
|
|
6827
|
-
unlinkSync5(cachePath);
|
|
6828
|
-
} catch {
|
|
6829
|
-
}
|
|
7437
|
+
const exeSession = resolveExeSession();
|
|
7438
|
+
if (!exeSession) {
|
|
7439
|
+
throw new Error("No exe session found");
|
|
7440
|
+
}
|
|
7441
|
+
const ensureResult = ensureEmployee(targetAgent, exeSession, process.cwd());
|
|
7442
|
+
if (ensureResult.status === "failed") {
|
|
7443
|
+
throw new Error(ensureResult.error ?? "ensureEmployee failed");
|
|
6830
7444
|
}
|
|
7445
|
+
await client.execute({
|
|
7446
|
+
sql: "UPDATE messages SET status = 'delivered', delivered_at = ? WHERE id = ?",
|
|
7447
|
+
args: [now, messageId]
|
|
7448
|
+
});
|
|
7449
|
+
return true;
|
|
6831
7450
|
} catch {
|
|
6832
|
-
|
|
6833
|
-
|
|
6834
|
-
|
|
6835
|
-
|
|
6836
|
-
if (input.status === "done" || input.status === "cancelled") {
|
|
6837
|
-
try {
|
|
6838
|
-
const client = getClient();
|
|
6839
|
-
const taskTitle = String(row.title);
|
|
6840
|
-
const escaped = taskTitle.replace(/%/g, "\\%").replace(/_/g, "\\_");
|
|
7451
|
+
const newRetryCount = msg.retryCount + 1;
|
|
7452
|
+
if (newRetryCount >= MAX_RETRIES2) {
|
|
7453
|
+
await markFailed(messageId, "session unavailable after 10 retries");
|
|
7454
|
+
} else {
|
|
6841
7455
|
await client.execute({
|
|
6842
|
-
sql:
|
|
6843
|
-
|
|
6844
|
-
args: [now, `%left '${escaped}' as in\\_progress%`]
|
|
6845
|
-
});
|
|
6846
|
-
} catch {
|
|
6847
|
-
}
|
|
6848
|
-
try {
|
|
6849
|
-
const client = getClient();
|
|
6850
|
-
const cascaded = await client.execute({
|
|
6851
|
-
sql: `UPDATE tasks SET status = 'done', updated_at = ?
|
|
6852
|
-
WHERE parent_task_id = ? AND status = 'needs_review'`,
|
|
6853
|
-
args: [now, taskId]
|
|
7456
|
+
sql: "UPDATE messages SET retry_count = ? WHERE id = ?",
|
|
7457
|
+
args: [newRetryCount, messageId]
|
|
6854
7458
|
});
|
|
6855
|
-
if (cascaded.rowsAffected > 0) {
|
|
6856
|
-
process.stderr.write(
|
|
6857
|
-
`[cascade] Closed ${cascaded.rowsAffected} orphaned review task(s) for parent ${taskId}
|
|
6858
|
-
`
|
|
6859
|
-
);
|
|
6860
|
-
}
|
|
6861
|
-
} catch {
|
|
6862
|
-
}
|
|
6863
|
-
}
|
|
6864
|
-
const isTerminal = input.status === "done" || input.status === "needs_review";
|
|
6865
|
-
if (isTerminal) {
|
|
6866
|
-
const isExe = String(row.assigned_to) === "exe";
|
|
6867
|
-
if (!isExe) {
|
|
6868
|
-
notifyTaskDone();
|
|
6869
|
-
}
|
|
6870
|
-
await markTaskNotificationsRead(taskFile);
|
|
6871
|
-
if (input.status === "done") {
|
|
6872
|
-
try {
|
|
6873
|
-
await cascadeUnblock(taskId, input.baseDir, now);
|
|
6874
|
-
} catch {
|
|
6875
|
-
}
|
|
6876
|
-
if (row.parent_task_id) {
|
|
6877
|
-
try {
|
|
6878
|
-
await checkSubtaskCompletion(String(row.parent_task_id), String(row.project_name));
|
|
6879
|
-
} catch {
|
|
6880
|
-
}
|
|
6881
|
-
}
|
|
6882
|
-
}
|
|
6883
|
-
}
|
|
6884
|
-
if (input.status === "done" && String(row.assigned_to) !== "exe" && !process.env.VITEST) {
|
|
6885
|
-
Promise.resolve().then(() => (init_skill_learning(), skill_learning_exports)).then(
|
|
6886
|
-
({ captureAndLearn: captureAndLearn2 }) => captureAndLearn2({
|
|
6887
|
-
taskId,
|
|
6888
|
-
agentId: String(row.assigned_to),
|
|
6889
|
-
projectName: String(row.project_name),
|
|
6890
|
-
taskTitle: String(row.title)
|
|
6891
|
-
})
|
|
6892
|
-
).catch((err) => {
|
|
6893
|
-
process.stderr.write(
|
|
6894
|
-
`[updateTask] skill learning failed: ${err instanceof Error ? err.message : String(err)}
|
|
6895
|
-
`
|
|
6896
|
-
);
|
|
6897
|
-
});
|
|
6898
|
-
}
|
|
6899
|
-
let nextTask;
|
|
6900
|
-
if (isTerminal && String(row.assigned_to) !== "exe") {
|
|
6901
|
-
try {
|
|
6902
|
-
nextTask = await findNextTask(String(row.assigned_to));
|
|
6903
|
-
} catch {
|
|
6904
7459
|
}
|
|
7460
|
+
return false;
|
|
6905
7461
|
}
|
|
6906
|
-
return {
|
|
6907
|
-
id: String(row.id),
|
|
6908
|
-
title: String(row.title),
|
|
6909
|
-
assignedTo: String(row.assigned_to),
|
|
6910
|
-
assignedBy: String(row.assigned_by),
|
|
6911
|
-
projectName: String(row.project_name),
|
|
6912
|
-
priority: String(row.priority),
|
|
6913
|
-
status: input.status,
|
|
6914
|
-
taskFile,
|
|
6915
|
-
createdAt: String(row.created_at),
|
|
6916
|
-
updatedAt: now,
|
|
6917
|
-
budgetTokens: row.budget_tokens !== void 0 && row.budget_tokens !== null ? Number(row.budget_tokens) : null,
|
|
6918
|
-
budgetFallbackModel: row.budget_fallback_model !== void 0 && row.budget_fallback_model !== null ? String(row.budget_fallback_model) : null,
|
|
6919
|
-
tokensUsed: Number(row.tokens_used ?? 0),
|
|
6920
|
-
tokensWarnedAt: row.tokens_warned_at !== void 0 && row.tokens_warned_at !== null ? Number(row.tokens_warned_at) : null,
|
|
6921
|
-
nextTask
|
|
6922
|
-
};
|
|
6923
7462
|
}
|
|
6924
|
-
async function
|
|
7463
|
+
async function getPendingMessages(targetAgent) {
|
|
7464
|
+
const client = getClient();
|
|
7465
|
+
const result = await client.execute({
|
|
7466
|
+
sql: `SELECT * FROM messages
|
|
7467
|
+
WHERE target_agent = ? AND status IN ('pending', 'delivered')
|
|
7468
|
+
ORDER BY id`,
|
|
7469
|
+
args: [targetAgent]
|
|
7470
|
+
});
|
|
7471
|
+
return result.rows.map((row) => rowToMessage(row));
|
|
7472
|
+
}
|
|
7473
|
+
async function markRead(messageId) {
|
|
6925
7474
|
const client = getClient();
|
|
6926
|
-
const { taskFile, assignedTo, assignedBy, taskSlug } = await deleteTaskCore(taskId, baseDir);
|
|
6927
|
-
const reviewer = assignedBy || "exe";
|
|
6928
|
-
const reviewSlug = `review-${assignedTo}-${taskSlug}`;
|
|
6929
|
-
const reviewFile = `exe/${reviewer}/${reviewSlug}.md`;
|
|
6930
7475
|
await client.execute({
|
|
6931
|
-
sql: "
|
|
6932
|
-
args: [
|
|
7476
|
+
sql: "UPDATE messages SET status = 'read' WHERE id = ? AND status IN ('pending', 'delivered')",
|
|
7477
|
+
args: [messageId]
|
|
6933
7478
|
});
|
|
6934
|
-
await markAsReadByTaskFile(taskFile);
|
|
6935
|
-
await markAsReadByTaskFile(reviewFile);
|
|
6936
7479
|
}
|
|
6937
|
-
|
|
6938
|
-
|
|
7480
|
+
async function markAcknowledged(messageId) {
|
|
7481
|
+
const client = getClient();
|
|
7482
|
+
await client.execute({
|
|
7483
|
+
sql: "UPDATE messages SET status = 'acknowledged', processed_at = ? WHERE id = ? AND status = 'read'",
|
|
7484
|
+
args: [(/* @__PURE__ */ new Date()).toISOString(), messageId]
|
|
7485
|
+
});
|
|
7486
|
+
}
|
|
7487
|
+
async function markProcessed(messageId) {
|
|
7488
|
+
const client = getClient();
|
|
7489
|
+
await client.execute({
|
|
7490
|
+
sql: "UPDATE messages SET status = 'processed', processed_at = ? WHERE id = ?",
|
|
7491
|
+
args: [(/* @__PURE__ */ new Date()).toISOString(), messageId]
|
|
7492
|
+
});
|
|
7493
|
+
}
|
|
7494
|
+
async function getMessageStatus(messageId) {
|
|
7495
|
+
const client = getClient();
|
|
7496
|
+
const result = await client.execute({
|
|
7497
|
+
sql: "SELECT status FROM messages WHERE id = ?",
|
|
7498
|
+
args: [messageId]
|
|
7499
|
+
});
|
|
7500
|
+
return result.rows[0]?.status ?? null;
|
|
7501
|
+
}
|
|
7502
|
+
async function getUnacknowledgedMessages(targetAgent) {
|
|
7503
|
+
const client = getClient();
|
|
7504
|
+
const result = await client.execute({
|
|
7505
|
+
sql: `SELECT * FROM messages
|
|
7506
|
+
WHERE target_agent = ? AND status IN ('pending', 'delivered', 'read')
|
|
7507
|
+
ORDER BY id`,
|
|
7508
|
+
args: [targetAgent]
|
|
7509
|
+
});
|
|
7510
|
+
return result.rows.map((row) => rowToMessage(row));
|
|
7511
|
+
}
|
|
7512
|
+
async function getReadMessages(targetAgent) {
|
|
7513
|
+
const client = getClient();
|
|
7514
|
+
const result = await client.execute({
|
|
7515
|
+
sql: "SELECT * FROM messages WHERE target_agent = ? AND status = 'read' ORDER BY id",
|
|
7516
|
+
args: [targetAgent]
|
|
7517
|
+
});
|
|
7518
|
+
return result.rows.map((row) => rowToMessage(row));
|
|
7519
|
+
}
|
|
7520
|
+
async function markFailed(messageId, reason) {
|
|
7521
|
+
const client = getClient();
|
|
7522
|
+
await client.execute({
|
|
7523
|
+
sql: "UPDATE messages SET status = 'failed', failed_at = ?, failure_reason = ? WHERE id = ?",
|
|
7524
|
+
args: [(/* @__PURE__ */ new Date()).toISOString(), reason, messageId]
|
|
7525
|
+
});
|
|
7526
|
+
}
|
|
7527
|
+
async function getFailedMessages() {
|
|
7528
|
+
const client = getClient();
|
|
7529
|
+
const result = await client.execute({
|
|
7530
|
+
sql: "SELECT * FROM messages WHERE status = 'failed' ORDER BY created_at DESC",
|
|
7531
|
+
args: []
|
|
7532
|
+
});
|
|
7533
|
+
return result.rows.map((row) => rowToMessage(row));
|
|
7534
|
+
}
|
|
7535
|
+
async function retryPendingMessages() {
|
|
7536
|
+
const client = getClient();
|
|
7537
|
+
const result = await client.execute({
|
|
7538
|
+
sql: `SELECT * FROM messages
|
|
7539
|
+
WHERE status = 'pending' AND retry_count < ?
|
|
7540
|
+
ORDER BY id`,
|
|
7541
|
+
args: [MAX_RETRIES2]
|
|
7542
|
+
});
|
|
7543
|
+
let delivered = 0;
|
|
7544
|
+
for (const row of result.rows) {
|
|
7545
|
+
const msg = rowToMessage(row);
|
|
7546
|
+
try {
|
|
7547
|
+
const success = await deliverLocalMessage(msg.id);
|
|
7548
|
+
if (success) delivered++;
|
|
7549
|
+
} catch {
|
|
7550
|
+
}
|
|
7551
|
+
}
|
|
7552
|
+
return delivered;
|
|
7553
|
+
}
|
|
7554
|
+
var MAX_RETRIES2, _wsClientSend;
|
|
7555
|
+
var init_messaging = __esm({
|
|
7556
|
+
"src/lib/messaging.ts"() {
|
|
6939
7557
|
"use strict";
|
|
6940
7558
|
init_database();
|
|
6941
|
-
|
|
6942
|
-
|
|
6943
|
-
|
|
6944
|
-
init_tasks_review();
|
|
6945
|
-
init_tasks_crud();
|
|
6946
|
-
init_tasks_chain();
|
|
6947
|
-
init_tasks_review();
|
|
6948
|
-
init_tasks_notify();
|
|
7559
|
+
init_tmux_routing();
|
|
7560
|
+
MAX_RETRIES2 = 10;
|
|
7561
|
+
_wsClientSend = null;
|
|
6949
7562
|
}
|
|
6950
7563
|
});
|
|
6951
7564
|
|
|
6952
7565
|
// src/automation/trigger-engine.ts
|
|
6953
7566
|
import { readFileSync as readFileSync12, writeFileSync as writeFileSync6, existsSync as existsSync14, mkdirSync as mkdirSync8 } from "fs";
|
|
6954
|
-
import { randomUUID as
|
|
7567
|
+
import { randomUUID as randomUUID8 } from "crypto";
|
|
6955
7568
|
import path18 from "path";
|
|
6956
7569
|
import os8 from "os";
|
|
6957
7570
|
function substituteTemplate(template, record) {
|
|
@@ -7496,6 +8109,9 @@ function sendJson(res, status, data) {
|
|
|
7496
8109
|
res.end(JSON.stringify(data));
|
|
7497
8110
|
}
|
|
7498
8111
|
|
|
8112
|
+
// src/gateway/gateway.ts
|
|
8113
|
+
init_state_bus();
|
|
8114
|
+
|
|
7499
8115
|
// src/gateway/router.ts
|
|
7500
8116
|
function matchesPlatform(msgPlatform, matchPlatform) {
|
|
7501
8117
|
if (!matchPlatform) return true;
|
|
@@ -7794,6 +8410,13 @@ var Gateway = class {
|
|
|
7794
8410
|
console.log(
|
|
7795
8411
|
`[gateway] ${msg.platform}/${msg.senderId} \u2192 ${route.employee} (${route.routeName})`
|
|
7796
8412
|
);
|
|
8413
|
+
orgBus.emit({
|
|
8414
|
+
type: "gateway_message",
|
|
8415
|
+
platform: msg.platform,
|
|
8416
|
+
senderId: msg.senderId,
|
|
8417
|
+
botId: route.employee,
|
|
8418
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
8419
|
+
});
|
|
7797
8420
|
const bot = this.botRegistry.get(route.employee);
|
|
7798
8421
|
if (!bot) {
|
|
7799
8422
|
console.error(`[gateway] No bot registered for target: ${route.employee}`);
|