codeconscious 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/cli.cjs +374 -113
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,8 +10,16 @@ npx codeconscious init
|
|
|
10
10
|
|
|
11
11
|
- `codeconscious init` install hooks in the current git repo (chains with husky, lefthook, `core.hooksPath`; nothing is overwritten) and log in
|
|
12
12
|
- `codeconscious uninstall` remove the hooks and restore the previous state
|
|
13
|
+
- `codeconscious quiz` answer the pending question for your current commit (and a few more, one at a time)
|
|
14
|
+
- `codeconscious status` show login and hook state
|
|
13
15
|
- `codeconscious --help` list all commands
|
|
14
16
|
|
|
17
|
+
## On push, and when an AI agent commits for you
|
|
18
|
+
|
|
19
|
+
On `git push` you see a one-line teaser: "Press space or enter to answer (15s), s to skip". Press a key within 15 s and the question starts with no countdown (the push waits for you; `s`, Esc, Ctrl-D or Ctrl-C always lets it continue, and Ctrl-C at the prompt skips the question). If you press nothing, or there is no terminal (AI agents, CI), nothing is recorded and unanswered questions wait for `npx codeconscious quiz`.
|
|
20
|
+
|
|
21
|
+
If an AI agent commits for you, run `npx codeconscious quiz` afterwards.
|
|
22
|
+
|
|
15
23
|
## Privacy
|
|
16
24
|
|
|
17
25
|
- Only diffs leave your machine, never the full repo.
|
package/dist/cli.cjs
CHANGED
|
@@ -7495,6 +7495,8 @@ var POST_COMMIT_BUDGET_MS = 4e3;
|
|
|
7495
7495
|
var UPLOAD_BUDGET_MS = 12e3;
|
|
7496
7496
|
var PRE_PUSH_FETCH_BUDGET_MS = 6e3;
|
|
7497
7497
|
var PRE_PUSH_QUIZ_BUDGET_MS = 15e3 + 2e3 + 1e3;
|
|
7498
|
+
var PRE_PUSH_ANSWER_BUDGET_MS = 10 * 6e4 + 2e3 + 1e3;
|
|
7499
|
+
var ANSWER_CEILING_MS = 10 * 6e4;
|
|
7498
7500
|
var HOOK_TIMEOUT_MS = 2e3;
|
|
7499
7501
|
var ANSWER_TIMEOUT_MS = 15e3;
|
|
7500
7502
|
function configDir(env = process.env) {
|
|
@@ -7578,8 +7580,8 @@ function logLine(message, env = process.env) {
|
|
|
7578
7580
|
if (import_node_fs3.default.statSync(file).size > MAX_LOG_BYTES) import_node_fs3.default.rmSync(file, { force: true });
|
|
7579
7581
|
} catch {
|
|
7580
7582
|
}
|
|
7581
|
-
const
|
|
7582
|
-
import_node_fs3.default.appendFileSync(file, `${(/* @__PURE__ */ new Date()).toISOString()} ${
|
|
7583
|
+
const clean2 = message.replace(/\s+/g, " ").slice(0, 500);
|
|
7584
|
+
import_node_fs3.default.appendFileSync(file, `${(/* @__PURE__ */ new Date()).toISOString()} ${clean2}
|
|
7583
7585
|
`, { mode: 384 });
|
|
7584
7586
|
} catch {
|
|
7585
7587
|
}
|
|
@@ -8089,11 +8091,359 @@ function runStatus(deps) {
|
|
|
8089
8091
|
} else {
|
|
8090
8092
|
lines.push("Not inside a git repository.");
|
|
8091
8093
|
}
|
|
8094
|
+
if (dir && getToken(deps.env)) {
|
|
8095
|
+
lines.push("Skipped a question, or an agent committed for you? Run: npx codeconscious quiz");
|
|
8096
|
+
}
|
|
8092
8097
|
if (deps.quiet) return 0;
|
|
8093
8098
|
note(lines.join("\n"), "CodeConscious status");
|
|
8094
8099
|
return 0;
|
|
8095
8100
|
}
|
|
8096
8101
|
|
|
8102
|
+
// src/quiz.ts
|
|
8103
|
+
var import_node_fs7 = __toESM(require("node:fs"), 1);
|
|
8104
|
+
var import_node_tty2 = __toESM(require("node:tty"), 1);
|
|
8105
|
+
function openTty() {
|
|
8106
|
+
try {
|
|
8107
|
+
if (!process.stderr.isTTY) return null;
|
|
8108
|
+
if (import_node_tty2.default.isatty(0)) {
|
|
8109
|
+
return { input: process.stdin, output: process.stderr, close: () => process.stdin.pause() };
|
|
8110
|
+
}
|
|
8111
|
+
const device = process.platform === "win32" ? "\\\\.\\CONIN$" : "/dev/tty";
|
|
8112
|
+
const fd = import_node_fs7.default.openSync(device, process.platform === "win32" ? "r+" : "r");
|
|
8113
|
+
const input = new import_node_tty2.default.ReadStream(fd);
|
|
8114
|
+
return {
|
|
8115
|
+
input,
|
|
8116
|
+
output: process.stderr,
|
|
8117
|
+
close: () => {
|
|
8118
|
+
try {
|
|
8119
|
+
input.destroy();
|
|
8120
|
+
} catch {
|
|
8121
|
+
}
|
|
8122
|
+
}
|
|
8123
|
+
};
|
|
8124
|
+
} catch {
|
|
8125
|
+
return null;
|
|
8126
|
+
}
|
|
8127
|
+
}
|
|
8128
|
+
var MAX_SNIPPET_LINES = 24;
|
|
8129
|
+
var UNSAFE_TERMINAL = new RegExp(
|
|
8130
|
+
// eslint-disable-next-line no-control-regex -- matching control characters is the point
|
|
8131
|
+
"\\u001b\\[[0-?]*[ -\\/]*[@-~]|\\u001b\\][^\\u0007\\u001b]*(\\u0007|\\u001b\\\\)|[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f]",
|
|
8132
|
+
"g"
|
|
8133
|
+
);
|
|
8134
|
+
function clean(text2) {
|
|
8135
|
+
return text2.replace(UNSAFE_TERMINAL, "");
|
|
8136
|
+
}
|
|
8137
|
+
function cleanQuestion(q) {
|
|
8138
|
+
return {
|
|
8139
|
+
...q,
|
|
8140
|
+
stem: clean(q.stem),
|
|
8141
|
+
choices: q.choices.map(clean),
|
|
8142
|
+
conceptSlug: clean(q.conceptSlug),
|
|
8143
|
+
codeRef: { ...q.codeRef, file: clean(q.codeRef.file), snippet: clean(q.codeRef.snippet) }
|
|
8144
|
+
};
|
|
8145
|
+
}
|
|
8146
|
+
function snippetForDisplay(snippet) {
|
|
8147
|
+
const lines = snippet.replace(/\t/g, " ").split(/\r?\n/);
|
|
8148
|
+
if (lines.length <= MAX_SNIPPET_LINES) return lines.join("\n");
|
|
8149
|
+
return [
|
|
8150
|
+
...lines.slice(0, MAX_SNIPPET_LINES),
|
|
8151
|
+
`... (${lines.length - MAX_SNIPPET_LINES} more lines)`
|
|
8152
|
+
].join("\n");
|
|
8153
|
+
}
|
|
8154
|
+
async function askQuestion(rawQuestion, io, opts = {}) {
|
|
8155
|
+
const question = cleanQuestion(rawQuestion);
|
|
8156
|
+
const timeoutMs = opts.timeoutMs ?? ANSWER_TIMEOUT_MS;
|
|
8157
|
+
const now = opts.now ?? Date.now;
|
|
8158
|
+
const { codeRef } = question;
|
|
8159
|
+
const hint = opts.countdown === false ? "enter to answer, s to skip" : `enter to answer, s to skip, ${Math.round(timeoutMs / 1e3)}s`;
|
|
8160
|
+
const width = textWidth(io.output);
|
|
8161
|
+
note(
|
|
8162
|
+
wrapCode(snippetForDisplay(codeRef.snippet), Math.max(10, width - 4)),
|
|
8163
|
+
`${codeRef.file}:${codeRef.lineStart}-${codeRef.lineEnd}`,
|
|
8164
|
+
{ output: io.output }
|
|
8165
|
+
);
|
|
8166
|
+
const controller = new AbortController();
|
|
8167
|
+
const state = { reason: "cancelled" };
|
|
8168
|
+
const onData = (chunk) => {
|
|
8169
|
+
const text2 = chunk.toString();
|
|
8170
|
+
if (text2 === "s" || text2 === "S" || text2 === "\x1B" || text2 === "") {
|
|
8171
|
+
state.reason = "skipped";
|
|
8172
|
+
controller.abort();
|
|
8173
|
+
}
|
|
8174
|
+
};
|
|
8175
|
+
io.input.on("data", onData);
|
|
8176
|
+
const timer = setTimeout(() => {
|
|
8177
|
+
state.reason = "timeout";
|
|
8178
|
+
controller.abort();
|
|
8179
|
+
}, timeoutMs);
|
|
8180
|
+
const startedAt = now();
|
|
8181
|
+
try {
|
|
8182
|
+
log.message(
|
|
8183
|
+
question.choices.map((c3, i2) => wrapText(`${String.fromCharCode(65 + i2)}) ${c3}`, width, 3)).join("\n"),
|
|
8184
|
+
{ output: io.output }
|
|
8185
|
+
);
|
|
8186
|
+
const result = await select({
|
|
8187
|
+
message: `${wrapText(question.stem, width)}
|
|
8188
|
+
(${hint})`,
|
|
8189
|
+
options: question.choices.map((_c, index) => ({
|
|
8190
|
+
value: index,
|
|
8191
|
+
label: `${String.fromCharCode(65 + index)})`
|
|
8192
|
+
})),
|
|
8193
|
+
input: io.input,
|
|
8194
|
+
output: io.output,
|
|
8195
|
+
signal: controller.signal
|
|
8196
|
+
});
|
|
8197
|
+
const msToAnswer = Math.max(0, Math.round(now() - startedAt));
|
|
8198
|
+
if (typeof result === "number") {
|
|
8199
|
+
return { choiceIndex: result, skipped: false, msToAnswer, reason: "answered" };
|
|
8200
|
+
}
|
|
8201
|
+
return {
|
|
8202
|
+
choiceIndex: null,
|
|
8203
|
+
skipped: true,
|
|
8204
|
+
msToAnswer: state.reason === "timeout" ? timeoutMs : msToAnswer,
|
|
8205
|
+
reason: state.reason
|
|
8206
|
+
};
|
|
8207
|
+
} catch {
|
|
8208
|
+
return { choiceIndex: null, skipped: true, msToAnswer: 0, reason: "cancelled" };
|
|
8209
|
+
} finally {
|
|
8210
|
+
clearTimeout(timer);
|
|
8211
|
+
io.input.off("data", onData);
|
|
8212
|
+
}
|
|
8213
|
+
}
|
|
8214
|
+
var DRAIN_MS = 200;
|
|
8215
|
+
async function askWithOptIn(rawQuestion, io, opts = {}) {
|
|
8216
|
+
const question = cleanQuestion(rawQuestion);
|
|
8217
|
+
const gateNow = opts.now ?? Date.now;
|
|
8218
|
+
const windowMs = opts.windowMs ?? ANSWER_TIMEOUT_MS;
|
|
8219
|
+
const ceilingMs = opts.ceilingMs ?? ANSWER_CEILING_MS;
|
|
8220
|
+
const { codeRef } = question;
|
|
8221
|
+
const input = io.input;
|
|
8222
|
+
const wasRaw = input.isRaw === true;
|
|
8223
|
+
let decide = () => void 0;
|
|
8224
|
+
const decision = new Promise((resolve) => {
|
|
8225
|
+
decide = resolve;
|
|
8226
|
+
});
|
|
8227
|
+
let acceptAfter = Number.POSITIVE_INFINITY;
|
|
8228
|
+
const onData = (chunk) => {
|
|
8229
|
+
if (gateNow() < acceptAfter) return;
|
|
8230
|
+
const text2 = chunk.toString();
|
|
8231
|
+
if (text2 === " " || text2 === "\r" || text2 === "\n" || text2 === "\r\n") decide("start");
|
|
8232
|
+
else if (["s", "S", "", "\x1B", ""].includes(text2)) decide("skip");
|
|
8233
|
+
};
|
|
8234
|
+
let timer;
|
|
8235
|
+
try {
|
|
8236
|
+
log.info(
|
|
8237
|
+
`Question waiting about ${codeRef.file}:${codeRef.lineStart}-${codeRef.lineEnd} (${question.conceptSlug}).
|
|
8238
|
+
Press space or enter to answer (${Math.round(windowMs / 1e3)}s), s to skip`,
|
|
8239
|
+
{ output: io.output }
|
|
8240
|
+
);
|
|
8241
|
+
try {
|
|
8242
|
+
input.setRawMode?.(true);
|
|
8243
|
+
} catch {
|
|
8244
|
+
}
|
|
8245
|
+
input.on("data", onData);
|
|
8246
|
+
input.resume?.();
|
|
8247
|
+
acceptAfter = gateNow() + DRAIN_MS;
|
|
8248
|
+
timer = setTimeout(() => decide("timeout"), windowMs);
|
|
8249
|
+
const choice = await decision;
|
|
8250
|
+
clearTimeout(timer);
|
|
8251
|
+
input.off("data", onData);
|
|
8252
|
+
if (choice !== "start") {
|
|
8253
|
+
return {
|
|
8254
|
+
choiceIndex: null,
|
|
8255
|
+
skipped: true,
|
|
8256
|
+
msToAnswer: choice === "timeout" ? windowMs : 0,
|
|
8257
|
+
reason: choice === "timeout" ? "timeout" : "skipped"
|
|
8258
|
+
};
|
|
8259
|
+
}
|
|
8260
|
+
opts.onStarted?.();
|
|
8261
|
+
return await askQuestion(question, io, {
|
|
8262
|
+
timeoutMs: ceilingMs,
|
|
8263
|
+
now: opts.now,
|
|
8264
|
+
countdown: false
|
|
8265
|
+
});
|
|
8266
|
+
} catch {
|
|
8267
|
+
return { choiceIndex: null, skipped: true, msToAnswer: 0, reason: "cancelled" };
|
|
8268
|
+
} finally {
|
|
8269
|
+
if (timer) clearTimeout(timer);
|
|
8270
|
+
input.off("data", onData);
|
|
8271
|
+
try {
|
|
8272
|
+
input.setRawMode?.(wasRaw);
|
|
8273
|
+
} catch {
|
|
8274
|
+
}
|
|
8275
|
+
}
|
|
8276
|
+
}
|
|
8277
|
+
function textWidth(output, gutter = 3) {
|
|
8278
|
+
const cols = output?.columns ?? process.stderr.columns;
|
|
8279
|
+
const total = typeof cols === "number" && cols > 0 ? cols : 80;
|
|
8280
|
+
return Math.max(20, total - gutter);
|
|
8281
|
+
}
|
|
8282
|
+
function wrapText(text2, width, hanging = 0) {
|
|
8283
|
+
const pad = " ".repeat(hanging);
|
|
8284
|
+
const out = [];
|
|
8285
|
+
for (const para of text2.replace(/\t/g, " ").split(/\r?\n/)) {
|
|
8286
|
+
const words = para.trim().split(/\s+/).filter(Boolean);
|
|
8287
|
+
if (words.length === 0) {
|
|
8288
|
+
out.push("");
|
|
8289
|
+
continue;
|
|
8290
|
+
}
|
|
8291
|
+
let line = "";
|
|
8292
|
+
const indent = () => out.length === 0 ? "" : pad;
|
|
8293
|
+
for (let word of words) {
|
|
8294
|
+
for (; ; ) {
|
|
8295
|
+
const candidate = line ? `${line} ${word}` : `${indent()}${word}`;
|
|
8296
|
+
if (candidate.length <= width) {
|
|
8297
|
+
line = candidate;
|
|
8298
|
+
break;
|
|
8299
|
+
}
|
|
8300
|
+
if (line) {
|
|
8301
|
+
out.push(line);
|
|
8302
|
+
line = "";
|
|
8303
|
+
continue;
|
|
8304
|
+
}
|
|
8305
|
+
const base = indent();
|
|
8306
|
+
const room = Math.max(1, width - base.length);
|
|
8307
|
+
out.push(`${base}${word.slice(0, room)}`);
|
|
8308
|
+
word = word.slice(room);
|
|
8309
|
+
if (!word) break;
|
|
8310
|
+
}
|
|
8311
|
+
}
|
|
8312
|
+
if (line) out.push(line);
|
|
8313
|
+
}
|
|
8314
|
+
return out.join("\n");
|
|
8315
|
+
}
|
|
8316
|
+
function wrapCode(code, width) {
|
|
8317
|
+
return code.split("\n").flatMap((l) => {
|
|
8318
|
+
if (l.length <= width) return [l];
|
|
8319
|
+
const parts = [];
|
|
8320
|
+
for (let i2 = 0; i2 < l.length; i2 += width) parts.push(l.slice(i2, i2 + width));
|
|
8321
|
+
return parts;
|
|
8322
|
+
}).join("\n");
|
|
8323
|
+
}
|
|
8324
|
+
function showFeedback(answer, result, io, question) {
|
|
8325
|
+
const opts = { output: io.output };
|
|
8326
|
+
const width = textWidth(io.output);
|
|
8327
|
+
const wrap = (t2, hanging = 0) => wrapText(t2, width, hanging);
|
|
8328
|
+
if (answer.skipped) {
|
|
8329
|
+
log.info("Skipped.", opts);
|
|
8330
|
+
return;
|
|
8331
|
+
}
|
|
8332
|
+
if (!result) {
|
|
8333
|
+
log.warn("Answer not recorded (offline), push continues.", opts);
|
|
8334
|
+
return;
|
|
8335
|
+
}
|
|
8336
|
+
const parts = [result.correct ? `Correct. Streak: ${result.streak}.` : "Not quite."];
|
|
8337
|
+
if (!result.correct) {
|
|
8338
|
+
const picked = answer.choiceIndex === null ? void 0 : question?.choices[answer.choiceIndex];
|
|
8339
|
+
if (picked !== void 0 && answer.choiceIndex !== null) {
|
|
8340
|
+
const l = String.fromCharCode(65 + answer.choiceIndex);
|
|
8341
|
+
parts.push(wrap(`You picked: ${l}) ${clean(picked)}`, 12));
|
|
8342
|
+
}
|
|
8343
|
+
const letter = String.fromCharCode(65 + result.correctIndex);
|
|
8344
|
+
const choice = question?.choices[result.correctIndex];
|
|
8345
|
+
parts.push(wrap(`Answer: ${letter})${choice === void 0 ? "" : ` ${clean(choice)}`}`, 8));
|
|
8346
|
+
}
|
|
8347
|
+
const m3 = result.mutation;
|
|
8348
|
+
if (m3) {
|
|
8349
|
+
const labeled = (label, value) => wrapText(value, Math.max(10, width - label.length)).split("\n").map((l, i2) => (i2 === 0 ? label : " ".repeat(label.length)) + l).join("\n");
|
|
8350
|
+
parts.push(labeled("Original: ", clean(m3.originalLine).trim()));
|
|
8351
|
+
parts.push(labeled("Changed: ", clean(m3.mutatedLine).trim()));
|
|
8352
|
+
}
|
|
8353
|
+
const explanation = clean(result.explanation).trim();
|
|
8354
|
+
if (explanation) parts.push(`
|
|
8355
|
+
${wrap(explanation)}`);
|
|
8356
|
+
const text2 = parts.join("\n");
|
|
8357
|
+
if (result.correct) log.success(text2, opts);
|
|
8358
|
+
else log.error(text2, opts);
|
|
8359
|
+
}
|
|
8360
|
+
|
|
8361
|
+
// src/quiz-command.ts
|
|
8362
|
+
var QUIZ_TIMEOUT_MS = 5e3;
|
|
8363
|
+
var MAX_QUESTIONS = 4;
|
|
8364
|
+
async function runQuiz(deps) {
|
|
8365
|
+
const write = deps.write ?? ((text2) => void process.stderr.write(text2));
|
|
8366
|
+
const say = (msg) => {
|
|
8367
|
+
if (!deps.quiet) write(`${msg}
|
|
8368
|
+
`);
|
|
8369
|
+
};
|
|
8370
|
+
let io = null;
|
|
8371
|
+
try {
|
|
8372
|
+
const token = getToken(deps.env);
|
|
8373
|
+
if (!token) {
|
|
8374
|
+
say("Not signed in. Run `npx codeconscious init` first.");
|
|
8375
|
+
return 0;
|
|
8376
|
+
}
|
|
8377
|
+
io = (deps.openTty ?? openTty)();
|
|
8378
|
+
if (!io) {
|
|
8379
|
+
say("`codeconscious quiz` needs an interactive terminal. Run it yourself in a terminal.");
|
|
8380
|
+
return 0;
|
|
8381
|
+
}
|
|
8382
|
+
const root = repoRoot(deps.cwd) ?? deps.cwd;
|
|
8383
|
+
const repo = repoFullName(root);
|
|
8384
|
+
const sha = headSha(root);
|
|
8385
|
+
if (!repo || !sha) {
|
|
8386
|
+
say("Run this inside a git repository with a GitHub remote and at least one commit.");
|
|
8387
|
+
return 0;
|
|
8388
|
+
}
|
|
8389
|
+
const api = deps.api ?? createApiClient({
|
|
8390
|
+
baseUrl: apiBaseUrl(deps.env),
|
|
8391
|
+
token,
|
|
8392
|
+
timeoutMs: QUIZ_TIMEOUT_MS,
|
|
8393
|
+
fetchImpl: deps.fetchImpl
|
|
8394
|
+
});
|
|
8395
|
+
const ask = deps.ask ?? ((q, t2) => askQuestion(q, t2, { timeoutMs: ANSWER_CEILING_MS, countdown: false }));
|
|
8396
|
+
const confirmAnother = deps.confirmAnother ?? (async (t2) => {
|
|
8397
|
+
const r2 = await confirm({
|
|
8398
|
+
message: "Another? (y/N)",
|
|
8399
|
+
initialValue: false,
|
|
8400
|
+
input: t2.input,
|
|
8401
|
+
output: t2.output
|
|
8402
|
+
});
|
|
8403
|
+
return r2 === true;
|
|
8404
|
+
});
|
|
8405
|
+
const seen = /* @__PURE__ */ new Set();
|
|
8406
|
+
for (let round = 0; round < MAX_QUESTIONS; round++) {
|
|
8407
|
+
const next = await api.nextQuestion(repo, sha);
|
|
8408
|
+
if (!next.ok) {
|
|
8409
|
+
say("Could not reach CodeConscious. Try again later.");
|
|
8410
|
+
return 0;
|
|
8411
|
+
}
|
|
8412
|
+
const question = next.data.question;
|
|
8413
|
+
if (!question) {
|
|
8414
|
+
say(round === 0 ? "No pending questions" : "No more pending questions");
|
|
8415
|
+
return 0;
|
|
8416
|
+
}
|
|
8417
|
+
if (seen.has(question.id)) {
|
|
8418
|
+
say("Could not record your last answer; try again later.");
|
|
8419
|
+
return 0;
|
|
8420
|
+
}
|
|
8421
|
+
seen.add(question.id);
|
|
8422
|
+
const answer = await ask(question, io);
|
|
8423
|
+
if (answer.reason === "cancelled") return 0;
|
|
8424
|
+
if (answer.reason === "timeout") return 0;
|
|
8425
|
+
const posted = await api.postAnswer({
|
|
8426
|
+
questionId: question.id,
|
|
8427
|
+
choiceIndex: answer.skipped ? null : answer.choiceIndex,
|
|
8428
|
+
skipped: answer.skipped,
|
|
8429
|
+
msToAnswer: answer.msToAnswer
|
|
8430
|
+
});
|
|
8431
|
+
showFeedback(answer, posted.ok ? posted.data : null, io, question);
|
|
8432
|
+
if (round === MAX_QUESTIONS - 1) return 0;
|
|
8433
|
+
if (!await confirmAnother(io)) return 0;
|
|
8434
|
+
}
|
|
8435
|
+
return 0;
|
|
8436
|
+
} catch (err) {
|
|
8437
|
+
logLine(`quiz failed: ${errMessage(err)}`, deps.env);
|
|
8438
|
+
return 0;
|
|
8439
|
+
} finally {
|
|
8440
|
+
try {
|
|
8441
|
+
io?.close();
|
|
8442
|
+
} catch {
|
|
8443
|
+
}
|
|
8444
|
+
}
|
|
8445
|
+
}
|
|
8446
|
+
|
|
8097
8447
|
// src/hooks-run.ts
|
|
8098
8448
|
var import_node_child_process3 = require("node:child_process");
|
|
8099
8449
|
var import_node_fs8 = __toESM(require("node:fs"), 1);
|
|
@@ -8338,9 +8688,9 @@ function splitDiff(diff) {
|
|
|
8338
8688
|
const f2 = /^(?:---|\+\+\+) (.+)$/.exec(line);
|
|
8339
8689
|
const value = r2?.[3] ?? f2?.[1];
|
|
8340
8690
|
if (!value) continue;
|
|
8341
|
-
const
|
|
8342
|
-
if (
|
|
8343
|
-
const p3 = r2 ?
|
|
8691
|
+
const clean2 = unquotePath(value.replace(/\t.*$/, ""));
|
|
8692
|
+
if (clean2 === "/dev/null") continue;
|
|
8693
|
+
const p3 = r2 ? clean2 : stripPrefix(clean2);
|
|
8344
8694
|
paths.add(p3);
|
|
8345
8695
|
if (r2?.[2] === "to" || line.startsWith("+++ ")) newPath = p3;
|
|
8346
8696
|
}
|
|
@@ -8392,111 +8742,6 @@ function filterDiff(rawDiff, opts = {}) {
|
|
|
8392
8742
|
return { diff: kept.join(""), files, truncated };
|
|
8393
8743
|
}
|
|
8394
8744
|
|
|
8395
|
-
// src/quiz.ts
|
|
8396
|
-
var import_node_fs7 = __toESM(require("node:fs"), 1);
|
|
8397
|
-
var import_node_tty2 = __toESM(require("node:tty"), 1);
|
|
8398
|
-
function openTty() {
|
|
8399
|
-
try {
|
|
8400
|
-
if (!process.stderr.isTTY) return null;
|
|
8401
|
-
if (import_node_tty2.default.isatty(0)) {
|
|
8402
|
-
return { input: process.stdin, output: process.stderr, close: () => process.stdin.pause() };
|
|
8403
|
-
}
|
|
8404
|
-
const device = process.platform === "win32" ? "\\\\.\\CONIN$" : "/dev/tty";
|
|
8405
|
-
const fd = import_node_fs7.default.openSync(device, process.platform === "win32" ? "r+" : "r");
|
|
8406
|
-
const input = new import_node_tty2.default.ReadStream(fd);
|
|
8407
|
-
return {
|
|
8408
|
-
input,
|
|
8409
|
-
output: process.stderr,
|
|
8410
|
-
close: () => {
|
|
8411
|
-
try {
|
|
8412
|
-
input.destroy();
|
|
8413
|
-
} catch {
|
|
8414
|
-
}
|
|
8415
|
-
}
|
|
8416
|
-
};
|
|
8417
|
-
} catch {
|
|
8418
|
-
return null;
|
|
8419
|
-
}
|
|
8420
|
-
}
|
|
8421
|
-
var MAX_SNIPPET_LINES = 24;
|
|
8422
|
-
function snippetForDisplay(snippet) {
|
|
8423
|
-
const lines = snippet.replace(/\t/g, " ").split(/\r?\n/);
|
|
8424
|
-
if (lines.length <= MAX_SNIPPET_LINES) return lines.join("\n");
|
|
8425
|
-
return [
|
|
8426
|
-
...lines.slice(0, MAX_SNIPPET_LINES),
|
|
8427
|
-
`... (${lines.length - MAX_SNIPPET_LINES} more lines)`
|
|
8428
|
-
].join("\n");
|
|
8429
|
-
}
|
|
8430
|
-
async function askQuestion(question, io, opts = {}) {
|
|
8431
|
-
const timeoutMs = opts.timeoutMs ?? ANSWER_TIMEOUT_MS;
|
|
8432
|
-
const now = opts.now ?? Date.now;
|
|
8433
|
-
const { codeRef } = question;
|
|
8434
|
-
const seconds = Math.round(timeoutMs / 1e3);
|
|
8435
|
-
note(
|
|
8436
|
-
snippetForDisplay(codeRef.snippet),
|
|
8437
|
-
`${codeRef.file}:${codeRef.lineStart}-${codeRef.lineEnd}`,
|
|
8438
|
-
{ output: io.output }
|
|
8439
|
-
);
|
|
8440
|
-
const controller = new AbortController();
|
|
8441
|
-
const state = { reason: "cancelled" };
|
|
8442
|
-
const onData = (chunk) => {
|
|
8443
|
-
const text2 = chunk.toString();
|
|
8444
|
-
if (text2 === "s" || text2 === "S") {
|
|
8445
|
-
state.reason = "skipped";
|
|
8446
|
-
controller.abort();
|
|
8447
|
-
}
|
|
8448
|
-
};
|
|
8449
|
-
io.input.on("data", onData);
|
|
8450
|
-
const timer = setTimeout(() => {
|
|
8451
|
-
state.reason = "timeout";
|
|
8452
|
-
controller.abort();
|
|
8453
|
-
}, timeoutMs);
|
|
8454
|
-
const startedAt = now();
|
|
8455
|
-
try {
|
|
8456
|
-
const result = await select({
|
|
8457
|
-
message: `${question.stem} (enter to answer, s to skip, ${seconds}s)`,
|
|
8458
|
-
options: question.choices.map((choice, index) => ({ value: index, label: choice })),
|
|
8459
|
-
input: io.input,
|
|
8460
|
-
output: io.output,
|
|
8461
|
-
signal: controller.signal
|
|
8462
|
-
});
|
|
8463
|
-
const msToAnswer = Math.max(0, Math.round(now() - startedAt));
|
|
8464
|
-
if (typeof result === "number") {
|
|
8465
|
-
return { choiceIndex: result, skipped: false, msToAnswer, reason: "answered" };
|
|
8466
|
-
}
|
|
8467
|
-
return {
|
|
8468
|
-
choiceIndex: null,
|
|
8469
|
-
skipped: true,
|
|
8470
|
-
msToAnswer: state.reason === "timeout" ? timeoutMs : msToAnswer,
|
|
8471
|
-
reason: state.reason
|
|
8472
|
-
};
|
|
8473
|
-
} catch {
|
|
8474
|
-
return { choiceIndex: null, skipped: true, msToAnswer: 0, reason: "cancelled" };
|
|
8475
|
-
} finally {
|
|
8476
|
-
clearTimeout(timer);
|
|
8477
|
-
io.input.off("data", onData);
|
|
8478
|
-
}
|
|
8479
|
-
}
|
|
8480
|
-
function oneLine(text2, max = 140) {
|
|
8481
|
-
const flat = text2.replace(/\s+/g, " ").trim();
|
|
8482
|
-
return flat.length > max ? `${flat.slice(0, max - 1)}\u2026` : flat;
|
|
8483
|
-
}
|
|
8484
|
-
function showFeedback(answer, result, io, question) {
|
|
8485
|
-
const opts = { output: io.output };
|
|
8486
|
-
if (answer.skipped) {
|
|
8487
|
-
log.info("Skipped.", opts);
|
|
8488
|
-
} else if (!result) {
|
|
8489
|
-
log.warn("Answer not recorded (offline), push continues.", opts);
|
|
8490
|
-
} else if (result.correct) {
|
|
8491
|
-
log.success(`Correct! Streak: ${result.streak}. ${oneLine(result.explanation, 90)}`, opts);
|
|
8492
|
-
} else {
|
|
8493
|
-
const letter = String.fromCharCode(65 + result.correctIndex);
|
|
8494
|
-
const choice = question?.choices[result.correctIndex];
|
|
8495
|
-
const key = choice ? `${letter}) ${oneLine(choice, 40)}` : letter;
|
|
8496
|
-
log.error(`Not quite, answer: ${key}. ${oneLine(result.explanation)}`, opts);
|
|
8497
|
-
}
|
|
8498
|
-
}
|
|
8499
|
-
|
|
8500
8745
|
// src/redact.ts
|
|
8501
8746
|
var REDACTED = "[REDACTED]";
|
|
8502
8747
|
var TOKEN_PATTERNS = [
|
|
@@ -8659,7 +8904,10 @@ async function runPrePush(deps) {
|
|
|
8659
8904
|
const token = getToken(deps.env);
|
|
8660
8905
|
if (!token) return "skipped-no-token";
|
|
8661
8906
|
io = (deps.openTty ?? openTty)();
|
|
8662
|
-
if (!io)
|
|
8907
|
+
if (!io) {
|
|
8908
|
+
logLine("pre-push: skipped-no-tty (question left pending)", deps.env);
|
|
8909
|
+
return "skipped-no-tty";
|
|
8910
|
+
}
|
|
8663
8911
|
const root = repoRoot(deps.cwd) ?? deps.cwd;
|
|
8664
8912
|
const repo = repoFullName(root);
|
|
8665
8913
|
const sha = headSha(root);
|
|
@@ -8675,7 +8923,14 @@ async function runPrePush(deps) {
|
|
|
8675
8923
|
const question = next.data.question;
|
|
8676
8924
|
if (!question) return "no-question";
|
|
8677
8925
|
deps.onQuestionShown?.();
|
|
8678
|
-
const
|
|
8926
|
+
const onStarted = () => deps.onQuestionStarted?.();
|
|
8927
|
+
const answer = await (deps.ask ?? ((q, t2, h2) => askWithOptIn(q, t2, h2)))(question, io, {
|
|
8928
|
+
onStarted
|
|
8929
|
+
});
|
|
8930
|
+
if (answer.reason === "timeout") {
|
|
8931
|
+
logLine("pre-push: timed-out (question left pending)", deps.env);
|
|
8932
|
+
return "timed-out";
|
|
8933
|
+
}
|
|
8679
8934
|
const posted = await api.postAnswer({
|
|
8680
8935
|
questionId: question.id,
|
|
8681
8936
|
choiceIndex: answer.skipped ? null : answer.choiceIndex,
|
|
@@ -8720,7 +8975,8 @@ async function hookMain(name, rest, deps) {
|
|
|
8720
8975
|
await (deps.prePush ?? runPrePush)({
|
|
8721
8976
|
cwd: deps.cwd,
|
|
8722
8977
|
env: deps.env,
|
|
8723
|
-
onQuestionShown: () => arm(b3.quiz ?? PRE_PUSH_QUIZ_BUDGET_MS)
|
|
8978
|
+
onQuestionShown: () => arm(b3.quiz ?? PRE_PUSH_QUIZ_BUDGET_MS),
|
|
8979
|
+
onQuestionStarted: () => arm(b3.answer ?? PRE_PUSH_ANSWER_BUDGET_MS)
|
|
8724
8980
|
});
|
|
8725
8981
|
}
|
|
8726
8982
|
} catch (err) {
|
|
@@ -8736,6 +8992,8 @@ var HELP = `codeconscious: understand the code you ship
|
|
|
8736
8992
|
Usage:
|
|
8737
8993
|
codeconscious init sign in and install git hooks in this repository
|
|
8738
8994
|
(--force: also write hooks into a tracked hooks dir such as .husky)
|
|
8995
|
+
codeconscious quiz answer pending questions for your recent commits
|
|
8996
|
+
(run it after an AI agent committed for you)
|
|
8739
8997
|
codeconscious status show login and hook state
|
|
8740
8998
|
codeconscious uninstall remove the hooks from this repository (--all also signs out)
|
|
8741
8999
|
|
|
@@ -8767,6 +9025,9 @@ async function main() {
|
|
|
8767
9025
|
case "uninstall":
|
|
8768
9026
|
process.exit(await runUninstall({ ...deps, all: rest.includes("--all") }));
|
|
8769
9027
|
break;
|
|
9028
|
+
case "quiz":
|
|
9029
|
+
process.exit(await runQuiz({ cwd: deps.cwd, env: deps.env }));
|
|
9030
|
+
break;
|
|
8770
9031
|
case "status":
|
|
8771
9032
|
process.exit(runStatus(deps));
|
|
8772
9033
|
break;
|
package/package.json
CHANGED