@stackmemoryai/stackmemory 1.10.5 → 1.14.0
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/LICENSE +131 -64
- package/README.md +107 -24
- package/bin/claude-sm +16 -1
- package/bin/claude-smd +16 -1
- package/bin/codex-smd +16 -1
- package/bin/gemini-sm +16 -1
- package/bin/hermes-sm +21 -0
- package/bin/hermes-smd +21 -0
- package/bin/opencode-sm +16 -1
- package/dist/src/cli/claude-sm.js +266 -84
- package/dist/src/cli/codex-sm.js +225 -33
- package/dist/src/cli/commands/bench.js +209 -2
- package/dist/src/cli/commands/brain.js +206 -0
- package/dist/src/cli/commands/cache.js +126 -0
- package/dist/src/cli/commands/company-os.js +184 -0
- package/dist/src/cli/commands/context.js +5 -0
- package/dist/src/cli/commands/daemon.js +41 -0
- package/dist/src/cli/commands/handoff.js +40 -9
- package/dist/src/cli/commands/onboard.js +70 -3
- package/dist/src/cli/commands/operator.js +127 -0
- package/dist/src/cli/commands/optimize.js +117 -0
- package/dist/src/cli/commands/orchestrate.js +232 -5
- package/dist/src/cli/commands/orchestrator.js +315 -26
- package/dist/src/cli/commands/pack.js +322 -0
- package/dist/src/cli/commands/patterns.js +254 -0
- package/dist/src/cli/commands/portal.js +161 -0
- package/dist/src/cli/commands/scaffold.js +92 -0
- package/dist/src/cli/commands/search.js +40 -1
- package/dist/src/cli/commands/setup.js +178 -11
- package/dist/src/cli/commands/skills.js +10 -1
- package/dist/src/cli/commands/state.js +265 -0
- package/dist/src/cli/commands/sync.js +253 -0
- package/dist/src/cli/commands/tasks.js +130 -1
- package/dist/src/cli/commands/vision.js +221 -0
- package/dist/src/cli/gemini-sm.js +19 -29
- package/dist/src/cli/hermes-sm.js +224 -0
- package/dist/src/cli/index.js +105 -39
- package/dist/src/cli/opencode-sm.js +38 -21
- package/dist/src/cli/utils/determinism-watcher.js +66 -0
- package/dist/src/cli/utils/real-cli-bin.js +116 -0
- package/dist/src/core/brain/brain-store.js +187 -0
- package/dist/src/core/brain/brain-sync.js +193 -0
- package/dist/src/core/brain/index.js +78 -0
- package/dist/src/core/brain/types.js +10 -0
- package/dist/src/core/cache/content-cache.js +238 -0
- package/dist/src/{integrations/diffmem → core/cache}/index.js +5 -5
- package/dist/src/core/cache/token-estimator.js +39 -0
- package/dist/src/core/config/feature-flags.js +2 -6
- package/dist/src/core/context/frame-database.js +79 -27
- package/dist/src/core/context/recursive-context-manager.js +1 -1
- package/dist/src/core/context/rehydration.js +2 -1
- package/dist/src/core/cross-search/cross-project-search.js +269 -0
- package/dist/src/core/cross-search/index.js +10 -0
- package/dist/src/core/database/sqlite-adapter.js +14 -84
- package/dist/src/core/extensions/provider-adapter.js +5 -0
- package/dist/src/core/models/model-router.js +54 -2
- package/dist/src/core/models/provider-pricing.js +58 -4
- package/dist/src/core/monitoring/logger.js +2 -1
- package/dist/src/core/optimization/trace-optimizer.js +413 -0
- package/dist/src/core/patterns/index.js +22 -0
- package/dist/src/core/patterns/pattern-applier.js +39 -0
- package/dist/src/core/patterns/pattern-observer.js +157 -0
- package/dist/src/core/patterns/pattern-store.js +259 -0
- package/dist/src/core/patterns/types.js +19 -0
- package/dist/src/core/provenance/confidence-scorer.js +128 -0
- package/dist/src/core/provenance/index.js +40 -0
- package/dist/src/core/provenance/provenance-store.js +194 -0
- package/dist/src/core/provenance/types.js +82 -0
- package/dist/src/core/retrieval/llm-context-retrieval.js +5 -4
- package/dist/src/core/retrieval/unified-context-assembler.js +11 -66
- package/dist/src/core/session/project-handoff.js +64 -0
- package/dist/src/core/session/session-manager.js +28 -0
- package/dist/src/core/shared-state/canonical-store.js +564 -0
- package/dist/src/core/skill-packs/index.js +18 -0
- package/dist/src/core/skill-packs/parser.js +42 -0
- package/dist/src/core/skill-packs/registry.js +224 -0
- package/dist/src/core/skill-packs/types.js +79 -0
- package/dist/src/core/storage/cloud-sync-manager.js +116 -0
- package/dist/src/core/storage/cloud-sync.js +574 -0
- package/dist/src/core/storage/two-tier-storage.js +5 -1
- package/dist/src/core/tasks/master-tasks-template.js +43 -0
- package/dist/src/core/tasks/md-task-parser.js +138 -0
- package/dist/src/core/trace/trace-event-store.js +282 -0
- package/dist/src/core/vision/index.js +27 -0
- package/dist/src/core/vision/signals.js +79 -0
- package/dist/src/core/vision/types.js +22 -0
- package/dist/src/core/vision/vision-file.js +146 -0
- package/dist/src/core/vision/vision-loop.js +220 -0
- package/dist/src/core/wiki/wiki-compiler.js +103 -1
- package/dist/src/daemon/daemon-config.js +52 -0
- package/dist/src/daemon/services/desire-path-service.js +566 -0
- package/dist/src/daemon/services/github-service.js +126 -0
- package/dist/src/daemon/services/research-stream-service.js +320 -0
- package/dist/src/daemon/services/telemetry-service.js +192 -0
- package/dist/src/daemon/unified-daemon.js +58 -1
- package/dist/src/features/browser/cli-browser-agent.js +417 -0
- package/dist/src/features/browser/stagehand-workflows.js +578 -0
- package/dist/src/features/operator/adapter-factory.js +62 -0
- package/dist/src/features/operator/browser-adapter.js +109 -0
- package/dist/src/features/operator/desktop-adapter.js +125 -0
- package/dist/src/features/operator/index.js +39 -0
- package/dist/src/features/operator/llm-decision.js +137 -0
- package/dist/src/features/operator/operator-logger.js +92 -0
- package/dist/src/features/operator/overnight-runner.js +327 -0
- package/dist/src/features/operator/screen-adapter.js +91 -0
- package/dist/src/features/operator/session-manager.js +127 -0
- package/dist/src/features/operator/state-machine.js +227 -0
- package/dist/src/features/operator/task-queue.js +81 -0
- package/dist/src/features/portal/index.js +26 -0
- package/dist/src/features/portal/server.js +240 -0
- package/dist/src/features/portal/types.js +14 -0
- package/dist/src/features/portal/ui.js +195 -0
- package/dist/src/features/sweep/pty-wrapper.js +13 -5
- package/dist/src/features/tasks/task-aware-context.js +2 -1
- package/dist/src/features/tui/simple-monitor.js +0 -23
- package/dist/src/features/tui/swarm-monitor.js +8 -66
- package/dist/src/features/web/client/hooks/use-socket.js +12 -0
- package/dist/src/{core/merge/index.js → features/web/client/lib/utils.js} +8 -4
- package/dist/src/features/web/client/next-env.d.js +4 -0
- package/dist/src/features/web/client/stores/session-store.js +12 -0
- package/dist/src/features/web/server/gcp-billing.js +76 -0
- package/dist/src/features/web/server/index.js +10 -0
- package/dist/src/features/web/server/spend-calculator.js +228 -0
- package/dist/src/hooks/schemas.js +6 -1
- package/dist/src/integrations/anthropic/client.js +3 -2
- package/dist/src/integrations/claude-code/agent-bridge.js +0 -3
- package/dist/src/integrations/claude-code/subagent-client.js +307 -11
- package/dist/src/integrations/claude-code/task-coordinator.js +2 -1
- package/dist/src/integrations/github/pr-state.js +158 -0
- package/dist/src/integrations/linear/client.js +4 -1
- package/dist/src/integrations/linear/webhook-retry.js +196 -0
- package/dist/src/integrations/linear/webhook-server.js +18 -22
- package/dist/src/integrations/mcp/handlers/cloud-sync-handlers.js +101 -0
- package/dist/src/integrations/mcp/handlers/index.js +40 -84
- package/dist/src/integrations/mcp/server.js +542 -641
- package/dist/src/integrations/mcp/tool-alias-registry.js +297 -0
- package/dist/src/integrations/mcp/tool-definitions.js +152 -682
- package/dist/src/mcp/stackmemory-mcp-server.js +571 -231
- package/dist/src/orchestrators/multimodal/determinism.js +244 -0
- package/dist/src/orchestrators/multimodal/harness.js +149 -78
- package/dist/src/orchestrators/multimodal/providers.js +44 -3
- package/dist/src/skills/recursive-agent-orchestrator.js +2 -4
- package/dist/src/utils/hook-installer.js +0 -8
- package/dist/src/utils/process-cleanup.js +1 -7
- package/docs/README.md +42 -0
- package/docs/guides/README_INSTALL.md +208 -0
- package/package.json +27 -9
- package/packs/coding/python-fastapi/instructions.md +60 -0
- package/packs/coding/python-fastapi/pack.yaml +28 -0
- package/packs/coding/typescript-react/instructions.md +47 -0
- package/packs/coding/typescript-react/pack.yaml +28 -0
- package/packs/core/commands/capture.md +32 -0
- package/packs/core/commands/learn.md +73 -0
- package/packs/core/commands/next.md +36 -0
- package/packs/core/commands/restart.md +58 -0
- package/packs/core/commands/restore.md +29 -0
- package/packs/core/commands/start.md +57 -0
- package/packs/core/commands/stop.md +65 -0
- package/packs/core/commands/summary.md +40 -0
- package/packs/core/manifest.json +24 -0
- package/packs/ops/decision-recovery/instructions.md +65 -0
- package/packs/ops/decision-recovery/pack.yaml +89 -0
- package/scripts/claude-code-wrapper.sh +11 -0
- package/scripts/claude-sm-setup.sh +12 -1
- package/scripts/codex-wrapper.sh +11 -0
- package/scripts/git-hooks/branch-context-manager.sh +11 -0
- package/scripts/git-hooks/post-checkout-stackmemory.sh +11 -0
- package/scripts/git-hooks/post-commit-stackmemory.sh +11 -0
- package/scripts/git-hooks/pre-commit-stackmemory.sh +11 -0
- package/scripts/hooks/cleanup-shell.sh +12 -1
- package/scripts/hooks/task-complete.sh +12 -1
- package/scripts/install-code-execution-hooks.sh +12 -1
- package/scripts/install-sweep-hook.sh +12 -0
- package/scripts/install.sh +11 -0
- package/scripts/opencode-wrapper.sh +11 -0
- package/scripts/portal/cloud-init.yaml +69 -0
- package/scripts/portal/setup.sh +69 -0
- package/scripts/portal/stackmemory-portal.service +34 -0
- package/scripts/setup-claude-integration.sh +12 -1
- package/scripts/smoke-init-db.sh +23 -0
- package/scripts/stackmemory-daemon.sh +11 -0
- package/scripts/verify-dist.cjs +11 -4
- package/dist/src/cli/commands/ralph.js +0 -1053
- package/dist/src/cli/commands/team.js +0 -168
- package/dist/src/core/context/shared-context-layer.js +0 -620
- package/dist/src/core/context/stack-merge-resolver.js +0 -748
- package/dist/src/core/merge/conflict-detector.js +0 -430
- package/dist/src/core/merge/resolution-engine.js +0 -557
- package/dist/src/core/merge/stack-diff.js +0 -531
- package/dist/src/core/merge/unified-merge-resolver.js +0 -302
- package/dist/src/hooks/diffmem-hooks.js +0 -376
- package/dist/src/integrations/diffmem/client.js +0 -208
- package/dist/src/integrations/diffmem/config.js +0 -14
- package/dist/src/integrations/greptile/client.js +0 -101
- package/dist/src/integrations/greptile/config.js +0 -14
- package/dist/src/integrations/greptile/index.js +0 -11
- package/dist/src/integrations/mcp/handlers/cord-handlers.js +0 -397
- package/dist/src/integrations/mcp/handlers/diffmem-handlers.js +0 -455
- package/dist/src/integrations/mcp/handlers/greptile-handlers.js +0 -456
- package/dist/src/integrations/mcp/handlers/provider-handlers.js +0 -227
- package/dist/src/integrations/mcp/handlers/team-handlers.js +0 -211
- package/dist/src/integrations/ralph/bridge/ralph-stackmemory-bridge.js +0 -863
- package/dist/src/integrations/ralph/context/context-budget-manager.js +0 -308
- package/dist/src/integrations/ralph/context/stackmemory-context-loader.js +0 -391
- package/dist/src/integrations/ralph/index.js +0 -17
- package/dist/src/integrations/ralph/learning/pattern-learner.js +0 -435
- package/dist/src/integrations/ralph/lifecycle/iteration-lifecycle.js +0 -448
- package/dist/src/integrations/ralph/loopmax.js +0 -488
- package/dist/src/integrations/ralph/monitoring/swarm-dashboard.js +0 -293
- package/dist/src/integrations/ralph/monitoring/swarm-registry.js +0 -107
- package/dist/src/integrations/ralph/orchestration/multi-loop-orchestrator.js +0 -508
- package/dist/src/integrations/ralph/patterns/compounding-engineering-pattern.js +0 -407
- package/dist/src/integrations/ralph/patterns/extended-coherence-sessions.js +0 -495
- package/dist/src/integrations/ralph/patterns/oracle-worker-pattern.js +0 -387
- package/dist/src/integrations/ralph/performance/performance-optimizer.js +0 -357
- package/dist/src/integrations/ralph/recovery/crash-recovery.js +0 -461
- package/dist/src/integrations/ralph/state/state-reconciler.js +0 -420
- package/dist/src/integrations/ralph/swarm/git-workflow-manager.js +0 -444
- package/dist/src/integrations/ralph/swarm/swarm-coordinator.js +0 -1007
- package/dist/src/integrations/ralph/visualization/ralph-debugger.js +0 -635
- package/scripts/ralph-loop-implementation.js +0 -404
- /package/dist/src/core/{merge → cache}/types.js +0 -0
- /package/dist/src/{integrations/diffmem/types.js → core/storage/cloud-sync-types.js} +0 -0
- /package/dist/src/{integrations/greptile/types.js → core/trace/trace-event.js} +0 -0
- /package/dist/src/{integrations/ralph → features/operator}/types.js +0 -0
|
@@ -1,397 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath as __fileURLToPath } from 'url';
|
|
2
|
-
import { dirname as __pathDirname } from 'path';
|
|
3
|
-
const __filename = __fileURLToPath(import.meta.url);
|
|
4
|
-
const __dirname = __pathDirname(__filename);
|
|
5
|
-
import { randomUUID } from "crypto";
|
|
6
|
-
import { logger } from "../../../core/monitoring/logger.js";
|
|
7
|
-
class CordHandlers {
|
|
8
|
-
constructor(deps) {
|
|
9
|
-
this.deps = deps;
|
|
10
|
-
}
|
|
11
|
-
MAX_DEPTH = 10;
|
|
12
|
-
MAX_TASKS = 50;
|
|
13
|
-
/**
|
|
14
|
-
* cord_spawn — create a child task with clean context (only blocker results visible)
|
|
15
|
-
*/
|
|
16
|
-
async handleCordSpawn(args) {
|
|
17
|
-
return this.createTask(args, "spawn");
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* cord_fork — create a child task with full sibling context
|
|
21
|
-
*/
|
|
22
|
-
async handleCordFork(args) {
|
|
23
|
-
return this.createTask(args, "fork");
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* cord_complete — mark a task as completed and unblock dependents
|
|
27
|
-
*/
|
|
28
|
-
async handleCordComplete(args) {
|
|
29
|
-
try {
|
|
30
|
-
const { task_id, result } = args;
|
|
31
|
-
if (!task_id) throw new Error("task_id is required");
|
|
32
|
-
if (result === void 0 || result === null) {
|
|
33
|
-
throw new Error("result is required");
|
|
34
|
-
}
|
|
35
|
-
const db = this.deps.dbAdapter.getRawDatabase();
|
|
36
|
-
if (!db) throw new Error("Database not available");
|
|
37
|
-
const task = db.prepare("SELECT * FROM cord_tasks WHERE task_id = ?").get(task_id);
|
|
38
|
-
if (!task) throw new Error(`Task not found: ${task_id}`);
|
|
39
|
-
if (task.status === "completed") {
|
|
40
|
-
throw new Error(`Task already completed: ${task_id}`);
|
|
41
|
-
}
|
|
42
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
43
|
-
db.prepare(
|
|
44
|
-
"UPDATE cord_tasks SET status = ?, result = ?, completed_at = ? WHERE task_id = ?"
|
|
45
|
-
).run("completed", String(result), now, task_id);
|
|
46
|
-
const unblocked = this.checkAndUnblockDependents(db, task_id);
|
|
47
|
-
logger.info("Cord task completed", { task_id, unblocked });
|
|
48
|
-
return {
|
|
49
|
-
content: [
|
|
50
|
-
{
|
|
51
|
-
type: "text",
|
|
52
|
-
text: `Task ${task_id} completed.${unblocked.length > 0 ? ` Unblocked: ${unblocked.join(", ")}` : ""}`
|
|
53
|
-
}
|
|
54
|
-
],
|
|
55
|
-
metadata: { task_id, status: "completed", unblocked }
|
|
56
|
-
};
|
|
57
|
-
} catch (error) {
|
|
58
|
-
logger.error(
|
|
59
|
-
"Error completing cord task",
|
|
60
|
-
error instanceof Error ? error : new Error(String(error))
|
|
61
|
-
);
|
|
62
|
-
throw error;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* cord_ask — create an "ask" task (question with optional options)
|
|
67
|
-
*/
|
|
68
|
-
async handleCordAsk(args) {
|
|
69
|
-
try {
|
|
70
|
-
const { question, options, parent_id } = args;
|
|
71
|
-
if (!question) throw new Error("question is required");
|
|
72
|
-
const db = this.deps.dbAdapter.getRawDatabase();
|
|
73
|
-
if (!db) throw new Error("Database not available");
|
|
74
|
-
const projectId = this.getProjectId();
|
|
75
|
-
const runId = this.getRunId();
|
|
76
|
-
const taskId = randomUUID();
|
|
77
|
-
this.checkTaskLimit(db, projectId);
|
|
78
|
-
let depth = 0;
|
|
79
|
-
if (parent_id) {
|
|
80
|
-
depth = this.computeDepth(db, parent_id);
|
|
81
|
-
}
|
|
82
|
-
const prompt = options ? JSON.stringify({ question, options }) : JSON.stringify({ question });
|
|
83
|
-
db.prepare(
|
|
84
|
-
`INSERT INTO cord_tasks (task_id, parent_id, project_id, run_id, goal, prompt, status, context_mode, depth)
|
|
85
|
-
VALUES (?, ?, ?, ?, ?, ?, 'asked', 'ask', ?)`
|
|
86
|
-
).run(
|
|
87
|
-
taskId,
|
|
88
|
-
parent_id || null,
|
|
89
|
-
projectId,
|
|
90
|
-
runId,
|
|
91
|
-
question,
|
|
92
|
-
prompt,
|
|
93
|
-
depth
|
|
94
|
-
);
|
|
95
|
-
logger.info("Cord ask created", { task_id: taskId });
|
|
96
|
-
return {
|
|
97
|
-
content: [
|
|
98
|
-
{
|
|
99
|
-
type: "text",
|
|
100
|
-
text: `Ask created: ${taskId} \u2014 "${question}"`
|
|
101
|
-
}
|
|
102
|
-
],
|
|
103
|
-
metadata: {
|
|
104
|
-
task_id: taskId,
|
|
105
|
-
status: "asked",
|
|
106
|
-
context_mode: "ask",
|
|
107
|
-
question,
|
|
108
|
-
options: options || null
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
} catch (error) {
|
|
112
|
-
logger.error(
|
|
113
|
-
"Error creating cord ask",
|
|
114
|
-
error instanceof Error ? error : new Error(String(error))
|
|
115
|
-
);
|
|
116
|
-
throw error;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* cord_tree — view the task tree with context scoping
|
|
121
|
-
*/
|
|
122
|
-
async handleCordTree(args) {
|
|
123
|
-
try {
|
|
124
|
-
const { task_id, include_results = false } = args;
|
|
125
|
-
const db = this.deps.dbAdapter.getRawDatabase();
|
|
126
|
-
if (!db) throw new Error("Database not available");
|
|
127
|
-
const projectId = this.getProjectId();
|
|
128
|
-
let tasks;
|
|
129
|
-
if (task_id) {
|
|
130
|
-
tasks = this.getSubtree(db, task_id);
|
|
131
|
-
} else {
|
|
132
|
-
tasks = db.prepare(
|
|
133
|
-
"SELECT * FROM cord_tasks WHERE project_id = ? ORDER BY depth ASC, created_at ASC"
|
|
134
|
-
).all(projectId);
|
|
135
|
-
}
|
|
136
|
-
if (tasks.length === 0) {
|
|
137
|
-
return {
|
|
138
|
-
content: [{ type: "text", text: "No cord tasks found." }],
|
|
139
|
-
metadata: { tasks: [] }
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
const taskMap = /* @__PURE__ */ new Map();
|
|
143
|
-
for (const t of tasks) taskMap.set(t.task_id, t);
|
|
144
|
-
const allTasks = db.prepare(
|
|
145
|
-
"SELECT * FROM cord_tasks WHERE project_id = ? ORDER BY depth ASC, created_at ASC"
|
|
146
|
-
).all(projectId);
|
|
147
|
-
const allTaskMap = /* @__PURE__ */ new Map();
|
|
148
|
-
for (const t of allTasks) allTaskMap.set(t.task_id, t);
|
|
149
|
-
const treeNodes = tasks.map((t) => {
|
|
150
|
-
const blockedBy = JSON.parse(t.blocked_by);
|
|
151
|
-
const node = {
|
|
152
|
-
task_id: t.task_id,
|
|
153
|
-
goal: t.goal,
|
|
154
|
-
status: t.status,
|
|
155
|
-
context_mode: t.context_mode,
|
|
156
|
-
depth: t.depth,
|
|
157
|
-
blocked_by: blockedBy,
|
|
158
|
-
parent_id: t.parent_id
|
|
159
|
-
};
|
|
160
|
-
if (include_results && t.result !== null) {
|
|
161
|
-
node.result = t.result;
|
|
162
|
-
}
|
|
163
|
-
node.visible_context = this.computeVisibleContext(
|
|
164
|
-
t,
|
|
165
|
-
allTaskMap,
|
|
166
|
-
include_results
|
|
167
|
-
);
|
|
168
|
-
return node;
|
|
169
|
-
});
|
|
170
|
-
const summary = tasks.map(
|
|
171
|
-
(t) => `${" ".repeat(t.depth)}[${t.status}] ${t.goal}${t.context_mode === "ask" ? " (ask)" : ""}`
|
|
172
|
-
).join("\n");
|
|
173
|
-
return {
|
|
174
|
-
content: [
|
|
175
|
-
{
|
|
176
|
-
type: "text",
|
|
177
|
-
text: `Cord Tree (${tasks.length} tasks):
|
|
178
|
-
${summary}`
|
|
179
|
-
}
|
|
180
|
-
],
|
|
181
|
-
metadata: { tasks: treeNodes }
|
|
182
|
-
};
|
|
183
|
-
} catch (error) {
|
|
184
|
-
logger.error(
|
|
185
|
-
"Error getting cord tree",
|
|
186
|
-
error instanceof Error ? error : new Error(String(error))
|
|
187
|
-
);
|
|
188
|
-
throw error;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
// --- Private helpers ---
|
|
192
|
-
async createTask(args, contextMode) {
|
|
193
|
-
try {
|
|
194
|
-
const { goal, prompt = "", blocked_by = [], parent_id } = args;
|
|
195
|
-
if (!goal) throw new Error("goal is required");
|
|
196
|
-
const db = this.deps.dbAdapter.getRawDatabase();
|
|
197
|
-
if (!db) throw new Error("Database not available");
|
|
198
|
-
const projectId = this.getProjectId();
|
|
199
|
-
const runId = this.getRunId();
|
|
200
|
-
const taskId = randomUUID();
|
|
201
|
-
this.checkTaskLimit(db, projectId);
|
|
202
|
-
let depth = 0;
|
|
203
|
-
if (parent_id) {
|
|
204
|
-
depth = this.computeDepth(db, parent_id);
|
|
205
|
-
}
|
|
206
|
-
const blockerIds = Array.isArray(blocked_by) ? blocked_by : [];
|
|
207
|
-
if (blockerIds.length > 0) {
|
|
208
|
-
this.validateBlockers(db, blockerIds);
|
|
209
|
-
this.detectCircularDeps(db, taskId, blockerIds);
|
|
210
|
-
}
|
|
211
|
-
const status = this.initialStatus(db, blockerIds);
|
|
212
|
-
db.prepare(
|
|
213
|
-
`INSERT INTO cord_tasks (task_id, parent_id, project_id, run_id, goal, prompt, status, context_mode, blocked_by, depth)
|
|
214
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
215
|
-
).run(
|
|
216
|
-
taskId,
|
|
217
|
-
parent_id || null,
|
|
218
|
-
projectId,
|
|
219
|
-
runId,
|
|
220
|
-
goal,
|
|
221
|
-
prompt,
|
|
222
|
-
status,
|
|
223
|
-
contextMode,
|
|
224
|
-
JSON.stringify(blockerIds),
|
|
225
|
-
depth
|
|
226
|
-
);
|
|
227
|
-
logger.info("Cord task created", {
|
|
228
|
-
task_id: taskId,
|
|
229
|
-
context_mode: contextMode,
|
|
230
|
-
status
|
|
231
|
-
});
|
|
232
|
-
return {
|
|
233
|
-
content: [
|
|
234
|
-
{
|
|
235
|
-
type: "text",
|
|
236
|
-
text: `Task ${taskId} created (${contextMode}, ${status}): ${goal}`
|
|
237
|
-
}
|
|
238
|
-
],
|
|
239
|
-
metadata: {
|
|
240
|
-
task_id: taskId,
|
|
241
|
-
status,
|
|
242
|
-
context_mode: contextMode,
|
|
243
|
-
depth,
|
|
244
|
-
blocked_by: blockerIds
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
} catch (error) {
|
|
248
|
-
logger.error(
|
|
249
|
-
"Error creating cord task",
|
|
250
|
-
error instanceof Error ? error : new Error(String(error))
|
|
251
|
-
);
|
|
252
|
-
throw error;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
checkTaskLimit(db, projectId) {
|
|
256
|
-
const row = db.prepare("SELECT COUNT(*) as count FROM cord_tasks WHERE project_id = ?").get(projectId);
|
|
257
|
-
if (row.count >= this.MAX_TASKS) {
|
|
258
|
-
throw new Error(
|
|
259
|
-
`Task limit reached: ${this.MAX_TASKS} tasks per project`
|
|
260
|
-
);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
computeDepth(db, parentId) {
|
|
264
|
-
const parent = db.prepare("SELECT depth FROM cord_tasks WHERE task_id = ?").get(parentId);
|
|
265
|
-
if (!parent) throw new Error(`Parent task not found: ${parentId}`);
|
|
266
|
-
const depth = parent.depth + 1;
|
|
267
|
-
if (depth >= this.MAX_DEPTH) {
|
|
268
|
-
throw new Error(`Max depth exceeded: ${this.MAX_DEPTH}`);
|
|
269
|
-
}
|
|
270
|
-
return depth;
|
|
271
|
-
}
|
|
272
|
-
validateBlockers(db, blockerIds) {
|
|
273
|
-
for (const id of blockerIds) {
|
|
274
|
-
const exists = db.prepare("SELECT 1 FROM cord_tasks WHERE task_id = ?").get(id);
|
|
275
|
-
if (!exists) throw new Error(`Blocker task not found: ${id}`);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
detectCircularDeps(db, newTaskId, blockerIds) {
|
|
279
|
-
const visited = /* @__PURE__ */ new Set();
|
|
280
|
-
const queue = [...blockerIds];
|
|
281
|
-
while (queue.length > 0) {
|
|
282
|
-
const current = queue.shift();
|
|
283
|
-
if (current === newTaskId) {
|
|
284
|
-
throw new Error("Circular dependency detected");
|
|
285
|
-
}
|
|
286
|
-
if (visited.has(current)) continue;
|
|
287
|
-
visited.add(current);
|
|
288
|
-
const task = db.prepare("SELECT blocked_by FROM cord_tasks WHERE task_id = ?").get(current);
|
|
289
|
-
if (task) {
|
|
290
|
-
const deps = JSON.parse(task.blocked_by);
|
|
291
|
-
for (const dep of deps) {
|
|
292
|
-
if (!visited.has(dep)) queue.push(dep);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
initialStatus(db, blockerIds) {
|
|
298
|
-
if (blockerIds.length === 0) return "active";
|
|
299
|
-
for (const id of blockerIds) {
|
|
300
|
-
const task = db.prepare("SELECT status FROM cord_tasks WHERE task_id = ?").get(id);
|
|
301
|
-
if (!task || task.status !== "completed") return "blocked";
|
|
302
|
-
}
|
|
303
|
-
return "active";
|
|
304
|
-
}
|
|
305
|
-
checkAndUnblockDependents(db, completedTaskId) {
|
|
306
|
-
const allBlocked = db.prepare("SELECT * FROM cord_tasks WHERE status = 'blocked'").all();
|
|
307
|
-
const unblocked = [];
|
|
308
|
-
for (const task of allBlocked) {
|
|
309
|
-
const blockers = JSON.parse(task.blocked_by);
|
|
310
|
-
if (!blockers.includes(completedTaskId)) continue;
|
|
311
|
-
const allDone = blockers.every((bid) => {
|
|
312
|
-
if (bid === completedTaskId) return true;
|
|
313
|
-
const blocker = db.prepare("SELECT status FROM cord_tasks WHERE task_id = ?").get(bid);
|
|
314
|
-
return blocker?.status === "completed";
|
|
315
|
-
});
|
|
316
|
-
if (allDone) {
|
|
317
|
-
db.prepare(
|
|
318
|
-
"UPDATE cord_tasks SET status = 'active' WHERE task_id = ?"
|
|
319
|
-
).run(task.task_id);
|
|
320
|
-
unblocked.push(task.task_id);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
return unblocked;
|
|
324
|
-
}
|
|
325
|
-
getSubtree(db, rootId) {
|
|
326
|
-
const result = [];
|
|
327
|
-
const queue = [rootId];
|
|
328
|
-
while (queue.length > 0) {
|
|
329
|
-
const current = queue.shift();
|
|
330
|
-
const task = db.prepare("SELECT * FROM cord_tasks WHERE task_id = ?").get(current);
|
|
331
|
-
if (task) {
|
|
332
|
-
result.push(task);
|
|
333
|
-
const children = db.prepare(
|
|
334
|
-
"SELECT task_id FROM cord_tasks WHERE parent_id = ? ORDER BY created_at ASC"
|
|
335
|
-
).all(current);
|
|
336
|
-
for (const c of children) queue.push(c.task_id);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return result;
|
|
340
|
-
}
|
|
341
|
-
computeVisibleContext(task, allTaskMap, includeResults) {
|
|
342
|
-
const ctx = { prompt: task.prompt };
|
|
343
|
-
if (task.context_mode === "ask") {
|
|
344
|
-
try {
|
|
345
|
-
const parsed = JSON.parse(task.prompt);
|
|
346
|
-
ctx.question = parsed.question;
|
|
347
|
-
ctx.options = parsed.options || null;
|
|
348
|
-
} catch {
|
|
349
|
-
ctx.question = task.goal;
|
|
350
|
-
}
|
|
351
|
-
if (task.status === "completed" && task.result !== null) {
|
|
352
|
-
ctx.answer = task.result;
|
|
353
|
-
}
|
|
354
|
-
return ctx;
|
|
355
|
-
}
|
|
356
|
-
const blockerIds = JSON.parse(task.blocked_by);
|
|
357
|
-
const blockerResults = [];
|
|
358
|
-
for (const bid of blockerIds) {
|
|
359
|
-
const blocker = allTaskMap.get(bid);
|
|
360
|
-
if (blocker?.status === "completed" && blocker.result !== null) {
|
|
361
|
-
blockerResults.push({
|
|
362
|
-
task_id: bid,
|
|
363
|
-
goal: blocker.goal,
|
|
364
|
-
result: includeResults ? blocker.result : "[completed]"
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
if (blockerResults.length > 0) {
|
|
369
|
-
ctx.blocker_results = blockerResults;
|
|
370
|
-
}
|
|
371
|
-
if (task.context_mode === "fork" && task.parent_id) {
|
|
372
|
-
const siblingResults = [];
|
|
373
|
-
for (const [, t] of allTaskMap) {
|
|
374
|
-
if (t.parent_id === task.parent_id && t.task_id !== task.task_id && t.status === "completed" && t.result !== null) {
|
|
375
|
-
siblingResults.push({
|
|
376
|
-
task_id: t.task_id,
|
|
377
|
-
goal: t.goal,
|
|
378
|
-
result: includeResults ? t.result : "[completed]"
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
if (siblingResults.length > 0) {
|
|
383
|
-
ctx.sibling_results = siblingResults;
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
return ctx;
|
|
387
|
-
}
|
|
388
|
-
getProjectId() {
|
|
389
|
-
return this.deps.dbAdapter.projectId;
|
|
390
|
-
}
|
|
391
|
-
getRunId() {
|
|
392
|
-
return this.deps.frameManager.currentRunId;
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
export {
|
|
396
|
-
CordHandlers
|
|
397
|
-
};
|