pi-python-helper 0.1.1 → 0.2.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.
@@ -2,6 +2,12 @@ import { Type } from 'typebox';
2
2
  import { failure, result, warn } from '../../src/core/result.ts';
3
3
  import { runCommand } from '../../src/core/runner.ts';
4
4
  import { pytestCommand, uvLockCheck, uvSyncFrozen } from '../../src/build/commands.ts';
5
+ import { describeRemovals, parseSyncOutput } from '../../src/build/sync.ts';
6
+ import {
7
+ qualityCommands,
8
+ selectQualityRunners,
9
+ type QualityRunner,
10
+ } from '../../src/build/quality.ts';
5
11
  import { diagnoseFailure } from '../../src/build/failure.ts';
6
12
  import { parsePytestOutput } from '../../src/build/pytest.ts';
7
13
  import { detectStaleArtifacts } from '../../src/build/staleness.ts';
@@ -10,18 +16,29 @@ import {
10
16
  requiredDeclarationsFrom,
11
17
  } from '../../src/project/conformance.ts';
12
18
  import { readInstalledDistributions } from '../../src/project/installed.ts';
19
+ import { buildDeclaredIndex } from '../../src/dependencies/plan.ts';
20
+ import { checkRequiredTools } from '../../src/environment/tools.ts';
13
21
  import { summarizeValidation, type ValidationStep } from '../../src/validation/bundle.ts';
14
22
  import { buildCompletionEvidence } from '../../src/validation/evidence.ts';
15
23
  import { checkTdd } from '../../src/validation/tdd.ts';
16
24
  import { join } from 'node:path';
17
25
  import { isFile } from '../../src/project/root.ts';
18
26
  import { runScanProject } from '../../src/project/scanner.ts';
19
- import { messageOf, resolveProjectRoot, text, type Pi } from '../shared.ts';
27
+ import { hasDirectory, messageOf, resolveProjectRoot, text, type Pi } from '../shared.ts';
20
28
 
21
29
  function stepFrom(run: { code: number | null; timedOut: boolean }): ValidationStep {
22
30
  return { executed: true, ok: run.code === 0 && !run.timedOut, exitCode: run.code };
23
31
  }
24
32
 
33
+ function skippedStep(name: string, reason: string): ValidationStep {
34
+ return { name, executed: false, ok: false, exitCode: null, skippedReason: reason };
35
+ }
36
+
37
+ /** A lock/quality step that the caller disabled rather than one that failed. */
38
+ function disabledStep(name: string, reason: string): ValidationStep {
39
+ return { name, executed: false, ok: true, exitCode: null, skippedReason: reason };
40
+ }
41
+
25
42
  export function registerValidationTools(pi: Pi): void {
26
43
  pi.registerTool({
27
44
  name: 'py_sync',
@@ -31,11 +48,19 @@ export function registerValidationTools(pi: Pi): void {
31
48
  promptSnippet: 'Preview or run the uv environment sync',
32
49
  promptGuidelines: [
33
50
  'Use py_sync with execute=false to preview the uv command, and execute=true only when the environment must be created or refreshed.',
51
+ 'Use py_sync after changing pyproject.toml or uv.lock; a sync that removed packages is reported because later steps cannot run without them.',
34
52
  ],
35
53
  parameters: Type.Object({
36
54
  mode: Type.Optional(
37
55
  Type.Union([Type.Literal('check'), Type.Literal('sync')], {
38
- description: 'check runs uv lock --check; sync runs uv sync --frozen --all-groups.',
56
+ description:
57
+ 'check runs uv lock --check; sync runs uv sync --frozen --all-groups --all-extras.',
58
+ }),
59
+ ),
60
+ extras: Type.Optional(
61
+ Type.Union([Type.Literal('all'), Type.Literal('none')], {
62
+ description:
63
+ 'Whether sync requests every [project.optional-dependencies] extra. Defaults to all: without it uv removes extras such as the dev tooling.',
39
64
  }),
40
65
  ),
41
66
  execute: Type.Optional(Type.Boolean()),
@@ -47,7 +72,8 @@ export function registerValidationTools(pi: Pi): void {
47
72
  try {
48
73
  const root = (await resolveProjectRoot(ctx.cwd, params.path)) ?? ctx.cwd;
49
74
  const mode = params.mode ?? 'check';
50
- const command = mode === 'check' ? uvLockCheck(ctx.cwd) : uvSyncFrozen(ctx.cwd);
75
+ const extras = params.extras ?? 'all';
76
+ const command = mode === 'check' ? uvLockCheck(ctx.cwd) : uvSyncFrozen(ctx.cwd, { extras });
51
77
  const lockPresent = await isFile(join(root, 'uv.lock'));
52
78
 
53
79
  if (!params.execute) {
@@ -55,7 +81,7 @@ export function registerValidationTools(pi: Pi): void {
55
81
  result(ctx.cwd, started, {
56
82
  ok: true,
57
83
  summary: `${mode} command preview generated; nothing was executed.`,
58
- data: { executed: false, mode, command, lockPresent },
84
+ data: { executed: false, mode, extras, command, lockPresent },
59
85
  evidence: [{ kind: 'command_preview', ...command }],
60
86
  warnings: lockPresent
61
87
  ? []
@@ -90,51 +116,148 @@ export function registerValidationTools(pi: Pi): void {
90
116
  });
91
117
  const output = `${run.stdout}\n${run.stderr}`;
92
118
  const diagnosis = run.code === 0 ? undefined : diagnoseFailure(output);
119
+ if (mode === 'check') {
120
+ return text(
121
+ result(ctx.cwd, started, {
122
+ ok: run.code === 0,
123
+ summary: run.timedOut
124
+ ? 'uv exceeded the time limit and was terminated.'
125
+ : run.code === 0
126
+ ? 'uv.lock matches pyproject.toml.'
127
+ : `uv check failed: ${diagnosis?.summary ?? 'see the captured output.'}`,
128
+ data: {
129
+ executed: true,
130
+ mode,
131
+ exitCode: run.code,
132
+ truncated: run.truncated,
133
+ timedOut: run.timedOut,
134
+ stdoutTail: run.stdout.slice(-4000),
135
+ stderrTail: run.stderr.slice(-4000),
136
+ diagnosis,
137
+ },
138
+ evidence: [{ kind: 'uv_command', mode, exitCode: run.code, executed: true }],
139
+ warnings: run.truncated
140
+ ? [
141
+ {
142
+ code: 'OUTPUT_TRUNCATED',
143
+ message: 'Command output was truncated; only the tail is reported.',
144
+ severity: 'warning' as const,
145
+ },
146
+ ]
147
+ : [],
148
+ errors:
149
+ run.code === 0
150
+ ? []
151
+ : [
152
+ {
153
+ code: 'LOCKFILE_OUT_OF_DATE',
154
+ message:
155
+ diagnosis?.summary ??
156
+ run.stderr.trim().slice(0, 500) ??
157
+ 'uv exited with a non-zero status.',
158
+ severity: 'error' as const,
159
+ },
160
+ ],
161
+ suggestions: diagnosis?.suggestions ?? [],
162
+ commands: [command],
163
+ truncated: run.truncated,
164
+ projectRoot: root,
165
+ }),
166
+ );
167
+ }
168
+
169
+ // A sync that removed distributions is the explanation for every later
170
+ // "command not found", so it is reported even on exit code 0.
171
+ const inventory = parseSyncOutput(run.stdout, run.stderr);
172
+ const removals = describeRemovals(inventory);
173
+ const venvPath = join(root, '.venv');
174
+ const venvDir = (await hasDirectory(venvPath)) ? venvPath : undefined;
175
+ const toolchain = await checkRequiredTools(venvDir, ['pytest']);
176
+ const pytestMissing = toolchain.checked && toolchain.missing.includes('pytest');
93
177
 
94
178
  return text(
95
179
  result(ctx.cwd, started, {
96
- ok: run.code === 0,
180
+ ok: run.code === 0 && !pytestMissing,
97
181
  summary: run.timedOut
98
182
  ? 'uv exceeded the time limit and was terminated.'
99
- : run.code === 0
100
- ? mode === 'check'
101
- ? 'uv.lock matches pyproject.toml.'
102
- : 'The environment was synchronised from the lockfile.'
103
- : `uv ${mode} failed: ${diagnosis?.summary ?? 'see the captured output.'}`,
183
+ : run.code !== 0
184
+ ? `uv sync failed: ${diagnosis?.summary ?? 'see the captured output.'}`
185
+ : pytestMissing
186
+ ? 'The environment was synchronised, but pytest is not installed in .venv afterwards.'
187
+ : removals
188
+ ? `The environment was synchronised from the lockfile. ${removals}`
189
+ : 'The environment was synchronised from the lockfile.',
104
190
  data: {
105
191
  executed: true,
106
192
  mode,
193
+ extras,
107
194
  exitCode: run.code,
108
195
  truncated: run.truncated,
109
196
  timedOut: run.timedOut,
197
+ inventory,
198
+ removed: inventory.uninstalled,
199
+ toolchain,
110
200
  stdoutTail: run.stdout.slice(-4000),
111
201
  stderrTail: run.stderr.slice(-4000),
112
202
  diagnosis,
113
203
  },
114
- evidence: [{ kind: 'uv_command', mode, exitCode: run.code, executed: true }],
115
- warnings: run.truncated
116
- ? [
117
- {
118
- code: 'OUTPUT_TRUNCATED',
119
- message: 'Command output was truncated; only the tail is reported.',
120
- severity: 'warning' as const,
121
- },
122
- ]
123
- : [],
204
+ evidence: [
205
+ {
206
+ kind: 'uv_command',
207
+ mode,
208
+ extras,
209
+ exitCode: run.code,
210
+ uninstalled: inventory.uninstalled,
211
+ toolchainMissing: toolchain.missing,
212
+ executed: true,
213
+ },
214
+ ],
215
+ warnings: [
216
+ ...(run.truncated
217
+ ? [
218
+ {
219
+ code: 'OUTPUT_TRUNCATED',
220
+ message: 'Command output was truncated; only the tail is reported.',
221
+ severity: 'warning' as const,
222
+ },
223
+ ]
224
+ : []),
225
+ ...(removals
226
+ ? [
227
+ warn(
228
+ 'SYNC_REMOVED_PACKAGES',
229
+ `${removals} Request the extras that provide them (for example uv sync --extra dev) or install everything with uv sync --all-extras.`,
230
+ join(root, 'uv.lock'),
231
+ ),
232
+ ]
233
+ : []),
234
+ ],
124
235
  errors:
125
- run.code === 0
236
+ run.code === 0 && !pytestMissing
126
237
  ? []
127
238
  : [
128
239
  {
129
- code: mode === 'check' ? 'LOCKFILE_OUT_OF_DATE' : 'SYNC_FAILED',
130
- message:
131
- diagnosis?.summary ??
132
- run.stderr.trim().slice(0, 500) ??
133
- 'uv exited with a non-zero status.',
240
+ code: pytestMissing ? 'TOOLCHAIN_BROKEN' : 'SYNC_FAILED',
241
+ message: pytestMissing
242
+ ? 'pytest is not installed in the project environment after uv sync, so the test step cannot run.'
243
+ : (diagnosis?.summary ??
244
+ run.stderr.trim().slice(0, 500) ??
245
+ 'uv exited with a non-zero status.'),
134
246
  severity: 'error' as const,
135
247
  },
136
248
  ],
137
- suggestions: diagnosis?.suggestions ?? [],
249
+ suggestions: pytestMissing
250
+ ? [
251
+ {
252
+ message:
253
+ extras === 'all'
254
+ ? 'pytest is declared in an extra that uv still removed. Check that the extra name is spelled correctly in [project.optional-dependencies].'
255
+ : 'Re-run the sync with extras=all, or add the extra that provides pytest.',
256
+ confidence: 'high' as const,
257
+ command: 'uv sync --frozen --all-groups --all-extras',
258
+ },
259
+ ]
260
+ : (diagnosis?.suggestions ?? []),
138
261
  commands: [command],
139
262
  truncated: run.truncated,
140
263
  projectRoot: root,
@@ -158,6 +281,12 @@ export function registerValidationTools(pi: Pi): void {
158
281
  ],
159
282
  parameters: Type.Object({
160
283
  targets: Type.Optional(Type.Array(Type.String())),
284
+ quality: Type.Optional(
285
+ Type.Boolean({
286
+ description:
287
+ 'Run the lint/type tools the project declares (ruff, pyright, and mypy when [tool.mypy] exists). Defaults to true.',
288
+ }),
289
+ ),
161
290
  execute: Type.Optional(Type.Boolean()),
162
291
  timeoutSeconds: Type.Optional(Type.Integer({ minimum: 1, maximum: 3600 })),
163
292
  path: Type.Optional(Type.String()),
@@ -171,14 +300,44 @@ export function registerValidationTools(pi: Pi): void {
171
300
  const sync = uvSyncFrozen(ctx.cwd);
172
301
  const test = pytestCommand(ctx.cwd, { targets: params.targets });
173
302
  const timeoutMs = (params.timeoutSeconds ?? 1800) * 1000;
174
- const commands = [lock, sync, test];
303
+ const runQuality = params.quality ?? true;
304
+
305
+ // The declared quality tools are read from the manifest so the bundle can
306
+ // match what CI runs without a separate configuration file.
307
+ const manifestScan = await runScanProject(ctx.cwd, { root, mode: 'manifest' }, signal);
308
+ const runners: QualityRunner[] = runQuality
309
+ ? selectQualityRunners({
310
+ declared: buildDeclaredIndex(
311
+ manifestScan.payload?.manifest ?? {
312
+ dependencies: [],
313
+ optionalDependencies: {},
314
+ dependencyGroups: {},
315
+ },
316
+ ).keys(),
317
+ toolConfiguration: manifestScan.payload?.manifest?.toolConfiguration,
318
+ })
319
+ : [];
320
+ const quality = qualityCommands(ctx.cwd, runners);
321
+ const commands = [lock, sync, test, ...quality];
175
322
 
176
323
  if (!params.execute) {
177
324
  return text(
178
325
  result(ctx.cwd, started, {
179
326
  ok: true,
180
- summary: `Validation sequence preview generated (${lockPresent ? 'uv lock --check, uv sync, pytest' : 'uv sync, pytest'}); nothing was executed.`,
181
- data: { executed: false, lockPresent, steps: commands },
327
+ summary: `Validation sequence preview generated (${[
328
+ lockPresent ? 'uv lock --check' : undefined,
329
+ 'uv sync',
330
+ 'pytest',
331
+ ...runners.map((runner) => runner.name),
332
+ ]
333
+ .filter(Boolean)
334
+ .join(', ')}); nothing was executed.`,
335
+ data: {
336
+ executed: false,
337
+ lockPresent,
338
+ quality: runners.map((runner) => runner.name),
339
+ steps: commands,
340
+ },
182
341
  evidence: [
183
342
  {
184
343
  kind: 'validation_preview',
@@ -197,7 +356,8 @@ export function registerValidationTools(pi: Pi): void {
197
356
  errors: [],
198
357
  suggestions: [
199
358
  {
200
- message: 'Set execute=true to run the sequence. This creates or refreshes .venv.',
359
+ message:
360
+ 'Set execute=true to run the sequence. This creates or refreshes .venv, including every declared extra.',
201
361
  confidence: 'high' as const,
202
362
  },
203
363
  ],
@@ -221,17 +381,35 @@ export function registerValidationTools(pi: Pi): void {
221
381
  timeoutMs,
222
382
  maxBytes: 256 * 1024,
223
383
  });
224
- const testRun =
225
- syncRun.code === 0
226
- ? await runCommand(test.executable, test.args, {
227
- cwd: ctx.cwd,
228
- signal,
229
- timeoutMs,
230
- maxBytes: 512 * 1024,
231
- })
232
- : undefined;
384
+ const syncInventory = parseSyncOutput(syncRun.stdout, syncRun.stderr);
385
+ const removals = describeRemovals(syncInventory);
386
+
387
+ // A sync can succeed while removing the interpreter-side tools the next
388
+ // step needs, so availability is re-checked instead of trusting exit 0.
389
+ const venvPath = join(root, '.venv');
390
+ const venvDir = (await hasDirectory(venvPath)) ? venvPath : undefined;
391
+ const toolchain = await checkRequiredTools(venvDir, [
392
+ 'pytest',
393
+ ...runners.map((runner) => runner.name),
394
+ ]);
395
+ const pytestMissing = toolchain.checked && toolchain.missing.includes('pytest');
396
+ const syncOk = syncRun.code === 0 && !pytestMissing;
397
+ const skippedReason = pytestMissing
398
+ ? 'pytest is not installed in the project environment after uv sync, so the test step was skipped and no test result exists.'
399
+ : undefined;
400
+
401
+ const testRun = syncOk
402
+ ? await runCommand(test.executable, test.args, {
403
+ cwd: ctx.cwd,
404
+ signal,
405
+ timeoutMs,
406
+ maxBytes: 512 * 1024,
407
+ })
408
+ : undefined;
233
409
 
234
410
  const report = testRun ? parsePytestOutput(testRun.stdout, testRun.stderr) : undefined;
411
+ const testPassed = Boolean(report && testRun?.code === 0 && !report.incomplete);
412
+
235
413
  const scan = await runScanProject(ctx.cwd, { root, mode: 'manifest' }, signal);
236
414
  const installed = await readInstalledDistributions(join(root, '.venv'));
237
415
  const conformance = compareInstalledConformance({
@@ -244,19 +422,63 @@ export function registerValidationTools(pi: Pi): void {
244
422
 
245
423
  const lockStep: ValidationStep = lockRun
246
424
  ? stepFrom(lockRun)
247
- : { executed: false, ok: false, exitCode: null };
248
- const syncStep = stepFrom(syncRun);
425
+ : disabledStep(
426
+ 'uv lock --check',
427
+ 'uv.lock was not found, so the lock check was skipped.',
428
+ );
429
+ const syncStep: ValidationStep = syncOk
430
+ ? stepFrom(syncRun)
431
+ : {
432
+ ...stepFrom(syncRun),
433
+ ok: false,
434
+ skippedReason,
435
+ };
249
436
  const testStep: ValidationStep = testRun
250
437
  ? {
251
438
  ...stepFrom(testRun),
252
439
  failures: report ? report.counts.failed + report.counts.errors : undefined,
253
440
  }
254
- : { executed: false, ok: false, exitCode: null };
441
+ : skippedStep(
442
+ 'pytest',
443
+ syncRun.code !== 0
444
+ ? 'The sync step did not complete, so pytest was not run.'
445
+ : (skippedReason ?? 'pytest was not run.'),
446
+ );
447
+
448
+ // Quality commands only run once the tests pass: a failing test is the
449
+ // first actionable signal, and running both wastes the caller's budget.
450
+ const qualitySteps: ValidationStep[] = [];
451
+ for (const [index, runner] of runners.entries()) {
452
+ if (!testPassed) {
453
+ qualitySteps.push(
454
+ skippedStep(runner.name, 'Tests did not pass, so the quality check was skipped.'),
455
+ );
456
+ continue;
457
+ }
458
+ if (toolchain.checked && toolchain.missing.includes(runner.name)) {
459
+ qualitySteps.push(
460
+ skippedStep(
461
+ runner.name,
462
+ `${runner.name} is not installed in the project environment, so the quality check was skipped.`,
463
+ ),
464
+ );
465
+ continue;
466
+ }
467
+ const preview = quality[index];
468
+ const run = await runCommand(preview.executable, preview.args, {
469
+ cwd: ctx.cwd,
470
+ signal,
471
+ timeoutMs,
472
+ maxBytes: 256 * 1024,
473
+ });
474
+ qualitySteps.push({ name: runner.name, ...stepFrom(run) });
475
+ }
255
476
 
256
477
  const summary = summarizeValidation({
257
478
  lock: lockStep,
258
479
  sync: syncStep,
259
480
  test: testStep,
481
+ quality: qualitySteps,
260
482
  conformance: conformance.verdict,
261
483
  stale: staleness.stale,
262
484
  });
@@ -274,12 +496,17 @@ export function registerValidationTools(pi: Pi): void {
274
496
  executed: true,
275
497
  checks: summary.checks,
276
498
  lock: { ...lockStep, present: lockPresent },
277
- sync: syncStep,
499
+ sync: {
500
+ ...syncStep,
501
+ uninstalled: syncInventory.uninstalled,
502
+ toolchainMissing: toolchain.missing,
503
+ },
278
504
  test: {
279
505
  ...testStep,
280
506
  counts: report?.counts,
281
507
  failures: report?.failures.slice(0, 20),
282
508
  },
509
+ quality: qualitySteps,
283
510
  conformance,
284
511
  staleArtifacts: staleness,
285
512
  firstFailure: diagnosis,
@@ -290,14 +517,32 @@ export function registerValidationTools(pi: Pi): void {
290
517
  checks: summary.checks,
291
518
  lockExitCode: lockStep.exitCode ?? null,
292
519
  syncExitCode: syncStep.exitCode ?? null,
520
+ syncUninstalled: syncInventory.uninstalled,
293
521
  testExitCode: testStep.exitCode ?? null,
294
522
  testCounts: report?.counts ?? null,
523
+ quality: qualitySteps.map((step) => ({ name: step.name, ok: step.ok })),
295
524
  conformanceVerdict: conformance.verdict,
296
525
  stale: staleness.stale,
297
526
  },
298
527
  ],
299
528
  warnings: [
529
+ ...(removals
530
+ ? [
531
+ warn(
532
+ 'SYNC_REMOVED_PACKAGES',
533
+ `${removals} The bundle requests every extra, so a removal here means the tool is not declared as one.`,
534
+ join(root, 'uv.lock'),
535
+ ),
536
+ ]
537
+ : []),
300
538
  ...conformance.warnings,
539
+ ...qualitySteps
540
+ .filter((step) => step.executed && !step.ok)
541
+ .map((step) => ({
542
+ code: 'QUALITY_CHECK_FAILED',
543
+ message: `${step.name ?? 'quality check'} exited with code ${step.exitCode ?? 'unknown'}.`,
544
+ severity: 'warning' as const,
545
+ })),
301
546
  ...staleness.artifacts.map((artifact) => ({
302
547
  code: artifact.code,
303
548
  message: artifact.message,
@@ -331,6 +576,16 @@ export function registerValidationTools(pi: Pi): void {
331
576
  'Fix the failing step before reporting completion; a partial run is not evidence.',
332
577
  confidence: 'high' as const,
333
578
  },
579
+ ...(pytestMissing
580
+ ? [
581
+ {
582
+ message:
583
+ 'pytest is declared in an extra but is not installed after uv sync. Remove the extras=none override, or add the extra that provides pytest to [project.optional-dependencies].',
584
+ confidence: 'high' as const,
585
+ command: 'uv sync --frozen --all-groups --all-extras',
586
+ },
587
+ ]
588
+ : []),
334
589
  ...(diagnosis?.suggestions ?? []),
335
590
  ],
336
591
  commands,
@@ -374,7 +629,7 @@ export function registerValidationTools(pi: Pi): void {
374
629
  .filter(Boolean);
375
630
  source = 'git diff';
376
631
  }
377
- const checkpoint = checkTdd(changedPaths, params.testChangedPaths ?? changedPaths);
632
+ const checkpoint = checkTdd(changedPaths, params.testChangedPaths ?? []);
378
633
  return text(
379
634
  result(ctx.cwd, started, {
380
635
  ok: checkpoint.ok,
@@ -382,7 +637,16 @@ export function registerValidationTools(pi: Pi): void {
382
637
  ? `TDD checkpoint passed across ${changedPaths.length} changed path(s) from ${source}.`
383
638
  : 'TDD checkpoint found production changes without a related test change.',
384
639
  data: { ...checkpoint, changedPaths, source },
385
- evidence: checkpoint.reasons.map((message) => ({ kind: 'tdd_blocker', message })),
640
+ evidence: [
641
+ ...checkpoint.reasons.map((message) => ({ kind: 'tdd_blocker', message })),
642
+ ...checkpoint.associations.map((entry) => ({
643
+ kind: 'tdd_association',
644
+ source: entry.source,
645
+ test: entry.test,
646
+ sharedTokens: entry.sharedTokens,
647
+ strength: entry.strength,
648
+ })),
649
+ ],
386
650
  warnings: checkpoint.reasons.map((message) => ({
387
651
  code: 'TDD_CHECKPOINT',
388
652
  message,
@@ -35,7 +35,9 @@ from pathlib import Path
35
35
 
36
36
  # Bumped whenever the request or the result document changes shape, so the
37
37
  # caller can refuse to interpret a document it does not understand.
38
- SCANNER_VERSION = 1
38
+ # 2: each scanned file reports `importModules`, the full dotted module names it
39
+ # references, so test selection can map a test file to the module it imports.
40
+ SCANNER_VERSION = 2
39
41
 
40
42
  KNOWN_SECTIONS = ("environment", "manifest", "imports")
41
43
  EXIT_OK = 0
@@ -510,9 +512,12 @@ def _is_type_checking_test(test) -> bool:
510
512
 
511
513
 
512
514
  class ImportCollector(ast.NodeVisitor):
515
+ """Collect top-level import names and the full dotted modules they reference."""
516
+
513
517
  def __init__(self) -> None:
514
518
  self.all: set[str] = set()
515
519
  self.type_checking: set[str] = set()
520
+ self.modules: set[str] = set()
516
521
  self._guard_depth = 0
517
522
 
518
523
  def _record(self, name: str) -> None:
@@ -536,12 +541,20 @@ class ImportCollector(ast.NodeVisitor):
536
541
  def visit_Import(self, node: ast.Import) -> None:
537
542
  for alias in node.names:
538
543
  self._record(alias.name.split(".")[0])
544
+ self.modules.add(alias.name)
539
545
 
540
546
  def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
541
547
  if node.level: # relative import -> always local
548
+ for alias in node.names:
549
+ self.modules.add(alias.name)
542
550
  return
543
551
  if node.module:
544
552
  self._record(node.module.split(".")[0])
553
+ self.modules.add(node.module)
554
+ # `from pkg.db import database` names a submodule, not the package,
555
+ # so both spellings are recorded and either can match a change.
556
+ for alias in node.names:
557
+ self.modules.add(f"{node.module}.{alias.name}")
545
558
 
546
559
 
547
560
  def scan_imports(root: Path, max_files: int) -> dict:
@@ -589,6 +602,7 @@ def scan_imports(root: Path, max_files: int) -> dict:
589
602
  {
590
603
  "path": relative,
591
604
  "imports": sorted(names),
605
+ "importModules": sorted(collector.modules),
592
606
  "typeCheckingImports": sorted(collector.type_checking),
593
607
  }
594
608
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-python-helper",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Python (uv) development tools for the pi coding agent",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -10,24 +10,24 @@ license: Apache-2.0
10
10
 
11
11
  ## 조사 및 작업 순서 (Investigation order)
12
12
 
13
- 1. 인터프리터나 가상환경 상태가 불확실할 때는 `py_environment`를 실행하세요. 잘못된 Python으로 테스트를 실행하는 것이 가장 흔한 실패 원인입니다.
13
+ 1. 인터프리터나 가상환경 상태가 불확실할 때는 `py_environment`를 실행하세요. 잘못된 Python으로 테스트를 실행하는 것이 가장 흔한 실패 원인입니다. `interpreterOrigin: 'venv'`이면 분석이 프로젝트 환경을 봤다는 뜻이고, `'path'`이면 호스트 PATH의 Python을 봤다는 뜻입니다. `available`은 "지금 실행 가능", `installable`은 "uv sync로 설치 가능"(선언되었지만 실행 파일 없음)입니다.
14
14
  2. `pyproject.toml`이나 `uv.lock`을 편집하기 전에 `py_project_inspect`로 레이아웃(src/flat), 의존성 그룹, lockfile 드리프트, **환경 정합성**(선언 ↔ lock ↔ 실제 설치본)을 확인하세요. 정합성은 `consistent` / `drifted` / `unverifiable` 중 하나이며, `unverifiable`을 일치로 해석하지 마세요.
15
15
  3. 의존성을 추가/이동하기 전에 `py_dependency_plan`을 사용하세요. `ast`로 실제 import를 스캔하여 다음을 구분합니다:
16
16
  - 선언되지 않은 import (런타임 오류로 이어짐)
17
17
  - `[project] dependencies`가 아니라 dev 그룹/extra에만 선언된 런타임 import
18
18
  - `uv.lock`에 없거나 스펙을 만족하지 않는 버전
19
19
  4. import 이름과 배포 이름은 다를 수 있습니다(`PIL`/`pillow`, `yaml`/`PyYAML`). `py_dependency_plan`의 제안을 `uv add`로 적용하세요.
20
- 5. 소스 코드를 수정한 후에는 `py_test_select`로 변경 파일과 연관된 테스트를 선별하세요. 매칭이 실패하면 전체 스위트가 범위가 되며, 선택 이유가 함께 반환됩니다.
21
- 6. 테스트 실행은 `py_test`를 사용하세요. `execute=false`로 먼저 미리보기하고, 실제 실행 시에만 `execute=true`를 전달합니다.
22
- 7. 실패 출력이 있을 때는 `py_failure_diagnose`를 사용하세요. `site-packages` 내부 프레임은 원인이 아니며, 도구는 첫 번째 프로젝트 프레임을 지목합니다.
23
- 8. 의존성이 바뀌었거나 `.venv`가 오래된 경우 `py_sync`로 `uv lock --check` 또는 `uv sync --frozen`을 미리보기/실행하세요.
20
+ 5. 소스 코드를 수정한 후에는 `py_test_select`로 변경 파일과 연관된 테스트를 선별하세요. 테스트가 변경 모듈을 **실제로 import**하면 가장 강한 근거이며, 이름 규약은 그 다음입니다. `narrowed: false`나 `NO_NARROWING`은 "30개 중 30개 선택"처럼 결과가 좁혀지지 않았다는 뜻이고, `SELECTION_WITHOUT_IMPORT_EVIDENCE`는 근거가 파일 이름뿐이라는 뜻이므로 변경이 넓다면 전체 스위트나 `lastFailed=true`를 사용하세요.
21
+ 6. 테스트 실행은 `py_test`를 사용하세요. `execute=false`로 먼저 미리보기하고, 실제 실행 시에만 `execute=true`를 전달합니다. 커버리지 플래그나 `-m` 마커 선택처럼 도구가 모델링하지 않는 프로젝트 표준 옵션은 `extraArgs`로 넘기세요.
22
+ 7. 실패 출력이 있을 때는 `py_failure_diagnose`를 사용하세요. `site-packages` 내부 프레임은 원인이 아니며, 도구는 첫 번째 프로젝트 프레임을 지목합니다. 실행 파일을 찾지 못해 명령이 시작되지 못한 경우(`Failed to spawn`, `command not found`)는 `tool_not_installed`로 분류됩니다.
23
+ 8. 의존성이 바뀌었거나 `.venv`가 오래된 경우 `py_sync`로 `uv lock --check` 또는 `uv sync --frozen`을 미리보기/실행하세요. sync는 `[project.optional-dependencies]`의 extra를 함께 요청하므로 dev 도구가 extra로 선언된 프로젝트에서도 삭제되지 않습니다. `SYNC_REMOVED_PACKAGES`가 보이면 그것이 이후 "command not found"의 원인입니다.
24
24
  9. 재현이 어려운 실패는 `py_test`의 `lastFailed=true`(`--lf`)로 직전 실패만 다시 실행하세요.
25
25
  10. 작업 완료를 보고하기 전에 `py_validation_bundle`(lock 검사 → sync → pytest → 환경 정합성 → 오래된 아티팩트 검사)을 실행하고, `py_completion_evidence`로 근거가 충분한지 확인하세요. 정합성이 `drifted`나 `unverifiable`이면 테스트가 통과했어도 게이트는 실패합니다.
26
26
  11. `py_tdd_checkpoint`로 프로덕션 변경에 대응하는 테스트 변경이 있는지 확인하세요.
27
27
 
28
28
  ## 안전 규칙 (Safety)
29
29
 
30
- - `py_sync`와 `py_validation_bundle`은 `execute: true`가 명시적으로 전달되기 전까지 명령을 실행하지 않고 미리보기만 반환합니다. `execute=true`는 `.venv`를 생성/갱신하므로 사용자 확인 없이 반복 실행하지 마세요.
30
+ - `py_sync`와 `py_validation_bundle`은 `execute: true`가 명시적으로 전달되기 전까지 명령을 실행하지 않고 미리보기만 반환합니다. `execute=true`는 `.venv`를 생성/갱신하므로 사용자 확인 없이 반복 실행하지 마세요. `extras: 'none'`은 프로젝트가 요청할 때만 사용하세요: extra로 선언된 dev 도구를 삭제할 수 있습니다.
31
31
  - Python에는 ROS의 `cmd_vel`처럼 위험을 결정론적으로 알려주는 이름이 없습니다. 다음은 되돌릴 수 없는 작업으로 취급하세요: `uv publish`/`twine upload`(공개 불가 회수), `git push --force`, `git reset --hard`/`git clean -fd`, `alembic downgrade`, `DROP`/`DELETE`(WHERE 없는), `rm -rf`, `conda env remove`.
32
32
  - 가상환경을 파괴하는 명령(`rm -rf .venv`, `uv venv --clear`)이나 전역 Python에 패키지를 설치하는 명령(`pip install` without a venv)을 임의로 실행하지 마세요.
33
33
  - 도구는 파일을 쓰지 않습니다. `pyproject.toml`/`uv.lock` 수정은 항상 명시적인 편집 도구로 수행하세요.
@@ -36,6 +36,11 @@ license: Apache-2.0
36
36
 
37
37
  - `PROJECT_INSTALLED_NOT_EDITABLE`는 프로젝트가 환경에 live link가 아니라 복사본으로 설치되어, 소스 변경이 테스트에 반영되지 않음을 의미합니다. `uv sync`로 해결하세요.
38
38
  - `PROJECT_NOT_INSTALLED`는 lock이 editable 설치를 기대하는데 `.venv`에 프로젝트가 없다는 뜻입니다. `uv sync`를 실행하고, 그래도 실패하면 빌드 백엔드가 패키지를 찾지 못한 것입니다(`[project] name`과 모듈 디렉터리 이름이 일치하는지 확인).
39
+ - `PROJECT_VIRTUAL_SOURCE`는 정상입니다. `[build-system]`이 없으면 uv는 프로젝트를 `virtual` 소스로 기록하고 `.venv`에 설치하지 않습니다. 누락(`PROJECT_NOT_INSTALLED`)으로 취급하지 말고 `[build-system]` 추가 여부만 검토하세요.
40
+ - `MARKER_SPLIT_LOCK_ENTRIES`는 정상입니다. uv가 같은 배포판을 마커별로 여러 버전으로 기록한 것이며, 설치 버전이 그중 하나와 일치하면 드리프트가 아닙니다.
41
+ - `TOOL_NOT_INSTALLED`는 lock에는 있지만 `.venv`에 실행 파일이 없다는 뜻입니다. `uv sync` 직후에 발생했다면 sync가 extra를 삭제한 것이므로 `uv sync --frozen --all-groups --all-extras`로 복구하세요.
42
+ - `SYNC_REMOVED_PACKAGES`는 sync가 `.venv`에서 패키지를 제거했음을 뜻하며, 같은 실행에서 이어지는 "command not found"의 원인입니다.
43
+ - `UNMAPPED_IMPORTS`는 분석한 인터프리터가 import를 배포판에 연결하지 못했다는 뜻입니다. 이 상태에서는 `UNDECLARED_IMPORT`가 "선언 누락"이 아니라 "배포 이름 미확인"일 수 있으므로, 제안된 `uv add` 명령이 없으면 배포 이름을 직접 확인하세요.
39
44
  - `INSTALLED_VERSION_MISMATCH`는 `uv add`/`uv lock` 후 `uv sync`를 잊은 상태입니다. `uv lock --check`는 이걸 잡지 못하니(락은 최신) 테스트를 신뢰하기 전에 `uv sync --frozen`을 실행하세요.
40
45
  - `INSTALLED_PACKAGE_UNTRACKED`는 `.venv`에만 있고 lock에 없는 패키지입니다. 에이전트가 `uv pip install`로 임의 설치했을 가능성을 의심하세요.
41
46
  - `CONDITIONAL_PACKAGES_ABSENT`는 정상입니다. `sys_platform == 'win32'`나 `python_version < '3.11'` 같은 마커 때문에 해당 플랫폼에 설치되지 않은 항목이며, 드리프트로 취급하지 마세요.