@buffcore/cli 0.8.47 → 0.8.48
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 +18 -0
- package/dist/commands/build/server-text.en.d.ts +5 -0
- package/dist/commands/build/server-text.en.js +5 -0
- package/dist/commands/build/server-text.js +5 -0
- package/dist/commands/build/server.d.ts +4 -0
- package/dist/commands/build/server.js +10 -1
- package/dist/commands/change-merge.d.ts +2 -2
- package/dist/commands/dev.d.ts +9 -2
- package/dist/commands/dev.js +24 -5
- package/dist/commands/hardening.d.ts +3 -0
- package/dist/commands/hardening.en.d.ts +3 -0
- package/dist/commands/hardening.en.js +3 -0
- package/dist/commands/hardening.es.d.ts +2 -0
- package/dist/commands/hardening.es.js +3 -0
- package/dist/commands/hardening.js +19 -0
- package/dist/commands/prototype/comp-record.d.ts +5 -0
- package/dist/commands/prototype/comp-record.js +22 -0
- package/dist/commands/server/build.d.ts +2 -1
- package/dist/commands/server/build.js +10 -2
- package/dist/commands/server/deploy-local.d.ts +3 -0
- package/dist/commands/server/deploy-local.js +9 -0
- package/dist/commands/server/deploy-plan.d.ts +5 -0
- package/dist/commands/server/deploy-plan.js +56 -0
- package/dist/commands/server/deploy-result.d.ts +7 -0
- package/dist/commands/server/deploy-result.js +1 -0
- package/dist/commands/server/deploy-script.d.ts +2 -0
- package/dist/commands/server/deploy-script.js +3 -2
- package/dist/commands/server/deploy.d.ts +9 -6
- package/dist/commands/server/deploy.js +39 -61
- package/dist/commands/server/files.d.ts +9 -1
- package/dist/commands/server/files.js +25 -5
- package/dist/commands/server/image.d.ts +4 -2
- package/dist/commands/server/image.js +6 -5
- package/dist/commands/server/setup.js +4 -0
- package/dist/commands/server/system-packages.d.ts +29 -0
- package/dist/commands/server/system-packages.js +103 -0
- package/dist/commands/services/compose.d.ts +1 -1
- package/dist/commands/services/compose.js +5 -3
- package/dist/commands/ui/agents-route.js +2 -0
- package/dist/commands/ui/approval-authority.en.d.ts +1 -0
- package/dist/commands/ui/approval-authority.en.js +1 -0
- package/dist/commands/ui/approval-authority.es.d.ts +1 -0
- package/dist/commands/ui/approval-authority.es.js +1 -0
- package/dist/commands/ui/approval-authority.js +7 -2
- package/dist/commands/ui/approve.js +2 -4
- package/dist/commands/ui/autopilot-records.d.ts +2 -0
- package/dist/commands/ui/autopilot-records.js +5 -0
- package/dist/commands/ui/brand-approval.js +5 -1
- package/dist/commands/ui/chat/permissions.js +1 -1
- package/dist/commands/ui/chat/routes.js +4 -0
- package/dist/commands/ui/comp-approval.d.ts +14 -2
- package/dist/commands/ui/comp-approval.js +45 -8
- package/dist/commands/ui/level-route.js +3 -0
- package/dist/commands/ui/merge-authority.d.ts +4 -1
- package/dist/commands/ui/merge-authority.en.d.ts +4 -0
- package/dist/commands/ui/merge-authority.en.js +5 -1
- package/dist/commands/ui/merge-authority.es.d.ts +4 -0
- package/dist/commands/ui/merge-authority.es.js +5 -1
- package/dist/commands/ui/merge-authority.js +6 -3
- package/dist/commands/ui/merge-claims.d.ts +7 -0
- package/dist/commands/ui/merge-claims.js +21 -0
- package/dist/commands/ui/merge-delegation.js +8 -3
- package/dist/commands/ui/merge-hold-name.d.ts +5 -0
- package/dist/commands/ui/merge-hold-name.js +5 -0
- package/dist/commands/ui/merge-hold.d.ts +6 -1
- package/dist/commands/ui/merge-hold.js +23 -5
- package/dist/commands/ui/merge-publish-host.d.ts +1 -0
- package/dist/commands/ui/merge-publish-host.js +2 -2
- package/dist/commands/ui/merge-record.d.ts +2 -0
- package/dist/commands/ui/merge-record.js +1 -1
- package/dist/commands/ui/merge-route.js +17 -1
- package/dist/commands/ui/merge-settle.d.ts +32 -0
- package/dist/commands/ui/merge-settle.js +133 -0
- package/dist/commands/ui/merge-state.d.ts +5 -0
- package/dist/commands/ui/merge-state.js +7 -1
- package/dist/commands/ui/merge-withdraw.d.ts +9 -0
- package/dist/commands/ui/merge-withdraw.js +29 -0
- package/dist/commands/ui/plan-files.js +5 -2
- package/dist/commands/ui/plan-structure.js +5 -7
- package/dist/commands/ui/prototype-comp-route.d.ts +12 -3
- package/dist/commands/ui/prototype-comp-route.js +35 -17
- package/dist/commands/ui/real-file.d.ts +11 -0
- package/dist/commands/ui/real-file.js +84 -0
- package/dist/commands/ui/save-route.js +4 -1
- package/dist/commands/ui/server.d.ts +1 -0
- package/dist/commands/ui/server.js +1 -0
- package/dist/commands/ui/studio-state.d.ts +1 -0
- package/dist/commands/ui/studio-versions.js +19 -3
- package/dist/ui/page.js +79 -79
- package/docs/PROTOTYPE.md +13 -0
- package/docs/WORKBENCH.md +30 -0
- package/package.json +11 -11
- package/skills/ape-deploy/SKILL.md +4 -2
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { statSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { effectiveLevel, mainAgentsRoot } from '../agents-file.js';
|
|
6
|
+
import { readRecord } from './autopilot-records.js';
|
|
7
|
+
import { fullSha, mergeHeldCode, mergeT, refusalCode, reservedCode } from './merge-authority.js';
|
|
8
|
+
import { changeHead } from './merge-delegation.js';
|
|
9
|
+
import { MergePublicationLockError, mergeHoldPath, mergePublication, publishAutomatic, } from './merge-hold.js';
|
|
10
|
+
import { mainHas } from './merge-observed.js';
|
|
11
|
+
import { closeRecord, published, settle, withdraw } from './merge-record.js';
|
|
12
|
+
const settlement = z.strictObject({
|
|
13
|
+
change: z.string().regex(/^C-\d{3,}$/),
|
|
14
|
+
head: fullSha,
|
|
15
|
+
claim: z.string().min(1).max(200),
|
|
16
|
+
outcome: z.enum(['merged', 'not_merged', 'check']),
|
|
17
|
+
code: refusalCode.optional(),
|
|
18
|
+
});
|
|
19
|
+
/** The claim matches the ask's hash, compared in constant time. */
|
|
20
|
+
const claimed = (pending, claim) => {
|
|
21
|
+
if (!pending.claimHash)
|
|
22
|
+
return false;
|
|
23
|
+
const given = createHash('sha256').update(claim).digest();
|
|
24
|
+
return timingSafeEqual(given, Buffer.from(pending.claimHash, 'hex'));
|
|
25
|
+
};
|
|
26
|
+
const modified = (file) => {
|
|
27
|
+
try {
|
|
28
|
+
return statSync(file).mtimeMs;
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (error.code === 'ENOENT')
|
|
32
|
+
return 0;
|
|
33
|
+
return Number.POSITIVE_INFINITY;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Any edit of `plan/agents.md` or of the hold since the ask ends the claim: the level may have left
|
|
38
|
+
* Autopilot and come back, or a person held merges meanwhile (an ask is never made while held).
|
|
39
|
+
*/
|
|
40
|
+
const ended = (root, path, askedAt) => {
|
|
41
|
+
const asked = Date.parse(askedAt);
|
|
42
|
+
let agents = Number.POSITIVE_INFINITY;
|
|
43
|
+
try {
|
|
44
|
+
agents = modified(join(mainAgentsRoot(root), 'plan/agents.md')) || agents;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// An unknown checkout ends the claim.
|
|
48
|
+
}
|
|
49
|
+
return agents > asked || modified(path) > asked;
|
|
50
|
+
};
|
|
51
|
+
const refused = (code) => ({
|
|
52
|
+
status: 'refused',
|
|
53
|
+
code,
|
|
54
|
+
detail: mergeT(code === mergeHeldCode ? 'ui.merge.settleHeld' : 'ui.merge.settleRefused', {
|
|
55
|
+
code,
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
/**
|
|
59
|
+
* Settles one waiting Autopilot claim from the host's own process; no HTTP route reaches it. It
|
|
60
|
+
* runs under the same publication gate as `publish`: a held repository, a level other than
|
|
61
|
+
* Autopilot, an edit of `plan/agents.md` or of the hold since the ask (which also withdraws the
|
|
62
|
+
* claim), another change's or head's claim, a withdrawn claim, a moved branch, or (for `merged`)
|
|
63
|
+
* a main without the exact head refuses; a record that cannot be written answers
|
|
64
|
+
* `merge_record_failed`, and a publication lock that stays busy `merge_publication_lock`. A claim
|
|
65
|
+
* that has ended is withdrawn by any settle, so Autopilot asks again. A `merged` settlement records what `publish` records; the studio's next
|
|
66
|
+
* read records the merge and starts its Snapshot, exactly as for a published claim. `check`
|
|
67
|
+
* answers `ready` when a `merged` settle would pass the gate now, and writes nothing.
|
|
68
|
+
*/
|
|
69
|
+
export async function settleHostMerge(root, input) {
|
|
70
|
+
const parsed = settlement.safeParse(input);
|
|
71
|
+
if (!parsed.success ||
|
|
72
|
+
(parsed.data.outcome === 'not_merged') !== Boolean(parsed.data.code) ||
|
|
73
|
+
(parsed.data.code !== undefined && reservedCode(parsed.data.code)))
|
|
74
|
+
return refused('merge_settle_invalid');
|
|
75
|
+
const { change, head, claim, outcome, code } = parsed.data;
|
|
76
|
+
const path = mergeHoldPath(root);
|
|
77
|
+
if (!path)
|
|
78
|
+
return refused('merge_settle_unknown');
|
|
79
|
+
const pending = readRecord(root)?.pending.find((entry) => entry.change === change);
|
|
80
|
+
if (!pending?.automatic || pending.head !== head || !claimed(pending, claim))
|
|
81
|
+
return refused('merge_settle_unknown');
|
|
82
|
+
if ((await changeHead(root, change).catch(() => undefined)) !== head)
|
|
83
|
+
return refused('merge_settle_unknown');
|
|
84
|
+
// Read again under the lock: the claim still on file and Autopilot unchanged since the ask.
|
|
85
|
+
const onFile = () => readRecord(root)?.pending.some((entry) => entry.revision === pending.revision && entry.head === head) === true;
|
|
86
|
+
const stale = () => onFile() && (effectiveLevel(root) !== 'auto' || ended(root, path, pending.askedAt));
|
|
87
|
+
const current = () => effectiveLevel(root) === 'auto' && !ended(root, path, pending.askedAt) && onFile();
|
|
88
|
+
// A claim Autopilot no longer stands behind ends here, whatever the host asked: the merge, if
|
|
89
|
+
// any, stays Git's alone, and Autopilot asks again after its next turn.
|
|
90
|
+
const endStale = () => mergePublication(path, async () => {
|
|
91
|
+
if (stale())
|
|
92
|
+
await closeRecord(root, withdraw(pending, undefined));
|
|
93
|
+
}).catch(() => undefined);
|
|
94
|
+
try {
|
|
95
|
+
if (outcome !== 'not_merged') {
|
|
96
|
+
const result = await publishAutomatic(path, head, current, async () => {
|
|
97
|
+
if (outcome === 'check')
|
|
98
|
+
return { status: 'ready' };
|
|
99
|
+
if (!(await mainHas(root, head)))
|
|
100
|
+
return refused('merge_not_observed');
|
|
101
|
+
const recorded = await closeRecord(root, published(pending)).catch(() => false);
|
|
102
|
+
return recorded ? { status: 'merged' } : refused('merge_record_failed');
|
|
103
|
+
});
|
|
104
|
+
if ('status' in result)
|
|
105
|
+
return result;
|
|
106
|
+
await endStale();
|
|
107
|
+
return refused('code' in result && result.code ? result.code : 'merge_settle_unknown');
|
|
108
|
+
}
|
|
109
|
+
const result = await mergePublication(path, async () => {
|
|
110
|
+
if (!current() || (await mainHas(root, head)))
|
|
111
|
+
return undefined;
|
|
112
|
+
const at = new Date().toISOString();
|
|
113
|
+
const done = await closeRecord(root, settle(pending, {
|
|
114
|
+
change,
|
|
115
|
+
head,
|
|
116
|
+
at,
|
|
117
|
+
kind: 'refused',
|
|
118
|
+
code: code ?? 'merge_settle_invalid',
|
|
119
|
+
})).catch(() => false);
|
|
120
|
+
return done ? { status: 'not_merged' } : refused('merge_record_failed');
|
|
121
|
+
});
|
|
122
|
+
if (result)
|
|
123
|
+
return result;
|
|
124
|
+
await endStale();
|
|
125
|
+
return refused('merge_settle_unknown');
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
// The publication lock stayed busy: the host asks again later.
|
|
129
|
+
if (error instanceof MergePublicationLockError)
|
|
130
|
+
return refused('merge_publication_lock');
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -2,6 +2,11 @@ import type { StudioState, WorkFacts } from '@buffcore/contracts/workbench';
|
|
|
2
2
|
import { type StudioRecord } from './autopilot-records.ts';
|
|
3
3
|
import type { UiRequest } from './routes.ts';
|
|
4
4
|
type Merge = NonNullable<WorkFacts['merges']>[number];
|
|
5
|
+
/** How long a wait has lasted, in whole minutes under two hours and whole hours after. */
|
|
6
|
+
export declare const waited: (at: string, now?: number) => {
|
|
7
|
+
minutes: number;
|
|
8
|
+
hours: number;
|
|
9
|
+
};
|
|
5
10
|
/**
|
|
6
11
|
* What the host's merge of each change amounts to, from facts on disk only: nothing here is
|
|
7
12
|
* stored, so a read that is slow or out of order cannot leave a stale copy behind. With the ask's
|
|
@@ -9,6 +9,11 @@ import { checkAfterMerge } from './post-merge.js';
|
|
|
9
9
|
const language = () => /^es(?:[-_.]|$)/i.test(process.env.LC_ALL || process.env.LANG || '') ? 'es' : 'en';
|
|
10
10
|
// The server's own language and zone, and the long time style names the zone.
|
|
11
11
|
const shown = (at) => new Intl.DateTimeFormat(language(), { dateStyle: 'medium', timeStyle: 'long' }).format(new Date(at));
|
|
12
|
+
/** How long a wait has lasted, in whole minutes under two hours and whole hours after. */
|
|
13
|
+
export const waited = (at, now = Date.now()) => {
|
|
14
|
+
const minutes = Math.max(0, Math.floor((now - Date.parse(at)) / 60_000));
|
|
15
|
+
return { minutes, hours: minutes < 120 ? 0 : Math.floor(minutes / 60) };
|
|
16
|
+
};
|
|
12
17
|
/**
|
|
13
18
|
* Git's own "no" (a numeric exit code) is an answer; a Git that did not answer (a timeout, a
|
|
14
19
|
* failed spawn) throws, so the read is unknown rather than a quiet change.
|
|
@@ -44,10 +49,11 @@ export async function mergeEntries(root, record, timeout, run = studioRun) {
|
|
|
44
49
|
...(!merged && timeout !== undefined && asksAgain(pending, timeout)
|
|
45
50
|
? { again: true }
|
|
46
51
|
: {}),
|
|
52
|
+
...(!merged && pending.automatic ? { automatic: true } : {}),
|
|
47
53
|
at: pending.askedAt,
|
|
48
54
|
detail: merged
|
|
49
55
|
? mergeT('ui.merge.observed', { head: pending.head.slice(0, 12) })
|
|
50
|
-
: mergeT('ui.merge.since', { at: shown(pending.askedAt) }),
|
|
56
|
+
: mergeT('ui.merge.since', { at: shown(pending.askedAt), ...waited(pending.askedAt) }),
|
|
51
57
|
});
|
|
52
58
|
}
|
|
53
59
|
for (const done of record.outcomes) {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** A person held, stopped or left Autopilot: its waiting claims end, under the publication lock. */
|
|
2
|
+
export declare function withdrawAutopilotClaims(root: string, changes?: readonly string[]): Promise<number>;
|
|
3
|
+
/** After a level write: a level other than Autopilot withdraws Autopilot's waiting claims. */
|
|
4
|
+
export declare function afterLevel(root: string): Promise<void>;
|
|
5
|
+
/**
|
|
6
|
+
* A person ends a wait the host never answered: only an Autopilot claim (a person's own wait is
|
|
7
|
+
* unchanged), and never a head main already holds. The change can then be merged again.
|
|
8
|
+
*/
|
|
9
|
+
export declare function withdrawMerge(root: string, change: string): Promise<boolean>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { effectiveLevel } from '../agents-file.js';
|
|
2
|
+
import { readRecord } from './autopilot-records.js';
|
|
3
|
+
import { withdrawWaiting } from './merge-claims.js';
|
|
4
|
+
import { mergeHoldPath, mergePublication } from './merge-hold.js';
|
|
5
|
+
/** A person held, stopped or left Autopilot: its waiting claims end, under the publication lock. */
|
|
6
|
+
export async function withdrawAutopilotClaims(root, changes) {
|
|
7
|
+
// The record first: it needs no git, and most saves have no Autopilot claim waiting.
|
|
8
|
+
if (!readRecord(root)?.pending.some((entry) => entry.automatic))
|
|
9
|
+
return 0;
|
|
10
|
+
const path = mergeHoldPath(root);
|
|
11
|
+
if (!path)
|
|
12
|
+
return 0;
|
|
13
|
+
return mergePublication(path, () => withdrawWaiting(root, changes));
|
|
14
|
+
}
|
|
15
|
+
/** After a level write: a level other than Autopilot withdraws Autopilot's waiting claims. */
|
|
16
|
+
export async function afterLevel(root) {
|
|
17
|
+
if (effectiveLevel(root) !== 'auto')
|
|
18
|
+
await withdrawAutopilotClaims(root).catch(() => 0);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A person ends a wait the host never answered: only an Autopilot claim (a person's own wait is
|
|
22
|
+
* unchanged), and never a head main already holds. The change can then be merged again.
|
|
23
|
+
*/
|
|
24
|
+
export async function withdrawMerge(root, change) {
|
|
25
|
+
const path = mergeHoldPath(root);
|
|
26
|
+
if (!path)
|
|
27
|
+
return false;
|
|
28
|
+
return mergePublication(path, async () => (await withdrawWaiting(root, [change])) > 0);
|
|
29
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readdir
|
|
1
|
+
import { readdir } from 'node:fs/promises';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { approvalHolds, approvalSchema } from '@buffcore/contracts/approvals';
|
|
4
4
|
import { readChange } from '../change-file.js';
|
|
@@ -11,12 +11,15 @@ import { planHistory } from './plan-history.js';
|
|
|
11
11
|
import { parseJourneys } from './plan-journeys.js';
|
|
12
12
|
import { readNotThis } from './plan-not.js';
|
|
13
13
|
import { structureFacts } from './plan-structure.js';
|
|
14
|
+
import { readInside } from './real-file.js';
|
|
14
15
|
import { stepFiles } from './save-ownership.js';
|
|
15
16
|
export { answer, briefSection, parseBrief, plain, planSections, withoutComments, } from './plan-brief.js';
|
|
16
17
|
export { parseJourneys } from './plan-journeys.js';
|
|
18
|
+
/** A plan file's text, only when it is a regular file whose real path stays in the product. */
|
|
17
19
|
async function rawTextAt(root, file) {
|
|
18
20
|
try {
|
|
19
|
-
|
|
21
|
+
const read = await readInside(join(root, file), [root]);
|
|
22
|
+
return read === undefined ? null : read.bytes.toString('utf8');
|
|
20
23
|
}
|
|
21
24
|
catch {
|
|
22
25
|
return null;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises';
|
|
2
1
|
import { join } from 'node:path';
|
|
3
2
|
import { createI18n } from '@buffcore/i18n';
|
|
4
3
|
import { readChanges } from '../change-file.js';
|
|
@@ -6,6 +5,7 @@ import { adminScreens, appScreens } from '../inventory-screens.js';
|
|
|
6
5
|
import { SURFACE_FIELD, SURFACE_LINE } from '../prototype/comp.js';
|
|
7
6
|
import { en } from './plan-structure.en.js';
|
|
8
7
|
import { es } from './plan-structure.es.js';
|
|
8
|
+
import { readInside } from './real-file.js';
|
|
9
9
|
/** `/billing (planned, C-020)`: a first screen a change adds, before its route exists. */
|
|
10
10
|
const PLANNED = /^(.*\S) \(planned, (C-\d{3,})\)$/u;
|
|
11
11
|
const messages = createI18n({ catalogs: [{ en, es }] });
|
|
@@ -162,13 +162,11 @@ export function parseStructure(text, screens, changes) {
|
|
|
162
162
|
}
|
|
163
163
|
/** Reuse the inventory's discovery; no product code is loaded and no navigation is inferred. */
|
|
164
164
|
export async function readStructure(root) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
throw error;
|
|
169
|
-
});
|
|
170
|
-
if (text === null)
|
|
165
|
+
// Missing, or a link, hard link or junction leading out of the product: no structure to show.
|
|
166
|
+
const read = await readInside(join(root, 'plan/structure.md'), [root]);
|
|
167
|
+
if (!read)
|
|
171
168
|
return null;
|
|
169
|
+
const text = read.bytes.toString('utf8');
|
|
172
170
|
const [app, admin, planned] = await Promise.all([
|
|
173
171
|
appScreens(root, root),
|
|
174
172
|
adminScreens(root, root),
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { type PrototypeCompReply } from '@buffcore/contracts/workbench';
|
|
2
|
+
import { type ApprovalContext } from './approval-authority.ts';
|
|
2
3
|
import type { ImageType, UiRoute } from './routes.ts';
|
|
3
4
|
export declare const pictureType: (file: string) => ImageType | undefined;
|
|
4
|
-
export declare function compReply(root: string, hosted?: boolean): Promise<PrototypeCompReply>;
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
export declare function compReply(root: string, hosted?: boolean, context?: ApprovalContext): Promise<PrototypeCompReply>;
|
|
6
|
+
/**
|
|
7
|
+
* The real file a picture name stands for, or undefined: only the comp's own shots and the
|
|
8
|
+
* references, and only a regular file whose real path (links followed) stays where it was listed:
|
|
9
|
+
* a shot inside the comp snapshot folder, a reference inside the product's folder unless the person's
|
|
10
|
+
* own studio lists it outside.
|
|
11
|
+
*/
|
|
12
|
+
export declare function compPicture(root: string, name: string, hosted?: boolean): Promise<{
|
|
13
|
+
bytes: Buffer;
|
|
14
|
+
type: ImageType;
|
|
15
|
+
} | undefined>;
|
|
7
16
|
export declare function prototypeCompRoutes(root: string): UiRoute[];
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// The comp page's reads: the newest comp snapshot's screens, the references beside them and the
|
|
2
2
|
// approval; and each picture by its name (never by a path from the request).
|
|
3
|
-
import { readFile } from 'node:fs/promises';
|
|
4
3
|
import { extname, join } from 'node:path';
|
|
5
4
|
import { frameSizes } from '@buffcore/config';
|
|
6
5
|
import { workbenchApi } from '@buffcore/contracts/workbench';
|
|
@@ -9,6 +8,9 @@ import { referencePictures } from '../design/references.js';
|
|
|
9
8
|
import { compDir, firstSurface, newestComp } from '../prototype/comp.js';
|
|
10
9
|
import { compApproved, readCompApproval } from '../prototype/comp-record.js';
|
|
11
10
|
import { snapshotsDir } from '../prototype/report.js';
|
|
11
|
+
import { authorityFor } from './approval-authority.js';
|
|
12
|
+
import { hostedComp } from './comp-approval.js';
|
|
13
|
+
import { readInside } from './real-file.js';
|
|
12
14
|
const MAX = 12;
|
|
13
15
|
/** The picture types the page shows, by extension; any other file is never served. */
|
|
14
16
|
const TYPES = {
|
|
@@ -43,12 +45,15 @@ async function desktopWidth(root) {
|
|
|
43
45
|
async function shownReferences(root, hosted) {
|
|
44
46
|
return (await referencePictures(root))
|
|
45
47
|
.slice(0, MAX)
|
|
46
|
-
.map(({ file, inside }) => file && (inside || !hosted) && pictureType(file) ? file : undefined);
|
|
48
|
+
.map(({ file, inside }) => file && (inside || !hosted) && pictureType(file) ? { file, inside } : undefined);
|
|
47
49
|
}
|
|
48
|
-
export async function compReply(root, hosted = false) {
|
|
50
|
+
export async function compReply(root, hosted = false, context) {
|
|
49
51
|
const surface = firstSurface(root);
|
|
50
52
|
const comp = newestComp(root);
|
|
51
53
|
const record = readCompApproval(root);
|
|
54
|
+
const approval = context
|
|
55
|
+
? await hostedComp(root, record, comp, context)
|
|
56
|
+
: { approved: compApproved(record, comp), approval: record?.approval ?? null };
|
|
52
57
|
const references = await shownReferences(root, hosted);
|
|
53
58
|
const screens = (comp?.report.scenes ?? []).slice(0, MAX).map((scene, index) => {
|
|
54
59
|
const { light, dark } = screenShots(scene);
|
|
@@ -63,13 +68,17 @@ export async function compReply(root, hosted = false) {
|
|
|
63
68
|
surface,
|
|
64
69
|
width: surface === 'phone' ? frameSizes().phone.width : await desktopWidth(root),
|
|
65
70
|
screens,
|
|
66
|
-
references: references.map((
|
|
71
|
+
references: references.map((shown, index) => (shown ? `reference-${index}` : null)),
|
|
67
72
|
...(comp ? { fingerprint: comp.fingerprint } : {}),
|
|
68
|
-
|
|
69
|
-
approval: record?.approval ?? null,
|
|
73
|
+
...approval,
|
|
70
74
|
};
|
|
71
75
|
}
|
|
72
|
-
/**
|
|
76
|
+
/**
|
|
77
|
+
* The real file a picture name stands for, or undefined: only the comp's own shots and the
|
|
78
|
+
* references, and only a regular file whose real path (links followed) stays where it was listed:
|
|
79
|
+
* a shot inside the comp snapshot folder, a reference inside the product's folder unless the person's
|
|
80
|
+
* own studio lists it outside.
|
|
81
|
+
*/
|
|
73
82
|
export async function compPicture(root, name, hosted = false) {
|
|
74
83
|
const screen = /^screen-(\d{1,2})-(light|dark)$/.exec(name);
|
|
75
84
|
if (screen) {
|
|
@@ -78,11 +87,19 @@ export async function compPicture(root, name, hosted = false) {
|
|
|
78
87
|
return;
|
|
79
88
|
const look = screenShots(scene)[screen[2]];
|
|
80
89
|
const path = look ? scene.screenshots[look] : undefined;
|
|
81
|
-
|
|
90
|
+
const folder = compDir(snapshotsDir(root));
|
|
91
|
+
return path && SHOT.test(path) ? picture(join(folder, path), [root, folder]) : undefined;
|
|
82
92
|
}
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
93
|
+
const index = /^reference-(\d{1,2})$/.exec(name)?.[1];
|
|
94
|
+
const shown = index === undefined ? undefined : (await shownReferences(root, hosted))[+index];
|
|
95
|
+
if (shown)
|
|
96
|
+
return picture(shown.file, shown.inside ? [root] : []);
|
|
97
|
+
}
|
|
98
|
+
/** The bytes read from the opened file, typed by its real name; any other file is never served. */
|
|
99
|
+
async function picture(file, folders) {
|
|
100
|
+
const read = await readInside(file, folders);
|
|
101
|
+
const type = read && pictureType(read.real);
|
|
102
|
+
return read && type ? { bytes: read.bytes, type } : undefined;
|
|
86
103
|
}
|
|
87
104
|
export function prototypeCompRoutes(root) {
|
|
88
105
|
return [
|
|
@@ -90,18 +107,19 @@ export function prototypeCompRoutes(root) {
|
|
|
90
107
|
method: 'GET',
|
|
91
108
|
path: workbenchApi.prototypeComp,
|
|
92
109
|
right: 'read:runs',
|
|
93
|
-
handle: async ({ hosted }) => ({
|
|
110
|
+
handle: async ({ hosted, identity, approval }) => ({
|
|
111
|
+
status: 200,
|
|
112
|
+
body: await compReply(root, !!hosted, authorityFor({ identity, approval })),
|
|
113
|
+
}),
|
|
94
114
|
},
|
|
95
115
|
{
|
|
96
116
|
method: 'GET',
|
|
97
117
|
path: `${workbenchApi.prototypeComp}/picture/:name`,
|
|
98
118
|
right: 'read:runs',
|
|
99
119
|
handle: async ({ params, hosted }) => {
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return bytes && type
|
|
104
|
-
? { status: 200, body: bytes, type }
|
|
120
|
+
const shown = await compPicture(root, params.name ?? '', !!hosted);
|
|
121
|
+
return shown
|
|
122
|
+
? { status: 200, body: shown.bytes, type: shown.type }
|
|
105
123
|
: { status: 404, body: { error: 'missing' } };
|
|
106
124
|
},
|
|
107
125
|
},
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type InsideFile = {
|
|
2
|
+
bytes: Buffer;
|
|
3
|
+
real: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* The bytes of `file` and its real path, or undefined. `afterOpen` is for tests only: it runs
|
|
7
|
+
* between the open and the checks, where a folder swap would happen.
|
|
8
|
+
*/
|
|
9
|
+
export declare function readInside(file: string, folders: readonly string[], afterOpen?: () => void | Promise<void>): Promise<InsideFile | undefined>;
|
|
10
|
+
/** `readInside` for synchronous readers: the bytes, or undefined. */
|
|
11
|
+
export declare function readInsideSync(file: string, folders: readonly string[]): Buffer | undefined;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Read a file a route may show: open it first, then prove the open handle is the regular file, with
|
|
2
|
+
// one link, whose real path (links and junctions followed) stays inside every given folder, and read
|
|
3
|
+
// the bytes from that handle (as app-icon.ts does). A link or junction leading out, a hard link, a
|
|
4
|
+
// folder, a missing file or a path swapped while it was checked gives undefined.
|
|
5
|
+
import { closeSync, existsSync, fstatSync, openSync, readFileSync, realpathSync, statSync, } from 'node:fs';
|
|
6
|
+
import { open, realpath, stat } from 'node:fs/promises';
|
|
7
|
+
import { isAbsolute, relative, sep } from 'node:path';
|
|
8
|
+
/** Linux names an open file's true path; elsewhere (Windows, macOS) Node has no such name. */
|
|
9
|
+
const FDS = '/proc/self/fd';
|
|
10
|
+
const byHandle = existsSync(FDS);
|
|
11
|
+
const within = (folder, file) => {
|
|
12
|
+
const from = relative(folder, file);
|
|
13
|
+
return !!from && !isAbsolute(from) && from !== '..' && !from.startsWith(`..${sep}`);
|
|
14
|
+
};
|
|
15
|
+
/** The handle is that path's file: same device and file id, a regular file with one link. */
|
|
16
|
+
const same = (handle, path) => handle.isFile() && handle.nlink === 1n && handle.dev === path.dev && handle.ino === path.ino;
|
|
17
|
+
/**
|
|
18
|
+
* The bytes of `file` and its real path, or undefined. `afterOpen` is for tests only: it runs
|
|
19
|
+
* between the open and the checks, where a folder swap would happen.
|
|
20
|
+
*/
|
|
21
|
+
export async function readInside(file, folders, afterOpen) {
|
|
22
|
+
const handle = await open(file, 'r').catch(() => undefined);
|
|
23
|
+
if (!handle)
|
|
24
|
+
return;
|
|
25
|
+
try {
|
|
26
|
+
const opened = await handle.stat({ bigint: true });
|
|
27
|
+
await afterOpen?.();
|
|
28
|
+
const real = await realpath(file);
|
|
29
|
+
for (const folder of folders)
|
|
30
|
+
if (!within(await realpath(folder), real))
|
|
31
|
+
return;
|
|
32
|
+
const checked = await stat(real, { bigint: true });
|
|
33
|
+
// The real path again: a swap during the checks changes it or the file id.
|
|
34
|
+
if (!same(opened, checked) || (await realpath(file)) !== real)
|
|
35
|
+
return;
|
|
36
|
+
// Where the system names the open file itself, that name must be inside too: no race left.
|
|
37
|
+
if (byHandle) {
|
|
38
|
+
const held = await realpath(`${FDS}/${handle.fd}`);
|
|
39
|
+
for (const folder of folders)
|
|
40
|
+
if (!within(await realpath(folder), held))
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
return { bytes: await handle.readFile(), real };
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
await handle.close().catch(() => { });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** `readInside` for synchronous readers: the bytes, or undefined. */
|
|
53
|
+
export function readInsideSync(file, folders) {
|
|
54
|
+
let fd;
|
|
55
|
+
try {
|
|
56
|
+
fd = openSync(file, 'r');
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const opened = fstatSync(fd, { bigint: true });
|
|
63
|
+
const real = realpathSync.native(file);
|
|
64
|
+
for (const folder of folders)
|
|
65
|
+
if (!within(realpathSync.native(folder), real))
|
|
66
|
+
return;
|
|
67
|
+
const checked = statSync(real, { bigint: true });
|
|
68
|
+
if (!same(opened, checked) || realpathSync.native(file) !== real)
|
|
69
|
+
return;
|
|
70
|
+
if (byHandle) {
|
|
71
|
+
const held = realpathSync.native(`${FDS}/${fd}`);
|
|
72
|
+
for (const folder of folders)
|
|
73
|
+
if (!within(realpathSync.native(folder), held))
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
return readFileSync(fd);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
closeSync(fd);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -9,6 +9,7 @@ import { autoProtection } from './autopilot-protection.js';
|
|
|
9
9
|
import { requestRefusal } from './host.js';
|
|
10
10
|
import { HostSaveRefusal } from './host-save.js';
|
|
11
11
|
import { factsChanged, mergeEvent } from './merge-events.js';
|
|
12
|
+
import { afterLevel } from './merge-withdraw.js';
|
|
12
13
|
import { reviewedStep, saveBody } from './save-body.js';
|
|
13
14
|
import { saveBuildCompletion } from './save-build-completion.js';
|
|
14
15
|
import { saveChangeStep } from './save-change-step.js';
|
|
@@ -102,8 +103,10 @@ export async function studioSave(root, body, viewer, automatic, options = {}) {
|
|
|
102
103
|
factsChanged('levels');
|
|
103
104
|
// Automatic saves already wake parked work when their turn finishes.
|
|
104
105
|
if (!automatic && result.ok) {
|
|
105
|
-
if (body.step === 'level' && effectiveLevel(root) !== 'auto')
|
|
106
|
+
if (body.step === 'level' && effectiveLevel(root) !== 'auto') {
|
|
106
107
|
mergeEvent('level', root);
|
|
108
|
+
await afterLevel(root);
|
|
109
|
+
}
|
|
107
110
|
mergeEvent('save', root);
|
|
108
111
|
}
|
|
109
112
|
return result;
|
|
@@ -21,6 +21,7 @@ export { ChatEngine, type ChatEngineOptions } from './chat/turn.ts';
|
|
|
21
21
|
export type { HostLevel, SignIn, UiIdentity } from './host.ts';
|
|
22
22
|
export type { HostSave, HostSaveAnswer, HostSaveInput } from './host-save.ts';
|
|
23
23
|
export type { HostMerge, HostMergeAnswer, HostMergeInput } from './merge-authority.ts';
|
|
24
|
+
export { type HostMergeSettled, type HostMergeSettlement, settleHostMerge, } from './merge-settle.ts';
|
|
24
25
|
export type { AnswerResult, HostOutside, HostOutsideReadInput } from './outside.ts';
|
|
25
26
|
export { pageScript } from './page.ts';
|
|
26
27
|
export { readProductPlan } from './product-plan.ts';
|
|
@@ -21,6 +21,7 @@ export { FreshLooks } from './chat/fresh.js';
|
|
|
21
21
|
export { freshRoutes } from './chat/fresh-routes.js';
|
|
22
22
|
export { chatRoutes } from './chat/routes.js';
|
|
23
23
|
export { ChatEngine } from './chat/turn.js';
|
|
24
|
+
export { settleHostMerge, } from './merge-settle.js';
|
|
24
25
|
export { pageScript } from './page.js';
|
|
25
26
|
export { readProductPlan } from './product-plan.js';
|
|
26
27
|
export { Runner } from './runner.js';
|
|
@@ -351,6 +351,7 @@ export declare function studioState(root: string, options: Pick<UiServerOptions,
|
|
|
351
351
|
head: string;
|
|
352
352
|
kind: "merged" | "refused" | "waiting";
|
|
353
353
|
again?: true | undefined;
|
|
354
|
+
automatic?: true | undefined;
|
|
354
355
|
at: string;
|
|
355
356
|
detail: string;
|
|
356
357
|
}[] | undefined;
|
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
import { existsSync, readdirSync
|
|
1
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { prototypeVersionsSchema } from '@buffcore/contracts/approvals';
|
|
4
4
|
import { MilestoneVersionsPending, requireLegacyChange } from '../prototype/milestone-key.js';
|
|
5
5
|
import { heldOnly, readVersions } from '../prototype/versions.js';
|
|
6
|
+
import { authorityT } from './approval-authority.js';
|
|
7
|
+
import { readInsideSync } from './real-file.js';
|
|
6
8
|
/** Host approval reads only the requested change; local signatures are not evidence. */
|
|
7
9
|
export function authorityVersion(root, change) {
|
|
8
10
|
requireLegacyChange(root, change);
|
|
9
11
|
if (!/^C-\d{3,}$/.test(change))
|
|
10
12
|
throw new Error();
|
|
11
|
-
const
|
|
13
|
+
const bytes = readInsideSync(join(root, 'prototype/versions', `${change}.json`), [root]);
|
|
14
|
+
if (!bytes)
|
|
15
|
+
throw new Error();
|
|
16
|
+
const value = JSON.parse(bytes.toString('utf8'));
|
|
12
17
|
const record = prototypeVersionsSchema.parse({ change: value.change, versions: value.versions });
|
|
13
18
|
if (record.change !== change)
|
|
14
19
|
throw new Error();
|
|
@@ -28,7 +33,7 @@ export function studioVersions(root, hosted = false, changes = []) {
|
|
|
28
33
|
requireLegacyChange(root, change);
|
|
29
34
|
// Preserve the old projection: unrecorded legacy changes have no version row.
|
|
30
35
|
if (saved.includes(change))
|
|
31
|
-
rows.push(hosted ?
|
|
36
|
+
rows.push(hosted ? hostedRow(root, change) : heldOnly(readVersions(root, change)));
|
|
32
37
|
}
|
|
33
38
|
catch (error) {
|
|
34
39
|
if (!(error instanceof MilestoneVersionsPending))
|
|
@@ -38,3 +43,14 @@ export function studioVersions(root, hosted = false, changes = []) {
|
|
|
38
43
|
}
|
|
39
44
|
return rows;
|
|
40
45
|
}
|
|
46
|
+
/** A record the host read can't use (linked out, hard-linked, malformed) is a refused row. */
|
|
47
|
+
function hostedRow(root, change) {
|
|
48
|
+
try {
|
|
49
|
+
return authorityVersion(root, change);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error instanceof MilestoneVersionsPending)
|
|
53
|
+
throw error;
|
|
54
|
+
return { change, versions: [], approvalRefusal: authorityT('ui.authority.unread') };
|
|
55
|
+
}
|
|
56
|
+
}
|