@osolmaz/pi-workflows 0.13.1 → 0.13.3
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 +9 -7
- package/dist/builtins/autodoc.workflow.d.ts +5 -5
- package/dist/builtins/autoimplement.workflow.d.ts +69 -61
- package/dist/builtins/autoimplement.workflow.js +47 -8
- package/dist/builtins/autoimplement.workflow.js.map +1 -1
- package/dist/builtins/autoplan.workflow.d.ts +11 -5
- package/dist/builtins/autoplan.workflow.js +68 -32
- package/dist/builtins/autoplan.workflow.js.map +1 -1
- package/dist/builtins/catalog.js +4 -4
- package/dist/builtins/change-verification.workflow.d.ts +1 -1
- package/dist/builtins/plain-summary.workflow.d.ts +1 -1
- package/dist/builtins/plain-summary.workflow.js +35 -24
- package/dist/builtins/plain-summary.workflow.js.map +1 -1
- package/dist/builtins/plan-approval.workflow.d.ts +1 -1
- package/dist/builtins/plan-change.workflow.d.ts +25 -23
- package/dist/builtins/sanity-check.workflow.d.ts +3 -3
- package/dist/builtins/sanity-check.workflow.js +19 -22
- package/dist/builtins/sanity-check.workflow.js.map +1 -1
- package/dist/builtins/workspace-preparation.workflow.d.ts +1 -1
- package/dist/controllers/index.d.ts +2 -2
- package/dist/controllers/index.js.map +1 -1
- package/dist/controllers/sqlite.js +88 -61
- package/dist/controllers/sqlite.js.map +1 -1
- package/dist/controllers/types.d.ts +34 -0
- package/dist/controllers/workflow-engine-scheduler.d.ts +5 -1
- package/dist/controllers/workflow-engine-scheduler.js +83 -1
- package/dist/controllers/workflow-engine-scheduler.js.map +1 -1
- package/dist/controllers/workflows.d.ts +8 -1
- package/dist/controllers/workflows.js +36 -0
- package/dist/controllers/workflows.js.map +1 -1
- package/dist/extension/controller-host.d.ts +2 -1
- package/dist/extension/controller-host.js +1 -1
- package/dist/extension/controller-host.js.map +1 -1
- package/dist/extension/decision-channels.js +45 -7
- package/dist/extension/decision-channels.js.map +1 -1
- package/dist/extension/follow-up-coordinator.d.ts +27 -0
- package/dist/extension/follow-up-coordinator.js +131 -0
- package/dist/extension/follow-up-coordinator.js.map +1 -0
- package/dist/extension/index.d.ts +9 -0
- package/dist/extension/index.js +462 -76
- package/dist/extension/index.js.map +1 -1
- package/dist/extension/recorder.d.ts +1 -1
- package/dist/extension/recorder.js +8 -8
- package/dist/extension/recorder.js.map +1 -1
- package/dist/extension/session-events.d.ts +2 -3
- package/dist/extension/session-events.js +11 -10
- package/dist/extension/session-events.js.map +1 -1
- package/dist/extension/widget.js +9 -0
- package/dist/extension/widget.js.map +1 -1
- package/dist/state/database.d.ts +2 -1
- package/dist/state/database.js +11 -13
- package/dist/state/database.js.map +1 -1
- package/dist/state/json.js +6 -1
- package/dist/state/json.js.map +1 -1
- package/dist/state/mutation.d.ts +1 -1
- package/dist/state/mutation.js.map +1 -1
- package/dist/state/prune.d.ts +18 -0
- package/dist/state/prune.js +373 -0
- package/dist/state/prune.js.map +1 -0
- package/dist/state/schema.d.ts +1 -1
- package/dist/state/schema.js +130 -13
- package/dist/state/schema.js.map +1 -1
- package/dist/viewer/cli.d.ts +3 -1
- package/dist/viewer/cli.js +55 -4
- package/dist/viewer/cli.js.map +1 -1
- package/dist/viewer/render.js +19 -1
- package/dist/viewer/render.js.map +1 -1
- package/dist/viewer/session-reducer.d.ts +0 -1
- package/dist/viewer/session-reducer.js +2 -24
- package/dist/viewer/session-reducer.js.map +1 -1
- package/dist/workflows/composition.d.ts +2 -0
- package/dist/workflows/composition.js +15 -0
- package/dist/workflows/composition.js.map +1 -1
- package/dist/workflows/definition.d.ts +6 -3
- package/dist/workflows/definition.js +3 -0
- package/dist/workflows/definition.js.map +1 -1
- package/dist/workflows/engine.d.ts +6 -1
- package/dist/workflows/engine.js +303 -36
- package/dist/workflows/engine.js.map +1 -1
- package/dist/workflows/human-decision.d.ts +1 -0
- package/dist/workflows/human-decision.js +25 -9
- package/dist/workflows/human-decision.js.map +1 -1
- package/dist/workflows/index.d.ts +3 -1
- package/dist/workflows/index.js +2 -0
- package/dist/workflows/index.js.map +1 -1
- package/dist/workflows/json-patch.d.ts +47 -0
- package/dist/workflows/json-patch.js +298 -0
- package/dist/workflows/json-patch.js.map +1 -0
- package/dist/workflows/schema.js +29 -1
- package/dist/workflows/schema.js.map +1 -1
- package/dist/workflows/settings.d.ts +128 -0
- package/dist/workflows/settings.js +199 -0
- package/dist/workflows/settings.js.map +1 -0
- package/dist/workflows/store.d.ts +81 -3
- package/dist/workflows/store.js +1563 -50
- package/dist/workflows/store.js.map +1 -1
- package/dist/workflows/tool-input.d.ts +22 -0
- package/dist/workflows/tool-input.js +43 -0
- package/dist/workflows/tool-input.js.map +1 -1
- package/dist/workflows/types.d.ts +41 -6
- package/docs/2026-08-25-workflow-follow-ups.md +132 -0
- package/docs/2026-08-25-workflow-settings.md +169 -0
- package/docs/DEFERRED_TURNS.md +6 -0
- package/docs/DESIGN_PHILOSOPHY.md +2 -0
- package/docs/SQLITE_STATE.md +36 -17
- package/docs/WORKFLOW_COMPOSITION.md +15 -0
- package/docs/WORKFLOW_STEP_MESSAGES.md +4 -2
- package/docs/plans/2026-08-25-autoplan-user-intent-capture-plan.md +107 -0
- package/docs/plans/2026-08-25-live-workflow-settings-plan.md +532 -0
- package/docs/plans/2026-08-25-workflow-run-storage-plan.md +67 -0
- package/docs/session-event-journal.md +2 -3
- package/docs/tui-viewer.md +7 -6
- package/docs/workflows.md +58 -10
- package/examples/workflows/live-settings.workflow.ts +93 -0
- package/herdr-plugin.toml +1 -1
- package/package.json +1 -1
- package/src/builtins/autoimplement.workflow.ts +56 -8
- package/src/builtins/autoplan.workflow.ts +80 -32
- package/src/builtins/catalog.ts +4 -4
- package/src/builtins/plain-summary.workflow.ts +38 -40
- package/src/builtins/sanity-check.workflow.ts +19 -22
- package/src/controllers/index.ts +6 -0
- package/src/controllers/sqlite.ts +132 -72
- package/src/controllers/types.ts +40 -0
- package/src/controllers/workflow-engine-scheduler.ts +103 -1
- package/src/controllers/workflows.ts +68 -0
- package/src/extension/controller-host.ts +6 -1
- package/src/extension/decision-channels.ts +47 -7
- package/src/extension/follow-up-coordinator.ts +151 -0
- package/src/extension/index.ts +540 -89
- package/src/extension/recorder.ts +10 -8
- package/src/extension/session-events.ts +15 -13
- package/src/extension/widget.ts +8 -0
- package/src/state/database.ts +12 -12
- package/src/state/json.ts +6 -1
- package/src/state/mutation.ts +4 -1
- package/src/state/prune.ts +502 -0
- package/src/state/schema.ts +130 -13
- package/src/viewer/cli.ts +52 -5
- package/src/viewer/render.ts +43 -1
- package/src/viewer/session-reducer.ts +2 -29
- package/src/workflows/composition.ts +19 -0
- package/src/workflows/definition.ts +19 -5
- package/src/workflows/engine.ts +365 -36
- package/src/workflows/human-decision.ts +41 -11
- package/src/workflows/index.ts +43 -0
- package/src/workflows/json-patch.ts +375 -0
- package/src/workflows/schema.ts +31 -1
- package/src/workflows/settings.ts +430 -0
- package/src/workflows/store.ts +2268 -121
- package/src/workflows/tool-input.ts +58 -0
- package/src/workflows/types.ts +43 -5
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { compositionMetadata } from "../workflows/composition.js";
|
|
2
|
+
import { WorkflowEngine, workflowIdentityMismatch } from "../workflows/engine.js";
|
|
2
3
|
import type { WorkflowRunStore } from "../workflows/store.js";
|
|
3
4
|
import type {
|
|
4
5
|
WorkflowDefinition,
|
|
@@ -7,6 +8,14 @@ import type {
|
|
|
7
8
|
WorkflowSource,
|
|
8
9
|
} from "../workflows/types.js";
|
|
9
10
|
import type {
|
|
11
|
+
ControllerFollowUpResult,
|
|
12
|
+
ControllerQueueFollowUpRequest,
|
|
13
|
+
ControllerRemoveFollowUpRequest,
|
|
14
|
+
ControllerSettingsChangeRequest,
|
|
15
|
+
ControllerSettingsChangeResult,
|
|
16
|
+
} from "./types.js";
|
|
17
|
+
import type {
|
|
18
|
+
ControllerWorkflowControlRequest,
|
|
10
19
|
ControllerWorkflowScheduler,
|
|
11
20
|
WorkflowSchedulerRequest,
|
|
12
21
|
WorkflowSchedulerResult,
|
|
@@ -96,6 +105,99 @@ export class WorkflowEngineScheduler implements ControllerWorkflowScheduler {
|
|
|
96
105
|
return { state: "running", runId };
|
|
97
106
|
}
|
|
98
107
|
|
|
108
|
+
async changeSettings(
|
|
109
|
+
request: ControllerWorkflowControlRequest<ControllerSettingsChangeRequest>,
|
|
110
|
+
signal: AbortSignal,
|
|
111
|
+
): Promise<ControllerSettingsChangeResult> {
|
|
112
|
+
signal.throwIfAborted();
|
|
113
|
+
const bundle = this.options.store.readRun(request.runId);
|
|
114
|
+
if (bundle === null) throw new Error(`Workflow run not found: ${request.runId}`);
|
|
115
|
+
const resolved = await this.options.resolveWorkflow(bundle.state.workflowName);
|
|
116
|
+
signal.throwIfAborted();
|
|
117
|
+
if (workflowIdentityMismatch(bundle.state, resolved.workflow, resolved.workflowSource)) {
|
|
118
|
+
throw new Error(`Workflow source changed since run ${request.runId} started`);
|
|
119
|
+
}
|
|
120
|
+
const scopes = this.options.store.listSettingsScopes(request.runId);
|
|
121
|
+
const scopeId = request.scopeId ?? (scopes.length === 1 ? scopes[0]?.scopeId : undefined);
|
|
122
|
+
if (scopeId === undefined) {
|
|
123
|
+
throw new Error("Controller settings changes require scopeId when several scopes exist");
|
|
124
|
+
}
|
|
125
|
+
const scope = this.options.store.getSettingsScope(scopeId);
|
|
126
|
+
if (scope === undefined || scope.activeRunId !== request.runId) {
|
|
127
|
+
throw new Error(`Workflow settings scope not found: ${scopeId}`);
|
|
128
|
+
}
|
|
129
|
+
const definition =
|
|
130
|
+
scope.mountPath === ""
|
|
131
|
+
? resolved.workflow.settings
|
|
132
|
+
: compositionMetadata(resolved.workflow)?.scopes[scope.mountPath]?.settings;
|
|
133
|
+
if (definition === undefined) {
|
|
134
|
+
throw new Error(`Workflow scope ${scopeId} does not declare editable settings`);
|
|
135
|
+
}
|
|
136
|
+
const result = await this.options.store.changeSettings(definition, {
|
|
137
|
+
runId: request.runId,
|
|
138
|
+
scopeId,
|
|
139
|
+
requestId: request.actorRequestKey,
|
|
140
|
+
...(request.expectedChangeNumber !== undefined
|
|
141
|
+
? { expectedChangeNumber: request.expectedChangeNumber }
|
|
142
|
+
: {}),
|
|
143
|
+
actor: { type: "controller", id: request.controllerResourceUid },
|
|
144
|
+
source: "controller-request",
|
|
145
|
+
patch: request.patch,
|
|
146
|
+
});
|
|
147
|
+
return {
|
|
148
|
+
runId: request.runId,
|
|
149
|
+
scopeId,
|
|
150
|
+
changeNumber: result.scope.changeNumber,
|
|
151
|
+
adopted: result.adopted,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async queueFollowUp(
|
|
156
|
+
request: ControllerWorkflowControlRequest<ControllerQueueFollowUpRequest>,
|
|
157
|
+
signal: AbortSignal,
|
|
158
|
+
): Promise<ControllerFollowUpResult> {
|
|
159
|
+
signal.throwIfAborted();
|
|
160
|
+
const targetSessionId = this.options.store.originSessionId(request.runId);
|
|
161
|
+
if (targetSessionId === undefined) {
|
|
162
|
+
throw new Error("Controller follow-up requires a workflow run with an origin Pi session");
|
|
163
|
+
}
|
|
164
|
+
const result = this.options.store.queueFollowUp({
|
|
165
|
+
runId: request.runId,
|
|
166
|
+
requestId: request.actorRequestKey,
|
|
167
|
+
targetSessionId,
|
|
168
|
+
actor: { type: "controller", id: request.controllerResourceUid },
|
|
169
|
+
source: "controller-request",
|
|
170
|
+
prompt: request.prompt,
|
|
171
|
+
});
|
|
172
|
+
return {
|
|
173
|
+
runId: request.runId,
|
|
174
|
+
followUpId: result.followUp.followUpId,
|
|
175
|
+
order: result.followUp.order,
|
|
176
|
+
state: result.followUp.state,
|
|
177
|
+
adopted: result.adopted,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async removeFollowUp(
|
|
182
|
+
request: ControllerWorkflowControlRequest<ControllerRemoveFollowUpRequest>,
|
|
183
|
+
signal: AbortSignal,
|
|
184
|
+
): Promise<ControllerFollowUpResult> {
|
|
185
|
+
signal.throwIfAborted();
|
|
186
|
+
const result = this.options.store.removeFollowUp({
|
|
187
|
+
runId: request.runId,
|
|
188
|
+
followUpId: request.followUpId,
|
|
189
|
+
actor: { type: "controller", id: request.controllerResourceUid },
|
|
190
|
+
source: "controller-request",
|
|
191
|
+
});
|
|
192
|
+
return {
|
|
193
|
+
runId: request.runId,
|
|
194
|
+
followUpId: result.followUpId,
|
|
195
|
+
order: result.order,
|
|
196
|
+
state: result.state,
|
|
197
|
+
adopted: false,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
99
201
|
async waitForIdle(): Promise<void> {
|
|
100
202
|
await Promise.all([...this.active.values()].map((entry) => entry.promise));
|
|
101
203
|
}
|
|
@@ -5,8 +5,13 @@ import type {
|
|
|
5
5
|
ChildWorkflowRecord,
|
|
6
6
|
ChildWorkflowRequest,
|
|
7
7
|
ChildWorkflowState,
|
|
8
|
+
ControllerFollowUpResult,
|
|
8
9
|
ControllerQueueClaim,
|
|
10
|
+
ControllerQueueFollowUpRequest,
|
|
11
|
+
ControllerRemoveFollowUpRequest,
|
|
9
12
|
ControllerResource,
|
|
13
|
+
ControllerSettingsChangeRequest,
|
|
14
|
+
ControllerSettingsChangeResult,
|
|
10
15
|
ControllerWorkflows,
|
|
11
16
|
} from "./types.js";
|
|
12
17
|
|
|
@@ -24,12 +29,36 @@ export type WorkflowSchedulerResult = {
|
|
|
24
29
|
error?: string;
|
|
25
30
|
};
|
|
26
31
|
|
|
32
|
+
export type ControllerWorkflowControlRequest<TRequest> = TRequest & {
|
|
33
|
+
controllerResourceUid: string;
|
|
34
|
+
actorRequestKey: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
27
37
|
export interface ControllerWorkflowScheduler {
|
|
28
38
|
ensure(
|
|
29
39
|
request: WorkflowSchedulerRequest,
|
|
30
40
|
signal: AbortSignal,
|
|
31
41
|
onComplete: (result: WorkflowSchedulerResult) => void,
|
|
32
42
|
): Promise<WorkflowSchedulerResult>;
|
|
43
|
+
changeSettings?(
|
|
44
|
+
request: ControllerWorkflowControlRequest<ControllerSettingsChangeRequest>,
|
|
45
|
+
signal: AbortSignal,
|
|
46
|
+
): Promise<ControllerSettingsChangeResult>;
|
|
47
|
+
queueFollowUp?(
|
|
48
|
+
request: ControllerWorkflowControlRequest<ControllerQueueFollowUpRequest>,
|
|
49
|
+
signal: AbortSignal,
|
|
50
|
+
): Promise<ControllerFollowUpResult>;
|
|
51
|
+
removeFollowUp?(
|
|
52
|
+
request: ControllerWorkflowControlRequest<ControllerRemoveFollowUpRequest>,
|
|
53
|
+
signal: AbortSignal,
|
|
54
|
+
): Promise<ControllerFollowUpResult>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function controllerRequestKey(resource: ControllerResource, requestKey: string): string {
|
|
58
|
+
if (requestKey.trim().length === 0) {
|
|
59
|
+
throw new Error("Controller workflow requestKey must not be empty");
|
|
60
|
+
}
|
|
61
|
+
return `controller:${resource.metadata.uid}:${requestKey}`;
|
|
33
62
|
}
|
|
34
63
|
|
|
35
64
|
export class ControllerWorkflowCoordinator {
|
|
@@ -45,6 +74,45 @@ export class ControllerWorkflowCoordinator {
|
|
|
45
74
|
): ControllerWorkflows {
|
|
46
75
|
return {
|
|
47
76
|
ensure: async (request) => await this.ensure(resource, claim, request, signal),
|
|
77
|
+
changeSettings: async (request) => {
|
|
78
|
+
if (this.scheduler?.changeSettings === undefined) {
|
|
79
|
+
throw new Error("This controller host does not support workflow settings changes");
|
|
80
|
+
}
|
|
81
|
+
return await this.scheduler.changeSettings(
|
|
82
|
+
{
|
|
83
|
+
...request,
|
|
84
|
+
controllerResourceUid: resource.metadata.uid,
|
|
85
|
+
actorRequestKey: controllerRequestKey(resource, request.requestKey),
|
|
86
|
+
},
|
|
87
|
+
signal,
|
|
88
|
+
);
|
|
89
|
+
},
|
|
90
|
+
queueFollowUp: async (request) => {
|
|
91
|
+
if (this.scheduler?.queueFollowUp === undefined) {
|
|
92
|
+
throw new Error("This controller host does not support workflow follow-ups");
|
|
93
|
+
}
|
|
94
|
+
return await this.scheduler.queueFollowUp(
|
|
95
|
+
{
|
|
96
|
+
...request,
|
|
97
|
+
controllerResourceUid: resource.metadata.uid,
|
|
98
|
+
actorRequestKey: controllerRequestKey(resource, request.requestKey),
|
|
99
|
+
},
|
|
100
|
+
signal,
|
|
101
|
+
);
|
|
102
|
+
},
|
|
103
|
+
removeFollowUp: async (request) => {
|
|
104
|
+
if (this.scheduler?.removeFollowUp === undefined) {
|
|
105
|
+
throw new Error("This controller host does not support workflow follow-ups");
|
|
106
|
+
}
|
|
107
|
+
return await this.scheduler.removeFollowUp(
|
|
108
|
+
{
|
|
109
|
+
...request,
|
|
110
|
+
controllerResourceUid: resource.metadata.uid,
|
|
111
|
+
actorRequestKey: controllerRequestKey(resource, request.requestKey),
|
|
112
|
+
},
|
|
113
|
+
signal,
|
|
114
|
+
);
|
|
115
|
+
},
|
|
48
116
|
};
|
|
49
117
|
}
|
|
50
118
|
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
loadDiscoveredControllers,
|
|
5
5
|
type AnyControllerDefinition,
|
|
6
6
|
type ControllerResource,
|
|
7
|
+
type ControllerWorkflowScheduler,
|
|
7
8
|
type WorkflowSchedulerRequest,
|
|
8
9
|
type WorkflowSchedulerResult,
|
|
9
10
|
} from "../controllers/index.js";
|
|
@@ -80,6 +81,10 @@ export class PiControllerHost {
|
|
|
80
81
|
static async create(options: {
|
|
81
82
|
cwd: string;
|
|
82
83
|
startChild: PiChildWorkflowStarter;
|
|
84
|
+
workflowControls?: Pick<
|
|
85
|
+
ControllerWorkflowScheduler,
|
|
86
|
+
"changeSettings" | "queueFollowUp" | "removeFollowUp"
|
|
87
|
+
>;
|
|
83
88
|
}): Promise<PiControllerHost | undefined> {
|
|
84
89
|
const definitions = await loadDiscoveredControllers({ cwd: options.cwd });
|
|
85
90
|
if (definitions.length === 0) {
|
|
@@ -89,7 +94,7 @@ export class PiControllerHost {
|
|
|
89
94
|
const manager = new ControllerManager({
|
|
90
95
|
store,
|
|
91
96
|
controllers: definitions,
|
|
92
|
-
workflowScheduler: { ensure: options.startChild },
|
|
97
|
+
workflowScheduler: { ensure: options.startChild, ...options.workflowControls },
|
|
93
98
|
});
|
|
94
99
|
return new PiControllerHost(definitions, store, manager);
|
|
95
100
|
}
|
|
@@ -28,6 +28,7 @@ const TELEGRAM_TEXT_LIMIT = 4_096;
|
|
|
28
28
|
const PI_PRESENTATION_WINDOW_LINES = 18;
|
|
29
29
|
const DEFAULT_API_BASE = "https://api.telegram.org";
|
|
30
30
|
const LEASE_TTL_MS = 60_000;
|
|
31
|
+
const LEASE_RENEW_WINDOW_MS = 20_000;
|
|
31
32
|
const LEASE_RETRY_MS = 5_000;
|
|
32
33
|
const POLL_BACKOFF_MS = 1_000;
|
|
33
34
|
const MAX_SETTLEMENT_ATTEMPTS = 3;
|
|
@@ -428,6 +429,21 @@ class TelegramProjection {
|
|
|
428
429
|
}
|
|
429
430
|
|
|
430
431
|
acquire(now = Date.now()): boolean {
|
|
432
|
+
const observed = this.lease();
|
|
433
|
+
const observedToken = tokenHash(this.token);
|
|
434
|
+
const ownedByThis =
|
|
435
|
+
observed.ownerId === this.owner &&
|
|
436
|
+
observed.tokenHash !== null &&
|
|
437
|
+
observed.tokenHash.equals(observedToken) &&
|
|
438
|
+
observed.expiresAt !== null &&
|
|
439
|
+
observed.expiresAt > now;
|
|
440
|
+
if (ownedByThis) {
|
|
441
|
+
this.generation = observed.generation;
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
if (observed.ownerId !== null && observed.expiresAt !== null && observed.expiresAt > now) {
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
431
447
|
return this.state.transaction(() => {
|
|
432
448
|
const lease = this.lease();
|
|
433
449
|
if (
|
|
@@ -438,7 +454,11 @@ class TelegramProjection {
|
|
|
438
454
|
) {
|
|
439
455
|
return false;
|
|
440
456
|
}
|
|
441
|
-
const
|
|
457
|
+
const sameToken =
|
|
458
|
+
lease.ownerId === this.owner &&
|
|
459
|
+
lease.tokenHash !== null &&
|
|
460
|
+
lease.tokenHash.equals(observedToken);
|
|
461
|
+
const generation = sameToken ? lease.generation : lease.generation + 1;
|
|
442
462
|
const result = this.state.connection
|
|
443
463
|
.prepare(
|
|
444
464
|
`UPDATE leases
|
|
@@ -449,7 +469,7 @@ class TelegramProjection {
|
|
|
449
469
|
.run(
|
|
450
470
|
generation,
|
|
451
471
|
this.owner,
|
|
452
|
-
|
|
472
|
+
observedToken,
|
|
453
473
|
now,
|
|
454
474
|
now,
|
|
455
475
|
now + LEASE_TTL_MS,
|
|
@@ -465,6 +485,18 @@ class TelegramProjection {
|
|
|
465
485
|
}
|
|
466
486
|
|
|
467
487
|
renew(now = Date.now()): boolean {
|
|
488
|
+
const lease = this.lease();
|
|
489
|
+
if (
|
|
490
|
+
lease.ownerId !== this.owner ||
|
|
491
|
+
lease.generation !== this.generation ||
|
|
492
|
+
lease.tokenHash === null ||
|
|
493
|
+
!lease.tokenHash.equals(tokenHash(this.token)) ||
|
|
494
|
+
lease.expiresAt === null ||
|
|
495
|
+
lease.expiresAt <= now
|
|
496
|
+
) {
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
if (lease.expiresAt - now > LEASE_RENEW_WINDOW_MS) return true;
|
|
468
500
|
return (
|
|
469
501
|
this.state.connection
|
|
470
502
|
.prepare(
|
|
@@ -1222,11 +1254,11 @@ export class TelegramDecisionChannel implements HumanDecisionChannel {
|
|
|
1222
1254
|
|
|
1223
1255
|
private async poll(signal: AbortSignal): Promise<void> {
|
|
1224
1256
|
while (this.running && !signal.aborted) {
|
|
1225
|
-
if (!this.projection.acquire()) {
|
|
1226
|
-
await wait(LEASE_RETRY_MS, signal);
|
|
1227
|
-
continue;
|
|
1228
|
-
}
|
|
1229
1257
|
try {
|
|
1258
|
+
if (!this.projection.acquire()) {
|
|
1259
|
+
await wait(leaseRetryDelay(), signal);
|
|
1260
|
+
continue;
|
|
1261
|
+
}
|
|
1230
1262
|
await this.deliverPendingRequests();
|
|
1231
1263
|
const result = await this.call(
|
|
1232
1264
|
"getUpdates",
|
|
@@ -1246,7 +1278,11 @@ export class TelegramDecisionChannel implements HumanDecisionChannel {
|
|
|
1246
1278
|
if (!this.projection.renew()) this.projection.release();
|
|
1247
1279
|
} catch {
|
|
1248
1280
|
if (signal.aborted) return;
|
|
1249
|
-
|
|
1281
|
+
try {
|
|
1282
|
+
await wait(POLL_BACKOFF_MS, signal);
|
|
1283
|
+
} catch {
|
|
1284
|
+
if (signal.aborted) return;
|
|
1285
|
+
}
|
|
1250
1286
|
}
|
|
1251
1287
|
}
|
|
1252
1288
|
}
|
|
@@ -1773,6 +1809,10 @@ async function writePrivateJson(filePath: string, value: unknown): Promise<void>
|
|
|
1773
1809
|
await fsp.chmod(filePath, 0o600);
|
|
1774
1810
|
}
|
|
1775
1811
|
|
|
1812
|
+
function leaseRetryDelay(): number {
|
|
1813
|
+
return LEASE_RETRY_MS + Math.floor(Math.random() * 1_000);
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1776
1816
|
async function wait(milliseconds: number, signal: AbortSignal): Promise<void> {
|
|
1777
1817
|
await new Promise<void>((resolve) => {
|
|
1778
1818
|
const timer = setTimeout(resolve, milliseconds);
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { LeaseClaim } from "../state/mutation.js";
|
|
3
|
+
import type { WorkflowFollowUpRecord } from "../workflows/settings.js";
|
|
4
|
+
import { WorkflowRunStore } from "../workflows/store.js";
|
|
5
|
+
|
|
6
|
+
const FOLLOW_UP_ID_PREFIX = "pi-workflows-follow-up:";
|
|
7
|
+
|
|
8
|
+
export type FollowUpDelivery = {
|
|
9
|
+
followUp: WorkflowFollowUpRecord;
|
|
10
|
+
claim: LeaseClaim;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class FollowUpCoordinator {
|
|
14
|
+
private pending: FollowUpDelivery | undefined;
|
|
15
|
+
private synchronizing = false;
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
private readonly store: WorkflowRunStore,
|
|
19
|
+
private readonly ownerId: string,
|
|
20
|
+
private readonly sendUserMessage: (text: string, options?: { deliverAs?: "followUp" }) => void,
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
async synchronize(ctx: ExtensionContext, workflowActive: boolean): Promise<void> {
|
|
24
|
+
if (this.synchronizing) return;
|
|
25
|
+
this.synchronizing = true;
|
|
26
|
+
try {
|
|
27
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
28
|
+
if (this.pending !== undefined) {
|
|
29
|
+
const entryId = findFollowUpEntryId(
|
|
30
|
+
ctx.sessionManager.getBranch(),
|
|
31
|
+
this.pending.followUp.followUpId,
|
|
32
|
+
);
|
|
33
|
+
if (entryId === undefined) return;
|
|
34
|
+
this.store.markFollowUpSent(this.pending.followUp.followUpId, this.pending.claim, entryId);
|
|
35
|
+
this.releaseClaim(this.pending.claim);
|
|
36
|
+
this.pending = undefined;
|
|
37
|
+
}
|
|
38
|
+
if (!ctx.isIdle() || workflowActive) return;
|
|
39
|
+
const claimed = this.store.claimNextFollowUp(sessionId, this.ownerId);
|
|
40
|
+
if (claimed === undefined) return;
|
|
41
|
+
const entryId = findFollowUpEntryId(
|
|
42
|
+
ctx.sessionManager.getBranch(),
|
|
43
|
+
claimed.followUp.followUpId,
|
|
44
|
+
);
|
|
45
|
+
if (entryId !== undefined) {
|
|
46
|
+
this.store.markFollowUpSent(claimed.followUp.followUpId, claimed.claim, entryId);
|
|
47
|
+
this.releaseClaim(claimed.claim);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.pending = claimed;
|
|
51
|
+
try {
|
|
52
|
+
this.sendUserMessage(followUpMessageText(claimed.followUp));
|
|
53
|
+
} catch (error) {
|
|
54
|
+
this.pending = undefined;
|
|
55
|
+
this.releaseClaim(claimed.claim);
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
} finally {
|
|
59
|
+
this.synchronizing = false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
clear(): void {
|
|
64
|
+
this.pending = undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private releaseClaim(claim: LeaseClaim): void {
|
|
68
|
+
try {
|
|
69
|
+
this.store.releaseFollowUpClaim(claim);
|
|
70
|
+
} catch {
|
|
71
|
+
// The lease can expire or move after delivery evidence is saved.
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function followUpMessageText(
|
|
77
|
+
followUp: Pick<WorkflowFollowUpRecord, "followUpId" | "prompt">,
|
|
78
|
+
): string {
|
|
79
|
+
return `${followUp.prompt}\n\n<!-- ${FOLLOW_UP_ID_PREFIX}${followUp.followUpId} -->`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function findFollowUpEntryId(
|
|
83
|
+
entries: readonly unknown[],
|
|
84
|
+
followUpId: string,
|
|
85
|
+
): string | undefined {
|
|
86
|
+
const idText = `${FOLLOW_UP_ID_PREFIX}${followUpId}`;
|
|
87
|
+
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
88
|
+
const entry = entries[index];
|
|
89
|
+
if (!isRecord(entry) || entry.type !== "message" || typeof entry.id !== "string") continue;
|
|
90
|
+
const message = entry.message;
|
|
91
|
+
if (!isRecord(message) || message.role !== "user") continue;
|
|
92
|
+
if (messageText(message.content).includes(idText)) return entry.id;
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function findSettledPresentationEntries(
|
|
98
|
+
entries: readonly unknown[],
|
|
99
|
+
runId: string,
|
|
100
|
+
): { presentationEntryId: string; assistantEntryId: string } | undefined {
|
|
101
|
+
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
102
|
+
const entry = entries[index];
|
|
103
|
+
if (!isPresentationEntry(entry, runId)) continue;
|
|
104
|
+
for (let childIndex = index + 1; childIndex < entries.length; childIndex += 1) {
|
|
105
|
+
const child = entries[childIndex];
|
|
106
|
+
if (!isRecord(child) || child.type !== "message" || typeof child.id !== "string") continue;
|
|
107
|
+
const message = child.message;
|
|
108
|
+
if (!isRecord(message)) continue;
|
|
109
|
+
if (message.role === "user") break;
|
|
110
|
+
if (
|
|
111
|
+
message.role === "assistant" &&
|
|
112
|
+
(message.stopReason === "stop" || message.stopReason === "length")
|
|
113
|
+
) {
|
|
114
|
+
return { presentationEntryId: entry.id, assistantEntryId: child.id };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function isPresentationEntry(value: unknown, runId: string): value is { id: string } {
|
|
122
|
+
if (!isRecord(value) || typeof value.id !== "string") return false;
|
|
123
|
+
if (value.type === "custom_message") {
|
|
124
|
+
return (
|
|
125
|
+
value.customType === "pi-workflows-presentation" &&
|
|
126
|
+
isRecord(value.details) &&
|
|
127
|
+
value.details.runId === runId
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
if (value.type !== "message" || !isRecord(value.message)) return false;
|
|
131
|
+
return (
|
|
132
|
+
value.message.role === "custom" &&
|
|
133
|
+
value.message.customType === "pi-workflows-presentation" &&
|
|
134
|
+
isRecord(value.message.details) &&
|
|
135
|
+
value.message.details.runId === runId
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function messageText(value: unknown): string {
|
|
140
|
+
if (typeof value === "string") return value;
|
|
141
|
+
if (!Array.isArray(value)) return "";
|
|
142
|
+
return value
|
|
143
|
+
.flatMap((item) =>
|
|
144
|
+
isRecord(item) && item.type === "text" && typeof item.text === "string" ? [item.text] : [],
|
|
145
|
+
)
|
|
146
|
+
.join("\n");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
150
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
151
|
+
}
|