@nathanstephenson/flow 0.1.0 → 0.1.1
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 +6 -1
- package/dist/backend/fake/index.js +3 -1
- package/dist/backend/pi/effort-capabilities.js +12 -0
- package/dist/backend/pi/index.js +25 -36
- package/dist/backend/pi/workflow-subagent.js +8 -3
- package/dist/build-id +1 -1
- package/dist/cli/application.js +9 -1
- package/dist/cli/bootstrap.js +258 -22
- package/dist/cli/install-guard.js +52 -9
- package/dist/cli/update.js +30 -13
- package/dist/cli/web-update.js +357 -0
- package/dist/client/model-choices.js +22 -4
- package/dist/client/reduce.js +18 -6
- package/dist/client/status.js +34 -20
- package/dist/client/workflow-effort.js +30 -0
- package/dist/daemon/credential-redaction.js +97 -4
- package/dist/daemon/host.js +341 -74
- package/dist/daemon/release-checker.js +104 -0
- package/dist/daemon/runtime.js +12 -4
- package/dist/daemon/server.js +81 -14
- package/dist/daemon/summariser.js +81 -15
- package/dist/daemon/update-api.js +3 -0
- package/dist/daemon/workflow-executions.js +146 -15
- package/dist/protocol/update.js +1 -0
- package/dist/tui/app.js +104 -10
- package/dist/tui/keys.js +46 -10
- package/dist/tui/render.js +58 -13
- package/dist/workflows/records.js +3 -1
- package/dist/workflows/scheduler.js +15 -1
- package/package.json +1 -1
- package/web/dist/apple-touch-icon.png +0 -0
- package/web/dist/assets/index-CaJfF2tQ.js +170 -0
- package/web/dist/assets/index-DWF4-3Fh.css +2 -0
- package/web/dist/assets/{settings-mcp-CcT0ADJR.js → settings-mcp-COi1p_1o.js} +1 -1
- package/web/dist/assets/{settings-secrets-CK4uF7fd.js → settings-secrets-C599riFl.js} +1 -1
- package/web/dist/assets/settings-workflows-BPjeFoaT.js +23 -0
- package/web/dist/assets/{workflows-D4T4Gy_7.css → workflows-9aO6cptD.css} +1 -1
- package/web/dist/assets/workflows-Bmsrl5kE.js +7 -0
- package/web/dist/assets/workflows-pane-DJ3UwHjd.js +1 -0
- package/web/dist/favicon-16x16.png +0 -0
- package/web/dist/favicon-32x32.png +0 -0
- package/web/dist/favicon.ico +0 -0
- package/web/dist/favicon.svg +8 -0
- package/web/dist/index.html +8 -3
- package/web/dist/safari-pinned-tab.svg +4 -0
- package/web/dist/assets/index-BC0gLS53.js +0 -106
- package/web/dist/assets/index-CCeZYKES.css +0 -2
- package/web/dist/assets/settings-workflows-CrbZF-nM.js +0 -23
- package/web/dist/assets/workflow-editors-naPqAmYO.js +0 -65
- package/web/dist/assets/workflows-hh6ioeEA.js +0 -7
- package/web/dist/assets/workflows-pane-Db-MHf7Z.js +0 -1
package/dist/cli/update.js
CHANGED
|
@@ -5,7 +5,8 @@ import { packageName, privatePath } from "./install-guard.js";
|
|
|
5
5
|
import { readHost } from "../daemon/ownership.js";
|
|
6
6
|
import { backgroundHostArgs, getHostStatus, HostRefusal, launchBackground, processAlive, requestHostStop, requireRestartable, waitUntil } from "./host-control.js";
|
|
7
7
|
import { InstallationProcessUncertain, replacePackage } from "./install-process.js";
|
|
8
|
-
|
|
8
|
+
import { newerStableVersion } from "../daemon/release-checker.js";
|
|
9
|
+
export function npmExecutable() {
|
|
9
10
|
for (const directory of (process.env.PATH ?? '').split(delimiter)) {
|
|
10
11
|
const path = resolve(directory, 'npm');
|
|
11
12
|
try {
|
|
@@ -19,7 +20,7 @@ function npmExecutable() {
|
|
|
19
20
|
function npmPath(npm, command) {
|
|
20
21
|
return realpathSync(execFileSync(npm, [command, '--global'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 15000 }).trim());
|
|
21
22
|
}
|
|
22
|
-
function packageVersion(slot) {
|
|
23
|
+
export function packageVersion(slot) {
|
|
23
24
|
privatePath(slot);
|
|
24
25
|
const metadata = JSON.parse(readFileSync(join(slot, 'package.json'), 'utf8'));
|
|
25
26
|
if (metadata?.name !== packageName || typeof metadata.version !== 'string' || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(metadata.version))
|
|
@@ -39,27 +40,42 @@ async function verify(install, transaction, expected) {
|
|
|
39
40
|
await transaction.assertAuthorizationConsumed();
|
|
40
41
|
return version;
|
|
41
42
|
}
|
|
42
|
-
async function restore(install, transaction, root, host, version) {
|
|
43
|
-
const
|
|
43
|
+
async function restore(install, transaction, root, host, version, preserveConcretePort) {
|
|
44
|
+
const settings = preserveConcretePort && host.settings.port === 0
|
|
45
|
+
? { ...host.settings, port: Number(new URL(host.url).port) }
|
|
46
|
+
: host.settings;
|
|
47
|
+
const env = await transaction.authorizeRestoration(backgroundHostArgs(settings));
|
|
44
48
|
await launchBackground({
|
|
45
|
-
root, settings
|
|
49
|
+
root, settings, entry: [join(install.slot, 'dist/cli/bootstrap.js')], env,
|
|
46
50
|
validate(status) {
|
|
47
51
|
if (status.instanceId === host.instanceId || status.version !== version || status.mode !== 'background' ||
|
|
48
|
-
['port', 'address', 'cwd', 'oidc'].some(key => status.settings[key] !==
|
|
52
|
+
['port', 'address', 'cwd', 'oidc'].some(key => status.settings[key] !== settings[key])) {
|
|
49
53
|
throw new Error('Restored Session Host settings or version differ');
|
|
50
54
|
}
|
|
51
55
|
},
|
|
52
56
|
});
|
|
53
57
|
await transaction.assertAuthorizationConsumed();
|
|
54
58
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
throw new Error('usage: flow update [--force]');
|
|
59
|
+
/** Reused by the web surface for an explanation; `update` repeats it immediately before mutation. */
|
|
60
|
+
export function inspectUpdateInstallation(install) {
|
|
58
61
|
const npm = npmExecutable();
|
|
59
62
|
const prefix = npmPath(npm, 'prefix'), root = npmPath(npm, 'root');
|
|
60
63
|
if (prefix !== install.prefix || root !== join(prefix, 'lib/node_modules') || realpathSync(join(root, packageName)) !== install.slot)
|
|
61
64
|
throw new Error('npm selects a different installation. Put the matching npm in PATH and select its prefix.');
|
|
62
|
-
|
|
65
|
+
return { npm, version: packageVersion(install.slot) };
|
|
66
|
+
}
|
|
67
|
+
export async function update(install, lease, args, options = {}) {
|
|
68
|
+
if (args.length > 2 || args[0] !== 'update' || (args.length === 2 && args[1] !== '--force'))
|
|
69
|
+
throw new Error('usage: flow update [--force]');
|
|
70
|
+
const inspected = inspectUpdateInstallation(install);
|
|
71
|
+
const npm = inspected.npm;
|
|
72
|
+
const prefix = install.prefix;
|
|
73
|
+
const previous = inspected.version;
|
|
74
|
+
if (options.expectedVersion !== undefined && !newerStableVersion(previous, options.expectedVersion)) {
|
|
75
|
+
throw new Error(`The confirmed web update target ${options.expectedVersion} is not a newer stable release`);
|
|
76
|
+
}
|
|
77
|
+
const target = options.expectedVersion ?? 'latest';
|
|
78
|
+
const preserveConcretePort = options.preserveConcreteHostPort === true;
|
|
63
79
|
let host = readHost(lease.root);
|
|
64
80
|
if (host && processAlive(host.pid)) {
|
|
65
81
|
const status = await getHostStatus(host);
|
|
@@ -77,7 +93,7 @@ export async function update(install, lease, args) {
|
|
|
77
93
|
await replacePackage(npm, prefix, `${packageName}@${version}`);
|
|
78
94
|
const installed = await verify(install, transaction, expected);
|
|
79
95
|
if (host && needsRestoration) {
|
|
80
|
-
await restore(install, transaction, lease.root, host, installed);
|
|
96
|
+
await restore(install, transaction, lease.root, host, installed, preserveConcretePort);
|
|
81
97
|
needsRestoration = false;
|
|
82
98
|
}
|
|
83
99
|
await transaction.complete();
|
|
@@ -97,8 +113,9 @@ export async function update(install, lease, args) {
|
|
|
97
113
|
const pid = host.pid;
|
|
98
114
|
await waitUntil(async () => !processAlive(pid), 'Session Host process did not exit; installation was not changed');
|
|
99
115
|
}
|
|
100
|
-
const version = await replace(
|
|
116
|
+
const version = await replace(target, options.expectedVersion);
|
|
101
117
|
console.log(version === previous ? `Flow is already at ${version}.` : `Flow updated: ${previous} → ${version}`);
|
|
118
|
+
return { previousVersion: previous, installedVersion: version, changed: version !== previous };
|
|
102
119
|
}
|
|
103
120
|
catch (error) {
|
|
104
121
|
if (error instanceof InstallationProcessUncertain)
|
|
@@ -108,7 +125,7 @@ export async function update(install, lease, args) {
|
|
|
108
125
|
throw new Error(`${String(error)}. No package files were changed; wait for the stopping host to exit. ${repair()}`);
|
|
109
126
|
if (needsRestoration && host) {
|
|
110
127
|
try {
|
|
111
|
-
await restore(install, transaction, lease.root, host, previous);
|
|
128
|
+
await restore(install, transaction, lease.root, host, previous, preserveConcretePort);
|
|
112
129
|
}
|
|
113
130
|
catch (failure) {
|
|
114
131
|
throw new Error(`${String(error)}; restoration failed: ${String(failure)}. ${repair()}`);
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { closeSync, constants, fchmodSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmdirSync, unlinkSync, writeFileSync, } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
|
+
import { newerStableVersion, ReleaseChecker } from "../daemon/release-checker.js";
|
|
6
|
+
import { UpdateRefusal } from "../daemon/update-api.js";
|
|
7
|
+
export { UpdateRefusal } from "../daemon/update-api.js";
|
|
8
|
+
import { processAlive } from "./host-control.js";
|
|
9
|
+
import { inspectUpdateInstallation } from "./update.js";
|
|
10
|
+
const STATUS_FILE = 'web-update.json';
|
|
11
|
+
const STATUS_LOCK = 'web-update-lock';
|
|
12
|
+
const STATUS_LOCK_ATTEMPTS = 100;
|
|
13
|
+
const STATUS_LOCK_WAIT_MS = 25;
|
|
14
|
+
export const WEB_UPDATE_LAUNCH_WINDOW_MS = 15_000;
|
|
15
|
+
function statusPath(root) { return join(root, STATUS_FILE); }
|
|
16
|
+
function readOperation(root) {
|
|
17
|
+
try {
|
|
18
|
+
const value = JSON.parse(readFileSync(statusPath(root), 'utf8'));
|
|
19
|
+
if (!value || typeof value.id !== 'string' || !['updating', 'succeeded', 'failed', 'unverified'].includes(value.state))
|
|
20
|
+
return undefined;
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function writeOperation(root, operation) {
|
|
28
|
+
const temporary = join(root, `${STATUS_FILE}-${process.pid}-${randomUUID()}`);
|
|
29
|
+
writeFileSync(temporary, JSON.stringify(operation), { mode: 0o600 });
|
|
30
|
+
renameSync(temporary, statusPath(root));
|
|
31
|
+
}
|
|
32
|
+
function removeLockOwner(path, filename) {
|
|
33
|
+
try {
|
|
34
|
+
unlinkSync(join(path, filename));
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error.code !== 'ENOENT')
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
rmdirSync(path);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (!['ENOENT', 'ENOTEMPTY', 'EEXIST'].includes(error.code ?? ''))
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function sleep(milliseconds) {
|
|
49
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds);
|
|
50
|
+
}
|
|
51
|
+
/** Serialize cross-process status transitions so reconciliation cannot replace a helper's result. */
|
|
52
|
+
function transitionOperation(root, transition) {
|
|
53
|
+
const ownerId = randomUUID();
|
|
54
|
+
const filename = `${ownerId}.json`;
|
|
55
|
+
const candidate = join(root, `${STATUS_LOCK}-${ownerId}`);
|
|
56
|
+
const lock = join(root, STATUS_LOCK);
|
|
57
|
+
mkdirSync(candidate, { mode: 0o700 });
|
|
58
|
+
writeFileSync(join(candidate, filename), JSON.stringify({ pid: process.pid, id: ownerId }), { mode: 0o600 });
|
|
59
|
+
let acquired = false;
|
|
60
|
+
try {
|
|
61
|
+
for (let attempt = 0; !acquired; attempt++) {
|
|
62
|
+
try {
|
|
63
|
+
renameSync(candidate, lock);
|
|
64
|
+
acquired = true;
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (!['ENOTEMPTY', 'EEXIST'].includes(error.code ?? ''))
|
|
68
|
+
throw error;
|
|
69
|
+
if (attempt >= STATUS_LOCK_ATTEMPTS)
|
|
70
|
+
throw new Error('Web update status is busy');
|
|
71
|
+
let entries;
|
|
72
|
+
try {
|
|
73
|
+
entries = readdirSync(lock);
|
|
74
|
+
}
|
|
75
|
+
catch (readError) {
|
|
76
|
+
if (readError.code === 'ENOENT')
|
|
77
|
+
continue;
|
|
78
|
+
throw readError;
|
|
79
|
+
}
|
|
80
|
+
if (entries.length === 0) {
|
|
81
|
+
sleep(STATUS_LOCK_WAIT_MS);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (entries.length !== 1 || !/^[a-f0-9-]+\.json$/.test(entries[0]))
|
|
85
|
+
throw new Error('Invalid web update status owner');
|
|
86
|
+
let owner;
|
|
87
|
+
try {
|
|
88
|
+
owner = JSON.parse(readFileSync(join(lock, entries[0]), 'utf8'));
|
|
89
|
+
}
|
|
90
|
+
catch (readError) {
|
|
91
|
+
if (readError.code === 'ENOENT')
|
|
92
|
+
continue;
|
|
93
|
+
throw readError;
|
|
94
|
+
}
|
|
95
|
+
if (!owner || typeof owner !== 'object' || !Number.isInteger(owner.pid) || owner.pid <= 0 || owner.id !== entries[0].slice(0, -5)) {
|
|
96
|
+
throw new Error('Invalid web update status owner');
|
|
97
|
+
}
|
|
98
|
+
if (!processAlive(owner.pid))
|
|
99
|
+
removeLockOwner(lock, entries[0]);
|
|
100
|
+
sleep(STATUS_LOCK_WAIT_MS);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const current = readOperation(root);
|
|
104
|
+
const next = transition(current);
|
|
105
|
+
if (next !== undefined && next !== current)
|
|
106
|
+
writeOperation(root, next);
|
|
107
|
+
return next;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
removeLockOwner(acquired ? lock : candidate, filename);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function publicError(error) {
|
|
114
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
115
|
+
return message
|
|
116
|
+
.replace(/Bearer\s+\S+/gi, 'Bearer [redacted]')
|
|
117
|
+
.replace(/((?:token|secret|password)[=:]\s*)[^\s;,]+/gi, '$1[redacted]')
|
|
118
|
+
.slice(0, 2000);
|
|
119
|
+
}
|
|
120
|
+
/** Called by the detached `flow update` helper after restoration/recovery has settled. */
|
|
121
|
+
export function finishWebUpdate(root, id, result, error) {
|
|
122
|
+
transitionOperation(root, current => {
|
|
123
|
+
// A replacement host may have reconciled the launch to unverified when the old host exited before
|
|
124
|
+
// recording the helper PID. The ID still binds this completion to the authorized detached helper,
|
|
125
|
+
// so its eventual verified result may safely replace that provisional recovery-needed state.
|
|
126
|
+
if (!current || current.id !== id || !['updating', 'unverified'].includes(current.state))
|
|
127
|
+
return current;
|
|
128
|
+
const finishedAt = new Date().toISOString();
|
|
129
|
+
if (error !== undefined)
|
|
130
|
+
return { ...current, state: 'failed', finishedAt, message: publicError(error) };
|
|
131
|
+
if (!result?.changed) {
|
|
132
|
+
return {
|
|
133
|
+
...current,
|
|
134
|
+
state: 'failed',
|
|
135
|
+
installedVersion: result?.installedVersion ?? current.previousVersion,
|
|
136
|
+
finishedAt,
|
|
137
|
+
message: `npm completed, but Flow remains at ${result?.installedVersion ?? current.previousVersion}. No update was installed.`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
...current,
|
|
142
|
+
state: 'succeeded',
|
|
143
|
+
installedVersion: result.installedVersion,
|
|
144
|
+
finishedAt,
|
|
145
|
+
message: `Flow updated from ${result.previousVersion} to ${result.installedVersion}.`,
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
export class WebUpdateController {
|
|
150
|
+
launching = false;
|
|
151
|
+
options;
|
|
152
|
+
checker;
|
|
153
|
+
constructor(options) {
|
|
154
|
+
this.options = options;
|
|
155
|
+
const registryUrl = process.env.FLOW_UPDATE_REGISTRY_URL;
|
|
156
|
+
this.checker = options.checker ?? new ReleaseChecker(registryUrl ? { registryUrl } : {});
|
|
157
|
+
}
|
|
158
|
+
async status(refresh = false) {
|
|
159
|
+
const installedVersion = this.installedVersion();
|
|
160
|
+
const check = await this.checker.check(refresh);
|
|
161
|
+
const operation = this.operation(installedVersion);
|
|
162
|
+
return {
|
|
163
|
+
installedVersion,
|
|
164
|
+
updateAvailable: 'latestVersion' in check && newerStableVersion(installedVersion, check.latestVersion),
|
|
165
|
+
...('latestVersion' in check ? { latestVersion: check.latestVersion } : { checkError: check.error }),
|
|
166
|
+
checkedAt: check.checkedAt,
|
|
167
|
+
eligibility: this.eligibility(),
|
|
168
|
+
...(operation === undefined ? {} : { operation }),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
async start(confirmedVersion) {
|
|
172
|
+
if (this.launching)
|
|
173
|
+
throw new UpdateRefusal('An update request is already starting.');
|
|
174
|
+
this.launching = true;
|
|
175
|
+
let launchedOperationId;
|
|
176
|
+
let helperStarted = false;
|
|
177
|
+
try {
|
|
178
|
+
const initiallyInstalledVersion = this.installedVersion();
|
|
179
|
+
this.refuseUnsettledOperation(this.operation(initiallyInstalledVersion), initiallyInstalledVersion);
|
|
180
|
+
const eligibility = this.eligibility();
|
|
181
|
+
if (eligibility.state !== 'eligible')
|
|
182
|
+
throw new UpdateRefusal(eligibility.reason);
|
|
183
|
+
const installedVersion = this.installedVersion();
|
|
184
|
+
// Mutation never trusts the up-to-fifteen-minute discovery cache. The confirmed version must
|
|
185
|
+
// still be npm's stable latest tag, and the detached helper installs that exact version.
|
|
186
|
+
const release = await this.checker.check(true);
|
|
187
|
+
if ('error' in release)
|
|
188
|
+
throw new UpdateRefusal(`${release.error} Check again before updating.`);
|
|
189
|
+
if (!newerStableVersion(installedVersion, release.latestVersion)) {
|
|
190
|
+
throw new UpdateRefusal(`Flow ${installedVersion} is already up to date.`);
|
|
191
|
+
}
|
|
192
|
+
if (release.latestVersion !== confirmedVersion) {
|
|
193
|
+
throw new UpdateRefusal(`The latest release changed from ${confirmedVersion} to ${release.latestVersion}. Check again and confirm the new version.`);
|
|
194
|
+
}
|
|
195
|
+
// Revalidate after the asynchronous release check and immediately before spawning. The helper
|
|
196
|
+
// repeats installation and target checks yet again before beginning its transaction.
|
|
197
|
+
const finalEligibility = this.eligibility();
|
|
198
|
+
if (finalEligibility.state !== 'eligible')
|
|
199
|
+
throw new UpdateRefusal(finalEligibility.reason);
|
|
200
|
+
const installation = this.options.installation;
|
|
201
|
+
const bootstrapEntry = this.options.bootstrapEntry;
|
|
202
|
+
if (!installation || !bootstrapEntry)
|
|
203
|
+
throw new UpdateRefusal('This Flow installation cannot update itself.');
|
|
204
|
+
const id = randomUUID();
|
|
205
|
+
const operation = {
|
|
206
|
+
id,
|
|
207
|
+
state: 'updating',
|
|
208
|
+
previousVersion: installedVersion,
|
|
209
|
+
targetVersion: confirmedVersion,
|
|
210
|
+
startedAt: new Date(this.now()).toISOString(),
|
|
211
|
+
message: 'Starting the guarded npm update.',
|
|
212
|
+
launcherPid: process.pid,
|
|
213
|
+
};
|
|
214
|
+
transitionOperation(this.options.root, current => {
|
|
215
|
+
// Discovery is asynchronous. Recheck under the cross-process status lock so an old helper
|
|
216
|
+
// cannot become provisional while this request replaces its operation ID.
|
|
217
|
+
this.refuseUnsettledOperation(current, installedVersion);
|
|
218
|
+
return operation;
|
|
219
|
+
});
|
|
220
|
+
launchedOperationId = id;
|
|
221
|
+
const log = openSync(join(this.options.root, 'web-update.log'), constants.O_WRONLY | constants.O_CREAT | constants.O_APPEND | constants.O_NOFOLLOW, 0o600);
|
|
222
|
+
let child;
|
|
223
|
+
try {
|
|
224
|
+
fchmodSync(log, 0o600);
|
|
225
|
+
child = spawn(process.execPath, [bootstrapEntry, 'update'], {
|
|
226
|
+
cwd: process.cwd(),
|
|
227
|
+
detached: true,
|
|
228
|
+
stdio: ['ignore', log, log],
|
|
229
|
+
env: {
|
|
230
|
+
...process.env,
|
|
231
|
+
FLOW_STATE_DIR: this.options.root,
|
|
232
|
+
FLOW_WEB_UPDATE_ID: id,
|
|
233
|
+
FLOW_WEB_UPDATE_VERSION: confirmedVersion,
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
closeSync(log);
|
|
239
|
+
}
|
|
240
|
+
await new Promise((resolvePromise, reject) => {
|
|
241
|
+
child.once('spawn', resolvePromise);
|
|
242
|
+
child.once('error', reject);
|
|
243
|
+
});
|
|
244
|
+
child.unref();
|
|
245
|
+
const helperPid = child.pid;
|
|
246
|
+
if (!helperPid)
|
|
247
|
+
throw new Error('The update helper did not start');
|
|
248
|
+
helperStarted = true;
|
|
249
|
+
// A fast refusal can finish before the parent observes `spawn`; the locked transition never
|
|
250
|
+
// overwrites that durable result merely to add the helper pid.
|
|
251
|
+
transitionOperation(this.options.root, current => current?.id === id && current.state === 'updating' ? { ...current, pid: helperPid } : current);
|
|
252
|
+
return await this.status(false);
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
if (launchedOperationId !== undefined && !helperStarted) {
|
|
256
|
+
const operationId = launchedOperationId;
|
|
257
|
+
transitionOperation(this.options.root, operation => {
|
|
258
|
+
if (!operation || operation.id !== operationId || !['updating', 'unverified'].includes(operation.state) || operation.pid !== undefined)
|
|
259
|
+
return operation;
|
|
260
|
+
return { ...operation, state: 'failed', finishedAt: new Date(this.now()).toISOString(), message: publicError(error) };
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
finally {
|
|
266
|
+
this.launching = false;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
installedVersion() {
|
|
270
|
+
try {
|
|
271
|
+
return this.options.installation ? inspectUpdateInstallation(this.options.installation).version : this.options.installedVersion;
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
return this.options.installedVersion;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
eligibility() {
|
|
278
|
+
if (this.options.mode !== 'background') {
|
|
279
|
+
return { state: 'unsupported', reason: 'Web updates require a background Session Host. Start Flow with `flow serve start`.' };
|
|
280
|
+
}
|
|
281
|
+
if (!this.options.installation || !this.options.bootstrapEntry) {
|
|
282
|
+
return { state: 'unsupported', reason: this.options.unsupportedReason ?? 'This installation cannot update itself. Use npm to update Flow manually.' };
|
|
283
|
+
}
|
|
284
|
+
try {
|
|
285
|
+
inspectUpdateInstallation(this.options.installation);
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
return { state: 'unsupported', reason: publicError(error) };
|
|
289
|
+
}
|
|
290
|
+
if (this.options.hasActiveWork()) {
|
|
291
|
+
return {
|
|
292
|
+
state: 'blocked',
|
|
293
|
+
reason: 'Finish active Agent Sessions, Workflow Executions, and Shells before updating. Then try again.',
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
return { state: 'eligible' };
|
|
297
|
+
}
|
|
298
|
+
operation(installedVersion) {
|
|
299
|
+
const observed = readOperation(this.options.root);
|
|
300
|
+
if (!observed)
|
|
301
|
+
return undefined;
|
|
302
|
+
let recoveryMessage;
|
|
303
|
+
if (observed.state === 'updating' && observed.pid !== undefined && !processAlive(observed.pid)) {
|
|
304
|
+
recoveryMessage = 'The update helper stopped before it reported a verified result. Reconnect to the Session Host; if it remains unavailable, repair the installation manually with npm.';
|
|
305
|
+
}
|
|
306
|
+
else if (observed.state === 'updating' && observed.pid === undefined && this.launchWasAbandoned(observed)) {
|
|
307
|
+
recoveryMessage = 'The Session Host stopped or timed out before it recorded the update helper process. Flow cannot verify whether the update started. Reconnect; if the host remains unavailable, repair the private global npm installation manually and restart it.';
|
|
308
|
+
}
|
|
309
|
+
const stored = recoveryMessage === undefined ? observed : transitionOperation(this.options.root, current => {
|
|
310
|
+
// `processAlive` and the abandonment checks happen outside the lock. Compare the launch fields
|
|
311
|
+
// again under the lock: a helper result or parent PID write that won meanwhile is authoritative.
|
|
312
|
+
if (current?.id !== observed.id || current.state !== 'updating' || current.startedAt !== observed.startedAt ||
|
|
313
|
+
current.pid !== observed.pid || current.launcherPid !== observed.launcherPid)
|
|
314
|
+
return current;
|
|
315
|
+
return {
|
|
316
|
+
...current,
|
|
317
|
+
state: 'unverified',
|
|
318
|
+
finishedAt: new Date(this.now()).toISOString(),
|
|
319
|
+
message: recoveryMessage,
|
|
320
|
+
};
|
|
321
|
+
});
|
|
322
|
+
if (!stored)
|
|
323
|
+
return undefined;
|
|
324
|
+
if (stored.state === 'succeeded' && stored.installedVersion !== installedVersion) {
|
|
325
|
+
return this.publicOperation({
|
|
326
|
+
...stored,
|
|
327
|
+
state: 'unverified',
|
|
328
|
+
message: `The update reported ${stored.installedVersion}, but the running Session Host is ${installedVersion}. Restart or repair Flow manually.`,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return this.publicOperation(stored);
|
|
332
|
+
}
|
|
333
|
+
refuseUnsettledOperation(operation, installedVersion) {
|
|
334
|
+
if (operation?.state === 'updating')
|
|
335
|
+
throw new UpdateRefusal('An update is already in progress.');
|
|
336
|
+
if (operation?.state === 'unverified' ||
|
|
337
|
+
(operation?.state === 'succeeded' && operation.installedVersion !== installedVersion)) {
|
|
338
|
+
throw new UpdateRefusal('The previous update still has an unverified result. Reconnect and verify or repair the Session Host before starting another update.');
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
launchWasAbandoned(operation) {
|
|
342
|
+
const startedAt = Date.parse(operation.startedAt);
|
|
343
|
+
const now = this.now();
|
|
344
|
+
if (!Number.isFinite(startedAt) || startedAt > now || now - startedAt >= WEB_UPDATE_LAUNCH_WINDOW_MS)
|
|
345
|
+
return true;
|
|
346
|
+
if (operation.launcherPid === undefined)
|
|
347
|
+
return false;
|
|
348
|
+
if (!Number.isInteger(operation.launcherPid) || operation.launcherPid <= 0)
|
|
349
|
+
return true;
|
|
350
|
+
return !processAlive(operation.launcherPid);
|
|
351
|
+
}
|
|
352
|
+
publicOperation(stored) {
|
|
353
|
+
const { pid: _pid, launcherPid: _launcherPid, ...operation } = stored;
|
|
354
|
+
return operation;
|
|
355
|
+
}
|
|
356
|
+
now() { return (this.options.now ?? Date.now)(); }
|
|
357
|
+
}
|
|
@@ -23,10 +23,28 @@ export function modelChoicesOf(models) {
|
|
|
23
23
|
.flatMap(([provider, models]) => models.map((model) => ({ provider, model })));
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
26
|
+
* Classify effort support without collapsing an absent/stale capability list into confirmed
|
|
27
|
+
* no-control. Only a model found in a confirmed list can produce `none`.
|
|
28
28
|
*/
|
|
29
|
+
export function effortCapability(capabilities, model) {
|
|
30
|
+
if (!capabilities)
|
|
31
|
+
return { status: "unknown", levels: [], reason: "Effort support is still loading." };
|
|
32
|
+
if (!model)
|
|
33
|
+
return { status: "unknown", levels: [], reason: "Select a model to confirm Effort support." };
|
|
34
|
+
const current = capabilities.models.find((candidate) => candidate.id === model.id);
|
|
35
|
+
if (!current)
|
|
36
|
+
return { status: "unknown", levels: [], reason: `Effort support for ${model.label ?? model.id} is unavailable.` };
|
|
37
|
+
const levels = current.effortLevels ?? [];
|
|
38
|
+
return levels.length > 0 ? { status: "supported", levels: [...levels] } : { status: "none", levels: [] };
|
|
39
|
+
}
|
|
40
|
+
/** The confirmed choices shared by the web and terminal pickers. */
|
|
29
41
|
export function effortChoices(view) {
|
|
30
|
-
|
|
31
|
-
|
|
42
|
+
return effortCapability(view.capabilities, view.model).levels;
|
|
43
|
+
}
|
|
44
|
+
/** First-model workflow default; never manufactures an unverified `medium`. */
|
|
45
|
+
export function initialWorkflowEffort(model) {
|
|
46
|
+
const levels = model.effortLevels ?? [];
|
|
47
|
+
if (levels.length === 0)
|
|
48
|
+
return "off";
|
|
49
|
+
return levels.includes("medium") ? "medium" : levels[0];
|
|
32
50
|
}
|
package/dist/client/reduce.js
CHANGED
|
@@ -70,7 +70,10 @@ export function reduce(state, entry) {
|
|
|
70
70
|
status: deriveStatus({
|
|
71
71
|
lifecycle: next.lifecycle,
|
|
72
72
|
turnInFlight: next.turnInFlight,
|
|
73
|
-
|
|
73
|
+
// Independent producers can still need input after the parent turn ends, but their
|
|
74
|
+
// requests do not occupy that turn.
|
|
75
|
+
awaiting: (next.asking !== undefined && next.asking.producer === undefined) ||
|
|
76
|
+
(next.authorising !== undefined && next.authorising.producer === undefined),
|
|
74
77
|
}),
|
|
75
78
|
lastSeq: entry.seq,
|
|
76
79
|
};
|
|
@@ -204,7 +207,12 @@ function applyEvent(state, event, at) {
|
|
|
204
207
|
const asking = open && state.asking?.askId === event.askId
|
|
205
208
|
? state.asking
|
|
206
209
|
: open
|
|
207
|
-
? {
|
|
210
|
+
? {
|
|
211
|
+
askId: event.askId,
|
|
212
|
+
questions: event.questions,
|
|
213
|
+
...(event.context === undefined ? {} : { context: event.context }),
|
|
214
|
+
...(event.producer === undefined ? {} : { producer: event.producer }),
|
|
215
|
+
}
|
|
208
216
|
: state.asking?.askId === event.askId
|
|
209
217
|
? undefined
|
|
210
218
|
: state.asking;
|
|
@@ -241,6 +249,7 @@ function applyEvent(state, event, at) {
|
|
|
241
249
|
...(event.context === undefined ? {} : { context: event.context }),
|
|
242
250
|
...(event.allowAlways === undefined ? {} : { allowAlways: event.allowAlways }),
|
|
243
251
|
...(event.authorizationScope === undefined ? {} : { authorizationScope: event.authorizationScope }),
|
|
252
|
+
...(event.producer === undefined ? {} : { producer: event.producer }),
|
|
244
253
|
})
|
|
245
254
|
: state.authorising?.callId === event.callId
|
|
246
255
|
? nextAwaiting(state.entries, event.callId)
|
|
@@ -258,10 +267,12 @@ function applyEvent(state, event, at) {
|
|
|
258
267
|
};
|
|
259
268
|
}
|
|
260
269
|
case "turn_ended":
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
270
|
+
return {
|
|
271
|
+
...state,
|
|
272
|
+
turnInFlight: false,
|
|
273
|
+
asking: state.asking?.producer === undefined ? undefined : state.asking,
|
|
274
|
+
authorising: state.authorising?.producer === undefined ? undefined : state.authorising,
|
|
275
|
+
};
|
|
265
276
|
case "queue_changed":
|
|
266
277
|
return {
|
|
267
278
|
...state,
|
|
@@ -435,6 +446,7 @@ function nextAwaiting(entries, except) {
|
|
|
435
446
|
...(next.permissionContext === undefined ? {} : { context: next.permissionContext }),
|
|
436
447
|
...(next.allowAlways === undefined ? {} : { allowAlways: next.allowAlways }),
|
|
437
448
|
...(next.authorizationScope === undefined ? {} : { authorizationScope: next.authorizationScope }),
|
|
449
|
+
...(next.producer === undefined ? {} : { producer: next.producer }),
|
|
438
450
|
} : undefined;
|
|
439
451
|
}
|
|
440
452
|
/** Spread onto an Entry, so an unattributed event does not carry an explicit `producer: undefined`. */
|
package/dist/client/status.js
CHANGED
|
@@ -33,27 +33,41 @@ export function deriveStatus(of) {
|
|
|
33
33
|
export function occupied(status) {
|
|
34
34
|
return status === "running" || status === "awaiting";
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
36
|
+
export const RAIL_GROUPS = [
|
|
37
|
+
"needs-input",
|
|
38
|
+
"unread",
|
|
39
|
+
"working",
|
|
40
|
+
"idle",
|
|
41
|
+
"dormant",
|
|
42
|
+
"filed-away",
|
|
43
|
+
];
|
|
44
|
+
export function railGroup(of) {
|
|
45
|
+
// Terminal Lifecycle wins over stale independent-work/request indexes. Settle and End file a row
|
|
46
|
+
// away; nothing belonging to a stopped Backend Session can remain answerable above that boundary.
|
|
47
|
+
if (of.status === "settled" || of.status === "ended")
|
|
48
|
+
return "filed-away";
|
|
49
|
+
if (of.attention?.group === "needs-input")
|
|
50
|
+
return "needs-input";
|
|
51
|
+
if (of.attention?.group === "unread")
|
|
52
|
+
return "unread";
|
|
53
|
+
if (of.status === "running" || of.status === "awaiting" || working(of))
|
|
54
|
+
return "working";
|
|
52
55
|
if (of.status === "idle")
|
|
53
|
-
return
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
return "idle";
|
|
57
|
+
return "dormant";
|
|
58
|
+
}
|
|
59
|
+
export function railGroupLabel(group) {
|
|
60
|
+
switch (group) {
|
|
61
|
+
case "needs-input": return "Needs input";
|
|
62
|
+
case "unread": return "Unread";
|
|
63
|
+
case "working": return "Working";
|
|
64
|
+
case "idle": return "Idle";
|
|
65
|
+
case "dormant": return "Dormant";
|
|
66
|
+
case "filed-away": return "Filed away";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export function railGroupRank(of) {
|
|
70
|
+
return RAIL_GROUPS.indexOf(railGroup(of));
|
|
57
71
|
}
|
|
58
72
|
/**
|
|
59
73
|
* Working, but not occupancy. A backgrounded Subagent or an open Background Call leaves the status
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { initialWorkflowEffort } from "./model-choices.js";
|
|
2
|
+
/**
|
|
3
|
+
* Effort after choosing a workflow model. Existing configured values remain untouched when invalid;
|
|
4
|
+
* only the first selection gets a confirmed default, while no-control always uses the `off`
|
|
5
|
+
* sentinel.
|
|
6
|
+
*/
|
|
7
|
+
export function effortForWorkflowModelSelection(currentModelId, currentEffort, nextModel) {
|
|
8
|
+
if ((nextModel.effortLevels?.length ?? 0) === 0)
|
|
9
|
+
return "off";
|
|
10
|
+
return currentModelId === "" ? initialWorkflowEffort(nextModel) : currentEffort;
|
|
11
|
+
}
|
|
12
|
+
/** The one allowed automatic workflow correction: confirmed no-control always stores `off`. */
|
|
13
|
+
export function normalizeNoControlEffort(definition, catalogue) {
|
|
14
|
+
if (!definition || !catalogue)
|
|
15
|
+
return definition;
|
|
16
|
+
const listing = catalogue.find((entry) => entry.backend === definition.backend);
|
|
17
|
+
if (!listing || listing.problem)
|
|
18
|
+
return definition;
|
|
19
|
+
let changed = false;
|
|
20
|
+
const steps = definition.steps.map((step) => {
|
|
21
|
+
if (step.kind !== "agent")
|
|
22
|
+
return step;
|
|
23
|
+
const model = listing.models.find((candidate) => candidate.id === step.model);
|
|
24
|
+
if (!model || (model.effortLevels?.length ?? 0) > 0 || step.effort === "off")
|
|
25
|
+
return step;
|
|
26
|
+
changed = true;
|
|
27
|
+
return { ...step, effort: "off" };
|
|
28
|
+
});
|
|
29
|
+
return changed ? { ...definition, steps } : definition;
|
|
30
|
+
}
|