@stackmemoryai/stackmemory 0.3.17 → 0.3.18
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/cli/commands/skills.js +15 -2
- package/dist/cli/commands/skills.js.map +2 -2
- package/dist/cli/index.js +113 -834
- package/dist/cli/index.js.map +3 -3
- package/dist/core/context/dual-stack-manager.js +1 -1
- package/dist/core/context/dual-stack-manager.js.map +1 -1
- package/dist/core/context/frame-manager.js +3 -0
- package/dist/core/context/frame-manager.js.map +2 -2
- package/dist/integrations/claude-code/subagent-client.js +106 -3
- package/dist/integrations/claude-code/subagent-client.js.map +2 -2
- package/dist/servers/railway/config.js +51 -0
- package/dist/servers/railway/config.js.map +7 -0
- package/dist/servers/railway/index-enhanced.js +156 -0
- package/dist/servers/railway/index-enhanced.js.map +7 -0
- package/dist/servers/railway/minimal.js +48 -3
- package/dist/servers/railway/minimal.js.map +2 -2
- package/dist/servers/railway/storage-test.js +455 -0
- package/dist/servers/railway/storage-test.js.map +7 -0
- package/dist/skills/claude-skills.js +13 -12
- package/dist/skills/claude-skills.js.map +2 -2
- package/dist/skills/recursive-agent-orchestrator.js +27 -18
- package/dist/skills/recursive-agent-orchestrator.js.map +2 -2
- package/dist/skills/unified-rlm-orchestrator.js.map +2 -2
- package/package.json +6 -18
- package/scripts/README-TESTING.md +186 -0
- package/scripts/analyze-cli-security.js +288 -0
- package/scripts/archive/add-phase-tasks-to-linear.js +163 -0
- package/scripts/archive/analyze-linear-duplicates.js +214 -0
- package/scripts/archive/analyze-remaining-duplicates.js +230 -0
- package/scripts/archive/analyze-sta-duplicates.js +292 -0
- package/scripts/archive/analyze-sta-graphql.js +399 -0
- package/scripts/archive/cancel-duplicate-tasks.ts +246 -0
- package/scripts/archive/check-all-duplicates.ts +419 -0
- package/scripts/archive/clean-duplicate-tasks.js +114 -0
- package/scripts/archive/cleanup-duplicate-tasks.ts +286 -0
- package/scripts/archive/create-phase-tasks.js +387 -0
- package/scripts/archive/delete-linear-duplicates.js +182 -0
- package/scripts/archive/delete-remaining-duplicates.js +158 -0
- package/scripts/archive/delete-sta-duplicates.js +201 -0
- package/scripts/archive/delete-sta-oauth.js +201 -0
- package/scripts/archive/export-sta-tasks.js +62 -0
- package/scripts/archive/install-auto-sync.js +266 -0
- package/scripts/archive/install-chromadb-hooks.sh +133 -0
- package/scripts/archive/install-enhanced-clear-hooks.sh +431 -0
- package/scripts/archive/install-post-task-hooks.sh +289 -0
- package/scripts/archive/install-stackmemory-hooks.sh +420 -0
- package/scripts/archive/merge-linear-duplicates-safe.ts +362 -0
- package/scripts/archive/merge-linear-duplicates.ts +180 -0
- package/scripts/archive/remove-sta-tasks.js +70 -0
- package/scripts/archive/setup-background-sync.sh +168 -0
- package/scripts/archive/setup-claude-auto-triggers.sh +181 -0
- package/scripts/archive/setup-claude-autostart.sh +305 -0
- package/scripts/archive/setup-git-hooks.sh +25 -0
- package/scripts/archive/setup-linear-oauth.sh +46 -0
- package/scripts/archive/setup-mcp.sh +113 -0
- package/scripts/archive/setup-railway-deployment.sh +81 -0
- package/scripts/auto-handoff.sh +262 -0
- package/scripts/background-sync-manager.js +416 -0
- package/scripts/benchmark-performance.ts +57 -0
- package/scripts/check-redis.ts +48 -0
- package/scripts/chromadb-auto-loader.sh +128 -0
- package/scripts/chromadb-context-loader.js +479 -0
- package/scripts/claude-chromadb-hook.js +460 -0
- package/scripts/claude-code-wrapper.sh +66 -0
- package/scripts/claude-linear-skill.js +455 -0
- package/scripts/claude-pre-commit.sh +302 -0
- package/scripts/claude-sm-autostart.js +532 -0
- package/scripts/claude-sm-setup.sh +367 -0
- package/scripts/claude-with-chromadb.sh +69 -0
- package/scripts/claude-worktree-manager.sh +323 -0
- package/scripts/claude-worktree-monitor.sh +371 -0
- package/scripts/claude-worktree-setup.sh +327 -0
- package/scripts/clean-linear-backlog.js +273 -0
- package/scripts/cleanup-old-sessions.sh +57 -0
- package/scripts/codex-wrapper.sh +88 -0
- package/scripts/create-sandbox.sh +269 -0
- package/scripts/debug-linear-update.js +174 -0
- package/scripts/delete-linear-tasks.js +167 -0
- package/scripts/deploy.sh +89 -0
- package/scripts/deployment/railway.sh +352 -0
- package/scripts/deployment/test-deployment.js +194 -0
- package/scripts/detect-and-rehydrate.js +162 -0
- package/scripts/detect-and-rehydrate.mjs +165 -0
- package/scripts/development/create-demo-tasks.js +143 -0
- package/scripts/development/debug-frame-test.js +16 -0
- package/scripts/development/demo-auto-sync.js +128 -0
- package/scripts/development/fix-all-imports.js +213 -0
- package/scripts/development/fix-imports.js +229 -0
- package/scripts/development/fix-lint-loop.cjs +103 -0
- package/scripts/development/fix-project-id.ts +161 -0
- package/scripts/development/fix-strict-mode-issues.ts +291 -0
- package/scripts/development/reorganize-structure.sh +228 -0
- package/scripts/development/test-persistence-direct.js +148 -0
- package/scripts/development/test-persistence.js +114 -0
- package/scripts/development/test-tasks.js +93 -0
- package/scripts/development/update-imports.js +212 -0
- package/scripts/fetch-linear-status.js +125 -0
- package/scripts/git-hooks/README.md +310 -0
- package/scripts/git-hooks/branch-context-manager.sh +342 -0
- package/scripts/git-hooks/post-checkout-stackmemory.sh +63 -0
- package/scripts/git-hooks/post-commit-stackmemory.sh +305 -0
- package/scripts/git-hooks/pre-commit-stackmemory.sh +275 -0
- package/scripts/hooks/cleanup-shell.sh +130 -0
- package/scripts/hooks/task-complete.sh +114 -0
- package/scripts/initialize.ts +129 -0
- package/scripts/install-claude-hooks-auto.js +104 -0
- package/scripts/install-claude-hooks.sh +133 -0
- package/scripts/install-global.sh +296 -0
- package/scripts/install.sh +235 -0
- package/scripts/linear-auto-sync.js +262 -0
- package/scripts/linear-auto-sync.sh +161 -0
- package/scripts/linear-sync-daemon.js +150 -0
- package/scripts/linear-task-review.js +237 -0
- package/scripts/list-linear-tasks.ts +178 -0
- package/scripts/mcp-proxy.js +66 -0
- package/scripts/opencode-wrapper.sh +85 -0
- package/scripts/publish-local.js +74 -0
- package/scripts/query-chromadb.ts +201 -0
- package/scripts/railway-env-setup.sh +39 -0
- package/scripts/reconcile-local-tasks.js +170 -0
- package/scripts/recreate-frames-db.js +89 -0
- package/scripts/setup/claude-integration.js +138 -0
- package/scripts/setup/configure-alias.js +125 -0
- package/scripts/setup/configure-codex-alias.js +161 -0
- package/scripts/setup/configure-opencode-alias.js +175 -0
- package/scripts/setup-claude-integration.js +204 -0
- package/scripts/setup-claude-integration.sh +183 -0
- package/scripts/setup.sh +31 -0
- package/scripts/show-linear-summary.ts +172 -0
- package/scripts/stackmemory-auto-handoff.sh +231 -0
- package/scripts/stackmemory-daemon.sh +40 -0
- package/scripts/start-linear-sync-daemon.sh +141 -0
- package/scripts/start-temporal-paradox.sh +214 -0
- package/scripts/status.ts +159 -0
- package/scripts/sync-and-clean-tasks.js +258 -0
- package/scripts/sync-frames-from-railway.js +228 -0
- package/scripts/sync-linear-graphql.js +303 -0
- package/scripts/sync-linear-tasks.js +186 -0
- package/scripts/test-auto-triggers.sh +57 -0
- package/scripts/test-browser-mcp.js +74 -0
- package/scripts/test-chromadb-full.js +115 -0
- package/scripts/test-chromadb-hooks.sh +28 -0
- package/scripts/test-chromadb-sync.ts +245 -0
- package/scripts/test-cli-security.js +293 -0
- package/scripts/test-hooks-persistence.sh +220 -0
- package/scripts/test-installation-scenarios.sh +359 -0
- package/scripts/test-installation.sh +224 -0
- package/scripts/test-mcp.js +163 -0
- package/scripts/test-pre-publish-quick.sh +75 -0
- package/scripts/test-quality-gates.sh +263 -0
- package/scripts/test-railway-db.js +222 -0
- package/scripts/test-redis-storage.ts +490 -0
- package/scripts/test-rlm-basic.sh +122 -0
- package/scripts/test-rlm-comprehensive.sh +260 -0
- package/scripts/test-rlm-e2e.sh +268 -0
- package/scripts/test-rlm-simple.js +90 -0
- package/scripts/test-rlm.js +110 -0
- package/scripts/test-session-handoff.sh +165 -0
- package/scripts/test-shell-integration.sh +275 -0
- package/scripts/testing/ab-test-runner.ts +508 -0
- package/scripts/testing/collect-metrics.ts +457 -0
- package/scripts/testing/quick-effectiveness-demo.js +187 -0
- package/scripts/testing/real-performance-test.js +422 -0
- package/scripts/testing/run-effectiveness-tests.sh +176 -0
- package/scripts/testing/scripts/testing/ab-test-runner.js +363 -0
- package/scripts/testing/scripts/testing/collect-metrics.js +292 -0
- package/scripts/testing/simple-effectiveness-test.js +310 -0
- package/scripts/testing/src/core/context/context-bridge.js +253 -0
- package/scripts/testing/src/core/context/frame-manager.js +746 -0
- package/scripts/testing/src/core/context/shared-context-layer.js +437 -0
- package/scripts/testing/src/core/database/database-adapter.js +54 -0
- package/scripts/testing/src/core/errors/index.js +291 -0
- package/scripts/testing/src/core/errors/recovery.js +268 -0
- package/scripts/testing/src/core/monitoring/logger.js +145 -0
- package/scripts/testing/src/core/retrieval/context-retriever.js +516 -0
- package/scripts/testing/src/core/session/index.js +1 -0
- package/scripts/testing/src/core/session/session-manager.js +323 -0
- package/scripts/testing/src/core/trace/cli-trace-wrapper.js +140 -0
- package/scripts/testing/src/core/trace/db-trace-wrapper.js +251 -0
- package/scripts/testing/src/core/trace/debug-trace.js +398 -0
- package/scripts/testing/src/core/trace/index.js +120 -0
- package/scripts/testing/src/core/trace/linear-api-wrapper.js +204 -0
- package/scripts/update-linear-status.js +268 -0
- package/scripts/update-linear-tasks-fixed.js +284 -0
- package/templates/claude-hooks/hooks.json +5 -0
- package/templates/claude-hooks/on-clear.js +56 -0
- package/templates/claude-hooks/on-startup.js +56 -0
- package/templates/claude-hooks/tool-use-trace.js +67 -0
- package/dist/features/tui/components/analytics-panel.js +0 -157
- package/dist/features/tui/components/analytics-panel.js.map +0 -7
- package/dist/features/tui/components/frame-visualizer.js +0 -377
- package/dist/features/tui/components/frame-visualizer.js.map +0 -7
- package/dist/features/tui/components/pr-tracker.js +0 -135
- package/dist/features/tui/components/pr-tracker.js.map +0 -7
- package/dist/features/tui/components/session-monitor.js +0 -299
- package/dist/features/tui/components/session-monitor.js.map +0 -7
- package/dist/features/tui/components/subagent-fleet.js +0 -395
- package/dist/features/tui/components/subagent-fleet.js.map +0 -7
- package/dist/features/tui/components/task-board.js +0 -1139
- package/dist/features/tui/components/task-board.js.map +0 -7
- package/dist/features/tui/index.js +0 -408
- package/dist/features/tui/index.js.map +0 -7
- package/dist/features/tui/services/data-service.js +0 -641
- package/dist/features/tui/services/data-service.js.map +0 -7
- package/dist/features/tui/services/linear-task-reader.js +0 -102
- package/dist/features/tui/services/linear-task-reader.js.map +0 -7
- package/dist/features/tui/services/websocket-client.js +0 -162
- package/dist/features/tui/services/websocket-client.js.map +0 -7
- package/dist/features/tui/terminal-compat.js +0 -220
- package/dist/features/tui/terminal-compat.js.map +0 -7
- package/dist/features/tui/types.js +0 -1
- package/dist/features/tui/types.js.map +0 -7
|
@@ -0,0 +1,746 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StackMemory Frame Manager - Call Stack Implementation
|
|
3
|
+
* Manages nested frames representing the call stack of work
|
|
4
|
+
*/
|
|
5
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
6
|
+
import { logger } from '../monitoring/logger.js';
|
|
7
|
+
import { DatabaseError, FrameError, ErrorCode, createErrorHandler, } from '../errors/index.js';
|
|
8
|
+
import { sessionManager, FrameQueryMode } from '../session/index.js';
|
|
9
|
+
import { contextBridge } from './context-bridge.js';
|
|
10
|
+
export class FrameManager {
|
|
11
|
+
constructor(db, projectId, runId) {
|
|
12
|
+
this.activeStack = []; // Stack of active frame IDs
|
|
13
|
+
this.queryMode = FrameQueryMode.PROJECT_ACTIVE;
|
|
14
|
+
this.db = db;
|
|
15
|
+
this.projectId = projectId;
|
|
16
|
+
// Use session manager for run ID if available
|
|
17
|
+
const session = sessionManager.getCurrentSession();
|
|
18
|
+
if (session) {
|
|
19
|
+
this.currentRunId = session.runId;
|
|
20
|
+
this.sessionId = session.sessionId;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
this.currentRunId = runId || uuidv4();
|
|
24
|
+
this.sessionId = this.currentRunId; // Fallback for legacy behavior
|
|
25
|
+
}
|
|
26
|
+
this.initializeSchema();
|
|
27
|
+
this.loadActiveStack();
|
|
28
|
+
// Initialize context bridge for automatic shared context
|
|
29
|
+
// Skip in test environment to avoid async method wrapping
|
|
30
|
+
if (process.env.NODE_ENV !== 'test' && !process.env.VITEST) {
|
|
31
|
+
contextBridge
|
|
32
|
+
.initialize(this, {
|
|
33
|
+
autoSync: true,
|
|
34
|
+
syncInterval: 60000, // 1 minute
|
|
35
|
+
minFrameScore: 0.5, // Sync frames above 0.5 score
|
|
36
|
+
importantTags: ['decision', 'error', 'milestone', 'learning'],
|
|
37
|
+
})
|
|
38
|
+
.catch((error) => {
|
|
39
|
+
logger.warn('Failed to initialize context bridge', { error });
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
setQueryMode(mode) {
|
|
44
|
+
this.queryMode = mode;
|
|
45
|
+
this.loadActiveStack(); // Reload with new mode
|
|
46
|
+
}
|
|
47
|
+
initializeSchema() {
|
|
48
|
+
const errorHandler = createErrorHandler({
|
|
49
|
+
operation: 'initializeSchema',
|
|
50
|
+
projectId: this.projectId,
|
|
51
|
+
runId: this.currentRunId,
|
|
52
|
+
});
|
|
53
|
+
try {
|
|
54
|
+
// Enhanced frames table matching architecture
|
|
55
|
+
this.db.exec(`
|
|
56
|
+
CREATE TABLE IF NOT EXISTS frames (
|
|
57
|
+
frame_id TEXT PRIMARY KEY,
|
|
58
|
+
run_id TEXT NOT NULL,
|
|
59
|
+
project_id TEXT NOT NULL,
|
|
60
|
+
parent_frame_id TEXT REFERENCES frames(frame_id),
|
|
61
|
+
depth INTEGER NOT NULL DEFAULT 0,
|
|
62
|
+
type TEXT NOT NULL,
|
|
63
|
+
name TEXT NOT NULL,
|
|
64
|
+
state TEXT DEFAULT 'active',
|
|
65
|
+
inputs TEXT DEFAULT '{}',
|
|
66
|
+
outputs TEXT DEFAULT '{}',
|
|
67
|
+
digest_text TEXT,
|
|
68
|
+
digest_json TEXT DEFAULT '{}',
|
|
69
|
+
created_at INTEGER DEFAULT (unixepoch()),
|
|
70
|
+
closed_at INTEGER
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
74
|
+
event_id TEXT PRIMARY KEY,
|
|
75
|
+
run_id TEXT NOT NULL,
|
|
76
|
+
frame_id TEXT NOT NULL,
|
|
77
|
+
seq INTEGER NOT NULL,
|
|
78
|
+
event_type TEXT NOT NULL,
|
|
79
|
+
payload TEXT NOT NULL,
|
|
80
|
+
ts INTEGER DEFAULT (unixepoch()),
|
|
81
|
+
FOREIGN KEY(frame_id) REFERENCES frames(frame_id)
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
CREATE TABLE IF NOT EXISTS anchors (
|
|
85
|
+
anchor_id TEXT PRIMARY KEY,
|
|
86
|
+
frame_id TEXT NOT NULL,
|
|
87
|
+
project_id TEXT NOT NULL,
|
|
88
|
+
type TEXT NOT NULL,
|
|
89
|
+
text TEXT NOT NULL,
|
|
90
|
+
priority INTEGER DEFAULT 0,
|
|
91
|
+
created_at INTEGER DEFAULT (unixepoch()),
|
|
92
|
+
metadata TEXT DEFAULT '{}',
|
|
93
|
+
FOREIGN KEY(frame_id) REFERENCES frames(frame_id)
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
CREATE TABLE IF NOT EXISTS handoff_requests (
|
|
97
|
+
request_id TEXT PRIMARY KEY,
|
|
98
|
+
source_stack_id TEXT NOT NULL,
|
|
99
|
+
target_stack_id TEXT NOT NULL,
|
|
100
|
+
frame_ids TEXT NOT NULL,
|
|
101
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
102
|
+
created_at INTEGER DEFAULT (unixepoch()),
|
|
103
|
+
expires_at INTEGER,
|
|
104
|
+
target_user_id TEXT,
|
|
105
|
+
message TEXT
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
CREATE INDEX IF NOT EXISTS idx_frames_run ON frames(run_id);
|
|
109
|
+
CREATE INDEX IF NOT EXISTS idx_frames_parent ON frames(parent_frame_id);
|
|
110
|
+
CREATE INDEX IF NOT EXISTS idx_frames_state ON frames(state);
|
|
111
|
+
CREATE INDEX IF NOT EXISTS idx_events_frame ON events(frame_id);
|
|
112
|
+
CREATE INDEX IF NOT EXISTS idx_events_seq ON events(frame_id, seq);
|
|
113
|
+
CREATE INDEX IF NOT EXISTS idx_anchors_frame ON anchors(frame_id);
|
|
114
|
+
CREATE INDEX IF NOT EXISTS idx_handoff_requests_status ON handoff_requests(status);
|
|
115
|
+
CREATE INDEX IF NOT EXISTS idx_handoff_requests_target ON handoff_requests(target_stack_id);
|
|
116
|
+
`);
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
const dbError = errorHandler(error, {
|
|
120
|
+
operation: 'initializeSchema',
|
|
121
|
+
schema: 'frames',
|
|
122
|
+
});
|
|
123
|
+
if (dbError instanceof DatabaseError) {
|
|
124
|
+
throw new DatabaseError('Failed to initialize frame database schema', ErrorCode.DB_MIGRATION_FAILED, {
|
|
125
|
+
projectId: this.projectId,
|
|
126
|
+
operation: 'initializeSchema',
|
|
127
|
+
originalError: error,
|
|
128
|
+
}, error instanceof Error ? error : undefined);
|
|
129
|
+
}
|
|
130
|
+
throw dbError;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
loadActiveStack() {
|
|
134
|
+
const errorHandler = createErrorHandler({
|
|
135
|
+
operation: 'loadActiveStack',
|
|
136
|
+
runId: this.currentRunId,
|
|
137
|
+
projectId: this.projectId,
|
|
138
|
+
});
|
|
139
|
+
try {
|
|
140
|
+
let query;
|
|
141
|
+
let params;
|
|
142
|
+
// Build query based on query mode
|
|
143
|
+
switch (this.queryMode) {
|
|
144
|
+
case FrameQueryMode.ALL_ACTIVE:
|
|
145
|
+
query = `
|
|
146
|
+
SELECT frame_id, parent_frame_id, depth
|
|
147
|
+
FROM frames
|
|
148
|
+
WHERE state = 'active'
|
|
149
|
+
ORDER BY created_at DESC, depth ASC
|
|
150
|
+
`;
|
|
151
|
+
params = [];
|
|
152
|
+
break;
|
|
153
|
+
case FrameQueryMode.PROJECT_ACTIVE:
|
|
154
|
+
query = `
|
|
155
|
+
SELECT frame_id, parent_frame_id, depth, run_id
|
|
156
|
+
FROM frames
|
|
157
|
+
WHERE state = 'active' AND project_id = ?
|
|
158
|
+
ORDER BY created_at DESC, depth ASC
|
|
159
|
+
`;
|
|
160
|
+
params = [this.projectId];
|
|
161
|
+
break;
|
|
162
|
+
case FrameQueryMode.HISTORICAL:
|
|
163
|
+
query = `
|
|
164
|
+
SELECT frame_id, parent_frame_id, depth
|
|
165
|
+
FROM frames
|
|
166
|
+
WHERE project_id = ?
|
|
167
|
+
ORDER BY created_at DESC, depth ASC
|
|
168
|
+
`;
|
|
169
|
+
params = [this.projectId];
|
|
170
|
+
break;
|
|
171
|
+
case FrameQueryMode.CURRENT_SESSION:
|
|
172
|
+
default:
|
|
173
|
+
query = `
|
|
174
|
+
SELECT frame_id, parent_frame_id, depth
|
|
175
|
+
FROM frames
|
|
176
|
+
WHERE run_id = ? AND state = 'active'
|
|
177
|
+
ORDER BY depth ASC
|
|
178
|
+
`;
|
|
179
|
+
params = [this.currentRunId];
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
const activeFrames = this.db.prepare(query).all(...params);
|
|
183
|
+
// Rebuild stack order
|
|
184
|
+
this.activeStack = this.buildStackOrder(activeFrames);
|
|
185
|
+
logger.info('Loaded active stack', {
|
|
186
|
+
runId: this.currentRunId,
|
|
187
|
+
stackDepth: this.activeStack.length,
|
|
188
|
+
activeFrames: this.activeStack,
|
|
189
|
+
queryMode: this.queryMode,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
const dbError = errorHandler(error, {
|
|
194
|
+
query: 'Frame loading query',
|
|
195
|
+
runId: this.currentRunId,
|
|
196
|
+
queryMode: this.queryMode,
|
|
197
|
+
});
|
|
198
|
+
if (dbError instanceof DatabaseError) {
|
|
199
|
+
throw new DatabaseError('Failed to load active frame stack', ErrorCode.DB_QUERY_FAILED, {
|
|
200
|
+
runId: this.currentRunId,
|
|
201
|
+
projectId: this.projectId,
|
|
202
|
+
operation: 'loadActiveStack',
|
|
203
|
+
}, error instanceof Error ? error : undefined);
|
|
204
|
+
}
|
|
205
|
+
throw dbError;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
buildStackOrder(frames) {
|
|
209
|
+
const stack = [];
|
|
210
|
+
// Find root frame (no parent)
|
|
211
|
+
const rootFrame = frames.find((f) => !f.parent_frame_id);
|
|
212
|
+
if (!rootFrame)
|
|
213
|
+
return [];
|
|
214
|
+
// Build stack by following parent-child relationships
|
|
215
|
+
let currentFrame = rootFrame;
|
|
216
|
+
stack.push(currentFrame.frame_id);
|
|
217
|
+
while (currentFrame) {
|
|
218
|
+
const childFrame = frames.find((f) => f.parent_frame_id === currentFrame.frame_id);
|
|
219
|
+
if (!childFrame)
|
|
220
|
+
break;
|
|
221
|
+
stack.push(childFrame.frame_id);
|
|
222
|
+
currentFrame = childFrame;
|
|
223
|
+
}
|
|
224
|
+
return stack;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Create a new frame and push to stack
|
|
228
|
+
*/
|
|
229
|
+
createFrame(options) {
|
|
230
|
+
return this._createFrame(options);
|
|
231
|
+
}
|
|
232
|
+
_createFrame(options) {
|
|
233
|
+
const frameId = uuidv4();
|
|
234
|
+
const parentFrameId = options.parentFrameId || this.getCurrentFrameId();
|
|
235
|
+
const depth = parentFrameId ? this.getFrameDepth(parentFrameId) + 1 : 0;
|
|
236
|
+
const frame = {
|
|
237
|
+
frame_id: frameId,
|
|
238
|
+
run_id: this.currentRunId,
|
|
239
|
+
project_id: this.projectId,
|
|
240
|
+
parent_frame_id: parentFrameId,
|
|
241
|
+
depth,
|
|
242
|
+
type: options.type,
|
|
243
|
+
name: options.name,
|
|
244
|
+
state: 'active',
|
|
245
|
+
inputs: options.inputs || {},
|
|
246
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
247
|
+
};
|
|
248
|
+
try {
|
|
249
|
+
this.db
|
|
250
|
+
.prepare(`
|
|
251
|
+
INSERT INTO frames (
|
|
252
|
+
frame_id, run_id, project_id, parent_frame_id, depth, type, name, state, inputs, created_at
|
|
253
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
254
|
+
`)
|
|
255
|
+
.run(frame.frame_id, frame.run_id, frame.project_id, frame.parent_frame_id, frame.depth, frame.type, frame.name, frame.state, JSON.stringify(frame.inputs), frame.created_at);
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
throw new DatabaseError(`Failed to create frame: ${options.name}`, ErrorCode.DB_QUERY_FAILED, {
|
|
259
|
+
frameId,
|
|
260
|
+
frameType: options.type,
|
|
261
|
+
frameName: options.name,
|
|
262
|
+
parentFrameId,
|
|
263
|
+
depth,
|
|
264
|
+
operation: 'createFrame',
|
|
265
|
+
}, error instanceof Error ? error : undefined);
|
|
266
|
+
}
|
|
267
|
+
// Push to active stack
|
|
268
|
+
this.activeStack.push(frameId);
|
|
269
|
+
logger.info('Created frame', {
|
|
270
|
+
frameId,
|
|
271
|
+
type: options.type,
|
|
272
|
+
name: options.name,
|
|
273
|
+
depth,
|
|
274
|
+
parentFrameId,
|
|
275
|
+
stackDepth: this.activeStack.length,
|
|
276
|
+
});
|
|
277
|
+
return frameId;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Close the current frame and generate digest
|
|
281
|
+
*/
|
|
282
|
+
closeFrame(frameId, outputs) {
|
|
283
|
+
this._closeFrame(frameId, outputs);
|
|
284
|
+
}
|
|
285
|
+
_closeFrame(frameId, outputs) {
|
|
286
|
+
const targetFrameId = frameId || this.getCurrentFrameId();
|
|
287
|
+
if (!targetFrameId) {
|
|
288
|
+
throw new FrameError('No active frame to close', ErrorCode.FRAME_INVALID_STATE, {
|
|
289
|
+
operation: 'closeFrame',
|
|
290
|
+
activeStack: this.activeStack,
|
|
291
|
+
stackDepth: this.activeStack.length,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
// Get frame details
|
|
295
|
+
const frame = this.getFrame(targetFrameId);
|
|
296
|
+
if (!frame) {
|
|
297
|
+
throw new FrameError(`Frame not found: ${targetFrameId}`, ErrorCode.FRAME_NOT_FOUND, {
|
|
298
|
+
frameId: targetFrameId,
|
|
299
|
+
operation: 'closeFrame',
|
|
300
|
+
runId: this.currentRunId,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
if (frame.state === 'closed') {
|
|
304
|
+
logger.warn('Attempted to close already closed frame', {
|
|
305
|
+
frameId: targetFrameId,
|
|
306
|
+
});
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
// Generate digest before closing
|
|
310
|
+
const digest = this.generateDigest(targetFrameId);
|
|
311
|
+
const finalOutputs = { ...outputs, ...digest.structured };
|
|
312
|
+
try {
|
|
313
|
+
// Update frame to closed state
|
|
314
|
+
this.db
|
|
315
|
+
.prepare(`
|
|
316
|
+
UPDATE frames
|
|
317
|
+
SET state = 'closed',
|
|
318
|
+
outputs = ?,
|
|
319
|
+
digest_text = ?,
|
|
320
|
+
digest_json = ?,
|
|
321
|
+
closed_at = unixepoch()
|
|
322
|
+
WHERE frame_id = ?
|
|
323
|
+
`)
|
|
324
|
+
.run(JSON.stringify(finalOutputs), digest.text, JSON.stringify(digest.structured), targetFrameId);
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
throw new DatabaseError(`Failed to close frame: ${targetFrameId}`, ErrorCode.DB_QUERY_FAILED, {
|
|
328
|
+
frameId: targetFrameId,
|
|
329
|
+
frameName: frame.name,
|
|
330
|
+
operation: 'closeFrame',
|
|
331
|
+
}, error instanceof Error ? error : undefined);
|
|
332
|
+
}
|
|
333
|
+
// Remove from active stack
|
|
334
|
+
this.activeStack = this.activeStack.filter((id) => id !== targetFrameId);
|
|
335
|
+
// Close all child frames recursively
|
|
336
|
+
this.closeChildFrames(targetFrameId);
|
|
337
|
+
logger.info('Closed frame', {
|
|
338
|
+
frameId: targetFrameId,
|
|
339
|
+
name: frame.name,
|
|
340
|
+
duration: Math.floor(Date.now() / 1000) - frame.created_at,
|
|
341
|
+
digestLength: digest.text.length,
|
|
342
|
+
stackDepth: this.activeStack.length,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Delete a frame completely from the database (used in handoffs)
|
|
347
|
+
*/
|
|
348
|
+
deleteFrame(frameId) {
|
|
349
|
+
try {
|
|
350
|
+
// First delete related data
|
|
351
|
+
this.db.prepare('DELETE FROM events WHERE frame_id = ?').run(frameId);
|
|
352
|
+
this.db.prepare('DELETE FROM anchors WHERE frame_id = ?').run(frameId);
|
|
353
|
+
// Remove from active stack if present
|
|
354
|
+
this.activeStack = this.activeStack.filter((id) => id !== frameId);
|
|
355
|
+
// Delete the frame itself
|
|
356
|
+
this.db.prepare('DELETE FROM frames WHERE frame_id = ?').run(frameId);
|
|
357
|
+
logger.debug('Deleted frame completely', { frameId });
|
|
358
|
+
}
|
|
359
|
+
catch (error) {
|
|
360
|
+
logger.error('Failed to delete frame', { frameId, error });
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
closeChildFrames(parentFrameId) {
|
|
365
|
+
try {
|
|
366
|
+
const children = this.db
|
|
367
|
+
.prepare(`
|
|
368
|
+
SELECT frame_id FROM frames
|
|
369
|
+
WHERE parent_frame_id = ? AND state = 'active'
|
|
370
|
+
`)
|
|
371
|
+
.all(parentFrameId);
|
|
372
|
+
children.forEach((child) => {
|
|
373
|
+
try {
|
|
374
|
+
this.closeFrame(child.frame_id);
|
|
375
|
+
}
|
|
376
|
+
catch (error) {
|
|
377
|
+
logger.error('Failed to close child frame', error instanceof Error ? error : new Error(String(error)), {
|
|
378
|
+
parentFrameId,
|
|
379
|
+
childFrameId: child.frame_id,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
throw new DatabaseError(`Failed to close child frames for parent: ${parentFrameId}`, ErrorCode.DB_QUERY_FAILED, {
|
|
386
|
+
parentFrameId,
|
|
387
|
+
operation: 'closeChildFrames',
|
|
388
|
+
}, error instanceof Error ? error : undefined);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Generate digest for a frame
|
|
393
|
+
*/
|
|
394
|
+
generateDigest(frameId) {
|
|
395
|
+
const frame = this.getFrame(frameId);
|
|
396
|
+
const events = this.getFrameEvents(frameId);
|
|
397
|
+
const anchors = this.getFrameAnchors(frameId);
|
|
398
|
+
if (!frame) {
|
|
399
|
+
throw new FrameError(`Cannot generate digest: frame not found ${frameId}`, ErrorCode.FRAME_NOT_FOUND, {
|
|
400
|
+
frameId,
|
|
401
|
+
operation: 'generateDigest',
|
|
402
|
+
runId: this.currentRunId,
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
// Extract key information
|
|
406
|
+
const decisions = anchors.filter((a) => a.type === 'DECISION');
|
|
407
|
+
const constraints = anchors.filter((a) => a.type === 'CONSTRAINT');
|
|
408
|
+
const risks = anchors.filter((a) => a.type === 'RISK');
|
|
409
|
+
const toolCalls = events.filter((e) => e.event_type === 'tool_call');
|
|
410
|
+
const artifacts = events.filter((e) => e.event_type === 'artifact');
|
|
411
|
+
// Generate structured digest
|
|
412
|
+
const structured = {
|
|
413
|
+
result: frame.name,
|
|
414
|
+
decisions: decisions.map((d) => ({ id: d.anchor_id, text: d.text })),
|
|
415
|
+
constraints: constraints.map((c) => ({ id: c.anchor_id, text: c.text })),
|
|
416
|
+
risks: risks.map((r) => ({ id: r.anchor_id, text: r.text })),
|
|
417
|
+
artifacts: artifacts.map((a) => ({
|
|
418
|
+
kind: a.payload.kind || 'unknown',
|
|
419
|
+
ref: a.payload.ref,
|
|
420
|
+
})),
|
|
421
|
+
tool_calls_count: toolCalls.length,
|
|
422
|
+
duration_seconds: frame.closed_at
|
|
423
|
+
? frame.closed_at - frame.created_at
|
|
424
|
+
: 0,
|
|
425
|
+
};
|
|
426
|
+
// Generate text summary
|
|
427
|
+
const text = this.generateDigestText(frame, structured, events.length);
|
|
428
|
+
return { text, structured };
|
|
429
|
+
}
|
|
430
|
+
generateDigestText(frame, structured, eventCount) {
|
|
431
|
+
let summary = `Completed: ${frame.name}\n`;
|
|
432
|
+
if (structured.decisions.length > 0) {
|
|
433
|
+
summary += `\nDecisions made:\n${structured.decisions.map((d) => `- ${d.text}`).join('\n')}`;
|
|
434
|
+
}
|
|
435
|
+
if (structured.constraints.length > 0) {
|
|
436
|
+
summary += `\nConstraints established:\n${structured.constraints.map((c) => `- ${c.text}`).join('\n')}`;
|
|
437
|
+
}
|
|
438
|
+
if (structured.risks.length > 0) {
|
|
439
|
+
summary += `\nRisks identified:\n${structured.risks.map((r) => `- ${r.text}`).join('\n')}`;
|
|
440
|
+
}
|
|
441
|
+
summary += `\nActivity: ${eventCount} events, ${structured.tool_calls_count} tool calls`;
|
|
442
|
+
if (structured.duration_seconds > 0) {
|
|
443
|
+
summary += `, ${Math.floor(structured.duration_seconds / 60)}m ${structured.duration_seconds % 60}s duration`;
|
|
444
|
+
}
|
|
445
|
+
return summary;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Add event to current frame
|
|
449
|
+
*/
|
|
450
|
+
addEvent(eventType, payload, frameId) {
|
|
451
|
+
const targetFrameId = frameId || this.getCurrentFrameId();
|
|
452
|
+
if (!targetFrameId) {
|
|
453
|
+
throw new FrameError('No active frame for event', ErrorCode.FRAME_INVALID_STATE, {
|
|
454
|
+
operation: 'addEvent',
|
|
455
|
+
eventType,
|
|
456
|
+
activeStack: this.activeStack,
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
const eventId = uuidv4();
|
|
460
|
+
const seq = this.getNextEventSequence(targetFrameId);
|
|
461
|
+
try {
|
|
462
|
+
this.db
|
|
463
|
+
.prepare(`
|
|
464
|
+
INSERT INTO events (event_id, run_id, frame_id, seq, event_type, payload)
|
|
465
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
466
|
+
`)
|
|
467
|
+
.run(eventId, this.currentRunId, targetFrameId, seq, eventType, JSON.stringify(payload));
|
|
468
|
+
}
|
|
469
|
+
catch (error) {
|
|
470
|
+
throw new DatabaseError(`Failed to add event to frame: ${targetFrameId}`, ErrorCode.DB_QUERY_FAILED, {
|
|
471
|
+
eventId,
|
|
472
|
+
frameId: targetFrameId,
|
|
473
|
+
eventType,
|
|
474
|
+
seq,
|
|
475
|
+
operation: 'addEvent',
|
|
476
|
+
}, error instanceof Error ? error : undefined);
|
|
477
|
+
}
|
|
478
|
+
return eventId;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Add anchor to frame
|
|
482
|
+
*/
|
|
483
|
+
addAnchor(type, text, priority = 0, metadata = {}, frameId) {
|
|
484
|
+
const targetFrameId = frameId || this.getCurrentFrameId();
|
|
485
|
+
if (!targetFrameId) {
|
|
486
|
+
throw new FrameError('No active frame for anchor', ErrorCode.FRAME_INVALID_STATE, {
|
|
487
|
+
operation: 'addAnchor',
|
|
488
|
+
anchorType: type,
|
|
489
|
+
text: text.substring(0, 100),
|
|
490
|
+
activeStack: this.activeStack,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
const anchorId = uuidv4();
|
|
494
|
+
try {
|
|
495
|
+
this.db
|
|
496
|
+
.prepare(`
|
|
497
|
+
INSERT INTO anchors (anchor_id, frame_id, project_id, type, text, priority, metadata)
|
|
498
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
499
|
+
`)
|
|
500
|
+
.run(anchorId, targetFrameId, this.projectId, type, text, priority, JSON.stringify(metadata));
|
|
501
|
+
}
|
|
502
|
+
catch (error) {
|
|
503
|
+
throw new DatabaseError(`Failed to add anchor to frame: ${targetFrameId}`, ErrorCode.DB_QUERY_FAILED, {
|
|
504
|
+
anchorId,
|
|
505
|
+
frameId: targetFrameId,
|
|
506
|
+
anchorType: type,
|
|
507
|
+
operation: 'addAnchor',
|
|
508
|
+
}, error instanceof Error ? error : undefined);
|
|
509
|
+
}
|
|
510
|
+
return anchorId;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Get hot stack context for current active frames
|
|
514
|
+
*/
|
|
515
|
+
getHotStackContext(maxEvents = 20) {
|
|
516
|
+
return this.activeStack
|
|
517
|
+
.map((frameId) => {
|
|
518
|
+
const frame = this.getFrame(frameId);
|
|
519
|
+
if (!frame)
|
|
520
|
+
return null;
|
|
521
|
+
return {
|
|
522
|
+
frameId,
|
|
523
|
+
header: {
|
|
524
|
+
goal: frame.name,
|
|
525
|
+
constraints: this.extractConstraints(frame.inputs),
|
|
526
|
+
definitions: frame.inputs.definitions,
|
|
527
|
+
},
|
|
528
|
+
anchors: this.getFrameAnchors(frameId),
|
|
529
|
+
recentEvents: this.getFrameEvents(frameId, maxEvents),
|
|
530
|
+
activeArtifacts: this.getActiveArtifacts(frameId),
|
|
531
|
+
};
|
|
532
|
+
})
|
|
533
|
+
.filter(Boolean);
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Get active frame path (root to current)
|
|
537
|
+
*/
|
|
538
|
+
getActiveFramePath() {
|
|
539
|
+
return this.activeStack
|
|
540
|
+
.map((frameId) => this.getFrame(frameId))
|
|
541
|
+
.filter(Boolean);
|
|
542
|
+
}
|
|
543
|
+
// Utility methods
|
|
544
|
+
getCurrentFrameId() {
|
|
545
|
+
return this.activeStack[this.activeStack.length - 1];
|
|
546
|
+
}
|
|
547
|
+
getStackDepth() {
|
|
548
|
+
return this.activeStack.length;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Get recent frames for context sharing
|
|
552
|
+
*/
|
|
553
|
+
async getRecentFrames(limit = 100) {
|
|
554
|
+
try {
|
|
555
|
+
const rows = this.db
|
|
556
|
+
.prepare(`
|
|
557
|
+
SELECT * FROM frames
|
|
558
|
+
WHERE project_id = ?
|
|
559
|
+
ORDER BY created_at DESC
|
|
560
|
+
LIMIT ?
|
|
561
|
+
`)
|
|
562
|
+
.all(this.projectId, limit);
|
|
563
|
+
return rows.map((row) => ({
|
|
564
|
+
...row,
|
|
565
|
+
frameId: row.frame_id,
|
|
566
|
+
runId: row.run_id,
|
|
567
|
+
projectId: row.project_id,
|
|
568
|
+
parentFrameId: row.parent_frame_id,
|
|
569
|
+
title: row.name,
|
|
570
|
+
timestamp: row.created_at,
|
|
571
|
+
metadata: {
|
|
572
|
+
tags: this.extractTagsFromFrame(row),
|
|
573
|
+
importance: this.calculateFrameImportance(row),
|
|
574
|
+
},
|
|
575
|
+
data: {
|
|
576
|
+
inputs: JSON.parse(row.inputs || '{}'),
|
|
577
|
+
outputs: JSON.parse(row.outputs || '{}'),
|
|
578
|
+
digest: JSON.parse(row.digest_json || '{}'),
|
|
579
|
+
},
|
|
580
|
+
inputs: JSON.parse(row.inputs || '{}'),
|
|
581
|
+
outputs: JSON.parse(row.outputs || '{}'),
|
|
582
|
+
digest_json: JSON.parse(row.digest_json || '{}'),
|
|
583
|
+
}));
|
|
584
|
+
}
|
|
585
|
+
catch (error) {
|
|
586
|
+
logger.error('Failed to get recent frames', error);
|
|
587
|
+
return [];
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Add context metadata to the current frame
|
|
592
|
+
*/
|
|
593
|
+
async addContext(key, value) {
|
|
594
|
+
const currentFrameId = this.getCurrentFrameId();
|
|
595
|
+
if (!currentFrameId)
|
|
596
|
+
return;
|
|
597
|
+
try {
|
|
598
|
+
const frame = this.getFrame(currentFrameId);
|
|
599
|
+
if (!frame)
|
|
600
|
+
return;
|
|
601
|
+
const metadata = frame.outputs || {};
|
|
602
|
+
metadata[key] = value;
|
|
603
|
+
this.db
|
|
604
|
+
.prepare(`UPDATE frames SET outputs = ? WHERE frame_id = ?`)
|
|
605
|
+
.run(JSON.stringify(metadata), currentFrameId);
|
|
606
|
+
}
|
|
607
|
+
catch (error) {
|
|
608
|
+
logger.warn('Failed to add context to frame', { error, key });
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
extractTagsFromFrame(frame) {
|
|
612
|
+
const tags = [];
|
|
613
|
+
// Add type as tag
|
|
614
|
+
if (frame.type)
|
|
615
|
+
tags.push(frame.type);
|
|
616
|
+
// Extract tags from name
|
|
617
|
+
if (frame.name) {
|
|
618
|
+
if (frame.name.toLowerCase().includes('error'))
|
|
619
|
+
tags.push('error');
|
|
620
|
+
if (frame.name.toLowerCase().includes('fix'))
|
|
621
|
+
tags.push('resolution');
|
|
622
|
+
if (frame.name.toLowerCase().includes('decision'))
|
|
623
|
+
tags.push('decision');
|
|
624
|
+
if (frame.name.toLowerCase().includes('milestone'))
|
|
625
|
+
tags.push('milestone');
|
|
626
|
+
}
|
|
627
|
+
// Extract from digest
|
|
628
|
+
try {
|
|
629
|
+
const digest = JSON.parse(frame.digest_json || '{}');
|
|
630
|
+
if (digest.tags)
|
|
631
|
+
tags.push(...digest.tags);
|
|
632
|
+
}
|
|
633
|
+
catch { }
|
|
634
|
+
return [...new Set(tags)];
|
|
635
|
+
}
|
|
636
|
+
calculateFrameImportance(frame) {
|
|
637
|
+
// Milestones and decisions are high importance
|
|
638
|
+
if (frame.type === 'milestone' || frame.name?.includes('decision'))
|
|
639
|
+
return 'high';
|
|
640
|
+
// Errors and resolutions are medium importance
|
|
641
|
+
if (frame.type === 'error' || frame.type === 'resolution')
|
|
642
|
+
return 'medium';
|
|
643
|
+
// Long-running frames are potentially important
|
|
644
|
+
if (frame.closed_at && frame.created_at) {
|
|
645
|
+
const duration = frame.closed_at - frame.created_at;
|
|
646
|
+
if (duration > 300)
|
|
647
|
+
return 'medium'; // More than 5 minutes
|
|
648
|
+
}
|
|
649
|
+
return 'low';
|
|
650
|
+
}
|
|
651
|
+
getFrameDepth(frameId) {
|
|
652
|
+
const frame = this.getFrame(frameId);
|
|
653
|
+
return frame?.depth || 0;
|
|
654
|
+
}
|
|
655
|
+
getFrame(frameId) {
|
|
656
|
+
try {
|
|
657
|
+
const row = this.db
|
|
658
|
+
.prepare(`
|
|
659
|
+
SELECT * FROM frames WHERE frame_id = ?
|
|
660
|
+
`)
|
|
661
|
+
.get(frameId);
|
|
662
|
+
if (!row)
|
|
663
|
+
return undefined;
|
|
664
|
+
return {
|
|
665
|
+
...row,
|
|
666
|
+
inputs: JSON.parse(row.inputs || '{}'),
|
|
667
|
+
outputs: JSON.parse(row.outputs || '{}'),
|
|
668
|
+
digest_json: JSON.parse(row.digest_json || '{}'),
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
catch (error) {
|
|
672
|
+
// Log the error but return undefined instead of throwing
|
|
673
|
+
logger.warn(`Failed to get frame: ${frameId}`, {
|
|
674
|
+
error: error instanceof Error ? error.message : String(error),
|
|
675
|
+
frameId,
|
|
676
|
+
operation: 'getFrame',
|
|
677
|
+
});
|
|
678
|
+
return undefined;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
getFrameEvents(frameId, limit) {
|
|
682
|
+
try {
|
|
683
|
+
const query = limit
|
|
684
|
+
? `SELECT * FROM events WHERE frame_id = ? ORDER BY seq DESC LIMIT ?`
|
|
685
|
+
: `SELECT * FROM events WHERE frame_id = ? ORDER BY seq ASC`;
|
|
686
|
+
const params = limit ? [frameId, limit] : [frameId];
|
|
687
|
+
const rows = this.db.prepare(query).all(...params);
|
|
688
|
+
return rows.map((row) => ({
|
|
689
|
+
...row,
|
|
690
|
+
payload: JSON.parse(row.payload),
|
|
691
|
+
}));
|
|
692
|
+
}
|
|
693
|
+
catch (error) {
|
|
694
|
+
throw new DatabaseError(`Failed to get frame events: ${frameId}`, ErrorCode.DB_QUERY_FAILED, {
|
|
695
|
+
frameId,
|
|
696
|
+
limit,
|
|
697
|
+
operation: 'getFrameEvents',
|
|
698
|
+
}, error instanceof Error ? error : undefined);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
getFrameAnchors(frameId) {
|
|
702
|
+
try {
|
|
703
|
+
const rows = this.db
|
|
704
|
+
.prepare(`
|
|
705
|
+
SELECT * FROM anchors WHERE frame_id = ? ORDER BY priority DESC, created_at ASC
|
|
706
|
+
`)
|
|
707
|
+
.all(frameId);
|
|
708
|
+
return rows.map((row) => ({
|
|
709
|
+
...row,
|
|
710
|
+
metadata: JSON.parse(row.metadata || '{}'),
|
|
711
|
+
}));
|
|
712
|
+
}
|
|
713
|
+
catch (error) {
|
|
714
|
+
throw new DatabaseError(`Failed to get frame anchors: ${frameId}`, ErrorCode.DB_QUERY_FAILED, {
|
|
715
|
+
frameId,
|
|
716
|
+
operation: 'getFrameAnchors',
|
|
717
|
+
}, error instanceof Error ? error : undefined);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
getNextEventSequence(frameId) {
|
|
721
|
+
try {
|
|
722
|
+
const result = this.db
|
|
723
|
+
.prepare(`
|
|
724
|
+
SELECT MAX(seq) as max_seq FROM events WHERE frame_id = ?
|
|
725
|
+
`)
|
|
726
|
+
.get(frameId);
|
|
727
|
+
return (result.max_seq || 0) + 1;
|
|
728
|
+
}
|
|
729
|
+
catch (error) {
|
|
730
|
+
throw new DatabaseError(`Failed to get next event sequence for frame: ${frameId}`, ErrorCode.DB_QUERY_FAILED, {
|
|
731
|
+
frameId,
|
|
732
|
+
operation: 'getNextEventSequence',
|
|
733
|
+
}, error instanceof Error ? error : undefined);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
extractConstraints(inputs) {
|
|
737
|
+
return inputs.constraints;
|
|
738
|
+
}
|
|
739
|
+
getActiveArtifacts(frameId) {
|
|
740
|
+
const artifacts = this.getFrameEvents(frameId)
|
|
741
|
+
.filter((e) => e.event_type === 'artifact')
|
|
742
|
+
.map((e) => e.payload.ref)
|
|
743
|
+
.filter(Boolean);
|
|
744
|
+
return artifacts;
|
|
745
|
+
}
|
|
746
|
+
}
|