toolroll 0.9.129 → 0.9.131
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 +44 -0
- package/dist/agent-fence.js +0 -2
- package/dist/agent-state.js +1 -1
- package/dist/agents-board.js +0 -3
- package/dist/assignment.js +4 -3
- package/dist/backup-ui.js +6 -3
- package/dist/bounded-spawn.js +0 -1
- package/dist/browser/manifest.json +57 -0
- package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.br +0 -0
- package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.gz +0 -0
- package/dist/browser/{terminal.js → terminal.8978c8cbb268e1006f4b0be371c2401b58078e6d9f1380f4a5c2a12da843909f.js} +1 -1
- package/dist/browser/terminal.8978c8cbb268e1006f4b0be371c2401b58078e6d9f1380f4a5c2a12da843909f.js.br +0 -0
- package/dist/browser/terminal.8978c8cbb268e1006f4b0be371c2401b58078e6d9f1380f4a5c2a12da843909f.js.gz +0 -0
- package/dist/browser/{workspace.js → workspace.4f5935dc80574baadf1adafd39a08c567fb1fac1323052ca355546622f9b06d2.js} +25 -25
- package/dist/browser/workspace.4f5935dc80574baadf1adafd39a08c567fb1fac1323052ca355546622f9b06d2.js.br +0 -0
- package/dist/browser/workspace.4f5935dc80574baadf1adafd39a08c567fb1fac1323052ca355546622f9b06d2.js.gz +0 -0
- package/dist/browser/{workspace.css → workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css} +1 -1
- package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.br +0 -0
- package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.gz +0 -0
- package/dist/browser-shell.js +271 -25
- package/dist/builder-restart.js +13 -4
- package/dist/builder.js +4 -1
- package/dist/canvas-freshness.js +122 -0
- package/dist/canvas-fs.js +273 -0
- package/dist/canvas-reads.js +747 -0
- package/dist/canvas-state-key.js +45 -0
- package/dist/canvas-state-worker.js +12 -0
- package/dist/central-recovery-stage.js +3 -3
- package/dist/chat-actions.js +26 -0
- package/dist/chat-channel.js +4 -3
- package/dist/chat-follow.js +4 -0
- package/dist/chat-task-actions.js +1 -1
- package/dist/chat-warmth.js +0 -1
- package/dist/check-report.js +0 -6
- package/dist/commit-extract.js +79 -40
- package/dist/companion/access.js +1 -0
- package/dist/companion/actions.js +79 -16
- package/dist/companion/canvas.js +141 -0
- package/dist/companion/chat-origin.js +92 -0
- package/dist/companion/chat.js +571 -0
- package/dist/companion/daemon.js +46 -5
- package/dist/companion/finish.js +51 -0
- package/dist/companion/reads.js +5 -1
- package/dist/companion/schema.js +64 -0
- package/dist/companion/service.js +1 -0
- package/dist/companion/store.js +1 -0
- package/dist/companion/webauthn.js +11 -2
- package/dist/console-instance.js +0 -4
- package/dist/contracts/console-api.js +1 -1
- package/dist/converse.js +0 -1
- package/dist/coordinator.js +1 -1
- package/dist/db-tuning.js +16 -0
- package/dist/desktop-config.js +156 -0
- package/dist/desktop-host.js +6 -152
- package/dist/desktop-update.js +1 -1
- package/dist/discord.js +9 -3
- package/dist/dispatch.js +32 -4
- package/dist/doctor.js +30 -0
- package/dist/evidence.js +49 -3
- package/dist/exec.js +3 -0
- package/dist/finished-task-sql.js +14 -0
- package/dist/flexible-answers.js +0 -1
- package/dist/git-views.js +7 -1
- package/dist/github-read.js +41 -0
- package/dist/guides.js +4 -3
- package/dist/invoke.js +12 -2
- package/dist/lead-doors.js +50 -3
- package/dist/lead-queue-runtime.js +18 -6
- package/dist/lead-queue.js +25 -0
- package/dist/lead-status.js +3 -3
- package/dist/live-backup-change.js +93 -0
- package/dist/live-backup.js +75 -31
- package/dist/live-bus.js +3 -2
- package/dist/live-records.js +0 -1
- package/dist/local-owner.js +0 -1
- package/dist/loop-recovery.js +40 -0
- package/dist/maintenance.js +29 -1
- package/dist/model-choices.js +0 -11
- package/dist/needs-you.js +2 -0
- package/dist/nightly-check.js +0 -3
- package/dist/nightly-github.js +1 -2
- package/dist/notification-retention.js +119 -0
- package/dist/observations.js +0 -1
- package/dist/operate.js +393 -56
- package/dist/planner.js +4 -1
- package/dist/pull-request-flow.js +56 -15
- package/dist/qr.js +0 -25
- package/dist/query-index-schema.js +10 -0
- package/dist/read-executor-worker.js +9 -7
- package/dist/read-executor.js +18 -1
- package/dist/read-worker-connection.js +7 -0
- package/dist/release-adapters.js +63 -48
- package/dist/release-auto.js +17 -13
- package/dist/release-card.js +0 -4
- package/dist/release-cli.js +58 -10
- package/dist/release-next.js +1 -1
- package/dist/release-stops.js +71 -0
- package/dist/release-supersede.js +125 -0
- package/dist/release-supervise.js +90 -0
- package/dist/release.js +265 -42
- package/dist/restore-retirement.js +4 -1
- package/dist/result-review.js +0 -3
- package/dist/retention-ui.js +1 -1
- package/dist/retention.js +14 -21
- package/dist/review-context.js +0 -5
- package/dist/route-rules.js +1 -5
- package/dist/runner.js +36 -1
- package/dist/sandbox-runtime.js +0 -1
- package/dist/scope.js +1 -13
- package/dist/scout.js +4 -7
- package/dist/serve.js +122 -12
- package/dist/server/kit.js +1 -0
- package/dist/server/live.js +9 -2
- package/dist/server/pages.js +3 -2
- package/dist/server/phone-engine-api.js +22 -2
- package/dist/server/render-pages.js +1 -11
- package/dist/server/render-results.js +0 -1
- package/dist/server/render-tasks.js +0 -68
- package/dist/server/route-table.js +1 -1
- package/dist/server/settings-phone.js +32 -9
- package/dist/server/settings-scripts.js +1 -1
- package/dist/server/settings-section-table.js +0 -3
- package/dist/server/settings-sections.js +4 -2
- package/dist/server/settings.js +20 -5
- package/dist/server/terminals.js +2 -1
- package/dist/shutdown-proof.js +29 -0
- package/dist/slack.js +9 -3
- package/dist/sqlite-telemetry.js +26 -14
- package/dist/statement-cache.js +137 -0
- package/dist/store.js +86 -14
- package/dist/structured-output.js +0 -1
- package/dist/style-asset.js +20 -7
- package/dist/tailnet.js +0 -3
- package/dist/task-control.js +5 -0
- package/dist/task-sizing.js +0 -1
- package/dist/task-status.js +1 -1
- package/dist/teams.js +9 -3
- package/dist/teardown-git.js +10 -1
- package/dist/telegram.js +4 -1
- package/dist/toolroll-update-cli.js +19 -3
- package/dist/toolroll-update-ui.js +2 -2
- package/dist/toolroll-update.js +95 -15
- package/dist/up-processes.js +16 -1
- package/dist/up-role-child.js +2 -0
- package/dist/wake-hub.js +15 -6
- package/dist/work-index.js +101 -44
- package/dist/work-reads.js +53 -9
- package/dist/work-snapshots.js +185 -0
- package/dist/worker-statement-cache.js +83 -0
- package/dist/workspace-revision.js +145 -21
- package/package.json +1 -1
- package/dist/companion/webauthn-test-support.js +0 -44
- package/dist/contracts/contract-test.js +0 -55
- package/dist/unfenced-test-support.js +0 -6
- /package/dist/browser/{terminal.css → terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css} +0 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { builderWish } from "./builder-restart.js";
|
|
2
|
+
import { updateAdmissionPaused } from "./desktop-update-gate.js";
|
|
3
|
+
import { drainOf } from "./runner.js";
|
|
4
|
+
const LEASE_CAUSES_RELAUNCHED = new Set(["taken", "missing", "unbound", "paused"]);
|
|
5
|
+
export function loopEndRecovery(end, relaunching) {
|
|
6
|
+
if (end.ok)
|
|
7
|
+
return "done";
|
|
8
|
+
if (end.cleanupIncomplete === true)
|
|
9
|
+
return "fatal";
|
|
10
|
+
switch (end.reason) {
|
|
11
|
+
case "watch-busy": return relaunching ? "relaunch" : "fatal";
|
|
12
|
+
case "lease-lost": return end.lost !== undefined && LEASE_CAUSES_RELAUNCHED.has(end.lost) ? "relaunch" : "fatal";
|
|
13
|
+
case "reconciliation-failed":
|
|
14
|
+
case "loop-failed": return end.busy === true ? "relaunch" : "fatal";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function relaunchAdmission(store, runner, repo, now) {
|
|
18
|
+
const found = store.getRunner(runner);
|
|
19
|
+
if (found === null || found.runner.retiredAt !== null)
|
|
20
|
+
return "identity";
|
|
21
|
+
if (!found.runner.repos.includes(repo))
|
|
22
|
+
return "gone";
|
|
23
|
+
if (updateAdmissionPaused(store.raw()))
|
|
24
|
+
return "update";
|
|
25
|
+
if (drainOf(store, found.runner, now) !== null)
|
|
26
|
+
return "drain";
|
|
27
|
+
if (builderWish(store)?.desired === "stopped")
|
|
28
|
+
return "stopped";
|
|
29
|
+
return "go";
|
|
30
|
+
}
|
|
31
|
+
export const RELAUNCH_WAIT_WORDS = {
|
|
32
|
+
update: "the app update to finish",
|
|
33
|
+
drain: "Toolroll's restart to finish",
|
|
34
|
+
stopped: "the builder to be turned back on",
|
|
35
|
+
};
|
|
36
|
+
export const LOOP_RELAUNCH_MAX_MS = 30_000;
|
|
37
|
+
export function relaunchDelayMs(attempt, firstMs = 1_000) {
|
|
38
|
+
return Math.min(LOOP_RELAUNCH_MAX_MS, firstMs * 2 ** Math.min(Math.max(attempt, 0), 16));
|
|
39
|
+
}
|
|
40
|
+
export const LOOP_STABLE_MS = 60_000;
|
package/dist/maintenance.js
CHANGED
|
@@ -4,19 +4,39 @@ export function startMaintenance(args) {
|
|
|
4
4
|
}
|
|
5
5
|
let stopped = false;
|
|
6
6
|
let pending = null;
|
|
7
|
+
let failures = 0;
|
|
8
|
+
let retry = null;
|
|
7
9
|
const runNow = () => {
|
|
8
10
|
if (stopped || args.shouldStop())
|
|
9
11
|
return Promise.resolve();
|
|
10
12
|
if (pending !== null)
|
|
11
13
|
return pending;
|
|
14
|
+
if (retry !== null) {
|
|
15
|
+
clearTimeout(retry);
|
|
16
|
+
retry = null;
|
|
17
|
+
}
|
|
12
18
|
pending = Promise.resolve().then(async () => {
|
|
13
|
-
if (!stopped && !args.shouldStop())
|
|
19
|
+
if (!stopped && !args.shouldStop()) {
|
|
14
20
|
await args.run();
|
|
21
|
+
failures = 0;
|
|
22
|
+
}
|
|
15
23
|
}).catch(error => {
|
|
24
|
+
failures++;
|
|
16
25
|
try {
|
|
17
26
|
args.onError(error);
|
|
18
27
|
}
|
|
19
28
|
catch { }
|
|
29
|
+
let again = null;
|
|
30
|
+
try {
|
|
31
|
+
again = args.retryAfter?.(error, failures) ?? null;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
again = null;
|
|
35
|
+
}
|
|
36
|
+
if (again !== null && !stopped) {
|
|
37
|
+
retry = setTimeout(() => { retry = null; void runNow(); }, again);
|
|
38
|
+
retry.unref?.();
|
|
39
|
+
}
|
|
20
40
|
}).finally(() => { pending = null; });
|
|
21
41
|
return pending;
|
|
22
42
|
};
|
|
@@ -28,9 +48,17 @@ export function startMaintenance(args) {
|
|
|
28
48
|
if (!stopped) {
|
|
29
49
|
stopped = true;
|
|
30
50
|
clearInterval(timer);
|
|
51
|
+
if (retry !== null) {
|
|
52
|
+
clearTimeout(retry);
|
|
53
|
+
retry = null;
|
|
54
|
+
}
|
|
31
55
|
args.onStop?.();
|
|
32
56
|
}
|
|
33
57
|
await pending;
|
|
34
58
|
},
|
|
35
59
|
};
|
|
36
60
|
}
|
|
61
|
+
export const BUSY_RETRY_MS = [5_000, 10_000, 30_000];
|
|
62
|
+
export function busyRetryAfter(busy, failuresInARow, steps = BUSY_RETRY_MS) {
|
|
63
|
+
return busy ? steps[failuresInARow - 1] ?? null : null;
|
|
64
|
+
}
|
package/dist/model-choices.js
CHANGED
|
@@ -17,9 +17,6 @@ export const ALL_SLOTS = Object.freeze([
|
|
|
17
17
|
]);
|
|
18
18
|
const order = (slot) => ALL_SLOTS.findIndex(one => one.phase === slot.phase && one.tier === slot.tier);
|
|
19
19
|
export const slotKey = (slot) => slot.tier === "routine" ? slot.phase : `${slot.phase}-${slot.tier}`;
|
|
20
|
-
export function slotOf(key) {
|
|
21
|
-
return SLOTS.find(one => slotKey(one) === key) ?? null;
|
|
22
|
-
}
|
|
23
20
|
export function slotWords(slot) {
|
|
24
21
|
if (slot.phase === "build")
|
|
25
22
|
return slot.tier === "light" ? "Builder for small tasks" : slot.tier === "strong" ? "Builder for large or risky tasks" : "Builder for routine tasks";
|
|
@@ -41,10 +38,6 @@ export function stateDigest(store, scope) {
|
|
|
41
38
|
});
|
|
42
39
|
return sha({ scope, installation: rows(INSTALLATION_SCOPE), project: scope === INSTALLATION_SCOPE ? null : rows(scope) });
|
|
43
40
|
}
|
|
44
|
-
export function changeDigest(scope, change) {
|
|
45
|
-
const ordered = [...change].sort((a, b) => order(a.slot) - order(b.slot));
|
|
46
|
-
return sha({ scope, change: ordered.map(one => [slotKey(one.slot), one.to === null ? null : [one.to.provider, one.to.model]]) });
|
|
47
|
-
}
|
|
48
41
|
export function diffDigest(scope, diff) {
|
|
49
42
|
return sha({ scope, diff: diff.map(row => [slotKey(row.slot), row.before, row.after]) });
|
|
50
43
|
}
|
|
@@ -220,10 +213,6 @@ export function browserWords(problem) {
|
|
|
220
213
|
const said = problem.split(" — ")[0].replace(/`[^`]*`/g, "").replace(/\s+([:;,.])/g, "$1").replace(/\s{2,}/g, " ").trim();
|
|
221
214
|
return said.length === 0 ? "This choice can't run." : `${said.charAt(0).toUpperCase()}${said.slice(1)}${/[.!?]$/.test(said) ? "" : "."}`;
|
|
222
215
|
}
|
|
223
|
-
export function scopeProblem(store, repo) {
|
|
224
|
-
const checked = checkScope(store, repo);
|
|
225
|
-
return checked.kind === "ok" ? null : checked.problem;
|
|
226
|
-
}
|
|
227
216
|
const pairWords = (pair) => pair === null ? "not set" : `${pair.provider} · ${pair.model ?? "its default model"}`;
|
|
228
217
|
const ACTION_OF = (slot) => slot.tier === "routine" ? `agent for ${slot.phase} changed` : `${slot.tier} agent for ${slot.phase} changed`;
|
|
229
218
|
export function applyModelChoices(store, input, authority, now) {
|
package/dist/needs-you.js
CHANGED
|
@@ -117,6 +117,7 @@ export const WAITS = {
|
|
|
117
117
|
"plan-retry-later": context => `Waiting: Toolroll tries the plan again ${context.retry ?? "soon"}.`,
|
|
118
118
|
"app-update": () => "Waiting: Toolroll is updating. New work starts after it.",
|
|
119
119
|
"other-task": context => context.task == null ? "Waiting for another task to finish first." : `Waiting for ${context.task} to finish first.`,
|
|
120
|
+
"builder-restarting": () => "Waiting: this project's builder stopped, and Toolroll is restarting it.",
|
|
120
121
|
};
|
|
121
122
|
export { BANNED_WORDS, PLAIN_WORDS, bannedWordsIn, hasInternalWords } from "./plain-words.js";
|
|
122
123
|
import { hasInternalWords } from "./plain-words.js";
|
|
@@ -165,6 +166,7 @@ export const CODE_READING = {
|
|
|
165
166
|
"worker-at-capacity": { stage: "waiting", wait: "builders-busy" }, "retry-scheduled": { stage: "waiting", wait: "retry-later" },
|
|
166
167
|
"plan-retry": { stage: "waiting", wait: "plan-retry-later" },
|
|
167
168
|
updating: { stage: "waiting", wait: "app-update" }, "waiting-dependency": { stage: "waiting", wait: "other-task" },
|
|
169
|
+
"watch-stalled": { stage: "waiting", wait: "builder-restarting" },
|
|
168
170
|
"history-problem": { stage: "needs-you", need: "history", act: "inspect-task" },
|
|
169
171
|
"earlier-active": { stage: "needs-you", need: "earlier-version", act: "inspect-task" },
|
|
170
172
|
"waiting-decision": { stage: "needs-you", need: "answer", act: "answer-decision" },
|
package/dist/nightly-check.js
CHANGED
|
@@ -286,6 +286,3 @@ export function nightlyHealthWords(store, repo, configuredAt, now) {
|
|
|
286
286
|
return last === undefined ? `the nightly check hasn't run since it was set up (${since.slice(0, 16).replace("T", " ")})` : `the nightly check hasn't run since ${since.slice(0, 16).replace("T", " ")}`;
|
|
287
287
|
}
|
|
288
288
|
export { fileIdentity, groupIdentity, journeyIdentity, unitIdentity, versionIdentity };
|
|
289
|
-
export function openMainFailureItems(store, repo) {
|
|
290
|
-
return store.handle.prepare("SELECT DISTINCT item FROM main_check_failure WHERE repo = ? AND cleared_at IS NULL ORDER BY item").all(repo).map(row => String(row["item"]));
|
|
291
|
-
}
|
package/dist/nightly-github.js
CHANGED
|
@@ -25,7 +25,6 @@ const LANE_PREFIX = {
|
|
|
25
25
|
};
|
|
26
26
|
const AGGREGATE = /^node (\d+) · (ubuntu|macos)-latest$/;
|
|
27
27
|
export const SHARDS_PASSED_STEP = "Every shard passed";
|
|
28
|
-
export const SHARDS_COVERED_STEP = "Every shard ran the whole suite";
|
|
29
28
|
const object = (value) => (value !== null && typeof value === "object" && !Array.isArray(value) ? value : null);
|
|
30
29
|
const isoOf = (value) => {
|
|
31
30
|
if (typeof value !== "string" || value === "")
|
|
@@ -834,7 +833,7 @@ export async function refreshNightlyForDeploy(store, input) {
|
|
|
834
833
|
const at = input.now();
|
|
835
834
|
try {
|
|
836
835
|
const gh = input.gh ?? await (async () => {
|
|
837
|
-
const [{ exec, githubReads }, { releaseTools }] = await Promise.all([import("./
|
|
836
|
+
const [{ exec, githubReads }, { releaseTools }] = await Promise.all([import("./github-read.js"), import("./release-git.js")]);
|
|
838
837
|
return githubReads(exec, releaseTools().gh);
|
|
839
838
|
})();
|
|
840
839
|
const deps = { ...mainHistoryOf(store, repo, at), gh, now: input.now };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { finishedTask } from "./finished-task-sql.js";
|
|
2
|
+
import { LIFECYCLE_KEY_PREFIX, LIFECYCLE_KINDS } from "./notification-rows.js";
|
|
3
|
+
export const LIFECYCLE_RETENTION_DAYS = 30;
|
|
4
|
+
export const NOTIFICATION_SWEEP_BATCH = 2000;
|
|
5
|
+
export const LEGACY_DESTINATION = "legacy:single-destination";
|
|
6
|
+
export const LEGACY_SETTLED_RECEIPT = "skipped:legacy-undeliverable";
|
|
7
|
+
export const NOTIFICATION_RETENTION_CATEGORIES = Object.freeze({
|
|
8
|
+
"lifecycle-ping": { days: LIFECYCLE_RETENTION_DAYS, words: "progress updates (task filed, build started, phase, finished…) once delivered and their task is finished" },
|
|
9
|
+
"legacy-delivery": { days: LIFECYCLE_RETENTION_DAYS, words: "old single-destination delivery records that can never be delivered" },
|
|
10
|
+
});
|
|
11
|
+
export const NOTIFICATION_RETENTION_WORDS = `Progress updates and undeliverable old delivery records go after ${LIFECYCLE_RETENTION_DAYS} days once settled, whatever this says.`;
|
|
12
|
+
const KINDS = LIFECYCLE_KINDS.map(kind => `'${kind}'`).join(", ");
|
|
13
|
+
const LIFECYCLE_PING = (n) => `(${n}.kind IN (${KINDS})
|
|
14
|
+
AND substr(${n}.dedupe_key, 1, length('${LIFECYCLE_KEY_PREFIX}' || ${n}.kind || ':')) = '${LIFECYCLE_KEY_PREFIX}' || ${n}.kind || ':'
|
|
15
|
+
AND length(${n}.dedupe_key) - length(replace(${n}.dedupe_key, ':', '')) = 3
|
|
16
|
+
AND rtrim(${n}.dedupe_key, '0123456789') <> ${n}.dedupe_key AND substr(rtrim(${n}.dedupe_key, '0123456789'), -1) = ':')`;
|
|
17
|
+
export const UNDER_LIFECYCLE_KEY = (n) => `substr(${n}.dedupe_key, 1, ${LIFECYCLE_KEY_PREFIX.length}) = '${LIFECYCLE_KEY_PREFIX}'`;
|
|
18
|
+
const FAMILY = (n) => `rtrim(${n}.dedupe_key, '0123456789')`;
|
|
19
|
+
const UNSETTLED = (d) => `((${d}.delivered_at IS NULL AND (${d}.receipt IS NULL OR ${d}.receipt NOT LIKE 'skipped:%'))
|
|
20
|
+
OR (${d}.claim_owner IS NOT NULL AND ${d}.claim_expires_at > :now))`;
|
|
21
|
+
const PING_MAY_GO = (n, outbound) => `(${LIFECYCLE_PING(n)} AND ${n}.created_at < :cutoff
|
|
22
|
+
AND NOT EXISTS (SELECT 1 FROM notification_delivery d WHERE d.notification = ${n}.id AND ${UNSETTLED("d")})
|
|
23
|
+
AND (EXISTS (SELECT 1 FROM notification_delivery d WHERE d.notification = ${n}.id)
|
|
24
|
+
OR NOT EXISTS (SELECT 1 FROM chat_binding b WHERE b.revoked IS NULL))
|
|
25
|
+
AND NOT EXISTS (SELECT 1 FROM push_delivery p WHERE p.notification = ${n}.id AND p.state IN ('pending', 'claimed'))
|
|
26
|
+
AND ${n}.id NOT IN (SELECT i.notification FROM chat_batch_item i JOIN chat_batch b ON b.id = i.batch WHERE i.notification IS NOT NULL AND b.opened_at >= :cutoff)
|
|
27
|
+
${outbound ? `AND ${n}.id NOT IN (SELECT o.notification FROM telegram_outbound_message o)` : ""}
|
|
28
|
+
AND (${n}.task_ref IS NULL OR ${n}.task_ref IN (SELECT value FROM json_each(:finished))))`;
|
|
29
|
+
function finishedRefs(store, cutoff, only) {
|
|
30
|
+
const among = only === undefined
|
|
31
|
+
? `SELECT DISTINCT n.task_ref FROM notification n WHERE substr(n.dedupe_key, 1, ${LIFECYCLE_KEY_PREFIX.length}) = '${LIFECYCLE_KEY_PREFIX}' AND n.created_at < :cutoff AND n.task_ref IS NOT NULL`
|
|
32
|
+
: "SELECT value FROM json_each(:only)";
|
|
33
|
+
const params = only === undefined ? { cutoff } : { only: JSON.stringify(only) };
|
|
34
|
+
return JSON.stringify(store.handle.prepare(`SELECT r.id AS id FROM task_ref r WHERE r.id IN (${among}) AND ${finishedTask("r")}`).all(params).map(row => Number(row["id"])));
|
|
35
|
+
}
|
|
36
|
+
const cutoffOf = (now) => new Date(now.getTime() - LIFECYCLE_RETENTION_DAYS * 86_400_000).toISOString();
|
|
37
|
+
const hasTable = (store, name) => store.handle.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(name) !== undefined;
|
|
38
|
+
function familiesThatMayGo(store, now, limit, finished = finishedRefs(store, cutoffOf(now))) {
|
|
39
|
+
const outbound = hasTable(store, "telegram_outbound_message");
|
|
40
|
+
return store.handle.prepare(`SELECT ${FAMILY("n")} AS family, COUNT(*) AS pings
|
|
41
|
+
FROM notification n WHERE substr(n.dedupe_key, 1, ${LIFECYCLE_KEY_PREFIX.length}) = '${LIFECYCLE_KEY_PREFIX}'
|
|
42
|
+
GROUP BY family HAVING MIN(${PING_MAY_GO("n", outbound)}) = 1 ORDER BY MIN(n.id) LIMIT :limit`)
|
|
43
|
+
.all({ now: now.toISOString(), cutoff: cutoffOf(now), limit, finished })
|
|
44
|
+
.map(row => ({ family: String(row["family"]), pings: Number(row["pings"]) }));
|
|
45
|
+
}
|
|
46
|
+
export function notificationLifecyclePreview(store, now) {
|
|
47
|
+
const db = store.handle;
|
|
48
|
+
const settle = Number(db.prepare(`SELECT COUNT(*) AS n FROM notification_delivery d WHERE d.destination = :legacy AND d.delivered_at IS NULL AND d.receipt IS NULL
|
|
49
|
+
AND NOT (d.claim_owner IS NOT NULL AND d.claim_expires_at > :now)`).get({ legacy: LEGACY_DESTINATION, now: now.toISOString() })?.["n"] ?? 0);
|
|
50
|
+
const pings = familiesThatMayGo(store, now, -1).reduce((sum, one) => sum + one.pings, 0);
|
|
51
|
+
const legacy = Number(db.prepare(`SELECT COUNT(*) AS n FROM notification_delivery d JOIN notification n ON n.id = d.notification
|
|
52
|
+
WHERE d.destination = :legacy AND (d.receipt = :settled OR (d.delivered_at IS NULL AND d.receipt IS NULL)) AND n.created_at < :cutoff`)
|
|
53
|
+
.get({ legacy: LEGACY_DESTINATION, settled: LEGACY_SETTLED_RECEIPT, cutoff: cutoffOf(now) })?.["n"] ?? 0);
|
|
54
|
+
return { settle, pings, legacy };
|
|
55
|
+
}
|
|
56
|
+
export function sweepNotificationLifecycle(store, now, batch = NOTIFICATION_SWEEP_BATCH) {
|
|
57
|
+
const db = store.handle;
|
|
58
|
+
const at = now.toISOString(), cutoff = cutoffOf(now);
|
|
59
|
+
const settled = store.transact(() => Number(db.prepare(`UPDATE notification_delivery SET receipt = :settled WHERE rowid IN (
|
|
60
|
+
SELECT d.rowid FROM notification_delivery d WHERE d.destination = :legacy AND d.delivered_at IS NULL AND d.receipt IS NULL
|
|
61
|
+
AND NOT (d.claim_owner IS NOT NULL AND d.claim_expires_at > :now) LIMIT :batch)`)
|
|
62
|
+
.run({ settled: LEGACY_SETTLED_RECEIPT, legacy: LEGACY_DESTINATION, now: at, batch }).changes));
|
|
63
|
+
const families = [];
|
|
64
|
+
const deferred = { families: 0, pings: 0 };
|
|
65
|
+
let pings = 0, bytes = 0, more = false;
|
|
66
|
+
for (const one of familiesThatMayGo(store, now, -1)) {
|
|
67
|
+
if (one.pings > batch) {
|
|
68
|
+
deferred.families++;
|
|
69
|
+
deferred.pings += one.pings;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (more || pings + one.pings > batch) {
|
|
73
|
+
more = true;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
families.push(one.family);
|
|
77
|
+
pings += one.pings;
|
|
78
|
+
}
|
|
79
|
+
return store.transact(() => {
|
|
80
|
+
let removed = 0;
|
|
81
|
+
if (families.length > 0) {
|
|
82
|
+
const outbound = hasTable(store, "telegram_outbound_message");
|
|
83
|
+
const named = { families: JSON.stringify(families) };
|
|
84
|
+
const inFamilies = `${FAMILY("n")} IN (SELECT value FROM json_each(:families)) AND substr(n.dedupe_key, 1, ${LIFECYCLE_KEY_PREFIX.length}) = '${LIFECYCLE_KEY_PREFIX}'`;
|
|
85
|
+
const all = db.prepare(`SELECT ${FAMILY("n")} AS family, n.id AS id, n.task_ref AS ref FROM notification n WHERE ${inFamilies}`).all(named);
|
|
86
|
+
const refs = [...new Set(all.map(row => row["ref"]).filter(ref => ref !== null && ref !== undefined).map(Number))];
|
|
87
|
+
const finished = finishedRefs(store, cutoff, refs);
|
|
88
|
+
const free = new Set(db.prepare(`SELECT n.id AS id FROM notification n WHERE ${inFamilies} AND ${PING_MAY_GO("n", outbound)}`)
|
|
89
|
+
.all({ ...named, now: at, cutoff, finished }).map(row => Number(row["id"])));
|
|
90
|
+
const blocked = new Set(all.filter(row => !free.has(Number(row["id"]))).map(row => String(row["family"])));
|
|
91
|
+
const ids = all.filter(row => !blocked.has(String(row["family"]))).map(row => Number(row["id"]));
|
|
92
|
+
if (ids.length > 0) {
|
|
93
|
+
const list = JSON.stringify(ids);
|
|
94
|
+
bytes = Number(db.prepare("SELECT SUM(length(subject) + length(body) + 64) AS n FROM notification WHERE id IN (SELECT value FROM json_each(?))").get(list)?.["n"] ?? 0);
|
|
95
|
+
for (const table of ["notification_delivery", "chat_message_ref", "push_delivery", "notification_actor"]) {
|
|
96
|
+
db.prepare(`DELETE FROM ${table} WHERE notification IN (SELECT value FROM json_each(?))`).run(list);
|
|
97
|
+
}
|
|
98
|
+
removed = Number(db.prepare("DELETE FROM notification WHERE id IN (SELECT value FROM json_each(?))").run(list).changes);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const legacyCleared = Number(db.prepare(`DELETE FROM notification_delivery WHERE rowid IN (
|
|
102
|
+
SELECT d.rowid FROM notification_delivery d JOIN notification n ON n.id = d.notification
|
|
103
|
+
WHERE d.destination = :legacy AND d.receipt = :settled AND n.created_at < :cutoff LIMIT :batch)`)
|
|
104
|
+
.run({ legacy: LEGACY_DESTINATION, settled: LEGACY_SETTLED_RECEIPT, cutoff, batch }).changes);
|
|
105
|
+
more ||= settled === batch || legacyCleared === batch;
|
|
106
|
+
return { settled, pings: removed, legacyCleared, bytes: removed > 0 ? bytes : 0, more, deferred };
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
export function lifecycleWords(sweep) {
|
|
110
|
+
const parts = [
|
|
111
|
+
sweep.pings > 0 ? `${sweep.pings} progress update${sweep.pings === 1 ? "" : "s"}` : "",
|
|
112
|
+
sweep.legacyCleared > 0 ? `${sweep.legacyCleared} undeliverable delivery record${sweep.legacyCleared === 1 ? "" : "s"}` : "",
|
|
113
|
+
].filter(Boolean);
|
|
114
|
+
const settled = sweep.settled > 0 ? `${sweep.settled} undeliverable delivery record${sweep.settled === 1 ? "" : "s"} settled` : "";
|
|
115
|
+
const held = sweep.deferred.families > 0
|
|
116
|
+
? `${sweep.deferred.families} progress update famil${sweep.deferred.families === 1 ? "y" : "ies"} (${sweep.deferred.pings} updates) held over: each is larger than one sweep's limit of ${NOTIFICATION_SWEEP_BATCH}`
|
|
117
|
+
: "";
|
|
118
|
+
return [parts.join(", "), settled, held].filter(Boolean).join("; ");
|
|
119
|
+
}
|
package/dist/observations.js
CHANGED
|
@@ -7,7 +7,6 @@ import { readVerifiedArtifact, scanForSecrets, storeEvidence } from "./evidence.
|
|
|
7
7
|
import { readObservationRequest } from "./contracts/observation-request.js";
|
|
8
8
|
export const OBSERVATION_MAILBOX = "STANDING-ORDERS-OBSERVATIONS.json";
|
|
9
9
|
export const OBSERVATION_CAPTURE = "machine focused observations v1";
|
|
10
|
-
export const OBSERVATION_FILE = "REVIEW-OBSERVATIONS.json";
|
|
11
10
|
const sha = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
12
11
|
const revision = (value) => typeof value === "string" && /^[0-9a-f]{40,64}$/.test(value);
|
|
13
12
|
const pathOkay = (value) => typeof value === "string" && value.length > 0 && value.length <= 500 &&
|