kodingo-cli 1.4.1 → 1.4.2
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.
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/** Validation fixtures only; no production or customer identifiers.
|
|
7
|
+
* Covers the "never scan outdated code" guard: a full-tree scan (init's
|
|
8
|
+
* first scan, or `kodingo deep-scan` without --changed) must refuse when
|
|
9
|
+
* HEAD is behind, ahead of, or diverged from origin/<branch> — checked via
|
|
10
|
+
* a real `git fetch` against a real local bare repo acting as origin, with
|
|
11
|
+
* no network involved. --changed (the hook's exact invocation) must never
|
|
12
|
+
* be affected, and --dry-run must only warn, never refuse, since it uploads
|
|
13
|
+
* nothing.
|
|
14
|
+
*/
|
|
15
|
+
const strict_1 = __importDefault(require("node:assert/strict"));
|
|
16
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
17
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
18
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
19
|
+
const node_child_process_1 = require("node:child_process");
|
|
20
|
+
const node_test_1 = require("node:test");
|
|
21
|
+
const deep_scan_1 = require("../commands/deep-scan");
|
|
22
|
+
function fakeFetch(responses) {
|
|
23
|
+
const calls = [];
|
|
24
|
+
const fn = async (url, init) => {
|
|
25
|
+
calls.push({ url, init });
|
|
26
|
+
const next = responses.shift();
|
|
27
|
+
if (!next)
|
|
28
|
+
throw new Error(`checkoutFreshnessGuard test: no mock response left for ${url}`);
|
|
29
|
+
return { ok: (next.status ?? 200) < 300, status: next.status ?? 200, json: async () => next.body };
|
|
30
|
+
};
|
|
31
|
+
return { fn, calls };
|
|
32
|
+
}
|
|
33
|
+
function withFetch(responses, run) {
|
|
34
|
+
const original = global.fetch;
|
|
35
|
+
const { fn, calls } = fakeFetch(responses);
|
|
36
|
+
global.fetch = fn;
|
|
37
|
+
return run(calls).finally(() => { global.fetch = original; });
|
|
38
|
+
}
|
|
39
|
+
function withConsoleCapture(run) {
|
|
40
|
+
const logs = { log: [], warn: [] };
|
|
41
|
+
const originalLog = console.log;
|
|
42
|
+
const originalWarn = console.warn;
|
|
43
|
+
console.log = (...args) => { logs.log.push(args.join(" ")); };
|
|
44
|
+
console.warn = (...args) => { logs.warn.push(args.join(" ")); };
|
|
45
|
+
return run(logs).finally(() => { console.log = originalLog; console.warn = originalWarn; });
|
|
46
|
+
}
|
|
47
|
+
function configureIdentity(root) {
|
|
48
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.email", "test@example.invalid"]);
|
|
49
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.name", "Validation Test"]);
|
|
50
|
+
}
|
|
51
|
+
/** A real local bare repo as origin, and a clone (the checkout under test)
|
|
52
|
+
* pushed up to date with it — no network involved anywhere. */
|
|
53
|
+
function makeRepoWithOrigin() {
|
|
54
|
+
const originRoot = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-freshness-origin-"));
|
|
55
|
+
(0, node_child_process_1.execFileSync)("git", ["init", "-q", "--bare", "-b", "main", originRoot]);
|
|
56
|
+
const root = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-freshness-work-"));
|
|
57
|
+
(0, node_child_process_1.execFileSync)("git", ["clone", "-q", originRoot, root]);
|
|
58
|
+
configureIdentity(root);
|
|
59
|
+
node_fs_1.default.writeFileSync(node_path_1.default.join(root, "page.ts"), "export function Page() { return 1; }");
|
|
60
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "."]);
|
|
61
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "commit", "-qm", "fixture"]);
|
|
62
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "push", "-q", "origin", "main"]);
|
|
63
|
+
return { root, originRoot };
|
|
64
|
+
}
|
|
65
|
+
/** Pushes one more commit to origin from a throwaway second clone, so the
|
|
66
|
+
* fixture's `root` checkout falls behind without `root` itself touching it. */
|
|
67
|
+
function pushExtraCommitToOrigin(originRoot) {
|
|
68
|
+
const second = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-freshness-second-"));
|
|
69
|
+
(0, node_child_process_1.execFileSync)("git", ["clone", "-q", originRoot, second]);
|
|
70
|
+
configureIdentity(second);
|
|
71
|
+
node_fs_1.default.writeFileSync(node_path_1.default.join(second, "extra.ts"), "export const extra = 1;\n");
|
|
72
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", second, "add", "."]);
|
|
73
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", second, "commit", "-qm", "extra, pushed from elsewhere"]);
|
|
74
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", second, "push", "-q", "origin", "main"]);
|
|
75
|
+
}
|
|
76
|
+
function commitLocallyWithoutPushing(root) {
|
|
77
|
+
node_fs_1.default.writeFileSync(node_path_1.default.join(root, "local-only.ts"), "export const localOnly = 1;\n");
|
|
78
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "."]);
|
|
79
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "commit", "-qm", "unpushed local work"]);
|
|
80
|
+
}
|
|
81
|
+
// A function, not a shared array constant — fakeFetch's queue mutates
|
|
82
|
+
// (shift()s) whatever array it's given, so a plain shared constant would be
|
|
83
|
+
// drained by the first test that uses it and come up empty for the next.
|
|
84
|
+
function fullScanResponses() {
|
|
85
|
+
return [
|
|
86
|
+
{ body: { excerptsEnabled: true } }, // GET /deep-scan/policy
|
|
87
|
+
{ body: { sessionId: "sess-1", heldBlobShas: [], limits: { filesPerBatch: 200 } } }, // POST /deep-scan/sessions
|
|
88
|
+
{ body: { accepted: 1 } }, // POST /deep-scan/sessions/:id/files
|
|
89
|
+
{ body: { runId: "run-1" } }, // POST /deep-scan/sessions/:id/finalize
|
|
90
|
+
{ body: { state: "completed", applied: true, entity_count: 1, edge_count: 0, file_count: 1 } }, // GET /deep-scan/runs/:id
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
(0, node_test_1.describe)("checkout freshness guard — full-tree scans only", () => {
|
|
94
|
+
let oldToken;
|
|
95
|
+
(0, node_test_1.beforeEach)(() => { oldToken = process.env.KODINGO_TOKEN; process.env.KODINGO_TOKEN = "validation-token"; });
|
|
96
|
+
(0, node_test_1.afterEach)(() => { if (oldToken === undefined)
|
|
97
|
+
delete process.env.KODINGO_TOKEN;
|
|
98
|
+
else
|
|
99
|
+
process.env.KODINGO_TOKEN = oldToken; });
|
|
100
|
+
(0, node_test_1.it)("up to date: proceeds and prints the summary line, via a real git fetch against a real origin", async () => {
|
|
101
|
+
const { root } = makeRepoWithOrigin();
|
|
102
|
+
await withConsoleCapture((logs) => withFetch(fullScanResponses(), async (calls) => {
|
|
103
|
+
await strict_1.default.doesNotReject(() => (0, deep_scan_1.deepScanCommand)({ repo: root, yes: true }));
|
|
104
|
+
strict_1.default.equal(calls.length, 5, `expected the normal full-scan call sequence, got: ${JSON.stringify(calls.map((c) => c.url))}`);
|
|
105
|
+
strict_1.default.ok(logs.log.some((l) => /^Reading main at [0-9a-f]+, up to date with origin\/main\.$/.test(l)), `expected the up-to-date summary line, got: ${JSON.stringify(logs.log)}`);
|
|
106
|
+
}));
|
|
107
|
+
});
|
|
108
|
+
(0, node_test_1.it)("behind: refuses before any Kortex API call is made", async () => {
|
|
109
|
+
const { root, originRoot } = makeRepoWithOrigin();
|
|
110
|
+
pushExtraCommitToOrigin(originRoot);
|
|
111
|
+
await withFetch([], async (calls) => {
|
|
112
|
+
await strict_1.default.rejects(() => (0, deep_scan_1.deepScanCommand)({ repo: root, yes: true }), /Your checkout of main is 1 commit behind origin\/main, so Kortex would read outdated code\. Run `git pull` and try again\./);
|
|
113
|
+
strict_1.default.equal(calls.length, 0, `expected zero Kortex API calls, got: ${JSON.stringify(calls.map((c) => c.url))}`);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
(0, node_test_1.it)("ahead: refuses with the unpushed-commits message", async () => {
|
|
117
|
+
const { root } = makeRepoWithOrigin();
|
|
118
|
+
commitLocallyWithoutPushing(root);
|
|
119
|
+
await withFetch([], async (calls) => {
|
|
120
|
+
await strict_1.default.rejects(() => (0, deep_scan_1.deepScanCommand)({ repo: root, yes: true }), /Your checkout of main has 1 commit that is not pushed\. Push first so Kortex reads the same code GitHub has\./);
|
|
121
|
+
strict_1.default.equal(calls.length, 0);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
(0, node_test_1.it)("diverged: refuses and reports both counts", async () => {
|
|
125
|
+
const { root, originRoot } = makeRepoWithOrigin();
|
|
126
|
+
pushExtraCommitToOrigin(originRoot);
|
|
127
|
+
commitLocallyWithoutPushing(root);
|
|
128
|
+
await withFetch([], async (calls) => {
|
|
129
|
+
await strict_1.default.rejects(() => (0, deep_scan_1.deepScanCommand)({ repo: root, yes: true }), /Your checkout of main has diverged from origin\/main: 1 commit not pushed and 1 commit behind\./);
|
|
130
|
+
strict_1.default.equal(calls.length, 0);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
(0, node_test_1.it)("fetch failure (no reachable origin): refuses under --yes without guessing, and makes no Kortex API call", async () => {
|
|
134
|
+
const { root } = makeRepoWithOrigin();
|
|
135
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "remote", "set-url", "origin", node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-nonexistent-origin-does-not-exist")]);
|
|
136
|
+
await withFetch([], async (calls) => {
|
|
137
|
+
await strict_1.default.rejects(() => (0, deep_scan_1.deepScanCommand)({ repo: root, yes: true }), /Couldn't check whether this checkout is up to date \(.+\)\. Refusing to guess under --yes\./);
|
|
138
|
+
strict_1.default.equal(calls.length, 0);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
(0, node_test_1.it)("fetch failure, interactive (non-TTY defaults to declining, same as answering \"no\"): refuses", async () => {
|
|
142
|
+
const { root } = makeRepoWithOrigin();
|
|
143
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "remote", "set-url", "origin", node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-nonexistent-origin-does-not-exist")]);
|
|
144
|
+
await withFetch([], async (calls) => {
|
|
145
|
+
await strict_1.default.rejects(() => (0, deep_scan_1.deepScanCommand)({ repo: root, yes: false }), /Couldn't check whether this checkout is up to date/);
|
|
146
|
+
strict_1.default.equal(calls.length, 0);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
(0, node_test_1.it)("the hook's exact invocation (--changed) never runs the freshness check, even when badly behind", async () => {
|
|
150
|
+
const { root } = makeRepoWithOrigin();
|
|
151
|
+
// A second, unpushed commit — this is "badly behind/ahead" from the
|
|
152
|
+
// freshness guard's point of view, and also gives --changed a real
|
|
153
|
+
// diff-tree parent to scope against (a root commit alone has none).
|
|
154
|
+
commitLocallyWithoutPushing(root);
|
|
155
|
+
await withFetch([
|
|
156
|
+
{ body: { outcome: "match", trackedBranch: "main" } }, // POST /deep-scan/branch/settle
|
|
157
|
+
...fullScanResponses(),
|
|
158
|
+
], async (calls) => {
|
|
159
|
+
await strict_1.default.doesNotReject(() => (0, deep_scan_1.deepScanCommand)({ repo: root, changed: true, yes: true }));
|
|
160
|
+
strict_1.default.equal(calls.length, 6);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
(0, node_test_1.it)("--dry-run: a stale checkout is reported as a warning, not a refusal, and the dry-run still completes (it uploads nothing)", async () => {
|
|
164
|
+
const { root, originRoot } = makeRepoWithOrigin();
|
|
165
|
+
pushExtraCommitToOrigin(originRoot);
|
|
166
|
+
const outFile = node_path_1.default.join(node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-freshness-dryrun-")), "payload.json");
|
|
167
|
+
await withConsoleCapture((logs) => withFetch([], async (calls) => {
|
|
168
|
+
await strict_1.default.doesNotReject(() => (0, deep_scan_1.deepScanCommand)({ repo: root, dryRun: outFile, yes: true }));
|
|
169
|
+
strict_1.default.equal(calls.length, 0, "dry-run never calls the Kortex API");
|
|
170
|
+
strict_1.default.ok(logs.warn.some((l) => /is 1 commit behind origin\/main, so Kortex would read outdated code/.test(l)), `expected a stale-checkout warning, got: ${JSON.stringify(logs.warn)}`);
|
|
171
|
+
strict_1.default.ok(node_fs_1.default.existsSync(outFile), "the dry-run payload should still be written");
|
|
172
|
+
}));
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
(0, node_test_1.describe)("decideFreshnessAction (pure)", () => {
|
|
176
|
+
const branch = "main";
|
|
177
|
+
(0, node_test_1.it)("up to date always proceeds, with the summary note", () => {
|
|
178
|
+
const decision = (0, deep_scan_1.decideFreshnessAction)({ check: { outcome: "up-to-date", shortSha: "abc1234" }, branch, dryRun: false, yes: false });
|
|
179
|
+
strict_1.default.deepEqual(decision, { action: "proceed", note: "Reading main at abc1234, up to date with origin/main." });
|
|
180
|
+
});
|
|
181
|
+
(0, node_test_1.it)("behind refuses outside dry-run", () => {
|
|
182
|
+
const decision = (0, deep_scan_1.decideFreshnessAction)({ check: { outcome: "behind", behind: 3 }, branch, dryRun: false, yes: true });
|
|
183
|
+
strict_1.default.equal(decision.action, "refuse");
|
|
184
|
+
if (decision.action === "refuse")
|
|
185
|
+
strict_1.default.match(decision.message, /is 3 commits behind origin\/main/);
|
|
186
|
+
});
|
|
187
|
+
(0, node_test_1.it)("behind warns under --dry-run instead of refusing", () => {
|
|
188
|
+
const decision = (0, deep_scan_1.decideFreshnessAction)({ check: { outcome: "behind", behind: 1 }, branch, dryRun: true, yes: false });
|
|
189
|
+
strict_1.default.equal(decision.action, "warn");
|
|
190
|
+
});
|
|
191
|
+
(0, node_test_1.it)("unknown + --yes refuses, never guessing", () => {
|
|
192
|
+
const decision = (0, deep_scan_1.decideFreshnessAction)({ check: { outcome: "unknown", reason: "offline" }, branch, dryRun: false, yes: true });
|
|
193
|
+
strict_1.default.equal(decision.action, "refuse");
|
|
194
|
+
if (decision.action === "refuse")
|
|
195
|
+
strict_1.default.match(decision.message, /Refusing to guess under --yes/);
|
|
196
|
+
});
|
|
197
|
+
(0, node_test_1.it)("unknown + interactive decline (promptAnswerYes: false) refuses", () => {
|
|
198
|
+
const decision = (0, deep_scan_1.decideFreshnessAction)({ check: { outcome: "unknown", reason: "offline" }, branch, dryRun: false, yes: false, promptAnswerYes: false });
|
|
199
|
+
strict_1.default.equal(decision.action, "refuse");
|
|
200
|
+
});
|
|
201
|
+
(0, node_test_1.it)("unknown + interactive accept (promptAnswerYes: true) proceeds", () => {
|
|
202
|
+
const decision = (0, deep_scan_1.decideFreshnessAction)({ check: { outcome: "unknown", reason: "offline" }, branch, dryRun: false, yes: false, promptAnswerYes: true });
|
|
203
|
+
strict_1.default.deepEqual(decision, { action: "proceed" });
|
|
204
|
+
});
|
|
205
|
+
(0, node_test_1.it)("unknown warns under --dry-run regardless of --yes", () => {
|
|
206
|
+
const decision = (0, deep_scan_1.decideFreshnessAction)({ check: { outcome: "unknown", reason: "offline" }, branch, dryRun: true, yes: true });
|
|
207
|
+
strict_1.default.equal(decision.action, "warn");
|
|
208
|
+
});
|
|
209
|
+
(0, node_test_1.it)("ahead message correctly pluralizes a single commit", () => {
|
|
210
|
+
const decision = (0, deep_scan_1.decideFreshnessAction)({ check: { outcome: "ahead", ahead: 1 }, branch, dryRun: false, yes: true });
|
|
211
|
+
strict_1.default.equal(decision.action, "refuse");
|
|
212
|
+
if (decision.action === "refuse")
|
|
213
|
+
strict_1.default.match(decision.message, /has 1 commit that is not pushed/);
|
|
214
|
+
});
|
|
215
|
+
(0, node_test_1.it)("ahead message correctly pluralizes multiple commits", () => {
|
|
216
|
+
const decision = (0, deep_scan_1.decideFreshnessAction)({ check: { outcome: "ahead", ahead: 2 }, branch, dryRun: false, yes: true });
|
|
217
|
+
strict_1.default.equal(decision.action, "refuse");
|
|
218
|
+
if (decision.action === "refuse")
|
|
219
|
+
strict_1.default.match(decision.message, /has 2 commits that are not pushed/);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
@@ -37,14 +37,22 @@ function withFetch(responses, run) {
|
|
|
37
37
|
return run(calls).finally(() => { global.fetch = original; });
|
|
38
38
|
}
|
|
39
39
|
function makeRepo() {
|
|
40
|
+
// A real local bare repo as origin (not the usual fake github.com remote
|
|
41
|
+
// URL used elsewhere in this file's sibling tests) — the checkout
|
|
42
|
+
// freshness guard now runs a real `git fetch origin main` ahead of the
|
|
43
|
+
// consent summary for every full-tree scan this file exercises, and a
|
|
44
|
+
// fake unreachable remote would make that fetch fail for real (and, worse,
|
|
45
|
+
// attempt a real network call to github.com).
|
|
46
|
+
const originRoot = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-consent-order-origin-"));
|
|
47
|
+
(0, node_child_process_1.execFileSync)("git", ["init", "-q", "--bare", "-b", "main", originRoot]);
|
|
40
48
|
const root = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), "kodingo-consent-order-"));
|
|
41
|
-
(0, node_child_process_1.execFileSync)("git", ["
|
|
49
|
+
(0, node_child_process_1.execFileSync)("git", ["clone", "-q", originRoot, root]);
|
|
42
50
|
(0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.email", "test@example.invalid"]);
|
|
43
51
|
(0, node_child_process_1.execFileSync)("git", ["-C", root, "config", "user.name", "Validation Test"]);
|
|
44
|
-
(0, node_child_process_1.execFileSync)("git", ["-C", root, "remote", "add", "origin", "git@github.com:validation/repository.git"]);
|
|
45
52
|
node_fs_1.default.writeFileSync(node_path_1.default.join(root, "page.ts"), "export function Page() { return 1; }");
|
|
46
53
|
(0, node_child_process_1.execFileSync)("git", ["-C", root, "add", "."]);
|
|
47
54
|
(0, node_child_process_1.execFileSync)("git", ["-C", root, "commit", "-qm", "fixture"]);
|
|
55
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "push", "-q", "origin", "main"]);
|
|
48
56
|
return root;
|
|
49
57
|
}
|
|
50
58
|
function withConsoleLog(run) {
|
|
@@ -47,6 +47,9 @@ exports.buildUploadSummary = buildUploadSummary;
|
|
|
47
47
|
exports.buildLocalReadSummary = buildLocalReadSummary;
|
|
48
48
|
exports.decideBranchSettlement = decideBranchSettlement;
|
|
49
49
|
exports.shouldSkipIncrementalScan = shouldSkipIncrementalScan;
|
|
50
|
+
exports.checkCheckoutFreshness = checkCheckoutFreshness;
|
|
51
|
+
exports.decideFreshnessAction = decideFreshnessAction;
|
|
52
|
+
exports.enforceCheckoutFreshness = enforceCheckoutFreshness;
|
|
50
53
|
exports.settleTrackedBranch = settleTrackedBranch;
|
|
51
54
|
exports.pollRun = pollRun;
|
|
52
55
|
exports.printFinalOutcome = printFinalOutcome;
|
|
@@ -265,6 +268,8 @@ function describeApplyReason(reason) {
|
|
|
265
268
|
return { sentence: "The CLI could not determine the current branch.", nextStep: "Check out a named branch, then scan again." };
|
|
266
269
|
if (reason === "stale_full_tree")
|
|
267
270
|
return { sentence: "This was a full-repository scan at a commit that's no longer the tracked branch's current head — re-applying it could incorrectly remove data for files changed since.", nextStep: "Run `kodingo deep-scan` (or `kodingo init --force`) to scan the current commit." };
|
|
271
|
+
if (reason === "unknown_commit")
|
|
272
|
+
return { sentence: "This scan's commit isn't on GitHub.", nextStep: "Push it and run `kodingo init --force`." };
|
|
268
273
|
const mismatch = /^branch_mismatch:([^:]*):(.*)$/.exec(reason);
|
|
269
274
|
if (mismatch) {
|
|
270
275
|
const [, scanned, tracked] = mismatch;
|
|
@@ -272,6 +277,20 @@ function describeApplyReason(reason) {
|
|
|
272
277
|
return { sentence: `No tracked branch is recorded for this repository (this scan ran on "${scanned}").`, nextStep: "Run `kodingo init` again to record a tracked branch." };
|
|
273
278
|
return { sentence: `The tracked branch is "${tracked}", but this scan ran on "${scanned}".`, nextStep: `Switch to "${tracked}", or ask an org admin to change the tracked branch.` };
|
|
274
279
|
}
|
|
280
|
+
const staleCommit = /^stale_commit:(\d+)$/.exec(reason);
|
|
281
|
+
if (staleCommit) {
|
|
282
|
+
const behind = staleCommit[1];
|
|
283
|
+
return { sentence: `This scan is ${behind} commit${behind === "1" ? "" : "s"} behind GitHub.`, nextStep: "Pull and run `kodingo init --force`." };
|
|
284
|
+
}
|
|
285
|
+
const deleteCapExceeded = /^delete_cap_exceeded:(\d+):([\d.]+)$/.exec(reason);
|
|
286
|
+
if (deleteCapExceeded) {
|
|
287
|
+
const entities = deleteCapExceeded[1];
|
|
288
|
+
const sharePct = Math.round(Number(deleteCapExceeded[2]) * 100);
|
|
289
|
+
return {
|
|
290
|
+
sentence: `This full-tree scan would have deleted ${entities} entities (${sharePct}% of what's stored), so it was held as a safety precaution.`,
|
|
291
|
+
nextStep: "An org admin must approve it; re-scanning won't resolve it.",
|
|
292
|
+
};
|
|
293
|
+
}
|
|
275
294
|
return { sentence: `The scan was held (${reason}).`, nextStep: "Run `kodingo deep-scan-status` for more detail." };
|
|
276
295
|
}
|
|
277
296
|
/** Pure so the "accurate" wording regression (165 read vs N uploaded vs M
|
|
@@ -359,6 +378,98 @@ async function askYesNo(question) {
|
|
|
359
378
|
rl.close();
|
|
360
379
|
}
|
|
361
380
|
}
|
|
381
|
+
/** Same shape as askYesNo, but defaults to "no" on an empty answer — used
|
|
382
|
+
* where silently guessing "yes" would risk reading a checkout Kortex can't
|
|
383
|
+
* actually verify is current. */
|
|
384
|
+
async function askYesNoDefaultNo(question) {
|
|
385
|
+
if (!process.stdin.isTTY)
|
|
386
|
+
return false;
|
|
387
|
+
const rl = promises_1.default.createInterface({ input: node_process_1.stdin, output: node_process_1.stdout });
|
|
388
|
+
try {
|
|
389
|
+
const answer = (await rl.question(question)).trim();
|
|
390
|
+
return /^y(?:es)?$/i.test(answer);
|
|
391
|
+
}
|
|
392
|
+
finally {
|
|
393
|
+
rl.close();
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function describeFreshnessFetchFailure(err) {
|
|
397
|
+
if (err && (err.killed || err.signal))
|
|
398
|
+
return "git fetch timed out after 15s";
|
|
399
|
+
const raw = (err?.stderr ? err.stderr.toString() : "") || (err?.message ? String(err.message) : "");
|
|
400
|
+
const firstLine = raw.split(/\r?\n/).map((line) => line.trim()).find((line) => line.length > 0);
|
|
401
|
+
return firstLine || "git fetch failed";
|
|
402
|
+
}
|
|
403
|
+
/** Fetches origin/<branch> fresh (quiet, no tags, bounded to 15s so a dead
|
|
404
|
+
* remote never hangs a scan) and compares HEAD against it. Never guesses —
|
|
405
|
+
* any failure (no origin, offline, auth, an unparseable result) comes back
|
|
406
|
+
* as "unknown" with the reason, for the caller to decide how to handle. */
|
|
407
|
+
function checkCheckoutFreshness(root, branch) {
|
|
408
|
+
try {
|
|
409
|
+
(0, node_child_process_1.execFileSync)("git", ["-C", root, "fetch", "--quiet", "--no-tags", "origin", branch], { timeout: 15000, stdio: ["ignore", "pipe", "pipe"] });
|
|
410
|
+
}
|
|
411
|
+
catch (err) {
|
|
412
|
+
return { outcome: "unknown", reason: describeFreshnessFetchFailure(err) };
|
|
413
|
+
}
|
|
414
|
+
let counts;
|
|
415
|
+
try {
|
|
416
|
+
counts = git(root, ["rev-list", "--left-right", "--count", `HEAD...origin/${branch}`]);
|
|
417
|
+
}
|
|
418
|
+
catch (err) {
|
|
419
|
+
return { outcome: "unknown", reason: describeFreshnessFetchFailure(err) };
|
|
420
|
+
}
|
|
421
|
+
const match = counts.match(/^(\d+)\s+(\d+)$/);
|
|
422
|
+
if (!match)
|
|
423
|
+
return { outcome: "unknown", reason: `unexpected git output comparing against origin/${branch}` };
|
|
424
|
+
const ahead = Number(match[1]);
|
|
425
|
+
const behind = Number(match[2]);
|
|
426
|
+
if (ahead === 0 && behind === 0)
|
|
427
|
+
return { outcome: "up-to-date", shortSha: git(root, ["rev-parse", "--short", "HEAD"]) };
|
|
428
|
+
if (ahead === 0)
|
|
429
|
+
return { outcome: "behind", behind };
|
|
430
|
+
if (behind === 0)
|
|
431
|
+
return { outcome: "ahead", ahead };
|
|
432
|
+
return { outcome: "diverged", ahead, behind };
|
|
433
|
+
}
|
|
434
|
+
/** The pure "what to do about a stale checkout" decision — separated from
|
|
435
|
+
* the fetch/compare and the interactive prompt so every rule (refuse full
|
|
436
|
+
* scans of outdated code, warn-only for --dry-run since it uploads nothing,
|
|
437
|
+
* never guess on an undetermined result) is directly unit-testable. */
|
|
438
|
+
function decideFreshnessAction(params) {
|
|
439
|
+
const { check, branch, dryRun, yes, promptAnswerYes } = params;
|
|
440
|
+
if (check.outcome === "up-to-date") {
|
|
441
|
+
return { action: "proceed", note: `Reading ${branch} at ${check.shortSha}, up to date with origin/${branch}.` };
|
|
442
|
+
}
|
|
443
|
+
if (check.outcome === "unknown") {
|
|
444
|
+
const base = `Couldn't check whether this checkout is up to date (${check.reason}).`;
|
|
445
|
+
if (dryRun)
|
|
446
|
+
return { action: "warn", message: base };
|
|
447
|
+
if (yes)
|
|
448
|
+
return { action: "refuse", message: `${base} Refusing to guess under --yes.` };
|
|
449
|
+
if (promptAnswerYes)
|
|
450
|
+
return { action: "proceed" };
|
|
451
|
+
return { action: "refuse", message: base };
|
|
452
|
+
}
|
|
453
|
+
const message = check.outcome === "behind"
|
|
454
|
+
? `Your checkout of ${branch} is ${check.behind} commit${check.behind === 1 ? "" : "s"} behind origin/${branch}, so Kortex would read outdated code. Run \`git pull\` and try again.`
|
|
455
|
+
: check.outcome === "ahead"
|
|
456
|
+
? `Your checkout of ${branch} has ${check.ahead} commit${check.ahead === 1 ? "" : "s"} that ${check.ahead === 1 ? "is" : "are"} not pushed. Push first so Kortex reads the same code GitHub has.`
|
|
457
|
+
: `Your checkout of ${branch} has diverged from origin/${branch}: ${check.ahead} commit${check.ahead === 1 ? "" : "s"} not pushed and ${check.behind} commit${check.behind === 1 ? "" : "s"} behind. Reconcile the branches, then try again.`;
|
|
458
|
+
return dryRun ? { action: "warn", message } : { action: "refuse", message };
|
|
459
|
+
}
|
|
460
|
+
/** Full-tree scans only (init's first scan, and `kodingo deep-scan` without
|
|
461
|
+
* --changed) — never the commit hook, which always invokes --changed on
|
|
462
|
+
* normal, possibly-unpushed local commits. Runs a real `git fetch` against
|
|
463
|
+
* origin before anything is read, so a checkout behind origin never gets a
|
|
464
|
+
* chance to replace newer stored data with older code. */
|
|
465
|
+
async function enforceCheckoutFreshness(opts) {
|
|
466
|
+
const check = checkCheckoutFreshness(opts.root, opts.branch);
|
|
467
|
+
let promptAnswerYes;
|
|
468
|
+
if (check.outcome === "unknown" && !opts.dryRun && !opts.yes) {
|
|
469
|
+
promptAnswerYes = await askYesNoDefaultNo(`Couldn't check whether this checkout is up to date (${check.reason}). Continue anyway? [y/N] `);
|
|
470
|
+
}
|
|
471
|
+
return decideFreshnessAction({ check, branch: opts.branch, dryRun: opts.dryRun, yes: opts.yes, promptAnswerYes });
|
|
472
|
+
}
|
|
362
473
|
/** Settles the project's tracked development branch against the current
|
|
363
474
|
* checkout via POST /deep-scan/branch/settle, before anything is uploaded —
|
|
364
475
|
* so a first scan can apply instead of landing held with no explanation. */
|
|
@@ -690,6 +801,21 @@ async function deepScanCommand(options = {}) {
|
|
|
690
801
|
const token = envToken || credentials.token;
|
|
691
802
|
if (!token)
|
|
692
803
|
throw new Error("No project token found. Run `kodingo init` once, or set KODINGO_TOKEN for CI.");
|
|
804
|
+
// Kortex must never read, scan, or upload a checkout whose HEAD is stale
|
|
805
|
+
// relative to origin — a clean checkout behind origin would otherwise
|
|
806
|
+
// apply and could silently replace newer stored data with older code.
|
|
807
|
+
// Full-tree scans only (never --changed, which the hook always passes for
|
|
808
|
+
// normal, possibly-unpushed local commits); detached HEAD has no branch to
|
|
809
|
+
// compare against origin and is already never applied regardless.
|
|
810
|
+
if (!options.changed && meta.branch && !meta.detachedHead) {
|
|
811
|
+
const freshness = await enforceCheckoutFreshness({ root: meta.root, branch: meta.branch, dryRun: Boolean(options.dryRun), yes: Boolean(options.yes) });
|
|
812
|
+
if (freshness.action === "refuse")
|
|
813
|
+
throw new Error(freshness.message);
|
|
814
|
+
if (freshness.action === "warn")
|
|
815
|
+
console.warn(`⚠ ${freshness.message}`);
|
|
816
|
+
if (freshness.action === "proceed" && freshness.note)
|
|
817
|
+
console.log(freshness.note);
|
|
818
|
+
}
|
|
693
819
|
// The commit hook invokes `deep-scan --changed` unconditionally, on every
|
|
694
820
|
// commit, regardless of branch — upload only for a commit on the tracked
|
|
695
821
|
// branch, per a confirmed mismatch (never on "none": there's nothing to
|