@opencorelabs/lucidity 0.6.0 → 0.8.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/README.md +18 -3
- package/dist/assets/speech/lucidity-speech +0 -0
- package/dist/automations/agent.d.ts.map +1 -1
- package/dist/automations/agent.js +2 -0
- package/dist/automations/designer.d.ts +1 -1
- package/dist/chat/server.d.ts +1 -0
- package/dist/chat/server.d.ts.map +1 -1
- package/dist/chat/server.js +6 -1
- package/dist/coding/conduct.d.ts +1 -22
- package/dist/coding/conduct.d.ts.map +1 -1
- package/dist/coding/conduct.js +9 -94
- package/dist/coding/runCodeTurn.d.ts +3 -16
- package/dist/coding/runCodeTurn.d.ts.map +1 -1
- package/dist/coding/runCodeTurn.js +157 -165
- package/dist/coding/workspaceRegistry.d.ts +24 -0
- package/dist/coding/workspaceRegistry.d.ts.map +1 -0
- package/dist/coding/workspaceRegistry.js +230 -0
- package/dist/coding/worktree.d.ts +11 -14
- package/dist/coding/worktree.d.ts.map +1 -1
- package/dist/coding/worktree.js +41 -21
- package/dist/config.d.ts +2 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/daemonLease.d.ts +12 -0
- package/dist/daemonLease.d.ts.map +1 -0
- package/dist/daemonLease.js +32 -0
- package/dist/goals/assessment.d.ts +115 -0
- package/dist/goals/assessment.d.ts.map +1 -0
- package/dist/goals/assessment.js +67 -0
- package/dist/goals/notes.d.ts +18 -0
- package/dist/goals/notes.d.ts.map +1 -0
- package/dist/goals/notes.js +72 -0
- package/dist/index.js +63 -15
- package/dist/jobs/goals.d.ts +15 -0
- package/dist/jobs/goals.d.ts.map +1 -0
- package/dist/jobs/goals.js +286 -0
- package/dist/jobs/weeklyReview.d.ts.map +1 -1
- package/dist/jobs/weeklyReview.js +3 -2
- package/dist/queue.d.ts +1 -0
- package/dist/queue.d.ts.map +1 -1
- package/dist/queue.js +4 -0
- package/dist/room/client.d.ts +5 -0
- package/dist/room/client.d.ts.map +1 -1
- package/dist/room/client.js +124 -107
- package/dist/room/computer.d.ts.map +1 -1
- package/dist/room/computer.js +2 -0
- package/dist/room/durablePublish.d.ts +6 -0
- package/dist/room/durablePublish.d.ts.map +1 -0
- package/dist/room/durablePublish.js +19 -0
- package/dist/room/incidents.d.ts +1 -1
- package/dist/room/incidents.d.ts.map +1 -1
- package/dist/room/incidents.js +14 -7
- package/dist/room/turnQueue.d.ts.map +1 -1
- package/dist/room/turnQueue.js +27 -24
- package/dist/runtimeStore.d.ts +44 -0
- package/dist/runtimeStore.d.ts.map +1 -0
- package/dist/runtimeStore.js +188 -0
- package/dist/setup/server.js +1 -1
- package/dist/turnLog.d.ts +10 -40
- package/dist/turnLog.d.ts.map +1 -1
- package/dist/turnLog.js +33 -56
- package/package.json +4 -4
- package/src/automations/agent.ts +2 -0
- package/src/chat/server.ts +7 -1
- package/src/coding/conduct.ts +10 -147
- package/src/coding/runCodeTurn.ts +42 -61
- package/src/coding/workspaceRegistry.ts +210 -0
- package/src/coding/worktree.ts +44 -20
- package/src/config.ts +2 -0
- package/src/daemonLease.ts +33 -0
- package/src/goals/assessment.ts +106 -0
- package/src/goals/notes.ts +88 -0
- package/src/index.ts +63 -18
- package/src/jobs/goals.ts +384 -0
- package/src/jobs/weeklyReview.ts +4 -3
- package/src/queue.ts +4 -0
- package/src/room/client.ts +85 -80
- package/src/room/computer.ts +2 -0
- package/src/room/durablePublish.ts +25 -0
- package/src/room/incidents.ts +16 -8
- package/src/room/turnQueue.ts +26 -27
- package/src/runtimeStore.ts +190 -0
- package/src/setup/server.ts +1 -1
- package/src/turnLog.ts +32 -75
- package/vendor/decisions/dist/dayPlan.d.ts +9 -0
- package/vendor/decisions/dist/dayPlan.js +82 -0
- package/vendor/decisions/dist/index.d.ts +1 -0
- package/vendor/decisions/dist/index.js +1 -0
- package/vendor/decisions/dist/jev.js +3 -1
- package/vendor/decisions/src/dayPlan.ts +80 -0
- package/vendor/decisions/src/index.ts +1 -0
- package/vendor/decisions/src/jev.ts +3 -1
- package/vendor/mcp-server/dist/index.js +2 -0
- package/vendor/mcp-server/dist/tools/goals.d.ts +4 -0
- package/vendor/mcp-server/dist/tools/goals.d.ts.map +1 -0
- package/vendor/mcp-server/dist/tools/goals.js +38 -0
- package/vendor/mcp-server/package.json +1 -1
- package/vendor/mcp-server/src/index.ts +2 -0
- package/vendor/mcp-server/src/tools/goals.ts +62 -0
- package/vendor/runtime/README.md +2 -2
- package/vendor/runtime/dist/executor/asyncQueue.d.ts +1 -0
- package/vendor/runtime/dist/executor/asyncQueue.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/asyncQueue.js +3 -0
- package/vendor/runtime/dist/executor/claudeAgent.d.ts +1 -1
- package/vendor/runtime/dist/executor/claudeAgent.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/claudeAgent.js +25 -10
- package/vendor/runtime/dist/executor/codex.d.ts +1 -0
- package/vendor/runtime/dist/executor/codex.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codex.js +69 -36
- package/vendor/runtime/dist/executor/codexProtocol/FunctionCallOutputContentItem.d.ts +4 -1
- package/vendor/runtime/dist/executor/codexProtocol/FunctionCallOutputContentItem.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/Personality.d.ts +3 -0
- package/vendor/runtime/dist/executor/codexProtocol/Personality.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/PlanType.d.ts +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/PlanType.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/CyberAccessProgram.d.ts +6 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/CyberAccessProgram.d.ts.map +1 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/CyberAccessProgram.js +2 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/GetAccountRateLimitsResponse.d.ts +5 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/GetAccountRateLimitsResponse.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/Model.d.ts +8 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/Model.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/ModelAccessPrograms.d.ts +11 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/ModelAccessPrograms.d.ts.map +1 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/ModelAccessPrograms.js +2 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/RateLimitSnapshot.d.ts +4 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/RateLimitSnapshot.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/ReviewStartParams.d.ts +2 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/ReviewStartParams.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/ThreadResumeParams.d.ts +4 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/ThreadResumeParams.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/ThreadStartParams.d.ts +3 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/ThreadStartParams.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/TurnStartParams.d.ts +7 -2
- package/vendor/runtime/dist/executor/codexProtocol/v2/TurnStartParams.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexProtocol/v2/UserInput.d.ts +3 -0
- package/vendor/runtime/dist/executor/codexProtocol/v2/UserInput.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexRpc.d.ts +10 -3
- package/vendor/runtime/dist/executor/codexRpc.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/codexRpc.js +18 -3
- package/vendor/runtime/dist/executor/codexSessions.d.ts +30 -0
- package/vendor/runtime/dist/executor/codexSessions.d.ts.map +1 -0
- package/vendor/runtime/dist/executor/codexSessions.js +88 -0
- package/vendor/runtime/dist/executor/health.d.ts +2 -1
- package/vendor/runtime/dist/executor/health.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/health.js +17 -5
- package/vendor/runtime/dist/executor/liveTurns.d.ts +2 -1
- package/vendor/runtime/dist/executor/liveTurns.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/liveTurns.js +12 -5
- package/vendor/runtime/dist/executor/ownership.d.ts +35 -0
- package/vendor/runtime/dist/executor/ownership.d.ts.map +1 -0
- package/vendor/runtime/dist/executor/ownership.js +99 -0
- package/vendor/runtime/dist/executor/providerRouter.d.ts +1 -0
- package/vendor/runtime/dist/executor/providerRouter.d.ts.map +1 -1
- package/vendor/runtime/dist/executor/providerRouter.js +5 -0
- package/vendor/runtime/dist/executor/types.d.ts +4 -0
- package/vendor/runtime/dist/executor/types.d.ts.map +1 -1
- package/vendor/runtime/dist/harness/index.d.ts +1 -0
- package/vendor/runtime/dist/harness/index.d.ts.map +1 -1
- package/vendor/runtime/dist/harness/index.js +12 -1
- package/vendor/runtime/dist/harness/types.d.ts +2 -1
- package/vendor/runtime/dist/harness/types.d.ts.map +1 -1
- package/vendor/runtime/dist/index.d.ts +1 -0
- package/vendor/runtime/dist/index.d.ts.map +1 -1
- package/vendor/runtime/dist/index.js +1 -0
- package/vendor/runtime/dist/prompts/dayPlanning.d.ts +44 -0
- package/vendor/runtime/dist/prompts/dayPlanning.d.ts.map +1 -0
- package/vendor/runtime/dist/prompts/dayPlanning.js +42 -0
- package/vendor/runtime/src/executor/asyncQueue.ts +3 -0
- package/vendor/runtime/src/executor/claudeAgent.ts +20 -9
- package/vendor/runtime/src/executor/codex.ts +76 -44
- package/vendor/runtime/src/executor/codexProtocol/FunctionCallOutputContentItem.ts +1 -1
- package/vendor/runtime/src/executor/codexProtocol/Personality.ts +3 -0
- package/vendor/runtime/src/executor/codexProtocol/PlanType.ts +1 -1
- package/vendor/runtime/src/executor/codexProtocol/README.md +2 -2
- package/vendor/runtime/src/executor/codexProtocol/v2/CyberAccessProgram.ts +9 -0
- package/vendor/runtime/src/executor/codexProtocol/v2/GetAccountRateLimitsResponse.ts +5 -0
- package/vendor/runtime/src/executor/codexProtocol/v2/Model.ts +11 -2
- package/vendor/runtime/src/executor/codexProtocol/v2/ModelAccessPrograms.ts +13 -0
- package/vendor/runtime/src/executor/codexProtocol/v2/RateLimitSnapshot.ts +5 -1
- package/vendor/runtime/src/executor/codexProtocol/v2/ReviewStartParams.ts +2 -0
- package/vendor/runtime/src/executor/codexProtocol/v2/ThreadResumeParams.ts +5 -1
- package/vendor/runtime/src/executor/codexProtocol/v2/ThreadStartParams.ts +4 -1
- package/vendor/runtime/src/executor/codexProtocol/v2/TurnStartParams.ts +7 -2
- package/vendor/runtime/src/executor/codexProtocol/v2/UserInput.ts +1 -1
- package/vendor/runtime/src/executor/codexRpc.ts +22 -4
- package/vendor/runtime/src/executor/codexSessions.ts +108 -0
- package/vendor/runtime/src/executor/health.ts +16 -6
- package/vendor/runtime/src/executor/liveTurns.ts +12 -5
- package/vendor/runtime/src/executor/ownership.ts +76 -0
- package/vendor/runtime/src/executor/providerRouter.ts +6 -0
- package/vendor/runtime/src/executor/types.ts +4 -0
- package/vendor/runtime/src/harness/index.ts +12 -1
- package/vendor/runtime/src/harness/types.ts +2 -1
- package/vendor/runtime/src/index.ts +1 -0
- package/vendor/runtime/src/prompts/dayPlanning.ts +46 -0
- package/vendor/shared/dist/agentCommands.d.ts +1 -0
- package/vendor/shared/dist/agentCommands.d.ts.map +1 -1
- package/vendor/shared/dist/cardStudio.d.ts +41 -0
- package/vendor/shared/dist/cardStudio.d.ts.map +1 -1
- package/vendor/shared/dist/cardStudio.js +13 -3
- package/vendor/shared/dist/commandLocal.d.ts.map +1 -1
- package/vendor/shared/dist/commandLocal.js +2 -0
- package/vendor/shared/dist/commands.d.ts +18 -0
- package/vendor/shared/dist/commands.d.ts.map +1 -1
- package/vendor/shared/dist/commands.js +2 -0
- package/vendor/shared/dist/computer.d.ts +7 -1
- package/vendor/shared/dist/computer.d.ts.map +1 -1
- package/vendor/shared/dist/computer.js +13 -2
- package/vendor/shared/dist/constants.d.ts +3 -3
- package/vendor/shared/dist/constants.d.ts.map +1 -1
- package/vendor/shared/dist/constants.js +5 -4
- package/vendor/shared/dist/dayPlan.d.ts +86 -0
- package/vendor/shared/dist/dayPlan.d.ts.map +1 -0
- package/vendor/shared/dist/dayPlan.js +46 -0
- package/vendor/shared/dist/dayPlanning.d.ts +139 -0
- package/vendor/shared/dist/dayPlanning.d.ts.map +1 -0
- package/vendor/shared/dist/dayPlanning.js +67 -0
- package/vendor/shared/dist/goalNotes.d.ts +4 -0
- package/vendor/shared/dist/goalNotes.d.ts.map +1 -0
- package/vendor/shared/dist/goalNotes.js +23 -0
- package/vendor/shared/dist/goals.d.ts +835 -0
- package/vendor/shared/dist/goals.d.ts.map +1 -0
- package/vendor/shared/dist/goals.js +198 -0
- package/vendor/shared/dist/index.d.ts +5 -0
- package/vendor/shared/dist/index.d.ts.map +1 -1
- package/vendor/shared/dist/index.js +5 -0
- package/vendor/shared/dist/schemas/harness.d.ts +155 -0
- package/vendor/shared/dist/schemas/harness.d.ts.map +1 -1
- package/vendor/shared/dist/schemas/harness.js +8 -0
- package/vendor/shared/dist/workspaces.d.ts +138 -0
- package/vendor/shared/dist/workspaces.d.ts.map +1 -0
- package/vendor/shared/dist/workspaces.js +40 -0
- package/vendor/shared/src/agentCommands.ts +1 -0
- package/vendor/shared/src/cardStudio.ts +13 -3
- package/vendor/shared/src/commandLocal.ts +2 -0
- package/vendor/shared/src/commands.ts +2 -0
- package/vendor/shared/src/computer.ts +18 -2
- package/vendor/shared/src/constants.ts +5 -4
- package/vendor/shared/src/dayPlan.ts +81 -0
- package/vendor/shared/src/dayPlanning.ts +77 -0
- package/vendor/shared/src/goalNotes.ts +23 -0
- package/vendor/shared/src/goals.ts +237 -0
- package/vendor/shared/src/index.ts +6 -0
- package/vendor/shared/src/schemas/harness.ts +8 -0
- package/vendor/shared/src/workspaces.ts +48 -0
- package/dist/coding/fleetIntegrate.d.ts +0 -37
- package/dist/coding/fleetIntegrate.d.ts.map +0 -1
- package/dist/coding/fleetIntegrate.js +0 -64
- package/dist/coding/openFleetPr.d.ts +0 -41
- package/dist/coding/openFleetPr.d.ts.map +0 -1
- package/dist/coding/openFleetPr.js +0 -121
- package/src/coding/fleetIntegrate.ts +0 -95
- package/src/coding/openFleetPr.ts +0 -148
package/dist/room/turnQueue.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdirSync, readFileSync, readdirSync,
|
|
1
|
+
import { mkdirSync, readFileSync, readdirSync, statSync, unlinkSync, } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { HarnessRequestSchema } from '@lucidity/shared';
|
|
4
4
|
export function turnLane(req) {
|
|
@@ -17,6 +17,8 @@ export class DurableTurnQueue {
|
|
|
17
17
|
constructor(deps) {
|
|
18
18
|
this.deps = deps;
|
|
19
19
|
mkdirSync(deps.dir, { recursive: true, mode: 0o700 });
|
|
20
|
+
for (const entry of deps.logs.runtime.list('queue'))
|
|
21
|
+
this.entries.set(entry.request.id, entry);
|
|
20
22
|
for (const name of readdirSync(deps.dir)
|
|
21
23
|
.filter((name) => name.endsWith('.json'))
|
|
22
24
|
.sort()) {
|
|
@@ -26,6 +28,12 @@ export class DurableTurnQueue {
|
|
|
26
28
|
unlinkSync(join(deps.dir, name));
|
|
27
29
|
continue;
|
|
28
30
|
}
|
|
31
|
+
if (this.entries.has(entry.request.id))
|
|
32
|
+
continue;
|
|
33
|
+
this.deps.logs.runtime.set('queue', entry.request.id, entry);
|
|
34
|
+
this.entries.set(entry.request.id, entry);
|
|
35
|
+
}
|
|
36
|
+
for (const entry of this.entries.values()) {
|
|
29
37
|
const parsed = HarnessRequestSchema.parse(entry.request);
|
|
30
38
|
if (parsed.kind !== 'ask' && parsed.kind !== 'code')
|
|
31
39
|
throw new Error('Invalid queued turn');
|
|
@@ -41,7 +49,7 @@ export class DurableTurnQueue {
|
|
|
41
49
|
});
|
|
42
50
|
}
|
|
43
51
|
else {
|
|
44
|
-
this.fail(entry, 'Lucid restarted during this turn. It was not run again. Review
|
|
52
|
+
this.fail(entry, 'Lucid restarted during this turn. It was not run again. Review its work before continuing.', 'interrupted_unknown');
|
|
45
53
|
}
|
|
46
54
|
}
|
|
47
55
|
else if (entry.state === 'skipped' && !terminal) {
|
|
@@ -53,7 +61,7 @@ export class DurableTurnQueue {
|
|
|
53
61
|
else if (entry.state === 'failed' && !terminal) {
|
|
54
62
|
this.publish(parsed.id, {
|
|
55
63
|
type: 'error',
|
|
56
|
-
message: 'This queued turn could not finish. Review it before resending.',
|
|
64
|
+
...(entry.failure ?? { message: 'This queued turn could not finish. Review it before resending.' }),
|
|
57
65
|
});
|
|
58
66
|
}
|
|
59
67
|
}
|
|
@@ -67,12 +75,7 @@ export class DurableTurnQueue {
|
|
|
67
75
|
}
|
|
68
76
|
}
|
|
69
77
|
save(entry) {
|
|
70
|
-
|
|
71
|
-
writeFileSync(`${path}.tmp`, JSON.stringify(entry), {
|
|
72
|
-
mode: 0o600,
|
|
73
|
-
flush: true,
|
|
74
|
-
});
|
|
75
|
-
renameSync(`${path}.tmp`, path);
|
|
78
|
+
this.deps.logs.runtime.set('queue', entry.request.id, entry);
|
|
76
79
|
this.entries.set(entry.request.id, entry);
|
|
77
80
|
}
|
|
78
81
|
publish(id, event) {
|
|
@@ -96,10 +99,11 @@ export class DurableTurnQueue {
|
|
|
96
99
|
.find((frame) => typeof frame.sessionId === 'string')?.sessionId;
|
|
97
100
|
return typeof value === 'string' ? value : undefined;
|
|
98
101
|
}
|
|
99
|
-
fail(entry, message) {
|
|
100
|
-
|
|
102
|
+
fail(entry, message, code) {
|
|
103
|
+
const failure = { message, ...(code ? { code } : {}) };
|
|
104
|
+
this.save({ ...entry, state: 'failed', failure });
|
|
101
105
|
if (!this.deps.logs.read(entry.request.id).terminated)
|
|
102
|
-
this.publish(entry.request.id, { type: 'error',
|
|
106
|
+
this.publish(entry.request.id, { type: 'error', ...failure });
|
|
103
107
|
this.wake();
|
|
104
108
|
}
|
|
105
109
|
owns(id) {
|
|
@@ -150,14 +154,10 @@ export class DurableTurnQueue {
|
|
|
150
154
|
throw new Error('The preceding turn does not belong to this conversation. Refresh it before sending again.');
|
|
151
155
|
}
|
|
152
156
|
const entry = { request, state: 'queued' };
|
|
153
|
-
this.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
catch (error) {
|
|
158
|
-
this.save({ ...entry, state: 'failed' });
|
|
159
|
-
throw error;
|
|
160
|
-
}
|
|
157
|
+
const accepted = this.deps.logs.runtime.accept(request, entry);
|
|
158
|
+
this.entries.set(request.id, entry);
|
|
159
|
+
for (const frame of accepted.frames)
|
|
160
|
+
this.deps.sendRaw(frame);
|
|
161
161
|
this.wake();
|
|
162
162
|
}
|
|
163
163
|
/** Skipped queue entries inherit their predecessor, so removing B from A→B→C
|
|
@@ -198,12 +198,12 @@ export class DurableTurnQueue {
|
|
|
198
198
|
return { ready: true, resume };
|
|
199
199
|
}
|
|
200
200
|
wake() {
|
|
201
|
-
if (this.stopped || this.pumping)
|
|
201
|
+
if (this.stopped || this.pumping || !this.deps.logs.runtime.writable)
|
|
202
202
|
return;
|
|
203
203
|
this.pumping = true;
|
|
204
204
|
queueMicrotask(() => {
|
|
205
205
|
this.pumping = false;
|
|
206
|
-
if (this.stopped)
|
|
206
|
+
if (this.stopped || !this.deps.logs.runtime.writable)
|
|
207
207
|
return;
|
|
208
208
|
const occupied = new Set();
|
|
209
209
|
for (const [id, entry] of this.entries) {
|
|
@@ -227,7 +227,7 @@ export class DurableTurnQueue {
|
|
|
227
227
|
void this.deps.lanes
|
|
228
228
|
.run(turnLane(entry.request), async () => {
|
|
229
229
|
const current = this.entries.get(id);
|
|
230
|
-
if (this.stopped || current.state !== 'queued')
|
|
230
|
+
if (this.stopped || !this.deps.logs.runtime.writable || current.state !== 'queued')
|
|
231
231
|
return;
|
|
232
232
|
const previous = this.predecessor(current);
|
|
233
233
|
if (!previous.ready)
|
|
@@ -249,7 +249,10 @@ export class DurableTurnQueue {
|
|
|
249
249
|
else
|
|
250
250
|
this.fail(current, 'The turn ended before completion. Review it before resending.');
|
|
251
251
|
})
|
|
252
|
-
.catch((error) =>
|
|
252
|
+
.catch((error) => {
|
|
253
|
+
if (this.deps.logs.runtime.writable)
|
|
254
|
+
this.fail(this.entries.get(id), String(error.message));
|
|
255
|
+
})
|
|
253
256
|
.finally(() => {
|
|
254
257
|
this.scheduled.delete(id);
|
|
255
258
|
this.wake();
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { HarnessRequest } from '@lucidity/shared';
|
|
2
|
+
import type { TurnFrame, TurnState } from './turnLog.js';
|
|
3
|
+
export declare function conversationKey(request: {
|
|
4
|
+
kind: string;
|
|
5
|
+
repoId?: string;
|
|
6
|
+
workerId?: string;
|
|
7
|
+
mode?: string;
|
|
8
|
+
}): string;
|
|
9
|
+
/** Local execution authority. Transactions commit before their receipts reach the transport. */
|
|
10
|
+
export declare class RuntimeStore {
|
|
11
|
+
private db;
|
|
12
|
+
private storageError?;
|
|
13
|
+
get writable(): boolean;
|
|
14
|
+
private write;
|
|
15
|
+
constructor(path: string);
|
|
16
|
+
transaction<T>(action: () => T): T;
|
|
17
|
+
get<T>(bucket: string, id: string): T | undefined;
|
|
18
|
+
set(bucket: string, id: string, value: unknown): void;
|
|
19
|
+
list<T>(bucket: string): T[];
|
|
20
|
+
/** The command and its first replayable receipt are one durable operation. */
|
|
21
|
+
accept(request: HarnessRequest, queuedEntry?: unknown): {
|
|
22
|
+
duplicate: boolean;
|
|
23
|
+
frames: TurnFrame[];
|
|
24
|
+
};
|
|
25
|
+
private appendInside;
|
|
26
|
+
append(id: string, event: Record<string, unknown>): TurnFrame;
|
|
27
|
+
status(id: string): Omit<TurnState, 'frames'>;
|
|
28
|
+
read(id: string): TurnState;
|
|
29
|
+
importLog(id: string, state: TurnState): void;
|
|
30
|
+
commands(conversation: string, page?: {
|
|
31
|
+
before?: string;
|
|
32
|
+
after?: string;
|
|
33
|
+
limit: number;
|
|
34
|
+
}): {
|
|
35
|
+
request: HarnessRequest;
|
|
36
|
+
state: TurnState;
|
|
37
|
+
}[];
|
|
38
|
+
latestSession(conversation: string, after?: string): string | undefined;
|
|
39
|
+
lastCommand(conversation: string): string | undefined;
|
|
40
|
+
unfinished(conversation: string): boolean;
|
|
41
|
+
reconcile(owns: (id: string) => boolean): void;
|
|
42
|
+
close(): void;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=runtimeStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtimeStore.d.ts","sourceRoot":"","sources":["../src/runtimeStore.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAWzD,wBAAgB,eAAe,CAAC,OAAO,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAIpH;AAED,gGAAgG;AAChG,qBAAa,YAAY;IACvB,OAAO,CAAC,EAAE,CAAM;IAChB,OAAO,CAAC,YAAY,CAAC,CAAQ;IAC7B,IAAI,QAAQ,IAAI,OAAO,CAA+B;IACtD,OAAO,CAAC,KAAK;gBAUD,IAAI,EAAE,MAAM;IAyBxB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC;IAclC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAIjD,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI;IAIrD,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,CAAC,EAAE;IAK5B,8EAA8E;IAC9E,MAAM,CAAC,OAAO,EAAE,cAAc,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG;QAAE,SAAS,EAAE,OAAO,CAAC;QAAC,MAAM,EAAE,SAAS,EAAE,CAAA;KAAE;IAwBnG,OAAO,CAAC,YAAY;IAiBpB,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS;IAI7D,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC;IAI7C,IAAI,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS;IAQ3B,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI;IAW7C,QAAQ,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,OAAO,EAAE,cAAc,CAAC;QAAC,KAAK,EAAE,SAAS,CAAA;KAAE,EAAE;IAS1I,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAOvE,WAAW,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAGrD,UAAU,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO;IAIzC,SAAS,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI;IAO9C,KAAK,IAAI,IAAI;CACd"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// @ts-expect-error Node 22 runtime API; the repository's Node type baseline is 20.
|
|
2
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
3
|
+
import { chmodSync, mkdirSync } from 'node:fs';
|
|
4
|
+
import { dirname } from 'node:path';
|
|
5
|
+
const terminalTypes = new Set(['done', 'error', 'cancelled']);
|
|
6
|
+
function canonical(value) {
|
|
7
|
+
if (Array.isArray(value))
|
|
8
|
+
return `[${value.map(canonical).join(',')}]`;
|
|
9
|
+
if (value && typeof value === 'object')
|
|
10
|
+
return `{${Object.entries(value).filter(([, v]) => v !== undefined).sort(([a], [b]) => a.localeCompare(b)).map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(',')}}`;
|
|
11
|
+
return JSON.stringify(value);
|
|
12
|
+
}
|
|
13
|
+
export function conversationKey(request) {
|
|
14
|
+
return request.kind === 'code'
|
|
15
|
+
? JSON.stringify(['code', request.repoId, request.workerId ?? request.mode ?? 'worktree'])
|
|
16
|
+
: request.kind;
|
|
17
|
+
}
|
|
18
|
+
/** Local execution authority. Transactions commit before their receipts reach the transport. */
|
|
19
|
+
export class RuntimeStore {
|
|
20
|
+
db;
|
|
21
|
+
storageError;
|
|
22
|
+
get writable() { return !this.storageError; }
|
|
23
|
+
write(action) {
|
|
24
|
+
if (this.storageError)
|
|
25
|
+
throw new Error(`Runtime storage is unavailable until restart: ${this.storageError.message}`);
|
|
26
|
+
try {
|
|
27
|
+
return action();
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
const code = error?.code ?? '';
|
|
31
|
+
if (code.includes('SQLITE') || code === 'ERR_INVALID_STATE')
|
|
32
|
+
this.storageError = error instanceof Error ? error : new Error(String(error));
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
constructor(path) {
|
|
37
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
38
|
+
this.db = new DatabaseSync(path);
|
|
39
|
+
chmodSync(path, 0o600);
|
|
40
|
+
this.db.exec(`
|
|
41
|
+
PRAGMA journal_mode=WAL;
|
|
42
|
+
PRAGMA synchronous=FULL;
|
|
43
|
+
PRAGMA busy_timeout=5000;
|
|
44
|
+
CREATE TABLE IF NOT EXISTS runs (
|
|
45
|
+
id TEXT PRIMARY KEY, command TEXT, conversation TEXT,
|
|
46
|
+
state TEXT NOT NULL DEFAULT 'accepted', cursor INTEGER NOT NULL DEFAULT 0,
|
|
47
|
+
terminal TEXT, updated INTEGER NOT NULL
|
|
48
|
+
);
|
|
49
|
+
CREATE INDEX IF NOT EXISTS runs_conversation ON runs(conversation, updated);
|
|
50
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
51
|
+
run_id TEXT NOT NULL, seq INTEGER NOT NULL, frame TEXT NOT NULL,
|
|
52
|
+
PRIMARY KEY(run_id, seq)
|
|
53
|
+
);
|
|
54
|
+
CREATE TABLE IF NOT EXISTS records (
|
|
55
|
+
bucket TEXT NOT NULL, id TEXT NOT NULL, value TEXT NOT NULL,
|
|
56
|
+
PRIMARY KEY(bucket, id)
|
|
57
|
+
);
|
|
58
|
+
`);
|
|
59
|
+
}
|
|
60
|
+
transaction(action) {
|
|
61
|
+
return this.write(() => {
|
|
62
|
+
this.db.exec('BEGIN IMMEDIATE');
|
|
63
|
+
try {
|
|
64
|
+
const result = action();
|
|
65
|
+
this.db.exec('COMMIT');
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
try {
|
|
70
|
+
this.db.exec('ROLLBACK');
|
|
71
|
+
}
|
|
72
|
+
catch { /* preserve the original storage failure */ }
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
get(bucket, id) {
|
|
78
|
+
const row = this.db.prepare('SELECT value FROM records WHERE bucket=? AND id=?').get(bucket, id);
|
|
79
|
+
return row ? JSON.parse(row.value) : undefined;
|
|
80
|
+
}
|
|
81
|
+
set(bucket, id, value) {
|
|
82
|
+
this.write(() => this.db.prepare('INSERT INTO records VALUES (?, ?, ?) ON CONFLICT(bucket,id) DO UPDATE SET value=excluded.value')
|
|
83
|
+
.run(bucket, id, JSON.stringify(value)));
|
|
84
|
+
}
|
|
85
|
+
list(bucket) {
|
|
86
|
+
return this.db.prepare('SELECT value FROM records WHERE bucket=? ORDER BY rowid').all(bucket)
|
|
87
|
+
.map((row) => JSON.parse(row.value));
|
|
88
|
+
}
|
|
89
|
+
/** The command and its first replayable receipt are one durable operation. */
|
|
90
|
+
accept(request, queuedEntry) {
|
|
91
|
+
return this.transaction(() => {
|
|
92
|
+
const payload = canonical(request);
|
|
93
|
+
const old = this.db.prepare('SELECT command, cursor FROM runs WHERE id=?').get(request.id);
|
|
94
|
+
if (old?.command && old.command !== payload)
|
|
95
|
+
throw new Error('This request ID already belongs to different input.');
|
|
96
|
+
if (old)
|
|
97
|
+
return { duplicate: true, frames: this.read(request.id).frames };
|
|
98
|
+
this.db.prepare('INSERT INTO runs (id,command,conversation,updated) VALUES (?,?,?,?)')
|
|
99
|
+
.run(request.id, payload, conversationKey(request), Date.now());
|
|
100
|
+
const scope = {
|
|
101
|
+
kind: request.kind,
|
|
102
|
+
...('repoId' in request ? { repoId: request.repoId } : {}),
|
|
103
|
+
...('workerId' in request ? { workerId: request.workerId } : {}),
|
|
104
|
+
...('mode' in request ? { mode: request.mode } : {}),
|
|
105
|
+
};
|
|
106
|
+
const frames = [this.appendInside(request.id, { type: 'accepted', requestId: request.id, scope })];
|
|
107
|
+
if (queuedEntry) {
|
|
108
|
+
this.set('queue', request.id, queuedEntry);
|
|
109
|
+
frames.push(this.appendInside(request.id, { type: 'queue_state', requestId: request.id, state: 'queued' }));
|
|
110
|
+
}
|
|
111
|
+
return { duplicate: false, frames };
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
appendInside(id, event) {
|
|
115
|
+
this.db.prepare('INSERT OR IGNORE INTO runs (id,updated) VALUES (?,?)').run(id, Date.now());
|
|
116
|
+
const run = this.db.prepare('SELECT cursor, terminal FROM runs WHERE id=?').get(id);
|
|
117
|
+
if (run.terminal) {
|
|
118
|
+
// Reconfirm the committed outcome, never continue a completed attempt.
|
|
119
|
+
this.set('protocol-fault', id, { type: event.type, at: new Date().toISOString() });
|
|
120
|
+
return JSON.parse(run.terminal);
|
|
121
|
+
}
|
|
122
|
+
const frame = { ...event, seq: run.cursor + 1 };
|
|
123
|
+
const serialized = JSON.stringify(frame);
|
|
124
|
+
const terminal = terminalTypes.has(String(event.type));
|
|
125
|
+
const state = terminal ? String(event.type) : event.type === 'accepted' ? 'accepted' : event.type === 'queue_state' ? String(event.state) : 'running';
|
|
126
|
+
this.db.prepare('INSERT INTO events VALUES (?,?,?)').run(id, frame.seq, serialized);
|
|
127
|
+
this.db.prepare('UPDATE runs SET cursor=?, terminal=?, state=?, updated=? WHERE id=?')
|
|
128
|
+
.run(frame.seq, terminal ? serialized : null, state, Date.now(), id);
|
|
129
|
+
return frame;
|
|
130
|
+
}
|
|
131
|
+
append(id, event) {
|
|
132
|
+
return this.transaction(() => this.appendInside(id, event));
|
|
133
|
+
}
|
|
134
|
+
status(id) {
|
|
135
|
+
const run = this.db.prepare('SELECT cursor, terminal FROM runs WHERE id=?').get(id);
|
|
136
|
+
return { exists: !!run, maxSeq: run?.cursor ?? 0, terminated: !!run?.terminal };
|
|
137
|
+
}
|
|
138
|
+
read(id) {
|
|
139
|
+
const status = this.status(id);
|
|
140
|
+
if (!status.exists)
|
|
141
|
+
return { ...status, frames: [] };
|
|
142
|
+
const frames = this.db.prepare('SELECT frame FROM events WHERE run_id=? ORDER BY seq').all(id)
|
|
143
|
+
.map((row) => JSON.parse(row.frame));
|
|
144
|
+
return { ...status, frames };
|
|
145
|
+
}
|
|
146
|
+
importLog(id, state) {
|
|
147
|
+
if (!state.exists || this.read(id).exists)
|
|
148
|
+
return;
|
|
149
|
+
this.transaction(() => {
|
|
150
|
+
const last = [...state.frames].reverse().find((f) => terminalTypes.has(String(f.type)));
|
|
151
|
+
this.db.prepare('INSERT OR IGNORE INTO runs (id,cursor,terminal,state,updated) VALUES (?,?,?,?,?)')
|
|
152
|
+
.run(id, state.maxSeq, last ? JSON.stringify(last) : null, last ? String(last.type) : 'interrupted_unknown', Date.now());
|
|
153
|
+
for (const frame of state.frames)
|
|
154
|
+
this.db.prepare('INSERT OR IGNORE INTO events VALUES (?,?,?)').run(id, frame.seq, JSON.stringify(frame));
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
commands(conversation, page) {
|
|
158
|
+
const rows = page
|
|
159
|
+
? this.db.prepare(`SELECT id,command FROM runs WHERE conversation=? AND command IS NOT NULL
|
|
160
|
+
AND rowid < COALESCE((SELECT rowid FROM runs WHERE id=?), 9223372036854775807)
|
|
161
|
+
AND rowid > COALESCE((SELECT rowid FROM runs WHERE id=?), 0)
|
|
162
|
+
ORDER BY rowid DESC LIMIT ?`).all(conversation, page.before ?? '', page.after ?? '', page.limit).reverse()
|
|
163
|
+
: this.db.prepare('SELECT id,command FROM runs WHERE conversation=? AND command IS NOT NULL ORDER BY rowid').all(conversation);
|
|
164
|
+
return rows.map((row) => ({ request: JSON.parse(row.command), state: this.read(row.id) }));
|
|
165
|
+
}
|
|
166
|
+
latestSession(conversation, after) {
|
|
167
|
+
return this.db.prepare(`SELECT json_extract(events.frame, '$.sessionId') AS session
|
|
168
|
+
FROM runs JOIN events ON events.run_id=runs.id WHERE runs.conversation=?
|
|
169
|
+
AND json_type(events.frame, '$.sessionId')='text'
|
|
170
|
+
AND runs.rowid > COALESCE((SELECT rowid FROM runs WHERE id=?), 0)
|
|
171
|
+
ORDER BY runs.rowid DESC, events.seq DESC LIMIT 1`).get(conversation, after ?? '')?.session;
|
|
172
|
+
}
|
|
173
|
+
lastCommand(conversation) {
|
|
174
|
+
return this.db.prepare('SELECT id FROM runs WHERE conversation=? AND command IS NOT NULL ORDER BY rowid DESC LIMIT 1').get(conversation)?.id;
|
|
175
|
+
}
|
|
176
|
+
unfinished(conversation) {
|
|
177
|
+
return !!this.db.prepare('SELECT id FROM runs WHERE conversation=? AND terminal IS NULL LIMIT 1').get(conversation);
|
|
178
|
+
}
|
|
179
|
+
reconcile(owns) {
|
|
180
|
+
for (const row of this.db.prepare('SELECT id FROM runs WHERE terminal IS NULL').all()) {
|
|
181
|
+
if (owns(row.id))
|
|
182
|
+
continue;
|
|
183
|
+
this.append(row.id, { type: 'error', requestId: row.id, code: 'interrupted_unknown',
|
|
184
|
+
message: 'Lucid restarted before this turn settled. It was not run again. Review its work before continuing.' });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
close() { this.db.close(); }
|
|
188
|
+
}
|
package/dist/setup/server.js
CHANGED
|
@@ -86,7 +86,7 @@ export async function checkSetupProviders() {
|
|
|
86
86
|
};
|
|
87
87
|
}
|
|
88
88
|
catch (e) {
|
|
89
|
-
const health = providerFailure(e);
|
|
89
|
+
const health = providerFailure(e) ?? { auth: 'unknown', problem: 'unavailable' };
|
|
90
90
|
return {
|
|
91
91
|
id,
|
|
92
92
|
label: id === 'claude' ? 'Claude' : 'Codex',
|
package/dist/turnLog.d.ts
CHANGED
|
@@ -1,19 +1,8 @@
|
|
|
1
|
+
import { RuntimeStore } from './runtimeStore.js';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* appended (with a monotonic `seq`) BEFORE it goes on the socket, so a frame produced
|
|
6
|
-
* while the socket is down — or while the phone is away — survives on disk. The phone
|
|
7
|
-
* recovers by sending a `pull` request; the daemon replays `readState().frames` after
|
|
8
|
-
* the phone's `sinceSeq`.
|
|
9
|
-
*
|
|
10
|
-
* The daemon OWNS `seq` here (starting at 1, per requestId). This is the linchpin of the
|
|
11
|
-
* whole recovery scheme: the same `seq` must appear on the live-forwarded frame, the
|
|
12
|
-
* server's replay-on-rejoin, and a daemon pull, so the phone can de-dup across all three.
|
|
13
|
-
* The relay forwards frames verbatim (it no longer stamps `seq`).
|
|
14
|
-
*
|
|
15
|
-
* Never throws — a logging or disk error must not take a turn down; it just makes that
|
|
16
|
-
* turn unrecoverable (the live frame still goes out). Mirrors `runlog.ts`.
|
|
3
|
+
* SQLite owns command receipts and ordered run events. JSONL files remain bounded
|
|
4
|
+
* diagnostic exports and are imported once when opening an older installation.
|
|
5
|
+
* A storage failure rejects admission or propagates to the execution owner.
|
|
17
6
|
*/
|
|
18
7
|
/** A logged frame: the exact wire frame plus its daemon-authored `seq`. */
|
|
19
8
|
export type TurnFrame = Record<string, unknown> & {
|
|
@@ -35,34 +24,13 @@ export interface TurnState {
|
|
|
35
24
|
export declare const MAX_TURN_LOG_BYTES: number;
|
|
36
25
|
/** Default retention: prune turn logs untouched for 24h. */
|
|
37
26
|
export declare const TURN_LOG_MAX_AGE_MS: number;
|
|
38
|
-
/**
|
|
39
|
-
* One turn's log. Owns the per-requestId `seq` counter (a shared handle, NOT a closure
|
|
40
|
-
* local, so a concurrent `cancel`'s terminal frame gets the next `seq` on the same
|
|
41
|
-
* counter). Seeds itself from any existing file so seq keeps climbing across a daemon
|
|
42
|
-
* restart (a re-created handle resumes from the file's `maxSeq`).
|
|
43
|
-
*/
|
|
44
|
-
export declare class TurnLog {
|
|
45
|
-
private readonly path;
|
|
46
|
-
private readonly maxBytes;
|
|
47
|
-
private nextSeq;
|
|
48
|
-
private bytes;
|
|
49
|
-
private terminatedFlag;
|
|
50
|
-
constructor(requestId: string, dir: string, maxBytes?: number);
|
|
51
|
-
get terminated(): boolean;
|
|
52
|
-
/** Stamp the next `seq`, append the frame (unless a droppable delta past the byte cap),
|
|
53
|
-
* and return the stamped frame to put on the wire. */
|
|
54
|
-
write(event: Record<string, unknown>): TurnFrame;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* All turn logs for this daemon. Keeps an in-memory {@link TurnLog} per in-flight turn
|
|
58
|
-
* (the live seq counter), dropped once the turn terminates — the file lives on for 24h.
|
|
59
|
-
* The file is the source of truth for replay ({@link read}).
|
|
60
|
-
*/
|
|
27
|
+
/** Compatibility reader/exporter around the transactional local runtime ledger. */
|
|
61
28
|
export declare class TurnLogStore {
|
|
62
29
|
private readonly dir;
|
|
63
30
|
private readonly clock;
|
|
64
31
|
private readonly maxBytes;
|
|
65
|
-
private readonly
|
|
32
|
+
private readonly exportBytes;
|
|
33
|
+
readonly runtime: RuntimeStore;
|
|
66
34
|
queueDirectory(): string;
|
|
67
35
|
constructor(opts?: {
|
|
68
36
|
dir?: string;
|
|
@@ -71,8 +39,10 @@ export declare class TurnLogStore {
|
|
|
71
39
|
});
|
|
72
40
|
/** Log + stamp one outgoing stream frame; returns the stamped frame to send. */
|
|
73
41
|
append(requestId: string, event: Record<string, unknown>): TurnFrame;
|
|
74
|
-
/** The durable state of a turn, read from disk — for serving a `pull`. */
|
|
75
42
|
read(requestId: string): TurnState;
|
|
43
|
+
/** Startup recovery is independent of whether a phone asks to replay a turn. */
|
|
44
|
+
reconcile(owns: (id: string) => boolean): void;
|
|
45
|
+
close(): void;
|
|
76
46
|
/** Delete turn logs whose file hasn't been touched in `maxAgeMs`. Never throws. */
|
|
77
47
|
prune(maxAgeMs?: number): void;
|
|
78
48
|
}
|
package/dist/turnLog.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"turnLog.d.ts","sourceRoot":"","sources":["../src/turnLog.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"turnLog.d.ts","sourceRoot":"","sources":["../src/turnLog.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD;;;;GAIG;AAEH,2EAA2E;AAC3E,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAElE,MAAM,WAAW,SAAS;IACxB,kFAAkF;IAClF,MAAM,EAAE,OAAO,CAAC;IAChB,8CAA8C;IAC9C,MAAM,EAAE,SAAS,EAAE,CAAC;IACpB,8CAA8C;IAC9C,MAAM,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,UAAU,EAAE,OAAO,CAAC;CACrB;AAUD;;sFAEsF;AACtF,eAAO,MAAM,kBAAkB,QAAmB,CAAC;AAEnD,4DAA4D;AAC5D,eAAO,MAAM,mBAAmB,QAAsB,CAAC;AA0DvD,mFAAmF;AACnF,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAe;IACrC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA6B;IACzD,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAE/B,cAAc,IAAI,MAAM;gBAKtB,IAAI,GAAE;QAAE,GAAG,CAAC,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAO;IAapE,gFAAgF;IAChF,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS;IAgBpE,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS;IAOlC,gFAAgF;IAChF,SAAS,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI;IAM9C,KAAK,IAAI,IAAI;IAEb,mFAAmF;IACnF,KAAK,CAAC,QAAQ,GAAE,MAA4B,GAAG,IAAI;CAkBpD"}
|
package/dist/turnLog.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, statSync, unlinkSync, } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { TURNS_DIR } from './config.js';
|
|
4
|
+
import { RuntimeStore } from './runtimeStore.js';
|
|
4
5
|
/** Frame `type`s that end a turn. Mirrors the relay's TERMINAL_TYPES — `code_result`
|
|
5
6
|
* and `clone_result` are NOT terminal (a `done` follows). */
|
|
6
7
|
const TERMINAL_TYPES = new Set(['done', 'error', 'cancelled']);
|
|
@@ -72,56 +73,13 @@ function appendLine(path, line) {
|
|
|
72
73
|
return false;
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
|
-
/**
|
|
76
|
-
* One turn's log. Owns the per-requestId `seq` counter (a shared handle, NOT a closure
|
|
77
|
-
* local, so a concurrent `cancel`'s terminal frame gets the next `seq` on the same
|
|
78
|
-
* counter). Seeds itself from any existing file so seq keeps climbing across a daemon
|
|
79
|
-
* restart (a re-created handle resumes from the file's `maxSeq`).
|
|
80
|
-
*/
|
|
81
|
-
export class TurnLog {
|
|
82
|
-
path;
|
|
83
|
-
maxBytes;
|
|
84
|
-
nextSeq;
|
|
85
|
-
bytes;
|
|
86
|
-
terminatedFlag;
|
|
87
|
-
constructor(requestId, dir, maxBytes = MAX_TURN_LOG_BYTES) {
|
|
88
|
-
this.path = join(dir, `${requestId}.jsonl`);
|
|
89
|
-
this.maxBytes = maxBytes;
|
|
90
|
-
const st = scanTurnFile(this.path);
|
|
91
|
-
this.nextSeq = st.maxSeq + 1;
|
|
92
|
-
this.bytes = st.bytes;
|
|
93
|
-
this.terminatedFlag = st.terminated;
|
|
94
|
-
}
|
|
95
|
-
get terminated() {
|
|
96
|
-
return this.terminatedFlag;
|
|
97
|
-
}
|
|
98
|
-
/** Stamp the next `seq`, append the frame (unless a droppable delta past the byte cap),
|
|
99
|
-
* and return the stamped frame to put on the wire. */
|
|
100
|
-
write(event) {
|
|
101
|
-
const seq = this.nextSeq++;
|
|
102
|
-
const frame = { ...event, seq };
|
|
103
|
-
const type = typeof event.type === 'string' ? event.type : '';
|
|
104
|
-
const droppable = type === 'delta' && this.bytes >= this.maxBytes;
|
|
105
|
-
if (!droppable) {
|
|
106
|
-
const line = JSON.stringify(frame) + '\n';
|
|
107
|
-
if (appendLine(this.path, line))
|
|
108
|
-
this.bytes += Buffer.byteLength(line, 'utf8');
|
|
109
|
-
}
|
|
110
|
-
if (isTerminalType(type))
|
|
111
|
-
this.terminatedFlag = true;
|
|
112
|
-
return frame;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* All turn logs for this daemon. Keeps an in-memory {@link TurnLog} per in-flight turn
|
|
117
|
-
* (the live seq counter), dropped once the turn terminates — the file lives on for 24h.
|
|
118
|
-
* The file is the source of truth for replay ({@link read}).
|
|
119
|
-
*/
|
|
76
|
+
/** Compatibility reader/exporter around the transactional local runtime ledger. */
|
|
120
77
|
export class TurnLogStore {
|
|
121
78
|
dir;
|
|
122
79
|
clock;
|
|
123
80
|
maxBytes;
|
|
124
|
-
|
|
81
|
+
exportBytes = new Map();
|
|
82
|
+
runtime;
|
|
125
83
|
queueDirectory() {
|
|
126
84
|
return join(this.dir, 'queue');
|
|
127
85
|
}
|
|
@@ -129,6 +87,7 @@ export class TurnLogStore {
|
|
|
129
87
|
this.dir = opts.dir ?? TURNS_DIR;
|
|
130
88
|
this.clock = opts.now ?? Date.now;
|
|
131
89
|
this.maxBytes = opts.maxBytes ?? MAX_TURN_LOG_BYTES;
|
|
90
|
+
this.runtime = new RuntimeStore(join(this.dir, 'runtime.sqlite'));
|
|
132
91
|
try {
|
|
133
92
|
mkdirSync(this.dir, { recursive: true });
|
|
134
93
|
}
|
|
@@ -138,21 +97,39 @@ export class TurnLogStore {
|
|
|
138
97
|
}
|
|
139
98
|
/** Log + stamp one outgoing stream frame; returns the stamped frame to send. */
|
|
140
99
|
append(requestId, event) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
100
|
+
if (!this.runtime.status(requestId).exists)
|
|
101
|
+
this.read(requestId);
|
|
102
|
+
const before = this.runtime.status(requestId);
|
|
103
|
+
const frame = this.runtime.append(requestId, event);
|
|
104
|
+
if (before.terminated)
|
|
105
|
+
return frame;
|
|
106
|
+
// Bounded diagnostic exports are optional; a failed export never loses the SQLite event.
|
|
107
|
+
const path = join(this.dir, `${requestId}.jsonl`);
|
|
108
|
+
const bytes = this.exportBytes.get(requestId) ?? (existsSync(path) ? statSync(path).size : 0);
|
|
109
|
+
if (event.type !== 'delta' || bytes < this.maxBytes) {
|
|
110
|
+
const line = JSON.stringify(frame) + '\n';
|
|
111
|
+
if (appendLine(path, line))
|
|
112
|
+
this.exportBytes.set(requestId, bytes + Buffer.byteLength(line));
|
|
145
113
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
this.logs.delete(requestId); // free memory; file persists
|
|
114
|
+
if (TERMINAL_TYPES.has(String(event.type)))
|
|
115
|
+
this.exportBytes.delete(requestId);
|
|
149
116
|
return frame;
|
|
150
117
|
}
|
|
151
|
-
/** The durable state of a turn, read from disk — for serving a `pull`. */
|
|
152
118
|
read(requestId) {
|
|
153
|
-
const
|
|
154
|
-
|
|
119
|
+
const state = this.runtime.read(requestId);
|
|
120
|
+
if (state.exists)
|
|
121
|
+
return state;
|
|
122
|
+
this.runtime.importLog(requestId, scanTurnFile(join(this.dir, `${requestId}.jsonl`)));
|
|
123
|
+
return this.runtime.read(requestId);
|
|
124
|
+
}
|
|
125
|
+
/** Startup recovery is independent of whether a phone asks to replay a turn. */
|
|
126
|
+
reconcile(owns) {
|
|
127
|
+
for (const name of readdirSync(this.dir))
|
|
128
|
+
if (name.endsWith('.jsonl'))
|
|
129
|
+
this.read(name.slice(0, -6));
|
|
130
|
+
this.runtime.reconcile(owns);
|
|
155
131
|
}
|
|
132
|
+
close() { this.runtime.close(); }
|
|
156
133
|
/** Delete turn logs whose file hasn't been touched in `maxAgeMs`. Never throws. */
|
|
157
134
|
prune(maxAgeMs = TURN_LOG_MAX_AGE_MS) {
|
|
158
135
|
const now = this.clock();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opencorelabs/lucidity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Lucidity's local personal agent, command-line interface, and MCP server",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "AGPL-3.0-only",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"native"
|
|
26
26
|
],
|
|
27
27
|
"engines": {
|
|
28
|
-
"node": ">=22"
|
|
28
|
+
"node": ">=22.13.0"
|
|
29
29
|
},
|
|
30
30
|
"keywords": [
|
|
31
31
|
"lucidity",
|
|
@@ -42,9 +42,9 @@
|
|
|
42
42
|
"chrono-node": "^2.10.1",
|
|
43
43
|
"zod": "^4.3.5 ^4.3.6",
|
|
44
44
|
"@js-temporal/polyfill": "^0.5.1",
|
|
45
|
-
"@anthropic-ai/claude-agent-sdk": "0.3.
|
|
45
|
+
"@anthropic-ai/claude-agent-sdk": "0.3.287",
|
|
46
46
|
"@modelcontextprotocol/sdk": "1.29.0 ^1.12.1",
|
|
47
|
-
"@openai/codex": "0.
|
|
47
|
+
"@openai/codex": "0.160.0",
|
|
48
48
|
"croner": "^10.0.0",
|
|
49
49
|
"uuidv7": "^1.1.0",
|
|
50
50
|
"ws": "^8.18.1",
|
package/src/automations/agent.ts
CHANGED
package/src/chat/server.ts
CHANGED
|
@@ -23,6 +23,7 @@ import type { McpWiring } from '../mcp/config.js';
|
|
|
23
23
|
import type { AutomationService } from '../automations/service.js';
|
|
24
24
|
|
|
25
25
|
export interface ChatServerDeps {
|
|
26
|
+
signal?: AbortSignal;
|
|
26
27
|
automations?: AutomationService;
|
|
27
28
|
config: DaemonConfig;
|
|
28
29
|
executor: AgentExecutor;
|
|
@@ -73,6 +74,11 @@ export function createChatServer(deps: ChatServerDeps): Server {
|
|
|
73
74
|
req: IncomingMessage,
|
|
74
75
|
res: ServerResponse,
|
|
75
76
|
): Promise<void> {
|
|
77
|
+
if (deps.signal?.aborted) {
|
|
78
|
+
res.writeHead(503);
|
|
79
|
+
res.end('Lucidity is restarting. Reconnect before sending.');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
76
82
|
if (req.method === 'GET' && req.url === '/health') {
|
|
77
83
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
78
84
|
res.end(JSON.stringify({ ok: true }));
|
|
@@ -170,7 +176,7 @@ export function createChatServer(deps: ChatServerDeps): Server {
|
|
|
170
176
|
const input = await buildChatInput();
|
|
171
177
|
for await (const ev of executor.runStream!({
|
|
172
178
|
...input,
|
|
173
|
-
signal: ac.signal,
|
|
179
|
+
signal: deps.signal ? AbortSignal.any([ac.signal, deps.signal]) : ac.signal,
|
|
174
180
|
})) {
|
|
175
181
|
if (ev.type === 'session')
|
|
176
182
|
send({ type: 'session', sessionId: ev.sessionId });
|