@ultimat3/cli 31.47.1 → 31.49.0
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/CLAUDE.md +30 -23
- package/README.md +96 -4
- package/package.json +29 -29
- package/src/api-registration.ts +28 -14
- package/src/app-load.ts +6 -1
- package/src/app-root.ts +7 -1
- package/src/clean-services.ts +142 -0
- package/src/cmd-auth.ts +3 -0
- package/src/cmd-clean-spec.ts +10 -3
- package/src/cmd-clean.ts +23 -9
- package/src/cmd-db-backfill.ts +6 -0
- package/src/cmd-db-branch.ts +10 -4
- package/src/cmd-db-recount.ts +5 -12
- package/src/cmd-db-rollback.ts +77 -0
- package/src/cmd-db-spec.ts +18 -2
- package/src/cmd-db-status.ts +5 -10
- package/src/cmd-db.ts +105 -33
- package/src/cmd-deploy-verify.ts +14 -6
- package/src/cmd-docs-topic.ts +94 -0
- package/src/cmd-docs.ts +15 -1
- package/src/cmd-doctor.ts +109 -41
- package/src/cmd-drill.ts +100 -19
- package/src/cmd-env.ts +30 -8
- package/src/cmd-errors-app.ts +63 -0
- package/src/cmd-errors.ts +50 -14
- package/src/cmd-generate.ts +17 -4
- package/src/cmd-heavy-spec.ts +36 -0
- package/src/cmd-heavy.ts +244 -0
- package/src/cmd-jobs.ts +27 -1
- package/src/cmd-merge-driver-spec.ts +12 -0
- package/src/cmd-merge-driver.ts +95 -0
- package/src/cmd-new.ts +23 -3
- package/src/cmd-policy-spec.ts +9 -1
- package/src/cmd-policy.ts +72 -17
- package/src/cmd-stop.ts +18 -4
- package/src/cmd-tasks-run.ts +87 -0
- package/src/cmd-tasks-spec.ts +10 -3
- package/src/cmd-tasks.ts +8 -5
- package/src/cmd-test.ts +15 -4
- package/src/cmd-upgrade-spec.ts +8 -1
- package/src/cmd-upgrade.ts +32 -5
- package/src/cmd-verify.ts +9 -41
- package/src/codemods/agents-doc-row.ts +73 -0
- package/src/codemods/define-error-leaf.ts +37 -12
- package/src/codemods/docs-many-agents.ts +26 -0
- package/src/codemods/docs-worker-commands.ts +26 -0
- package/src/codemods/index.ts +6 -0
- package/src/codemods/merge-drivers.ts +66 -0
- package/src/command.ts +3 -0
- package/src/db-applied-edits.ts +117 -0
- package/src/db-dev-wire.ts +18 -0
- package/src/db-diagnostics.ts +5 -10
- package/src/db-generate.ts +82 -7
- package/src/db-migrate-pass.ts +51 -0
- package/src/db-migrate-through-dev.ts +209 -0
- package/src/db-rollback-consent.ts +128 -0
- package/src/db-rollback-pass.ts +76 -0
- package/src/db-seed-generate.ts +5 -1
- package/src/db-seed.ts +7 -1
- package/src/deploy-verify-findings.ts +3 -0
- package/src/deploy-verify-grade.ts +3 -0
- package/src/deploy-verify-processes.ts +32 -0
- package/src/dev-boot.ts +3 -0
- package/src/dev-db-migrate.ts +155 -0
- package/src/dev-db-rollback.ts +33 -0
- package/src/dev-db-settled.ts +22 -0
- package/src/dev-lock-errors.ts +78 -0
- package/src/dev-lock-holder-dir.ts +64 -0
- package/src/dev-lock-live.ts +44 -0
- package/src/dev-lock.ts +59 -84
- package/src/dev-policy.ts +11 -8
- package/src/dev-rebuild-schema.ts +116 -13
- package/src/dev-rebuild.ts +13 -4
- package/src/dev-route-table.ts +7 -0
- package/src/dispatch.ts +21 -1
- package/src/doctor-advisory-floors.ts +30 -8
- package/src/doctor-api-listing.ts +114 -0
- package/src/doctor-guarded.ts +41 -0
- package/src/doctor-port.ts +90 -17
- package/src/doctor-probe.ts +36 -5
- package/src/doctor-processes-probe.ts +127 -0
- package/src/doctor-processes.ts +136 -0
- package/src/doctor-warnings.ts +17 -0
- package/src/error-codes-agents.ts +35 -0
- package/src/error-codes-processes.ts +30 -0
- package/src/error-codes-tooling.ts +11 -3
- package/src/error-codes.ts +12 -13
- package/src/errors-suite.ts +27 -0
- package/src/errors.ts +18 -22
- package/src/exec.ts +11 -3
- package/src/exercise-registration.ts +4 -3
- package/src/framework-env.ts +7 -3
- package/src/generate-files.ts +1 -0
- package/src/generate-grant-findings.ts +7 -5
- package/src/generate-grants.ts +9 -8
- package/src/generate-kinds.ts +30 -6
- package/src/generate-roles-file.ts +30 -0
- package/src/generate-slice-policy.ts +3 -3
- package/src/guards.ts +2 -2
- package/src/heavy-cores.ts +51 -0
- package/src/heavy-env.ts +21 -0
- package/src/heavy-errors.ts +54 -0
- package/src/heavy-flock.ts +73 -0
- package/src/heavy-holders.ts +78 -0
- package/src/heavy-ledger.ts +400 -0
- package/src/heavy-memory.ts +67 -0
- package/src/heavy-pressure.ts +104 -0
- package/src/heavy-weights.ts +47 -0
- package/src/heavy.ts +308 -0
- package/src/i18n-registration.ts +22 -2
- package/src/island-harness.ts +2 -2
- package/src/job-budget.ts +4 -2
- package/src/job-registration.ts +64 -8
- package/src/jobs-queue-known.ts +43 -0
- package/src/local-cli.ts +6 -5
- package/src/mcp-errors-agents.ts +20 -0
- package/src/mcp-errors-doctor.ts +27 -0
- package/src/mcp-errors-processes.ts +23 -0
- package/src/mcp-errors.ts +18 -29
- package/src/mcp-host.ts +2 -2
- package/src/merge-driver-catalog.ts +58 -0
- package/src/merge-driver-config.ts +43 -0
- package/src/merge-driver-lines.ts +140 -0
- package/src/merge-state.ts +105 -0
- package/src/messages-db.ts +22 -1
- package/src/messages-processes.ts +13 -0
- package/src/messages-upgrade.ts +6 -0
- package/src/messages.ts +62 -1
- package/src/output-findings.ts +48 -21
- package/src/parse.ts +37 -6
- package/src/policy-facts.ts +38 -21
- package/src/policy-row.ts +99 -0
- package/src/port-pair-free.ts +28 -0
- package/src/registry-lines.ts +110 -0
- package/src/registry.ts +4 -0
- package/src/retain-sweep.ts +1 -1
- package/src/role-replicator.ts +9 -4
- package/src/role-select.ts +86 -0
- package/src/role-start.ts +13 -55
- package/src/role-sync.ts +10 -5
- package/src/runtime-cache-frame.ts +46 -0
- package/src/runtime-cache-ttl.ts +23 -0
- package/src/runtime-cache.ts +56 -18
- package/src/runtime-database-ssl.ts +84 -0
- package/src/runtime-idempotency-scope.ts +16 -2
- package/src/runtime-jobs-share.ts +52 -0
- package/src/runtime-jobs.ts +43 -12
- package/src/runtime-overrides.ts +4 -2
- package/src/runtime-queue.ts +18 -3
- package/src/runtime-services.ts +8 -1
- package/src/schema-drift.ts +19 -2
- package/src/schema-dump-drift.ts +4 -1
- package/src/serve-boot-probe.ts +30 -2
- package/src/serve-boot.ts +11 -1
- package/src/serve-env.ts +39 -3
- package/src/serve.ts +61 -10
- package/src/stop-run-start.ts +12 -0
- package/src/supervise-boot.ts +61 -0
- package/src/supervise-cgroup.ts +84 -0
- package/src/supervise-child.ts +206 -0
- package/src/supervise-count.ts +244 -0
- package/src/supervise-errors.ts +83 -0
- package/src/supervise-facts.ts +40 -0
- package/src/supervise-messages.ts +84 -0
- package/src/supervise-policy.ts +46 -0
- package/src/supervise-pool.ts +10 -0
- package/src/supervise-probe.ts +158 -0
- package/src/supervise-run.ts +54 -0
- package/src/supervise.ts +383 -0
- package/src/templates/generate-purge.ts +6 -6
- package/src/templates/guard-animated-layout-property.ts +2 -2
- package/src/templates/guard-bare-error.ts +1 -1
- package/src/templates/guard-error-declared.ts +2 -2
- package/src/templates/guard-focus-visible.ts +1 -1
- package/src/templates/guard-image-dimensions.ts +2 -2
- package/src/templates/guard-island-mount-clears.ts +1 -1
- package/src/templates/guard-native-confirm.ts +1 -1
- package/src/templates/guard-raw-breakpoint.ts +2 -2
- package/src/templates/guard-raw-colour.ts +2 -2
- package/src/templates/guard-raw-fetch.ts +1 -1
- package/src/templates/guard-raw-interval.ts +1 -1
- package/src/templates/guard-raw-length.ts +3 -3
- package/src/templates/guard-raw-motion.ts +1 -1
- package/src/templates/guard-raw-shadow.ts +1 -1
- package/src/templates/guard-raw-z-index.ts +1 -1
- package/src/templates/guard-released-forms.ts +59 -13
- package/src/templates/guard-repo-await-in-loop.ts +1 -1
- package/src/templates/guard-repo-raw-sql.ts +4 -4
- package/src/templates/guard-repo-unbounded.ts +1 -1
- package/src/templates/guard-runtime-pin.ts +1 -1
- package/src/templates/guard-semantic-interactive.ts +2 -2
- package/src/templates/guard-unbounded-fanout.ts +1 -1
- package/src/templates/guard-undeclared-custom-property.ts +3 -3
- package/src/templates/guard-undefined-style-class.ts +3 -3
- package/src/templates/guard-unzoned-date.ts +3 -3
- package/src/templates/query-slice.ts +82 -0
- package/src/templates/query.ts +76 -30
- package/src/templates/resource-page.ts +3 -2
- package/src/templates/scaffold-bin.ts +2 -0
- package/src/templates/scaffold-claude-agents.ts +9 -6
- package/src/templates/scaffold-claude-commands.ts +7 -1
- package/src/templates/scaffold-claude.ts +4 -4
- package/src/templates/scaffold-container-compose.ts +47 -7
- package/src/templates/scaffold-docs.ts +11 -6
- package/src/templates/scaffold-gitattributes.ts +18 -1
- package/src/templates/scaffold-helm-templates.ts +9 -0
- package/src/templates/scaffold-repo.ts +4 -1
- package/src/templates/scaffold-test-services.ts +33 -11
- package/src/templates/shapes/offline.ts +0 -7
- package/src/templates/shapes/realtime.ts +2 -6
- package/src/templates/sign-in-action-tests.ts +6 -4
- package/src/templates/sign-in-challenge.ts +62 -0
- package/src/templates/sign-in-oidc-mfa.ts +31 -0
- package/src/templates/sign-in-oidc.ts +11 -3
- package/src/templates/sign-in-password.ts +18 -56
- package/src/test-batches.ts +9 -3
- package/src/test-shards.ts +117 -37
- package/src/test-slots.ts +35 -161
- package/src/test-verdict.ts +79 -0
- package/src/test-workers.ts +15 -0
- package/src/upgrade-gate-codemods.ts +5 -3
- package/src/upgrade-gate.ts +19 -1
- package/src/upgrade-merge-drivers.ts +25 -0
- package/src/upgrade-report.ts +18 -4
- package/src/upgrade-scope.ts +62 -0
- package/src/verify-checks.ts +6 -11
- package/src/verify-coverage-run.ts +9 -5
- package/src/verify-deadline.ts +18 -2
- package/src/verify-heavy-steps.ts +26 -0
- package/src/verify-live.ts +38 -14
- package/src/verify-only.ts +67 -0
- package/src/verify-progress.ts +7 -3
- package/src/verify-run.ts +20 -2
- package/src/verify-services-stack.ts +165 -0
- package/src/verify-services.ts +84 -2
- package/src/verify-test-run.ts +9 -9
- package/src/verify-tests.ts +35 -22
- package/src/verify-typecheck.ts +25 -2
- package/wiki/Actions.md +2 -1
- package/wiki/Admin-Dashboard.md +2 -2
- package/wiki/Agents.md +5 -5
- package/wiki/Auth.md +15 -5
- package/wiki/Bare-VM.md +1 -1
- package/wiki/Building-Your-Own-Base.md +15 -2
- package/wiki/CI-Parallel-Gate.md +4 -4
- package/wiki/CLI-Reference.md +127 -48
- package/wiki/Caching-And-Invalidation.md +2 -2
- package/wiki/Client-Navigation.md +1 -1
- package/wiki/Configuration.md +32 -13
- package/wiki/Deployment.md +44 -4
- package/wiki/Entities-And-Migrations.md +5 -1
- package/wiki/FAQ.md +1 -1
- package/wiki/Getting-Started.md +2 -2
- package/wiki/Home.md +2 -1
- package/wiki/I18n.md +5 -3
- package/wiki/Installation.md +12 -11
- package/wiki/Interface-Rules.md +1 -1
- package/wiki/Jobs-And-Workflows.md +24 -4
- package/wiki/Known-Gaps.md +2 -2
- package/wiki/MCP-And-AI.md +8 -5
- package/wiki/Mail.md +1 -1
- package/wiki/Many-Agents.md +127 -0
- package/wiki/Migrations-And-Backfills.md +18 -0
- package/wiki/Notify.md +1 -1
- package/wiki/Observability.md +13 -0
- package/wiki/Queries-And-Live-Queries.md +2 -2
- package/wiki/Realtime.md +10 -1
- package/wiki/Routes-And-Render-Modes.md +1 -1
- package/wiki/Scheduled-Tasks.md +4 -1
- package/wiki/Testing.md +12 -9
- package/wiki/Timezones-And-Dates.md +5 -1
- package/wiki/Troubleshooting.md +1 -0
- package/wiki/Tutorial-01-First-App.md +5 -5
- package/wiki/Tutorial-02-First-Feature.md +1 -1
- package/wiki/Tutorial-03-Auth-And-Admin.md +7 -6
- package/wiki/Upgrading.md +37 -0
package/src/cmd-errors.ts
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
// invented should come back refused: "no such code", never a plausible-sounding explanation.
|
|
4
4
|
|
|
5
5
|
import { nearestName, singleLine } from '@ultimat3/core';
|
|
6
|
+
import { definedErrorStatus } from '@ultimat3/core/internal/error-status';
|
|
6
7
|
import type { ErrorExplanation } from '@ultimat3/mcp';
|
|
8
|
+
import { findAppRoot } from './app-root';
|
|
9
|
+
import { type AppCodeExplanation, AppCodeUnknownError, explainAppCode } from './cmd-errors-app';
|
|
7
10
|
import { explainGuardCode } from './cmd-errors-guards';
|
|
8
11
|
import { withRecent } from './cmd-errors-recent';
|
|
9
12
|
import { errorsSpec } from './cmd-errors-spec';
|
|
@@ -15,6 +18,8 @@ import { ErrorCodeUnknownError, MissingPositionalError } from './errors';
|
|
|
15
18
|
import { explainErrorCode, explainEveryErrorCode } from './mcp-errors';
|
|
16
19
|
import { msg } from './messages';
|
|
17
20
|
import type { CommandResult, JsonValue } from './output';
|
|
21
|
+
import type { ReaderOptions } from './output-findings';
|
|
22
|
+
import { fixForReader } from './output-findings';
|
|
18
23
|
import { flagBool } from './parse';
|
|
19
24
|
|
|
20
25
|
export { ERRORS_SUBCOMMANDS } from './cmd-errors-spec';
|
|
@@ -25,21 +30,21 @@ export { ERRORS_SUBCOMMANDS } from './cmd-errors-spec';
|
|
|
25
30
|
* caller that has to regex `packages/x/src/y.ts:80` back out of an English sentence is a caller the
|
|
26
31
|
* machine-readable surface failed. `null` when the installed framework raises the code nowhere.
|
|
27
32
|
*/
|
|
28
|
-
const asJson = (explanation: ErrorExplanation): JsonValue => {
|
|
33
|
+
const asJson = (explanation: ErrorExplanation, reader?: ReaderOptions): JsonValue => {
|
|
29
34
|
const site = codeFixes().get(explanation.code)?.[0];
|
|
30
35
|
return {
|
|
31
36
|
code: explanation.code,
|
|
32
37
|
cause: explanation.cause,
|
|
33
|
-
fix: explanation.fix,
|
|
38
|
+
fix: fixForReader(explanation.fix, reader),
|
|
34
39
|
docs: explanation.docs,
|
|
35
40
|
site: site === undefined ? null : { at: site.at, line: site.line },
|
|
36
41
|
};
|
|
37
42
|
};
|
|
38
43
|
|
|
39
44
|
/** The 3-line contract format, minus the leading blank code line `renderFinding` would add. */
|
|
40
|
-
const detailLines = (explanation: ErrorExplanation): readonly string[] => [
|
|
45
|
+
const detailLines = (explanation: ErrorExplanation, reader?: ReaderOptions): readonly string[] => [
|
|
41
46
|
` cause: ${singleLine(explanation.cause)}`,
|
|
42
|
-
` fix: ${singleLine(explanation.fix)}`,
|
|
47
|
+
` fix: ${singleLine(fixForReader(explanation.fix, reader))}`,
|
|
43
48
|
` docs: ${singleLine(explanation.docs)}`,
|
|
44
49
|
];
|
|
45
50
|
|
|
@@ -47,30 +52,61 @@ const detailLines = (explanation: ErrorExplanation): readonly string[] => [
|
|
|
47
52
|
* A registered code, else a guard's — a shipped guard's or this app's own (`cmd-errors-guards.ts`):
|
|
48
53
|
* the gate prints both, and the app's `CLAUDE.md` sends every printed code here.
|
|
49
54
|
*/
|
|
50
|
-
async function explainOne(
|
|
51
|
-
|
|
55
|
+
async function explainOne(
|
|
56
|
+
code: string,
|
|
57
|
+
cwd: string,
|
|
58
|
+
reader?: ReaderOptions,
|
|
59
|
+
): Promise<CommandResult> {
|
|
60
|
+
// A `defineError()` code is an app's, even once this process has loaded the app (`x mcp serve`, a
|
|
61
|
+
// test rerun): the registry then knows it, and it was answered as a framework code with no throw
|
|
62
|
+
// site in the installed framework, never from the app that declares it.
|
|
63
|
+
const appDeclared = definedErrorStatus(code) !== undefined;
|
|
64
|
+
const explanation = appDeclared
|
|
65
|
+
? undefined
|
|
66
|
+
: (explainErrorCode(code) ?? (await explainGuardCode(code, cwd)));
|
|
52
67
|
if (explanation === undefined) {
|
|
68
|
+
// Before the app loads, so the near miss is a framework code the caller mistyped.
|
|
53
69
|
const suggestion = nearestName(
|
|
54
70
|
code,
|
|
55
71
|
explainEveryErrorCode().map((entry) => entry.code),
|
|
56
72
|
);
|
|
57
|
-
|
|
73
|
+
const declared = await explainAppCode(code, cwd);
|
|
74
|
+
if (declared !== undefined) return appCodeResult(declared);
|
|
75
|
+
if (suggestion !== undefined) throw new ErrorCodeUnknownError({ code, suggestion });
|
|
76
|
+
throw new AppCodeUnknownError({ code, inApp: findAppRoot(cwd) !== undefined });
|
|
58
77
|
}
|
|
59
78
|
return {
|
|
60
79
|
ok: true,
|
|
61
80
|
command: 'errors',
|
|
62
81
|
summary: msg('cli.errors.explained', { code: explanation.code, title: explanation.cause }),
|
|
63
|
-
lines: detailLines(explanation),
|
|
64
|
-
data: asJson(explanation),
|
|
82
|
+
lines: detailLines(explanation, reader),
|
|
83
|
+
data: asJson(explanation, reader),
|
|
65
84
|
};
|
|
66
85
|
}
|
|
67
86
|
|
|
87
|
+
/**
|
|
88
|
+
* An app's own code: what the registry holds (title, status) and the declaring line. No `cause:` or
|
|
89
|
+
* `fix:` line — the registry keeps neither for a `defineError` code, and the declaration is one open
|
|
90
|
+
* file away.
|
|
91
|
+
*/
|
|
92
|
+
const appCodeResult = (declared: AppCodeExplanation): CommandResult => ({
|
|
93
|
+
ok: true,
|
|
94
|
+
command: 'errors',
|
|
95
|
+
summary: msg('cli.errors.explained', { code: declared.code, title: declared.title }),
|
|
96
|
+
lines: [
|
|
97
|
+
` status: ${declared.status ?? 'off-socket'}`,
|
|
98
|
+
...(declared.site === null ? [] : [` declared: ${declared.site.at}:${declared.site.line}`]),
|
|
99
|
+
` docs: ${singleLine(declared.docs)}`,
|
|
100
|
+
],
|
|
101
|
+
data: { ...declared },
|
|
102
|
+
});
|
|
103
|
+
|
|
68
104
|
/**
|
|
69
105
|
* A package that resolved and then threw is a defect, not a host gap: its codes are missing from
|
|
70
106
|
* this answer and something is broken that a `fix:` can address. Reported as findings — and `ok`
|
|
71
107
|
* goes false — so the incomplete catalog cannot read as a complete one.
|
|
72
108
|
*/
|
|
73
|
-
function listAll(catalog: ErrorCatalog): CommandResult {
|
|
109
|
+
function listAll(catalog: ErrorCatalog, reader?: ReaderOptions): CommandResult {
|
|
74
110
|
const all = explainEveryErrorCode();
|
|
75
111
|
return {
|
|
76
112
|
ok: catalog.failed.length === 0,
|
|
@@ -79,7 +115,7 @@ function listAll(catalog: ErrorCatalog): CommandResult {
|
|
|
79
115
|
lines: all.map((entry) => ` ${entry.code.padEnd(30)} ${singleLine(entry.cause)}`),
|
|
80
116
|
findings: catalog.failed,
|
|
81
117
|
data: {
|
|
82
|
-
codes: all.map(asJson),
|
|
118
|
+
codes: all.map((entry) => asJson(entry, reader)),
|
|
83
119
|
unavailable: [...catalog.unavailable],
|
|
84
120
|
failed: catalog.failed.map((finding) => ({
|
|
85
121
|
code: finding.code,
|
|
@@ -100,7 +136,7 @@ export const errorsCommand: CliCommand = {
|
|
|
100
136
|
// and the fix index is what each one instructs. `x errors list` answering 397 codes with the
|
|
101
137
|
// fallback line would be a complete-looking table of shrugs.
|
|
102
138
|
const [catalog] = await Promise.all([loadErrorCatalog(), loadCodeFixes()]);
|
|
103
|
-
if (ctx.args.subcommand === 'list') return listAll(catalog);
|
|
139
|
+
if (ctx.args.subcommand === 'list') return listAll(catalog, ctx.reader);
|
|
104
140
|
const code = ctx.args.positionals[0];
|
|
105
141
|
if (code === undefined) {
|
|
106
142
|
// Not a `BadFlagError`: naming `--code` invented a flag that does not exist, so an agent
|
|
@@ -114,8 +150,8 @@ export const errorsCommand: CliCommand = {
|
|
|
114
150
|
}
|
|
115
151
|
// `--recent`: what THIS app has seen of the code — groups and the bundles `x repro` replays.
|
|
116
152
|
if (flagBool(ctx.args, 'recent')) {
|
|
117
|
-
return withRecent(ctx, code, await explainOne(code, ctx.cwd));
|
|
153
|
+
return withRecent(ctx, code, await explainOne(code, ctx.cwd, ctx.reader));
|
|
118
154
|
}
|
|
119
|
-
return explainOne(code, ctx.cwd);
|
|
155
|
+
return explainOne(code, ctx.cwd, ctx.reader);
|
|
120
156
|
},
|
|
121
157
|
};
|
package/src/cmd-generate.ts
CHANGED
|
@@ -77,7 +77,10 @@ export const generateCommand: CliCommand = {
|
|
|
77
77
|
spec: generateSpec,
|
|
78
78
|
async run(ctx: CommandContext): Promise<CommandResult> {
|
|
79
79
|
const root = requireAppRoot('g', ctx.cwd).dir;
|
|
80
|
-
const kind = readKind(ctx.args.positionals[0]
|
|
80
|
+
const kind = readKind(ctx.args.positionals[0], {
|
|
81
|
+
name: ctx.args.positionals[1],
|
|
82
|
+
feature: flagString(ctx.args, 'feature'),
|
|
83
|
+
});
|
|
81
84
|
refuseSignInFlags(kind, ctx.args);
|
|
82
85
|
refuseChartAlertsFlags(kind, ctx.args);
|
|
83
86
|
// One sign-in per app, named by a flag rather than a `<name>`, and edits no other kind makes.
|
|
@@ -301,7 +304,9 @@ export const generateCommand: CliCommand = {
|
|
|
301
304
|
const written = [...report.written, ...edited, ...artifacts];
|
|
302
305
|
// Named only over a clean run: with a finding, the finding's own `fix:` is the next command.
|
|
303
306
|
const table = names(isEntityKind(kind) ? name : (featureFlag ?? name)).table;
|
|
304
|
-
|
|
307
|
+
// `cronJob()` pins its schedule as `<kebab name>.schedule` (`templates/generate-cron-job.ts`).
|
|
308
|
+
const schedule = kind === 'cron-job' ? `${kebab(name)}.schedule` : undefined;
|
|
309
|
+
const next = findings.length === 0 ? nextSteps(report.written, edited, table, schedule) : [];
|
|
305
310
|
return {
|
|
306
311
|
ok: findings.length === 0,
|
|
307
312
|
command: 'g',
|
|
@@ -340,9 +345,13 @@ export function nextSteps(
|
|
|
340
345
|
written: readonly string[],
|
|
341
346
|
edited: readonly string[],
|
|
342
347
|
table: string,
|
|
348
|
+
/** A schedule this run declared (`x g cron-job`): fired now, so its first run is not its cron. */
|
|
349
|
+
task?: string,
|
|
343
350
|
): readonly string[] {
|
|
344
|
-
|
|
351
|
+
const fire = task === undefined ? [] : [`bunx x tasks run ${quoteArg(task)}`];
|
|
352
|
+
if (!written.some(isEntityModule)) return fire;
|
|
345
353
|
return [
|
|
354
|
+
...fire,
|
|
346
355
|
...(edited.some((path) => path.endsWith('package.json')) ? ['bun install'] : []),
|
|
347
356
|
`bunx x db gen "create ${table}"`,
|
|
348
357
|
'bunx x db migrate',
|
|
@@ -373,7 +382,11 @@ async function readSliceErrors(
|
|
|
373
382
|
slice: string,
|
|
374
383
|
name: 'errors.ts' | 'policy.ts' = 'errors.ts',
|
|
375
384
|
): Promise<string | undefined> {
|
|
376
|
-
|
|
385
|
+
// A read imports its rule from `policy.ts` too (`can<Feature>Read`), and throws nothing of the
|
|
386
|
+
// slice's — so `query` reads the policy and never the errors.
|
|
387
|
+
const reads =
|
|
388
|
+
kind === 'action' || kind === 'mutator' || (kind === 'query' && name === 'policy.ts');
|
|
389
|
+
if (!reads) return undefined;
|
|
377
390
|
const file = containedPath(root, `${slice}/${name}`);
|
|
378
391
|
return existsSync(file) ? await Bun.file(file).text() : undefined;
|
|
379
392
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// `x heavy`'s declaration, apart from its body: the parser, `x help` and the `errors` step read it
|
|
2
|
+
// without loading `cmd-heavy.ts`, which `registry.ts` imports only when the command runs.
|
|
3
|
+
|
|
4
|
+
import type { CommandSpec } from './parse';
|
|
5
|
+
|
|
6
|
+
export const heavySpec: CommandSpec = {
|
|
7
|
+
name: 'heavy',
|
|
8
|
+
summary:
|
|
9
|
+
"the machine's shared memory ledger: who is running, who is queued, and any command run inside it",
|
|
10
|
+
usage:
|
|
11
|
+
'x heavy [status] [--json] · x heavy run [--label s] [--mb n] [--max-wait s] [--json] -- <command…>',
|
|
12
|
+
subcommands: ['status', 'run'],
|
|
13
|
+
defaultSubcommand: 'status',
|
|
14
|
+
passthrough: true,
|
|
15
|
+
flags: [
|
|
16
|
+
{
|
|
17
|
+
name: 'label',
|
|
18
|
+
type: 'string',
|
|
19
|
+
summary: 'what other waiters see this run called (default: the command)',
|
|
20
|
+
subcommands: ['run'],
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: 'mb',
|
|
24
|
+
type: 'string',
|
|
25
|
+
summary: 'the memory to reserve, its measured peak (default: a whole-repo typecheck)',
|
|
26
|
+
subcommands: ['run'],
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: 'max-wait',
|
|
30
|
+
type: 'string',
|
|
31
|
+
summary:
|
|
32
|
+
'seconds to stay queued before giving up with X_HEAVY_WAIT_EXPIRED, exit 75 (default: wait)',
|
|
33
|
+
subcommands: ['run'],
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
};
|
package/src/cmd-heavy.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// `x heavy`: the machine ledger as a command. `status` is a read that changes nothing; `run` puts any
|
|
2
|
+
// command inside one reservation, passes its exit code through and forwards stop signals, which is
|
|
3
|
+
// how a repository with no Ultimate in it shares the same queue.
|
|
4
|
+
|
|
5
|
+
// why: Bun exposes no load average, core count or total-memory primitive.
|
|
6
|
+
import { availableParallelism, loadavg, totalmem } from 'node:os';
|
|
7
|
+
import { heavySpec } from './cmd-heavy-spec';
|
|
8
|
+
import type { CliCommand, CommandContext } from './command';
|
|
9
|
+
import { MissingPositionalError } from './errors';
|
|
10
|
+
import { readIntFlag } from './flag-number';
|
|
11
|
+
import { describeEntry, heavyState, withHeavy } from './heavy';
|
|
12
|
+
import { coreCap, runnableTasks } from './heavy-cores';
|
|
13
|
+
import { HeavyCommandNotFoundError, HeavyWaitExpiredError } from './heavy-errors';
|
|
14
|
+
import { holderPids, isProcessRunning } from './heavy-holders';
|
|
15
|
+
import type { LedgerEntry, LedgerHold } from './heavy-ledger';
|
|
16
|
+
import { readLedger } from './heavy-ledger';
|
|
17
|
+
import { availableMemoryBytes, usableMemoryBytes } from './heavy-memory';
|
|
18
|
+
import { cpuGateOn, cpuText, readCpuPressure } from './heavy-pressure';
|
|
19
|
+
import { DEFAULT_RUN_MB } from './heavy-weights';
|
|
20
|
+
import type { HandOffChild } from './local-cli-handoff';
|
|
21
|
+
import { handOff } from './local-cli-handoff';
|
|
22
|
+
import { msg } from './messages';
|
|
23
|
+
import type { CommandResult, JsonValue } from './output';
|
|
24
|
+
import { findingFrom } from './output';
|
|
25
|
+
import { flagString } from './parse';
|
|
26
|
+
|
|
27
|
+
/** Read through `readIntFlag`, which refuses `5m`, `NaN` and a fraction with the flag's name. */
|
|
28
|
+
const MAX_WAIT_FLAG = 'max-wait';
|
|
29
|
+
|
|
30
|
+
const gb = (bytes: number): string => (bytes / 2 ** 30).toFixed(1);
|
|
31
|
+
const MAX_RUN_MB = 1024 * 1024;
|
|
32
|
+
const MAX_WAIT_SECONDS = 7 * 24 * 60 * 60;
|
|
33
|
+
/** `EX_TEMPFAIL`: never the command's own answer, because the command never ran. */
|
|
34
|
+
const WAIT_EXPIRED_EXIT = 75;
|
|
35
|
+
|
|
36
|
+
/** A reservation by the processes keeping it: its wrapper may be gone while its command runs on. */
|
|
37
|
+
async function holdProcesses(hold: LedgerHold, ctx: CommandContext) {
|
|
38
|
+
const pids = await holderPids(hold.file, ctx.runner, ctx.cwd);
|
|
39
|
+
return {
|
|
40
|
+
wrapperAlive: pids === undefined ? isProcessRunning(hold.pid) : pids.includes(hold.pid),
|
|
41
|
+
childPids: (pids ?? []).filter((pid) => pid !== hold.pid),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
type HoldProcesses = Awaited<ReturnType<typeof holdProcesses>>;
|
|
46
|
+
|
|
47
|
+
const pidsText = (hold: LedgerHold, processes: HoldProcesses): string =>
|
|
48
|
+
[
|
|
49
|
+
msg(processes.wrapperAlive ? 'cli.heavy.status.wrapper' : 'cli.heavy.status.wrapperGone', {
|
|
50
|
+
pid: hold.pid,
|
|
51
|
+
}),
|
|
52
|
+
...(processes.childPids.length === 0
|
|
53
|
+
? []
|
|
54
|
+
: [msg('cli.heavy.status.command', { pids: processes.childPids.join(', ') })]),
|
|
55
|
+
].join(', ');
|
|
56
|
+
|
|
57
|
+
async function status(ctx: CommandContext): Promise<CommandResult> {
|
|
58
|
+
const state = await heavyState({ ...ctx.env, ULTIMATE_HEAVY_HOLD: undefined });
|
|
59
|
+
const now = Date.now();
|
|
60
|
+
const view = state.on ? readLedger(state.dir, state.tryLock) : { holds: [], queue: [] };
|
|
61
|
+
const cores = coreCap(ctx.env);
|
|
62
|
+
const coreText = cores === undefined ? String(availableParallelism()) : String(cores);
|
|
63
|
+
const usedMb = view.holds.reduce((sum, entry) => sum + entry.mb, 0);
|
|
64
|
+
const [one = 0, five = 0, fifteen = 0] = loadavg();
|
|
65
|
+
const cpu = readCpuPressure();
|
|
66
|
+
// The gate applies where admission does; the reading is shown either way.
|
|
67
|
+
const cpuGate = state.on && cpuGateOn(ctx.env);
|
|
68
|
+
const machine = {
|
|
69
|
+
cores: availableParallelism(),
|
|
70
|
+
runnable: runnableTasks(),
|
|
71
|
+
load: { one, five, fifteen },
|
|
72
|
+
totalBytes: usableMemoryBytes(totalmem()),
|
|
73
|
+
availableBytes: availableMemoryBytes(),
|
|
74
|
+
cpu: { ...cpu },
|
|
75
|
+
};
|
|
76
|
+
const processes = await Promise.all(view.holds.map((hold) => holdProcesses(hold, ctx)));
|
|
77
|
+
const processesOf = (at: number): HoldProcesses =>
|
|
78
|
+
processes[at] ?? { wrapperAlive: false, childPids: [] };
|
|
79
|
+
const entryJson = (entry: LedgerEntry): JsonValue => ({
|
|
80
|
+
pid: entry.pid,
|
|
81
|
+
mb: entry.mb,
|
|
82
|
+
label: entry.label,
|
|
83
|
+
cwd: entry.cwd,
|
|
84
|
+
ageMs: Math.max(0, now - entry.at),
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
ok: true,
|
|
88
|
+
command: 'heavy',
|
|
89
|
+
summary: state.on
|
|
90
|
+
? msg('cli.heavy.status.summary', {
|
|
91
|
+
used: usedMb,
|
|
92
|
+
capacity: state.capacityMb,
|
|
93
|
+
holds: view.holds.length,
|
|
94
|
+
queued: view.queue.length,
|
|
95
|
+
cores: coreText,
|
|
96
|
+
})
|
|
97
|
+
: msg('cli.heavy.status.off', { reason: state.detail ?? state.off, cores: coreText }),
|
|
98
|
+
lines: [
|
|
99
|
+
msg('cli.heavy.status.machine', {
|
|
100
|
+
load: one.toFixed(1),
|
|
101
|
+
cpus: machine.cores,
|
|
102
|
+
runnable: machine.runnable,
|
|
103
|
+
free: gb(machine.availableBytes),
|
|
104
|
+
total: gb(machine.totalBytes),
|
|
105
|
+
}),
|
|
106
|
+
cpuGate
|
|
107
|
+
? msg('cli.heavy.status.cpu', { cpu: cpuText(cpu) })
|
|
108
|
+
: msg('cli.heavy.status.cpuOff', {
|
|
109
|
+
cpu: cpuText(cpu),
|
|
110
|
+
reason: state.on ? 'a test run, or ULTIMATE_HEAVY_CPU=0' : state.off,
|
|
111
|
+
}),
|
|
112
|
+
...view.holds.map((entry, at) =>
|
|
113
|
+
msg('cli.heavy.status.hold', {
|
|
114
|
+
entry: describeEntry(entry, now),
|
|
115
|
+
pids: pidsText(entry, processesOf(at)),
|
|
116
|
+
}),
|
|
117
|
+
),
|
|
118
|
+
...view.queue.map((entry, at) =>
|
|
119
|
+
msg('cli.heavy.status.queued', { position: at + 1, entry: describeEntry(entry, now) }),
|
|
120
|
+
),
|
|
121
|
+
],
|
|
122
|
+
data: {
|
|
123
|
+
enabled: state.on,
|
|
124
|
+
...(state.on
|
|
125
|
+
? { dir: state.dir, capacityMb: state.capacityMb, usedMb }
|
|
126
|
+
: { off: state.off, ...(state.detail === undefined ? {} : { detail: state.detail }) }),
|
|
127
|
+
coreCap: cores ?? null,
|
|
128
|
+
cpuGate,
|
|
129
|
+
machine,
|
|
130
|
+
holds: view.holds.map((entry, at): JsonValue => {
|
|
131
|
+
const { wrapperAlive, childPids } = processesOf(at);
|
|
132
|
+
return {
|
|
133
|
+
...(entryJson(entry) as Record<string, JsonValue>),
|
|
134
|
+
wrapperAlive,
|
|
135
|
+
childPids: [...childPids],
|
|
136
|
+
};
|
|
137
|
+
}),
|
|
138
|
+
queue: view.queue.map(entryJson),
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const seconds = (ms: number): string => `${(ms / 1000).toFixed(1)}s`;
|
|
144
|
+
|
|
145
|
+
async function run(ctx: CommandContext): Promise<CommandResult> {
|
|
146
|
+
const command = ctx.args.passthrough;
|
|
147
|
+
const first = command[0];
|
|
148
|
+
if (first === undefined || ctx.args.positionals.length > 0) {
|
|
149
|
+
throw new MissingPositionalError({
|
|
150
|
+
command: 'heavy run',
|
|
151
|
+
positional: '-- command',
|
|
152
|
+
example: 'x heavy run --label tests -- bun test',
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
// Under `--json` fd 1 is this command's one document, so the child's stdout moves to fd 2.
|
|
156
|
+
const stream = ctx.args.json ? undefined : ('stderr' as const);
|
|
157
|
+
const envelope = { command: 'heavy', ...(stream === undefined ? {} : { stream }) };
|
|
158
|
+
const path = ctx.env['PATH'];
|
|
159
|
+
if (Bun.which(first, { cwd: ctx.cwd, ...(path === undefined ? {} : { PATH: path }) }) === null) {
|
|
160
|
+
const finding = findingFrom(new HeavyCommandNotFoundError(first));
|
|
161
|
+
return { ...envelope, ok: false, summary: finding.cause, findings: [finding], exitCode: 127 };
|
|
162
|
+
}
|
|
163
|
+
const mb = readIntFlag(ctx.args, {
|
|
164
|
+
name: 'mb',
|
|
165
|
+
command: 'heavy',
|
|
166
|
+
min: 1,
|
|
167
|
+
max: MAX_RUN_MB,
|
|
168
|
+
example: 'x heavy run --mb 4096 -- bun test',
|
|
169
|
+
});
|
|
170
|
+
const maxWait = readIntFlag(ctx.args, {
|
|
171
|
+
name: MAX_WAIT_FLAG,
|
|
172
|
+
command: 'heavy',
|
|
173
|
+
min: 0,
|
|
174
|
+
max: MAX_WAIT_SECONDS,
|
|
175
|
+
example: 'x heavy run --max-wait 300 -- bun test',
|
|
176
|
+
});
|
|
177
|
+
const label = flagString(ctx.args, 'label') ?? command.slice(0, 3).join(' ');
|
|
178
|
+
const queued = performance.now();
|
|
179
|
+
let startedAt = queued;
|
|
180
|
+
const request = {
|
|
181
|
+
label,
|
|
182
|
+
cwd: ctx.cwd,
|
|
183
|
+
env: ctx.env,
|
|
184
|
+
unitMb: mb ?? DEFAULT_RUN_MB,
|
|
185
|
+
...(maxWait === undefined ? {} : { maxWaitMs: maxWait * 1000 }),
|
|
186
|
+
};
|
|
187
|
+
const admitted = withHeavy(request, (child) => {
|
|
188
|
+
startedAt = performance.now();
|
|
189
|
+
return handOff(
|
|
190
|
+
command,
|
|
191
|
+
{ ...ctx.env, ...child.env },
|
|
192
|
+
{
|
|
193
|
+
spawn: (argv, env): HandOffChild =>
|
|
194
|
+
// Untimed: the child is the command the caller asked to run, and its stop signals are
|
|
195
|
+
// forwarded by `handOff`. Counted under `cli`'s spawn-timeout pin with that hand-off.
|
|
196
|
+
Bun.spawn([...argv], {
|
|
197
|
+
cwd: ctx.cwd,
|
|
198
|
+
env,
|
|
199
|
+
// The reservation's lock goes with it: killing this wrapper frees nothing the
|
|
200
|
+
// command is still using.
|
|
201
|
+
stdio: ['inherit', ctx.args.json ? 2 : 'inherit', 'inherit', ...child.fds],
|
|
202
|
+
}),
|
|
203
|
+
},
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
let code: number;
|
|
207
|
+
try {
|
|
208
|
+
code = await admitted;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
if (!(error instanceof HeavyWaitExpiredError)) throw error;
|
|
211
|
+
const finding = findingFrom(error);
|
|
212
|
+
return {
|
|
213
|
+
...envelope,
|
|
214
|
+
ok: false,
|
|
215
|
+
summary: finding.cause,
|
|
216
|
+
findings: [finding],
|
|
217
|
+
exitCode: WAIT_EXPIRED_EXIT,
|
|
218
|
+
data: {
|
|
219
|
+
exitCode: WAIT_EXPIRED_EXIT,
|
|
220
|
+
started: false,
|
|
221
|
+
waitedMs: Math.round(performance.now() - queued),
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
const ranMs = performance.now() - startedAt;
|
|
226
|
+
const waitedMs = startedAt - queued;
|
|
227
|
+
return {
|
|
228
|
+
...envelope,
|
|
229
|
+
ok: code === 0,
|
|
230
|
+
summary: msg('cli.heavy.run.done', {
|
|
231
|
+
command: label,
|
|
232
|
+
code,
|
|
233
|
+
ran: seconds(ranMs),
|
|
234
|
+
waited: seconds(waitedMs),
|
|
235
|
+
}),
|
|
236
|
+
exitCode: code,
|
|
237
|
+
data: { exitCode: code, ranMs: Math.round(ranMs), waitedMs: Math.round(waitedMs) },
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export const heavyCommand: CliCommand = {
|
|
242
|
+
spec: heavySpec,
|
|
243
|
+
run: (ctx) => (ctx.args.subcommand === 'run' ? run(ctx) : status(ctx)),
|
|
244
|
+
};
|
package/src/cmd-jobs.ts
CHANGED
|
@@ -20,6 +20,7 @@ import { requireAppRoot } from './app-root';
|
|
|
20
20
|
import { runResolveStep } from './cmd-jobs-resolve';
|
|
21
21
|
import { jobsSpec } from './cmd-jobs-spec';
|
|
22
22
|
import type { CliCommand, CommandContext } from './command';
|
|
23
|
+
import { refuseEmbeddedWriteBesideDev } from './dev-lock-live';
|
|
23
24
|
import { BadFlagError, JobUnknownError, MissingPositionalError } from './errors';
|
|
24
25
|
import { withJobDriver } from './jobs-driver';
|
|
25
26
|
import {
|
|
@@ -31,6 +32,7 @@ import {
|
|
|
31
32
|
pausedToJson,
|
|
32
33
|
workerToJson,
|
|
33
34
|
} from './jobs-json';
|
|
35
|
+
import { assertQueueServed } from './jobs-queue-known';
|
|
34
36
|
import { listJobs, retryJob, showJob } from './jobs-report';
|
|
35
37
|
import { renderJobTable } from './jobs-table';
|
|
36
38
|
import { msg } from './messages';
|
|
@@ -239,8 +241,16 @@ async function runPause(
|
|
|
239
241
|
driver: JobDriver,
|
|
240
242
|
ctx: CommandContext,
|
|
241
243
|
sub: 'pause' | 'resume',
|
|
244
|
+
root: string,
|
|
242
245
|
): Promise<CommandResult> {
|
|
243
246
|
const queue = requireQueuePositional(ctx, sub);
|
|
247
|
+
const before = (await driver.introspect?.pausedQueues()) ?? [];
|
|
248
|
+
await assertQueueServed(
|
|
249
|
+
root,
|
|
250
|
+
sub,
|
|
251
|
+
queue,
|
|
252
|
+
before.map((one) => one.name),
|
|
253
|
+
);
|
|
244
254
|
const paused = await (sub === 'pause' ? pauseQueue(driver, queue) : resumeQueue(driver, queue));
|
|
245
255
|
return {
|
|
246
256
|
ok: true,
|
|
@@ -253,6 +263,17 @@ async function runPause(
|
|
|
253
263
|
/** The subcommands that answer a `JobTrace`. `list`, `delete`, `promote` and the rest read rows only. */
|
|
254
264
|
const TRACE_SUBCOMMANDS: ReadonlySet<string> = new Set(['show', 'retry', 'cancel', 'resolve-step']);
|
|
255
265
|
|
|
266
|
+
/** Every verb but `list` and `show`: each changes a row the workers read. */
|
|
267
|
+
const WRITING_SUBCOMMANDS: ReadonlySet<string> = new Set([
|
|
268
|
+
'retry',
|
|
269
|
+
'cancel',
|
|
270
|
+
'delete',
|
|
271
|
+
'promote',
|
|
272
|
+
'resolve-step',
|
|
273
|
+
'pause',
|
|
274
|
+
'resume',
|
|
275
|
+
]);
|
|
276
|
+
|
|
256
277
|
export const jobsCommand: CliCommand = {
|
|
257
278
|
spec: jobsSpec,
|
|
258
279
|
async run(ctx: CommandContext): Promise<CommandResult> {
|
|
@@ -264,6 +285,11 @@ export const jobsCommand: CliCommand = {
|
|
|
264
285
|
// every app. The findings are deliberately dropped: a trace is read to debug a stuck queue,
|
|
265
286
|
// so an app that half-loads degrades those two fields and still answers.
|
|
266
287
|
if (TRACE_SUBCOMMANDS.has(sub)) await loadApp(root);
|
|
288
|
+
// A verb that writes a queue row, through this command's own boot: beside an `x dev` holding
|
|
289
|
+
// the embedded database it is a second PGlite writer, and the holder's next write undoes it.
|
|
290
|
+
if (WRITING_SUBCOMMANDS.has(sub)) {
|
|
291
|
+
await refuseEmbeddedWriteBesideDev(root, ctx.env, ['jobs', sub, ...ctx.args.positionals]);
|
|
292
|
+
}
|
|
267
293
|
return withJobDriver(root, ctx, (driver) => {
|
|
268
294
|
if (sub === 'show') return runShow(driver, ctx);
|
|
269
295
|
if (sub === 'retry') return runRetry(driver, ctx);
|
|
@@ -273,7 +299,7 @@ export const jobsCommand: CliCommand = {
|
|
|
273
299
|
if (sub === 'resolve-step') {
|
|
274
300
|
return runResolveStep(driver, ctx, requireIdPositional(ctx, sub, driver));
|
|
275
301
|
}
|
|
276
|
-
if (sub === 'pause' || sub === 'resume') return runPause(driver, ctx, sub);
|
|
302
|
+
if (sub === 'pause' || sub === 'resume') return runPause(driver, ctx, sub, root);
|
|
277
303
|
return runList(driver, ctx);
|
|
278
304
|
});
|
|
279
305
|
},
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// `x merge-driver`'s declaration, apart from its body: the parser, `x help` and the `errors` step
|
|
2
|
+
// read it without loading `cmd-merge-driver.ts`, which `registry.ts` imports only when it runs.
|
|
3
|
+
|
|
4
|
+
import type { CommandSpec } from './parse';
|
|
5
|
+
|
|
6
|
+
export const mergeDriverSpec: CommandSpec = {
|
|
7
|
+
name: 'merge-driver',
|
|
8
|
+
summary: 'merges what parallel branches added to the registries; git runs it, setup installs it',
|
|
9
|
+
usage: 'x merge-driver install [--json] · x merge-driver run <base> <ours> <theirs> <path>',
|
|
10
|
+
subcommands: ['install', 'run'],
|
|
11
|
+
flags: [],
|
|
12
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// `x merge-driver`: `install` writes the two git drivers into this clone, `run` is the one git
|
|
2
|
+
// calls for a registry both sides changed. Exit 0 is "merged"; anything else leaves the file holding
|
|
3
|
+
// its remaining conflict markers and git reporting it as conflicted, as for any text merge.
|
|
4
|
+
|
|
5
|
+
import { ERROR_DOCS_URL } from '@ultimat3/core';
|
|
6
|
+
import { mergeDriverSpec } from './cmd-merge-driver-spec';
|
|
7
|
+
import type { CliCommand, CommandContext } from './command';
|
|
8
|
+
import { failed, ok } from './command';
|
|
9
|
+
import { MissingPositionalError } from './errors';
|
|
10
|
+
import { mergeCatalogText } from './merge-driver-catalog';
|
|
11
|
+
import { driverConfig, gitPrefix } from './merge-driver-config';
|
|
12
|
+
import { conflictsLeft, resolveAdditions } from './merge-driver-lines';
|
|
13
|
+
import { UNMERGED_FIX } from './merge-state';
|
|
14
|
+
import type { CommandResult, Finding } from './output';
|
|
15
|
+
|
|
16
|
+
const NAME = mergeDriverSpec.name;
|
|
17
|
+
|
|
18
|
+
async function install(ctx: CommandContext): Promise<CommandResult> {
|
|
19
|
+
const prefix = await gitPrefix(ctx.cwd, ctx.runner);
|
|
20
|
+
if (prefix === undefined) {
|
|
21
|
+
return ok(NAME, 'not a git checkout: no merge drivers to install', {
|
|
22
|
+
data: { installed: false },
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
for (const [key, value] of driverConfig(prefix)) {
|
|
26
|
+
const set = await ctx.runner(['git', 'config', '--local', key, value], { cwd: ctx.cwd });
|
|
27
|
+
if (!set.ok) {
|
|
28
|
+
return failed(NAME, `could not set ${key}`, {
|
|
29
|
+
findings: [
|
|
30
|
+
{
|
|
31
|
+
code: 'X_MERGE_DRIVER_UNSET',
|
|
32
|
+
cause: `git config refused ${key}: ${set.stderr.trim()}`,
|
|
33
|
+
fix: 'bunx x merge-driver install',
|
|
34
|
+
docs: ERROR_DOCS_URL,
|
|
35
|
+
at: '.git/config',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return ok(NAME, 'merge drivers installed for this clone', { data: { installed: true } });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const conflictFinding = (path: string, cause: string): Finding => ({
|
|
45
|
+
code: 'X_MERGE_UNRESOLVED',
|
|
46
|
+
cause,
|
|
47
|
+
fix: UNMERGED_FIX,
|
|
48
|
+
docs: ERROR_DOCS_URL,
|
|
49
|
+
at: path,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
async function run(ctx: CommandContext): Promise<CommandResult> {
|
|
53
|
+
const [base, ours, theirs, path] = ctx.args.positionals;
|
|
54
|
+
if (base === undefined || ours === undefined || theirs === undefined || path === undefined) {
|
|
55
|
+
throw new MissingPositionalError({
|
|
56
|
+
command: 'merge-driver run',
|
|
57
|
+
positional: 'base> <ours> <theirs> <path',
|
|
58
|
+
example: 'bunx x merge-driver install # git passes these four itself, once installed',
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
const text = (file: string): Promise<string> => Bun.file(file).text();
|
|
62
|
+
if (path.endsWith('.json')) {
|
|
63
|
+
const merged = mergeCatalogText(await text(base), await text(ours), await text(theirs));
|
|
64
|
+
if (merged !== undefined) {
|
|
65
|
+
await Bun.write(ours, merged);
|
|
66
|
+
return ok(NAME, `${path}: merged both sides' keys`, { data: { path, conflicts: 0 } });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const labels = ['-L', 'ours', '-L', 'base', '-L', 'theirs'];
|
|
70
|
+
const argv = ['git', 'merge-file', '-p', '--diff3', ...labels, ours, base, theirs];
|
|
71
|
+
const merge = await ctx.runner(argv, { cwd: ctx.cwd });
|
|
72
|
+
// A positive exit is how many conflicts it left; git's own failures are negative, so 255 here.
|
|
73
|
+
if (merge.code < 0 || merge.code > 127) {
|
|
74
|
+
return failed(NAME, `${path}: git merge-file failed`, {
|
|
75
|
+
findings: [conflictFinding(path, `git merge-file refused ${path}: ${merge.stderr.trim()}`)],
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const resolved = path.endsWith('.json')
|
|
79
|
+
? { text: merge.stdout, conflicts: merge.code }
|
|
80
|
+
: resolveAdditions(merge.stdout);
|
|
81
|
+
const result = resolved.text;
|
|
82
|
+
// Never exit 0 over a marker: git then records a clean merge of a file that still holds one.
|
|
83
|
+
const conflicts = conflictsLeft(result, resolved.conflicts, merge.code);
|
|
84
|
+
await Bun.write(ours, result);
|
|
85
|
+
if (conflicts === 0) {
|
|
86
|
+
return ok(NAME, `${path}: merged both sides' additions`, { data: { path, conflicts } });
|
|
87
|
+
}
|
|
88
|
+
const cause = `${path}: both branches changed the same existing lines (${conflicts} conflict(s))`;
|
|
89
|
+
return failed(NAME, cause, { findings: [conflictFinding(path, cause)] });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const mergeDriverCommand: CliCommand = {
|
|
93
|
+
spec: mergeDriverSpec,
|
|
94
|
+
run: (ctx) => (ctx.args.subcommand === 'install' ? install(ctx) : run(ctx)),
|
|
95
|
+
};
|