toolroll 0.9.108 → 0.9.110
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/CHANGELOG.md +69 -0
- package/dist/advanced-view.js +0 -1
- package/dist/agent-detect.js +244 -0
- package/dist/agent-fence.js +47 -5
- package/dist/agent-state.js +5 -2
- package/dist/agentconfig.js +2 -2
- package/dist/approval-summary.js +344 -0
- package/dist/assignment-ui.js +5 -5
- package/dist/assignment.js +5 -2
- package/dist/attest.js +2 -2
- package/dist/browser/terminal.js +1 -1
- package/dist/browser/workspace.css +2 -2
- package/dist/browser/workspace.js +37 -34
- package/dist/browser-crew.js +4 -4
- package/dist/browser-workspace.js +4 -11
- package/dist/builder.js +45 -3
- package/dist/chat-actions.js +24 -17
- package/dist/chat-controls.js +6 -5
- package/dist/chat-core.js +3 -1
- package/dist/chat-decide.js +102 -47
- package/dist/chat-delivery-state.js +2 -0
- package/dist/chat-delivery.js +3 -2
- package/dist/chat-live.js +2 -1
- package/dist/chat-quiet.js +3 -3
- package/dist/chat-task-actions.js +6 -0
- package/dist/chat-voice.js +2 -2
- package/dist/check-report.js +1 -1
- package/dist/cli-help.js +12 -13
- package/dist/cli.js +6 -3
- package/dist/console-chrome.js +10 -3
- package/dist/console-instance.js +10 -1
- package/dist/contracts/console-api.js +14 -8
- package/dist/contracts/engine-api.js +271 -0
- package/dist/contracts/handoff.js +4 -0
- package/dist/coordinator.js +2 -2
- package/dist/decision.js +17 -0
- package/dist/demo-chat.js +1 -1
- package/dist/demo.js +6 -0
- package/dist/design/kit.js +4 -3
- package/dist/discord-api.js +4 -3
- package/dist/discord-chat.js +3 -1
- package/dist/dispatch.js +3 -8
- package/dist/easy-start-args.js +7 -3
- package/dist/easy-start.js +25 -0
- package/dist/egress.js +72 -10
- package/dist/engine-handoff.js +34 -0
- package/dist/engine-reads.js +261 -0
- package/dist/eval-world.js +3 -2
- package/dist/evidence.js +2 -0
- package/dist/fast-start.js +55 -14
- package/dist/fast-terms.js +2 -2
- package/dist/first-run.js +10 -2
- package/dist/flexible-agents.js +234 -0
- package/dist/flexible-answers-schema.js +61 -0
- package/dist/flexible-answers.js +889 -0
- package/dist/flexible-available.js +1 -0
- package/dist/flexible-custody.js +494 -0
- package/dist/flexible-import.js +782 -0
- package/dist/flexible-launch-schema.js +72 -0
- package/dist/flexible-launch.js +331 -0
- package/dist/flexible-launcher.js +88 -0
- package/dist/flexible-levels.js +34 -0
- package/dist/flexible-merge-guard.js +47 -0
- package/dist/flexible-refs.js +47 -0
- package/dist/flexible-relay-cancel.js +19 -0
- package/dist/flexible-relay-schema.js +55 -0
- package/dist/flexible-relay-upstreams.js +36 -0
- package/dist/flexible-relay.js +1032 -0
- package/dist/flexible-schema.js +32 -0
- package/dist/flexible-session.js +732 -0
- package/dist/flexible-ship-schema.js +81 -0
- package/dist/flexible-ship.js +694 -0
- package/dist/flexible-start.js +80 -0
- package/dist/flexible-thread-schema.js +30 -0
- package/dist/flexible-thread.js +676 -0
- package/dist/flexible-turns-schema.js +69 -0
- package/dist/flexible-turns.js +490 -0
- package/dist/flexible-ui.js +44 -0
- package/dist/flexible.js +368 -0
- package/dist/flow-actions.js +6 -4
- package/dist/flow-steps.js +1 -1
- package/dist/flows-cli.js +2 -1
- package/dist/git-views.js +44 -41
- package/dist/guides.js +11 -10
- package/dist/install-mode.js +225 -0
- package/dist/keymap.js +1 -1
- package/dist/lead-context.js +1 -1
- package/dist/lead-doors.js +7 -1
- package/dist/limits-ui.js +8 -1
- package/dist/live-bus.js +70 -13
- package/dist/live-reply.js +6 -0
- package/dist/live-terminal.js +425 -29
- package/dist/local-owner.js +6 -0
- package/dist/model-choices.js +12 -6
- package/dist/needs-you.js +3 -3
- package/dist/nightly-github.js +2 -2
- package/dist/notification-delivery.js +11 -0
- package/dist/operate.js +185 -33
- package/dist/outbound-privacy.js +204 -0
- package/dist/passwordless.js +2 -1
- package/dist/project-delete-ui.js +2 -1
- package/dist/project-delete.js +15 -0
- package/dist/provider-auth.js +1 -1
- package/dist/provider-connection.js +5 -3
- package/dist/publish.js +5 -0
- package/dist/pull-request-flow.js +26 -7
- package/dist/push.js +5 -1
- package/dist/release-card.js +11 -3
- package/dist/release.js +11 -1
- package/dist/result-actions.js +3 -0
- package/dist/result-evidence-readers.js +8 -2
- package/dist/result-review.js +20 -9
- package/dist/route-store.js +2 -2
- package/dist/sandbox-launch.js +32 -8
- package/dist/sandbox-runtime.js +16 -7
- package/dist/sandbox-tools.js +176 -0
- package/dist/scope.js +12 -36
- package/dist/scout-net.js +7 -2
- package/dist/serve.js +90 -40
- package/dist/server/chat.js +9 -9
- package/dist/server/chrome.js +19 -17
- package/dist/server/console.css +33 -6
- package/dist/server/engine-api.js +378 -0
- package/dist/server/flows.js +2 -2
- package/dist/server/guards.js +10 -4
- package/dist/server/home.js +37 -5
- package/dist/server/http.js +11 -2
- package/dist/server/in-place.js +5 -4
- package/dist/server/kit.js +2 -2
- package/dist/server/link-redemption.js +52 -0
- package/dist/server/live.js +100 -10
- package/dist/server/mutation-key.js +21 -7
- package/dist/server/pages.js +129 -74
- package/dist/server/people-tokens.js +17 -41
- package/dist/server/render-chat.js +2 -1
- package/dist/server/render-history.js +18 -4
- package/dist/server/render-new-task.js +4 -4
- package/dist/server/render-pages.js +21 -39
- package/dist/server/render-people.js +2 -2
- package/dist/server/render-projects.js +5 -5
- package/dist/server/render-results.js +30 -14
- package/dist/server/render-tasks.js +155 -155
- package/dist/server/retired-routes.js +37 -4
- package/dist/server/route-policy.js +8 -0
- package/dist/server/route-table.js +43 -5
- package/dist/server/settings-computers.js +140 -0
- package/dist/server/settings-scripts.js +15 -0
- package/dist/server/settings-section-table.js +59 -0
- package/dist/server/settings-sections.js +291 -0
- package/dist/server/settings.js +247 -28
- package/dist/server/task-views.js +55 -15
- package/dist/server/tasks.js +168 -98
- package/dist/server/terminals.js +181 -28
- package/dist/server/welcome.js +40 -14
- package/dist/settings-groups-ui.js +26 -107
- package/dist/settings-proofs.js +18 -11
- package/dist/slack-api.js +4 -3
- package/dist/slack-chat.js +5 -3
- package/dist/spend.js +46 -12
- package/dist/sqlite-telemetry.js +70 -21
- package/dist/status-probe.js +343 -0
- package/dist/store.js +206 -10
- package/dist/task-link.js +64 -0
- package/dist/teams-api.js +4 -3
- package/dist/teams-chat.js +4 -2
- package/dist/telegram-lead.js +2 -1
- package/dist/telegram-live.js +2 -1
- package/dist/telegram-team.js +2 -1
- package/dist/telegram.js +17 -5
- package/dist/trusted-path.js +162 -0
- package/dist/tui/darwin-arm64/toolroll-tui +0 -0
- package/dist/tui/darwin-x64/toolroll-tui +0 -0
- package/dist/tui/linux-arm64/toolroll-tui +0 -0
- package/dist/tui/linux-x64/toolroll-tui +0 -0
- package/dist/tui-launch.js +282 -0
- package/dist/tui-source.js +5 -3
- package/dist/tui.js +2 -2
- package/dist/typed-words-schema.js +18 -0
- package/dist/webhooks.js +8 -4
- package/dist/welcome-facts.js +25 -17
- package/dist/work-index.js +126 -29
- package/dist/work-reads.js +279 -0
- package/dist/workspace-ui.js +5 -0
- package/package.json +4 -2
- package/scripts/crash-canary.mjs +1 -1
- package/scripts/pilot.mjs +1 -1
- package/scripts/provider-canary.mjs +5 -3
package/dist/fast-start.js
CHANGED
|
@@ -8,13 +8,18 @@ import { levelOfProfile } from "./policy.js";
|
|
|
8
8
|
import { fileTaskProposal } from "./proposal.js";
|
|
9
9
|
import { releaseCiHoldOf } from "./release-ci-hold.js";
|
|
10
10
|
import { COMPLETION_ACTION, COMPLETION_BY_PERSON } from "./result-completion.js";
|
|
11
|
+
import { approvalSummaryOf, summaryContextOf } from "./approval-summary.js";
|
|
11
12
|
import { deployGateScope, machineActing, propose, releaseCheckScope } from "./scope.js";
|
|
12
13
|
import { BUILT_IN, withdrawApproval } from "./store.js";
|
|
13
14
|
import { classifyTask, filingSizer, heuristicSizing } from "./task-sizing.js";
|
|
14
15
|
import { issueImported } from "./task-source.js";
|
|
15
16
|
import { titleWithin } from "./task-text.js";
|
|
16
17
|
import { viewedGitSync } from "./teardown-git.js";
|
|
18
|
+
import { soloNow } from "./install-mode.js";
|
|
19
|
+
import { withLocalAct } from "./local-owner.js";
|
|
17
20
|
export const FAST_START_RULE = "small-v1";
|
|
21
|
+
export const SOLO_START_RULE = "solo-v1";
|
|
22
|
+
export const SOLO_START_ACTION = "solo start";
|
|
18
23
|
export const FAST_WINDOW_MS = 30 * 60_000;
|
|
19
24
|
export const FAST_MAX_FILES = 3;
|
|
20
25
|
export const FAST_MAX_LINES = 150;
|
|
@@ -37,6 +42,7 @@ const FAST_WORDS = {
|
|
|
37
42
|
machine: "Only a person starts work this way.",
|
|
38
43
|
mode: "This project's mode approves it.",
|
|
39
44
|
limit: "This is more than your chat approval setting allows.",
|
|
45
|
+
team: "Someone else uses this install now, so this needs its plan approved.",
|
|
40
46
|
};
|
|
41
47
|
export const fastRefusalWords = (reason) => FAST_WORDS[reason];
|
|
42
48
|
export function fastStartOn(store, repo) {
|
|
@@ -63,16 +69,19 @@ export function fastStartEligible(store, input, now) {
|
|
|
63
69
|
}
|
|
64
70
|
export function fastStartRules(store, input, now) {
|
|
65
71
|
const no = (reason) => ({ ok: false, reason });
|
|
72
|
+
const solo = input.rule === "solo";
|
|
73
|
+
if (solo && ("chat" in input.proof || !soloNow(store)))
|
|
74
|
+
return no("team");
|
|
66
75
|
if (!fastStartOn(store, input.repo))
|
|
67
76
|
return no("setting");
|
|
68
77
|
const account = store.accountOf(input.actor);
|
|
69
78
|
if (account === null || account.revokedAt !== null || account.role !== "approver" || !store.accountCanAccess(input.actor, input.repo))
|
|
70
79
|
return no("rules");
|
|
71
|
-
if (heuristicSizing({ title: input.title, goal: input.title }).size !== "small")
|
|
80
|
+
if (!solo && heuristicSizing({ title: input.title, goal: input.title }).size !== "small")
|
|
72
81
|
return no("size");
|
|
73
|
-
if (input.sizing.source !== "classifier")
|
|
82
|
+
if (!solo && input.sizing.source !== "classifier")
|
|
74
83
|
return no("classifier");
|
|
75
|
-
if (input.sizing.size !== "small" || input.sizing.risky)
|
|
84
|
+
if (!solo && (input.sizing.size !== "small" || input.sizing.risky))
|
|
76
85
|
return no("size");
|
|
77
86
|
const scope = store.getScope(input.taskId);
|
|
78
87
|
if (scope === null)
|
|
@@ -84,7 +93,7 @@ export function fastStartRules(store, input, now) {
|
|
|
84
93
|
const route = routeFromJson(scope.proposedRouteJson ?? null);
|
|
85
94
|
if (route === null)
|
|
86
95
|
return no("agents");
|
|
87
|
-
if (!makesNoPlan(input.sizing, route.risk))
|
|
96
|
+
if (!solo && !makesNoPlan(input.sizing, route.risk))
|
|
88
97
|
return no("plan");
|
|
89
98
|
if ((scope.qualityMode ?? "default") !== "default")
|
|
90
99
|
return no("quality");
|
|
@@ -146,6 +155,16 @@ function dryRun(store, body) {
|
|
|
146
155
|
}
|
|
147
156
|
throw new Error("unreachable");
|
|
148
157
|
}
|
|
158
|
+
function withTermsKey(store, read, taskId, now) {
|
|
159
|
+
const scope = store.getScope(taskId);
|
|
160
|
+
return { ...read, termsKey: scope === null ? "" : approvalSummaryOf(scope, summaryContextOf(store, taskId, scope, now)).key };
|
|
161
|
+
}
|
|
162
|
+
const previewDigest = (scope, terms, rule = "small") => rule === "solo" ? `${scope} ${terms} ${SOLO_START_RULE}` : `${scope} ${terms}`;
|
|
163
|
+
function previewParts(value) {
|
|
164
|
+
const [scope, terms, rule, ...rest] = String(value ?? "").split(" ");
|
|
165
|
+
return { scope: scope ?? "", terms: terms === undefined || terms === "" ? null : terms,
|
|
166
|
+
rule: rest.length > 0 ? null : rule === undefined ? "small" : rule === SOLO_START_RULE ? "solo" : null };
|
|
167
|
+
}
|
|
149
168
|
export async function prepareFastStart(store, input, now) {
|
|
150
169
|
if (machineActing())
|
|
151
170
|
return { eligible: false, reason: "machine" };
|
|
@@ -153,25 +172,31 @@ export async function prepareFastStart(store, input, now) {
|
|
|
153
172
|
return { eligible: false, reason: "rules" };
|
|
154
173
|
if (!fastStartOn(store, input.repo))
|
|
155
174
|
return { eligible: false, reason: "setting" };
|
|
156
|
-
|
|
175
|
+
const small = heuristicSizing({ title: input.title, goal: input.title }).size === "small";
|
|
176
|
+
const solo = input.planFirst !== true && soloNow(store);
|
|
177
|
+
if (!small && !solo)
|
|
157
178
|
return { eligible: false, reason: "size" };
|
|
158
179
|
const mode = store.activeMode(input.repo, now);
|
|
159
180
|
if (mode !== null && /"autoApproveFiling":true/.test(mode.termsJson))
|
|
160
181
|
return { eligible: false, reason: "mode" };
|
|
161
182
|
const sizer = filingSizer();
|
|
162
|
-
const sizing = await classifyTask({ title: input.title, goal: input.title }, sizer.sizer, sizer.budget);
|
|
183
|
+
const sizing = small ? await classifyTask({ title: input.title, goal: input.title }, sizer.sizer, sizer.budget) : heuristicSizing({ title: input.title, goal: input.title });
|
|
163
184
|
const taskId = `task-${randomBytes(8).toString("hex")}`;
|
|
164
|
-
const
|
|
185
|
+
const readUnder = (rule) => dryRun(store, () => {
|
|
165
186
|
const filed = fileIt(store, taskId, { ...input, sizing }, now);
|
|
166
187
|
if (!filed.ok)
|
|
167
188
|
return { ok: false, reason: "plan" };
|
|
168
|
-
return fastStartEligible(store, { ...input, taskId, sizing }, now);
|
|
189
|
+
return withTermsKey(store, fastStartEligible(store, { ...input, taskId, sizing, rule }, now), taskId, now);
|
|
169
190
|
});
|
|
191
|
+
let rule = small ? "small" : "solo";
|
|
192
|
+
let read = readUnder(rule);
|
|
193
|
+
if (!read.ok && rule === "small" && solo && (read.reason === "size" || read.reason === "classifier" || read.reason === "plan"))
|
|
194
|
+
read = readUnder(rule = "solo");
|
|
170
195
|
if (!read.ok)
|
|
171
196
|
return { eligible: false, reason: read.reason };
|
|
172
197
|
const preview = randomBytes(16).toString("hex");
|
|
173
198
|
store.handle.prepare(`INSERT INTO fast_preview (id, actor, via, task_id, repo, title, permission, sizing_json, digest, created_at, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
174
|
-
.run(preview, input.actor, input.via, taskId, input.repo, input.title, input.permissionMode ?? null, JSON.stringify(sizing), read.scope.digest, now.toISOString(), new Date(now.getTime() + FAST_PREVIEW_MS).toISOString());
|
|
199
|
+
.run(preview, input.actor, input.via, taskId, input.repo, input.title, input.permissionMode ?? null, JSON.stringify(sizing), previewDigest(read.scope.digest, read.termsKey, rule), now.toISOString(), new Date(now.getTime() + FAST_PREVIEW_MS).toISOString());
|
|
175
200
|
return { eligible: true, preview, digest: read.scope.digest, scope: read.scope, taskId };
|
|
176
201
|
}
|
|
177
202
|
export function fastStartBuild(store, input, now) {
|
|
@@ -201,16 +226,32 @@ export function fastStartBuild(store, input, now) {
|
|
|
201
226
|
const filed = fileIt(store, taskId, { ...filing, via: input.via, sizing }, now);
|
|
202
227
|
if (!filed.ok)
|
|
203
228
|
return refuse({ ok: false, reason: "changed", message: filed.message });
|
|
204
|
-
const
|
|
229
|
+
const shown = previewParts(row["digest"]);
|
|
230
|
+
if (shown.rule === null)
|
|
231
|
+
return refuse({ ok: false, reason: "gone", message: "This form has expired. Start again." });
|
|
232
|
+
const rule = shown.rule;
|
|
233
|
+
const eligible = fastStartEligible(store, { ...filing, taskId, sizing, proof, rule }, now);
|
|
205
234
|
if (!eligible.ok)
|
|
206
235
|
return refuse({ ok: false, reason: eligible.reason, message: FAST_WORDS[eligible.reason] });
|
|
236
|
+
const shownKey = shown.terms;
|
|
237
|
+
const filedScope = store.getScope(taskId);
|
|
238
|
+
if (shownKey === null || filedScope === null || approvalSummaryOf(filedScope, summaryContextOf(store, taskId, filedScope, now)).key !== shownKey)
|
|
239
|
+
return refuse({ ok: false, reason: "changed", message: "This changed since you read it. Read it again." });
|
|
207
240
|
const at = now.toISOString();
|
|
208
|
-
const approved =
|
|
241
|
+
const approved = rule === "solo"
|
|
242
|
+
? withLocalAct("approve-plan", () => input.approveNow(taskId, input.digest, undefined))
|
|
243
|
+
: input.approveNow(taskId, input.digest, { rule: FAST_START_RULE, at });
|
|
209
244
|
if (!approved.ok && approved.reason === "changed")
|
|
210
245
|
return refuse({ ok: false, reason: "changed", message: "This changed since you read it. Read it again." });
|
|
211
246
|
if (!approved.ok)
|
|
212
247
|
return refuse(input.via === "chat" ? { ok: false, reason: "rules", message: approved.reason === "policy" ? approved.message : FAST_WORDS.rules } : { ok: false, reason: "password", message: FAST_WORDS.password });
|
|
213
248
|
store.handle.prepare("UPDATE fast_preview SET consumed_at = ? WHERE id = ?").run(at, input.preview);
|
|
249
|
+
if (rule === "solo") {
|
|
250
|
+
store.recordAction({ at, actor: input.actor, repo: filing.repo, taskId, runId: null, action: SOLO_START_ACTION, outcome: "approved", source: "request",
|
|
251
|
+
detail: JSON.stringify({ v: 1, rule: SOLO_START_RULE, digest: input.digest, terms: shownKey, via: input.via }) });
|
|
252
|
+
store.bumpWake();
|
|
253
|
+
return { ok: true, taskId };
|
|
254
|
+
}
|
|
214
255
|
store.handle.prepare("UPDATE task_ref SET fast_start_at = ?, fast_start_until = ? WHERE backend = ? AND external_id = ?").run(at, new Date(now.getTime() + FAST_WINDOW_MS).toISOString(), BUILT_IN, taskId);
|
|
215
256
|
store.bumpWake();
|
|
216
257
|
return { ok: true, taskId };
|
|
@@ -403,20 +444,20 @@ export async function prepareChatStart(store, input, now) {
|
|
|
403
444
|
const filed = fileIt(store, taskId, { ...filing, via: "chat", sizing }, now);
|
|
404
445
|
if (!filed.ok)
|
|
405
446
|
return { ok: false, reason: "plan" };
|
|
406
|
-
return fastStartRules(store, { ...filing, taskId, sizing, proof: { chat: { fullAccess: false, capMicrousd: null } } }, now);
|
|
447
|
+
return withTermsKey(store, fastStartRules(store, { ...filing, taskId, sizing, proof: { chat: { fullAccess: false, capMicrousd: null } } }, now), taskId, now);
|
|
407
448
|
});
|
|
408
449
|
if (!read.ok)
|
|
409
450
|
return { eligible: false, reason: read.reason };
|
|
410
451
|
const preview = randomBytes(16).toString("hex");
|
|
411
452
|
store.handle.prepare(`INSERT INTO fast_preview (id, actor, via, task_id, repo, title, permission, sizing_json, digest, created_at, expires_at, turn) VALUES (?, ?, 'chat', ?, ?, ?, NULL, ?, ?, ?, ?, ?)`)
|
|
412
|
-
.run(preview, input.actor, taskId, input.repo, input.title, JSON.stringify(sizing), read.scope.digest, now.toISOString(), new Date(now.getTime() + FAST_PREVIEW_MS).toISOString(), input.turn);
|
|
453
|
+
.run(preview, input.actor, taskId, input.repo, input.title, JSON.stringify(sizing), previewDigest(read.scope.digest, read.termsKey), now.toISOString(), new Date(now.getTime() + FAST_PREVIEW_MS).toISOString(), input.turn);
|
|
413
454
|
return { eligible: true, preview, digest: read.scope.digest, scope: read.scope, taskId };
|
|
414
455
|
}
|
|
415
456
|
export function chatStartPreview(store, preview, actor, turn, now) {
|
|
416
457
|
const row = store.handle.prepare("SELECT * FROM fast_preview WHERE id = ? AND consumed_at IS NULL AND expires_at > ?").get(preview, now.toISOString());
|
|
417
458
|
if (row === undefined || row["actor"] !== actor || row["via"] !== "chat" || row["turn"] === null || Number(row["turn"]) !== turn)
|
|
418
459
|
return null;
|
|
419
|
-
return { taskId: String(row["task_id"]), repo: String(row["repo"]), title: String(row["title"]), sizing: JSON.parse(String(row["sizing_json"])), digest:
|
|
460
|
+
return { taskId: String(row["task_id"]), repo: String(row["repo"]), title: String(row["title"]), sizing: JSON.parse(String(row["sizing_json"])), digest: previewParts(row["digest"]).scope };
|
|
420
461
|
}
|
|
421
462
|
export function consumeChatStartPreview(store, preview, now) {
|
|
422
463
|
store.handle.prepare("UPDATE fast_preview SET consumed_at = ? WHERE id = ? AND via = 'chat' AND consumed_at IS NULL").run(now.toISOString(), preview);
|
package/dist/fast-terms.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { describeScope } from "./
|
|
1
|
+
import { describeScope, summaryContextOf } from "./approval-summary.js";
|
|
2
2
|
const LABELS = { goal: "Goal", "not this": "Not this", files: "Files", budget: "Budget", network: "Network", writes: "Writes", reads: "Reads", checks: "Checks", services: "Services" };
|
|
3
3
|
export function fastTermsLines(store, scope, repo) {
|
|
4
4
|
const lines = [];
|
|
5
|
-
for (const raw of describeScope(scope)) {
|
|
5
|
+
for (const raw of describeScope(scope, undefined, null, summaryContextOf(store, scope.taskId, scope, new Date(), null, repo))) {
|
|
6
6
|
const line = raw.trimEnd();
|
|
7
7
|
const named = /^ {2}(\S+(?: \S+)?) {2,}(.*)$/.exec(line);
|
|
8
8
|
if (named !== null && (named[1] === "version" || named[1] === "approved"))
|
package/dist/first-run.js
CHANGED
|
@@ -2,6 +2,7 @@ import { run } from "./exec.js";
|
|
|
2
2
|
import { hardenedGit } from "./teardown-git.js";
|
|
3
3
|
import { githubIssues } from "./issues.js";
|
|
4
4
|
import { START_BUILDER_HREF } from "./needs-you.js";
|
|
5
|
+
import { firstRunAgentLines, firstTaskOf, withNext } from "./agent-detect.js";
|
|
5
6
|
export const SIGN_IN_COMMAND = "claude auth login";
|
|
6
7
|
export const SANDBOX_COMMAND = "npx toolroll demo";
|
|
7
8
|
export const START_COMMAND = "npx toolroll";
|
|
@@ -198,6 +199,13 @@ export function welcomeModel(facts) {
|
|
|
198
199
|
for (const one of agents)
|
|
199
200
|
one.optional = signedIn ? one.state !== "signed-in" : installed && one.state === "not-installed";
|
|
200
201
|
const checking = !signedIn && agents.some(one => one.state === "checking");
|
|
202
|
+
const others = (facts.others ?? []).filter(one => one.state !== "not-installed" && !WELCOME_AGENTS.some(own => own.id === one.id))
|
|
203
|
+
.map(one => ({ id: one.id, name: one.name, state: one.state, signIn: one.signIn, next: one.next, ready: one.ready }));
|
|
204
|
+
const rows = [...agents.map(one => withNext({ id: one.id, name: one.name, how: null, ready: false, next: null,
|
|
205
|
+
state: one.state === "checking" ? "unknown" : one.state, signIn: one.state === "signed-out" ? WELCOME_AGENTS.find(own => own.id === one.id).signIn : null }, facts.routed ?? null)), ...others];
|
|
206
|
+
const firstTask = firstTaskOf(rows, facts.routed ?? null);
|
|
207
|
+
const firstTaskName = firstTask === null ? null : rows.find(one => one.id === firstTask)?.name ?? null;
|
|
208
|
+
const lines = firstRunAgentLines(rows, firstTask, checking, facts.routed ?? null);
|
|
201
209
|
const builds = facts.buildsOn === null ? null : WELCOME_AGENTS.find(one => one.id === facts.buildsOn)?.name ?? facts.buildsOn;
|
|
202
210
|
const buildsWith = facts.inUse !== null && facts.buildsOn !== null && facts.buildsOn !== facts.inUse ? builds : null;
|
|
203
211
|
const hasProject = facts.projects.length > 0;
|
|
@@ -207,11 +215,11 @@ export function welcomeModel(facts) {
|
|
|
207
215
|
const taskOpen = hasProject;
|
|
208
216
|
const starters = facts.starters.slice(0, 3).map(one => ({ label: one.label, draft: one.draft, href: `/tasks/new?text=${encodeURIComponent(one.draft)}` }));
|
|
209
217
|
const model = {
|
|
210
|
-
agent: { done:
|
|
218
|
+
agent: { done: firstTask !== null, checking, agents, inUse: facts.inUse, buildsWith, others, firstTask: firstTaskName, lines },
|
|
211
219
|
project: { done: projectDone, projects: [...facts.projects], sample, notTakingWork: hasProject && !facts.ready },
|
|
212
220
|
task: { done: facts.hasTask, open: taskOpen, starters: taskOpen ? starters : [] },
|
|
213
221
|
};
|
|
214
|
-
const agentAct =
|
|
222
|
+
const agentAct = firstTask === null ? false : facts.inUse === null && agents.some(one => one.canUse);
|
|
215
223
|
const next = agentAct ? "agent" : !projectDone ? "project" : !facts.hasTask ? "task" : null;
|
|
216
224
|
const left = [model.agent.done, model.project.done, model.task.done].filter(done => !done).length;
|
|
217
225
|
return { ...model, next, left };
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { FLEXIBLE_AVAILABLE } from "./flexible-available.js";
|
|
3
|
+
import { flexibleDelegationAtUse, FLEXIBLE_LEVEL_WORDS, FLEXIBLE_LEVELS, FLEXIBLE_UNAVAILABLE, isFlexibleLevel } from "./flexible.js";
|
|
4
|
+
import { threadLevelAllowed, FLEXIBLE_PROVEN } from "./flexible-levels.js";
|
|
5
|
+
import { defaultCustodyIO, flexibleLaunch, freshProviderFolder, onLaunchStopped, unsettledLaunch, watchLaunch } from "./flexible-launch.js";
|
|
6
|
+
import { relayUrlInside } from "./flexible-relay.js";
|
|
7
|
+
import { startIdentityOf } from "./flexible-custody.js";
|
|
8
|
+
import { flexibleFenceDigest, flexibleThread, flexibleThreadFence, flexibleThreadSpawn, stopFlexibleThreadAgent, withFlexibleThreadLock } from "./flexible-thread.js";
|
|
9
|
+
import { beginFlexibleTurn, finishFlexibleTurn, flexibleTurn, interruptFlexibleTurnsOf, withdrawFlexibleTurn, withdrawFlexibleTurnsAbove } from "./flexible-turns.js";
|
|
10
|
+
import { CLAUDE_BUILD_ISOLATION_ARGV, HEADLESS_DISALLOWED_TOOLS } from "./provider.js";
|
|
11
|
+
const WORDS = Object.freeze({
|
|
12
|
+
"not-available": FLEXIBLE_UNAVAILABLE,
|
|
13
|
+
"codex-unsupported": "Codex's own sandbox can't keep a Flexible thread away from threads and files made after it starts, so Codex and OpenRouter threads can't run yet. Nothing was started.",
|
|
14
|
+
"invalid-request": "That request isn't one Toolroll can read. Nothing was started.",
|
|
15
|
+
"level-pending": "This thread's agent is still running with other settings. Stop it, and the next turn starts with the new ones.",
|
|
16
|
+
"not-owner": "Only the person who started this thread can change its level. Nothing changed.",
|
|
17
|
+
"not-lower": "Lowering can't raise a thread's level. Send the next message at the higher level with your password instead. Nothing changed.",
|
|
18
|
+
"thread-unknown": "There's no such thread.",
|
|
19
|
+
"stale-generation": "Your account changed since this started. Sign in again and retry. Nothing changed.",
|
|
20
|
+
});
|
|
21
|
+
const refuse = (reason, message = WORDS[reason]) => ({ ok: false, reason, message });
|
|
22
|
+
const MODEL = /^[A-Za-z0-9._:/@+-]{1,200}$/;
|
|
23
|
+
const EMPTY_MCP = '{"mcpServers":{}}';
|
|
24
|
+
export function flexibleAgentLaunch(provider, level, input) {
|
|
25
|
+
if (!FLEXIBLE_AVAILABLE)
|
|
26
|
+
return refuse("not-available");
|
|
27
|
+
if (typeof input.model !== "string" || !MODEL.test(input.model) || !isFlexibleLevel(level))
|
|
28
|
+
return refuse("invalid-request");
|
|
29
|
+
if (provider === "codex" || provider === "openrouter")
|
|
30
|
+
return refuse("codex-unsupported");
|
|
31
|
+
if (level !== "full")
|
|
32
|
+
return refuse("invalid-request", `${FLEXIBLE_LEVEL_WORDS[level]} isn't offered for any agent yet. Nothing was started.`);
|
|
33
|
+
if (provider === "claude") {
|
|
34
|
+
return { ok: true, spec: { file: "claude", args: [
|
|
35
|
+
"-p", "--input-format", "stream-json", "--output-format", "stream-json", "--verbose",
|
|
36
|
+
"--permission-mode", "bypassPermissions",
|
|
37
|
+
"--model", input.model,
|
|
38
|
+
...CLAUDE_BUILD_ISOLATION_ARGV, "--mcp-config", EMPTY_MCP,
|
|
39
|
+
"--disallowedTools", HEADLESS_DISALLOWED_TOOLS.join(","),
|
|
40
|
+
"--settings", JSON.stringify({ autoMemoryEnabled: false }),
|
|
41
|
+
], env: {
|
|
42
|
+
ANTHROPIC_BASE_URL: input.relay.url, ANTHROPIC_AUTH_TOKEN: input.relay.token,
|
|
43
|
+
CLAUDE_CONFIG_DIR: input.configHome, DISABLE_AUTOUPDATER: "1", CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1",
|
|
44
|
+
} } };
|
|
45
|
+
}
|
|
46
|
+
return { ok: true, spec: { file: "gemini", args: ["--output-format", "stream-json", "--approval-mode", "yolo", "-m", input.model],
|
|
47
|
+
env: { GOOGLE_GEMINI_BASE_URL: input.relay.url, GEMINI_API_KEY: input.relay.token, GEMINI_CLI_HOME: input.configHome } } };
|
|
48
|
+
}
|
|
49
|
+
function stillMayStart(store, turnId, holder) {
|
|
50
|
+
const turn = flexibleTurn(store, turnId);
|
|
51
|
+
if (turn === null || turn.status !== "running" || turn.holder !== holder)
|
|
52
|
+
return "the turn is no longer running";
|
|
53
|
+
const chosen = store.handle.prepare("SELECT level FROM flexible_thread_level WHERE thread = ?").get(turn.thread);
|
|
54
|
+
if (chosen !== undefined && isFlexibleLevel(chosen["level"]) && FLEXIBLE_LEVELS.indexOf(turn.level) > FLEXIBLE_LEVELS.indexOf(chosen["level"])) {
|
|
55
|
+
return `the thread was lowered to ${FLEXIBLE_LEVEL_WORDS[chosen["level"]]}`;
|
|
56
|
+
}
|
|
57
|
+
const delegation = flexibleDelegationAtUse(store, { repo: turn.repo, starter: { name: turn.owner, generation: turn.ownerGeneration }, digest: turn.termsDigest, level: turn.level, freshProof: true });
|
|
58
|
+
if (!delegation.ok)
|
|
59
|
+
return delegation.message;
|
|
60
|
+
if (delegation.termsId !== turn.termsId)
|
|
61
|
+
return "the project's Flexible terms changed";
|
|
62
|
+
const allowed = threadLevelAllowed(store.orgPolicy(), turn.provider, turn.level);
|
|
63
|
+
return allowed.ok ? null : allowed.message;
|
|
64
|
+
}
|
|
65
|
+
export async function startFlexibleTurn(store, input) {
|
|
66
|
+
if (!FLEXIBLE_AVAILABLE)
|
|
67
|
+
return refuse("not-available");
|
|
68
|
+
const first = flexibleTurn(store, input.turn);
|
|
69
|
+
if (first === null)
|
|
70
|
+
return { ok: false, reason: "turn-unknown", message: "There's no such turn." };
|
|
71
|
+
const unused = async () => { try {
|
|
72
|
+
await input.network.close();
|
|
73
|
+
}
|
|
74
|
+
catch { } };
|
|
75
|
+
let started;
|
|
76
|
+
try {
|
|
77
|
+
started = await withFlexibleThreadLock(first.thread, () => startLocked(store, input));
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
await unused();
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
if (!started.ok || started.launched === "reused")
|
|
84
|
+
await unused();
|
|
85
|
+
return started;
|
|
86
|
+
}
|
|
87
|
+
async function startLocked(store, input) {
|
|
88
|
+
const turn = flexibleTurn(store, input.turn);
|
|
89
|
+
if (turn === null)
|
|
90
|
+
return { ok: false, reason: "turn-unknown", message: "There's no such turn." };
|
|
91
|
+
if (turn.status !== "admitted")
|
|
92
|
+
return { ok: false, reason: "turn-not-admitted", message: "This turn isn't waiting to start, so it wasn't started again." };
|
|
93
|
+
const clocked = input.clock === undefined ? {} : { clock: input.clock };
|
|
94
|
+
if (turn.holder !== input.holder)
|
|
95
|
+
return { ok: false, reason: "held-elsewhere", message: "Another Toolroll process holds this turn, so this one didn't touch it." };
|
|
96
|
+
const io = input.seams?.io ?? defaultCustodyIO();
|
|
97
|
+
const withdraw = (refusal) => { withdrawFlexibleTurn(store, { turn: turn.id, holder: input.holder, detail: refusal.message, ...clocked }); return refusal; };
|
|
98
|
+
const allowed = threadLevelAllowed(store.orgPolicy(), turn.provider, turn.level, input.proven ?? FLEXIBLE_PROVEN);
|
|
99
|
+
if (!allowed.ok)
|
|
100
|
+
return withdraw(allowed);
|
|
101
|
+
const network = { proxyPort: input.network.proxyPort, token: input.network.token, relayPort: input.relay.port };
|
|
102
|
+
const fenced = flexibleThreadFence(store, { id: turn.thread, network, storage: input.storage });
|
|
103
|
+
if (!fenced.ok)
|
|
104
|
+
return withdraw(fenced);
|
|
105
|
+
const running = unsettledLaunch(store, turn.thread);
|
|
106
|
+
if (running !== null) {
|
|
107
|
+
const same = running.state === "live" && running.holder === input.holder && running.provider === turn.provider && running.level === turn.level
|
|
108
|
+
&& running.leaderPid !== null && running.leaderStart !== null && startIdentityOf(running.leaderPid, io) === running.leaderStart
|
|
109
|
+
&& running.fenceDigest === flexibleFenceDigest(fenced.launch);
|
|
110
|
+
if (same) {
|
|
111
|
+
const begun = beginFlexibleTurn(store, { turn: turn.id, holder: input.holder, ...clocked });
|
|
112
|
+
if (!begun.ok)
|
|
113
|
+
return begun;
|
|
114
|
+
return { ok: true, launched: "reused", launchId: running.id };
|
|
115
|
+
}
|
|
116
|
+
const stopped = await stopFlexibleThreadAgent(store, { id: turn.thread, reason: "its agent was replaced", ...clocked, ...(input.seams?.io === undefined ? {} : { io: input.seams.io }), keepTurn: turn.id });
|
|
117
|
+
if ("reason" in stopped)
|
|
118
|
+
return withdraw(stopped);
|
|
119
|
+
if (!stopped.ok)
|
|
120
|
+
return withdraw(refuse("level-pending", `This thread's earlier agent couldn't be stopped (${stopped.why}), so no new one started.`));
|
|
121
|
+
}
|
|
122
|
+
const configHome = join(fenced.paths.cache, "provider", turn.id);
|
|
123
|
+
const platform = input.platform ?? process.platform;
|
|
124
|
+
const relayInside = relayUrlInside(input.relay.port, platform);
|
|
125
|
+
const agent = flexibleAgentLaunch(turn.provider, turn.level, { model: turn.model, relay: { url: relayInside, token: "0".repeat(57) }, configHome });
|
|
126
|
+
if (!agent.ok)
|
|
127
|
+
return withdraw(agent);
|
|
128
|
+
const begun = beginFlexibleTurn(store, { turn: turn.id, holder: input.holder, ...clocked });
|
|
129
|
+
if (!begun.ok)
|
|
130
|
+
return begun;
|
|
131
|
+
const endTurn = (detail) => finishFlexibleTurn(store, { turn: turn.id, holder: input.holder, outcome: "unknown", usage: { tokensIn: 0, tokensOut: 0, costUsd: 0 }, detail, ...clocked });
|
|
132
|
+
const providerProblem = freshProviderFolder(store, turn.thread, fenced.paths, configHome);
|
|
133
|
+
if (providerProblem !== null) {
|
|
134
|
+
endTurn(`the agent's configuration folder couldn't be made: ${providerProblem}`);
|
|
135
|
+
store.recordAction({ at: (input.clock ?? (() => new Date()))().toISOString(), actor: "toolroll", repo: null, taskId: null, runId: null, source: "policy", action: "flexible turn refused",
|
|
136
|
+
outcome: "refused", detail: `thread ${turn.thread} · turn ${turn.id} · ${providerProblem}` });
|
|
137
|
+
return { ok: false, reason: "setup-failed", message: "The agent's configuration folder isn't the one Toolroll made, so it didn't start. Nothing was removed." };
|
|
138
|
+
}
|
|
139
|
+
let endpoint = null;
|
|
140
|
+
const agentEnvFor = (launchId) => {
|
|
141
|
+
endpoint = input.relay.issue({ thread: turn.thread, turn: turn.id, launch: launchId });
|
|
142
|
+
const spec = flexibleAgentLaunch(turn.provider, turn.level, { model: turn.model, relay: { url: relayInside, token: endpoint.token }, configHome });
|
|
143
|
+
if (!spec.ok)
|
|
144
|
+
throw new Error(spec.message);
|
|
145
|
+
return spec.spec.env;
|
|
146
|
+
};
|
|
147
|
+
const spawned = await flexibleThreadSpawn(store, { id: turn.thread, network, storage: input.storage,
|
|
148
|
+
proxyEnv: input.network.proxyEnv, agentEnvFor, launch: { provider: turn.provider, level: turn.level, turn: turn.id, holder: input.holder },
|
|
149
|
+
beforeOpen: () => stillMayStart(store, turn.id, input.holder) }, agent.spec.file, agent.spec.args, platform, input.seams ?? {});
|
|
150
|
+
const issued = endpoint;
|
|
151
|
+
if (!spawned.ok) {
|
|
152
|
+
issued?.revoke();
|
|
153
|
+
endTurn(`the agent didn't start: ${spawned.message}`);
|
|
154
|
+
return spawned;
|
|
155
|
+
}
|
|
156
|
+
onLaunchStopped(spawned.launchId, input.network.close);
|
|
157
|
+
if (issued !== null)
|
|
158
|
+
onLaunchStopped(spawned.launchId, issued.revoke);
|
|
159
|
+
const stopWatching = watchLaunch(store, spawned.launchId, fenced.paths.folder, input.seams?.io);
|
|
160
|
+
spawned.child.once("exit", () => {
|
|
161
|
+
void stopFlexibleThreadAgent(store, { id: turn.thread, reason: "its agent exited", ...(input.seams?.io === undefined ? {} : { io: input.seams.io }) }).catch(() => { });
|
|
162
|
+
});
|
|
163
|
+
return { ok: true, launched: "started", launchId: spawned.launchId, pid: spawned.pid, child: spawned.child, stopWatching };
|
|
164
|
+
}
|
|
165
|
+
export async function lowerFlexibleThreadLevel(store, input) {
|
|
166
|
+
if (!FLEXIBLE_AVAILABLE)
|
|
167
|
+
return refuse("not-available");
|
|
168
|
+
if (!isFlexibleLevel(input.level) || typeof input.by?.name !== "string" || !Number.isSafeInteger(input.by.generation))
|
|
169
|
+
return refuse("invalid-request");
|
|
170
|
+
return withFlexibleThreadLock(input.thread, () => lowerLocked(store, input)).then(result => result);
|
|
171
|
+
}
|
|
172
|
+
async function lowerLocked(store, input) {
|
|
173
|
+
const rank = (level) => FLEXIBLE_LEVELS.indexOf(level);
|
|
174
|
+
const words = FLEXIBLE_LEVEL_WORDS[input.level];
|
|
175
|
+
const written = store.transact(() => {
|
|
176
|
+
const thread = flexibleThread(store, input.thread);
|
|
177
|
+
if (thread === null || thread.state !== "ready")
|
|
178
|
+
return refuse("thread-unknown");
|
|
179
|
+
if (thread.createdBy !== input.by.name)
|
|
180
|
+
return refuse("not-owner");
|
|
181
|
+
const account = store.accountOf(input.by.name);
|
|
182
|
+
if (account === null || account.revokedAt !== null)
|
|
183
|
+
return refuse("not-owner");
|
|
184
|
+
if (account.generation !== input.by.generation)
|
|
185
|
+
return refuse("stale-generation");
|
|
186
|
+
const chosen = store.handle.prepare("SELECT level FROM flexible_thread_level WHERE thread = ?").get(input.thread);
|
|
187
|
+
const running = unsettledLaunch(store, input.thread);
|
|
188
|
+
const last = store.handle.prepare("SELECT level FROM thread_turn WHERE thread = ? AND status <> 'withdrawn' ORDER BY seq DESC LIMIT 1").get(input.thread);
|
|
189
|
+
const current = [chosen?.["level"], running?.level, last?.["level"]].find(isFlexibleLevel) ?? null;
|
|
190
|
+
if (current !== null && rank(input.level) > rank(current))
|
|
191
|
+
return refuse("not-lower");
|
|
192
|
+
const at = (input.clock ?? (() => new Date()))().toISOString();
|
|
193
|
+
store.handle.prepare(`INSERT INTO flexible_thread_level (thread, level, set_by, set_at) VALUES (?, ?, ?, ?)
|
|
194
|
+
ON CONFLICT (thread) DO UPDATE SET level = excluded.level, set_by = excluded.set_by, set_at = excluded.set_at`).run(input.thread, input.level, input.by.name, at);
|
|
195
|
+
withdrawFlexibleTurnsAbove(store, { thread: input.thread, level: input.level, detail: `the thread was lowered to ${words}`, clock: () => new Date(at) });
|
|
196
|
+
store.recordAction({ at, actor: input.by.name, repo: thread.repo, taskId: null, runId: null, source: "policy", action: "flexible thread lowered", outcome: "lowered",
|
|
197
|
+
detail: `thread ${input.thread} · ${current ?? "unset"} → ${input.level}` });
|
|
198
|
+
return null;
|
|
199
|
+
});
|
|
200
|
+
if (written !== null)
|
|
201
|
+
return written;
|
|
202
|
+
const running = unsettledLaunch(store, input.thread);
|
|
203
|
+
if (running === null || rank(running.level) <= rank(input.level))
|
|
204
|
+
return { ok: true, level: input.level, stopped: "none", message: `Lowered to ${words}. The next turn starts at ${words}.` };
|
|
205
|
+
const stopped = await stopFlexibleThreadAgent(store, { id: input.thread, reason: `lowered to ${words}`, ...(input.clock === undefined ? {} : { clock: input.clock }), ...(input.io === undefined ? {} : { io: input.io }) });
|
|
206
|
+
if ("reason" in stopped)
|
|
207
|
+
return stopped;
|
|
208
|
+
return stopped.ok
|
|
209
|
+
? { ok: true, level: input.level, stopped: "stopped", message: `Lowered to ${words}; the current step was stopped.` }
|
|
210
|
+
: { ok: true, level: input.level, stopped: "pending", message: `Lowered to ${words}. Some of the agent's processes are still running, so no new turn starts until they stop (${stopped.why}).` };
|
|
211
|
+
}
|
|
212
|
+
export async function recoverFlexibleLaunchesOf(store, input) {
|
|
213
|
+
if (!FLEXIBLE_AVAILABLE)
|
|
214
|
+
return refuse("not-available");
|
|
215
|
+
if (typeof input.holder !== "string" || !/^[A-Za-z0-9._:-]{1,128}$/.test(input.holder))
|
|
216
|
+
return refuse("invalid-request");
|
|
217
|
+
const launches = store.handle.prepare("SELECT thread, id FROM flexible_thread_launch WHERE holder = ? AND state <> 'exited' ORDER BY started_at, id").all(input.holder)
|
|
218
|
+
.map(row => ({ thread: String(row["thread"]), launch: String(row["id"]) }));
|
|
219
|
+
let settled = 0, unresolved = 0;
|
|
220
|
+
for (const one of launches) {
|
|
221
|
+
const result = await stopFlexibleThreadAgent(store, { id: one.thread, reason: "Toolroll restarted", holderGone: true, expect: { launch: one.launch, holder: input.holder },
|
|
222
|
+
...(input.clock === undefined ? {} : { clock: input.clock }), ...(input.io === undefined ? {} : { io: input.io }) });
|
|
223
|
+
if ("reason" in result || !result.ok)
|
|
224
|
+
unresolved++;
|
|
225
|
+
else if (result.how === "replaced" && flexibleLaunch(store, one.launch)?.state !== "exited")
|
|
226
|
+
unresolved++;
|
|
227
|
+
else
|
|
228
|
+
settled++;
|
|
229
|
+
}
|
|
230
|
+
if (unresolved > 0)
|
|
231
|
+
return { ok: true, settled, unresolved, withdrawn: 0, interrupted: 0 };
|
|
232
|
+
const ended = interruptFlexibleTurnsOf(store, { holder: input.holder, ...(input.clock === undefined ? {} : { clock: input.clock }) });
|
|
233
|
+
return { ok: true, settled, unresolved, withdrawn: ended.ok ? ended.withdrawn : 0, interrupted: ended.ok ? ended.interrupted : 0 };
|
|
234
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export const V167_SCHEMA = `
|
|
2
|
+
CREATE TABLE IF NOT EXISTS thread_decision (
|
|
3
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
4
|
+
decision_id TEXT NOT NULL UNIQUE CHECK (length(decision_id) = 32 AND decision_id NOT GLOB '*[^0-9a-f]*'),
|
|
5
|
+
thread TEXT NOT NULL CHECK (length(thread) = 32 AND thread NOT GLOB '*[^0-9a-f]*'),
|
|
6
|
+
turn TEXT NOT NULL CHECK (length(turn) = 32 AND turn NOT GLOB '*[^0-9a-f]*'),
|
|
7
|
+
request TEXT CHECK (request IS NULL OR (length(request) = 32 AND request NOT GLOB '*[^0-9a-f]*')),
|
|
8
|
+
request_ref TEXT NOT NULL CHECK (length(request_ref) BETWEEN 1 AND 200),
|
|
9
|
+
source TEXT NOT NULL CHECK (source IN ('person', 'policy', 'provider')),
|
|
10
|
+
rule TEXT CHECK (rule IS NULL OR (length(rule) BETWEEN 1 AND 40 AND rule NOT GLOB '*[^a-z-]*')),
|
|
11
|
+
decision TEXT NOT NULL CHECK (decision IN ('accept', 'decline', 'cancel', 'answer')),
|
|
12
|
+
request_digest TEXT CHECK (request_digest IS NULL OR (length(request_digest) = 64 AND request_digest NOT GLOB '*[^0-9a-f]*')),
|
|
13
|
+
payload_digest TEXT CHECK (payload_digest IS NULL OR (length(payload_digest) = 64 AND payload_digest NOT GLOB '*[^0-9a-f]*')),
|
|
14
|
+
connection_id TEXT NOT NULL CHECK (length(connection_id) = 32 AND connection_id NOT GLOB '*[^0-9a-f]*'),
|
|
15
|
+
state TEXT NOT NULL CHECK (state IN ('decided', 'sending', 'sent', 'delivered', 'not-sent', 'uncertain')),
|
|
16
|
+
at TEXT NOT NULL,
|
|
17
|
+
by TEXT NOT NULL CHECK (length(by) BETWEEN 1 AND 200),
|
|
18
|
+
by_generation INTEGER,
|
|
19
|
+
attempt_id TEXT CHECK (attempt_id IS NULL OR (length(attempt_id) = 32 AND attempt_id NOT GLOB '*[^0-9a-f]*')),
|
|
20
|
+
write_started_at TEXT,
|
|
21
|
+
state_at TEXT NOT NULL,
|
|
22
|
+
detail TEXT CHECK (detail IS NULL OR length(detail) <= 500),
|
|
23
|
+
CHECK ((source = 'provider') = (request IS NULL)),
|
|
24
|
+
CHECK ((source = 'person') = (by_generation IS NOT NULL)),
|
|
25
|
+
CHECK ((source = 'policy') = (rule IS NOT NULL)),
|
|
26
|
+
CHECK (source <> 'policy' OR decision IN ('decline', 'cancel')),
|
|
27
|
+
CHECK (source = 'provider' OR (request_digest IS NOT NULL AND payload_digest IS NOT NULL)),
|
|
28
|
+
CHECK (source <> 'provider' OR (state = 'delivered' AND decision IN ('accept', 'decline') AND write_started_at IS NULL)),
|
|
29
|
+
CHECK (state <> 'sent' OR write_started_at IS NOT NULL),
|
|
30
|
+
CHECK ((attempt_id IS NULL) = (write_started_at IS NULL)),
|
|
31
|
+
CHECK (write_started_at IS NULL OR state IN ('sending', 'sent', 'delivered', 'uncertain'))
|
|
32
|
+
);
|
|
33
|
+
CREATE UNIQUE INDEX IF NOT EXISTS thread_decision_request ON thread_decision (request) WHERE request IS NOT NULL;
|
|
34
|
+
CREATE INDEX IF NOT EXISTS thread_decision_turn ON thread_decision (thread, turn);
|
|
35
|
+
CREATE INDEX IF NOT EXISTS thread_decision_connection ON thread_decision (connection_id, state);
|
|
36
|
+
CREATE TRIGGER IF NOT EXISTS thread_decision_born BEFORE INSERT ON thread_decision
|
|
37
|
+
WHEN NEW.state IS NOT (CASE NEW.source WHEN 'provider' THEN 'delivered' ELSE 'decided' END) OR NEW.write_started_at IS NOT NULL OR NEW.attempt_id IS NOT NULL
|
|
38
|
+
BEGIN
|
|
39
|
+
SELECT RAISE(ABORT, 'a thread decision is born decided (a provider''s observed delivered)');
|
|
40
|
+
END;
|
|
41
|
+
CREATE TRIGGER IF NOT EXISTS thread_decision_written_once BEFORE UPDATE ON thread_decision
|
|
42
|
+
WHEN NEW.id IS NOT OLD.id OR NEW.decision_id IS NOT OLD.decision_id OR NEW.thread IS NOT OLD.thread OR NEW.turn IS NOT OLD.turn
|
|
43
|
+
OR NEW.request IS NOT OLD.request OR NEW.request_ref IS NOT OLD.request_ref OR NEW.source IS NOT OLD.source OR NEW.rule IS NOT OLD.rule
|
|
44
|
+
OR NEW.decision IS NOT OLD.decision OR NEW.request_digest IS NOT OLD.request_digest OR NEW.payload_digest IS NOT OLD.payload_digest
|
|
45
|
+
OR NEW.connection_id IS NOT OLD.connection_id OR NEW.at IS NOT OLD.at OR NEW.by IS NOT OLD.by OR NEW.by_generation IS NOT OLD.by_generation
|
|
46
|
+
OR OLD.state IN ('delivered', 'not-sent', 'uncertain')
|
|
47
|
+
OR (OLD.state = 'decided' AND NEW.state NOT IN ('decided', 'sending', 'not-sent', 'uncertain'))
|
|
48
|
+
OR (OLD.state = 'sending' AND NEW.state NOT IN ('sending', 'sent', 'not-sent', 'uncertain'))
|
|
49
|
+
OR (OLD.state = 'sent' AND NEW.state NOT IN ('sent', 'delivered', 'uncertain'))
|
|
50
|
+
OR (OLD.write_started_at IS NOT NULL AND (NEW.write_started_at IS NOT OLD.write_started_at OR NEW.attempt_id IS NOT OLD.attempt_id))
|
|
51
|
+
OR (OLD.write_started_at IS NULL AND NEW.write_started_at IS NOT NULL AND (OLD.state <> 'sending' OR NEW.state <> 'sending'))
|
|
52
|
+
BEGIN
|
|
53
|
+
SELECT RAISE(ABORT, 'a thread decision is written once and its state only moves forward');
|
|
54
|
+
END;
|
|
55
|
+
`;
|
|
56
|
+
export const V167_TABLES = Object.freeze(["thread_decision"]);
|
|
57
|
+
export const V167_STEPS = Object.freeze(["thread-decision"]);
|
|
58
|
+
export const V167_OBJECTS = Object.freeze([
|
|
59
|
+
["table", "thread_decision"], ["index", "thread_decision_request"], ["index", "thread_decision_turn"], ["index", "thread_decision_connection"],
|
|
60
|
+
["trigger", "thread_decision_born"], ["trigger", "thread_decision_written_once"],
|
|
61
|
+
]);
|