@tiangong-lca/cli 0.0.22 → 0.0.24

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 CHANGED
@@ -17,8 +17,8 @@ checkPaths:
17
17
  - bin/**
18
18
  - src/cli.ts
19
19
  - src/main.ts
20
- lastReviewedAt: 2026-07-12
21
- lastReviewedCommit: 192ce9cb233af85b8bcf50136d37fd08d4ae8292
20
+ lastReviewedAt: 2026-07-14
21
+ lastReviewedCommit: ce8c18f270725adad789ada8f4582ca0e97e4117
22
22
  ---
23
23
 
24
24
  # TianGong LCA CLI
@@ -27,6 +27,10 @@ Package: `@tiangong-lca/cli` Executable: `tiangong-lca` Node: `24.x`
27
27
 
28
28
  Review note, 2026-07-12: `dataset maintenance plan/apply/verify` provides current-user RLS-scoped exact-row maintenance with immutable plans, explicit approval, per-action logs, platform audit correlation, and independent readback. `merge-support-aliases` now runs only in `target_mode=owner_draft`: source/target support and all changed rows stay private `state_code=0`; publication is a separate future workflow.
29
29
 
30
+ Review note, 2026-07-13: maintenance scans now prove exact-count pagination even when PostgREST returns fewer rows than the requested `--page-size`. An incomplete or inconsistent scan fails before artifacts, approval, or mutation; under stable filtered membership/order the proof represents a complete ordered multi-request traversal, not one transaction-level/MVCC snapshot.
31
+
32
+ Review note, 2026-07-14: maintenance now includes the protected derivative-only `rebuild-derivatives` operation. V1 plans exactly one current-owner state-0 process with `action=rebuild_derivatives`, `target_mode=owner_draft`, and components `extracted_md` plus `embedding_ft`. Apply only proves guarded-RPC admission (`accepted`/`queued`); independent verify reports `pending`, `passed`, or `failed`.
33
+
30
34
  ## Run
31
35
 
32
36
  One-off published run:
@@ -276,6 +280,7 @@ tiangong-lca dataset evidence-search plan --query "中国2026年电力结构数
276
280
  tiangong-lca dataset evidence-search run --input ./evidence-search.request.json --results ./search-results.json --out-dir /abs/path/to/evidence-search --json
277
281
  tiangong-lca dataset references rewrite --input ./rows.jsonl --from flow:<old-id>@<old-version> --to flow:<new-id>@<new-version> --out-dir /abs/path/to/dataset-rewrite --json
278
282
  tiangong-lca dataset maintenance plan --scope ./maintenance-scope.json --operation redo-import --out-dir /abs/path/to/dataset-maintenance --page-size 1000 --timeout-ms 10000 --json
283
+ tiangong-lca dataset maintenance plan --scope ./derivative-rebuild-scope.json --operation rebuild-derivatives --out-dir /abs/path/to/derivative-rebuild --json
279
284
  tiangong-lca dataset maintenance apply --plan /abs/path/to/dataset-maintenance/maintenance-plan.json --commit --approve-plan <sha256> --confirm <current-account-email> --timeout-ms 10000 --json
280
285
  tiangong-lca dataset maintenance verify --plan /abs/path/to/dataset-maintenance/maintenance-plan.json --out-dir /abs/path/to/dataset-maintenance/verify --page-size 1000 --timeout-ms 10000 --json
281
286
  tiangong-lca lifecyclemodel auto-build --input ./examples/lifecyclemodel-auto-build.request.json --out-dir /abs/path/to/lifecyclemodel-run --json
@@ -322,7 +327,7 @@ For `dataset references rewrite`, `--commit` executes the state-aware save-draft
322
327
 
323
328
  ## Dataset Maintenance
324
329
 
325
- `dataset maintenance plan/apply/verify` is the row-level cleanup surface for bad imports and the fixed BAFU private alias rewrite. It runs as the currently authenticated user and relies on RLS for visibility and ownership enforcement.
330
+ `dataset maintenance plan/apply/verify` is the row-level cleanup surface for bad imports, the fixed BAFU private alias rewrite, and one protected derivative-only process rebuild. It runs as the currently authenticated user and relies on RLS for visibility and ownership enforcement.
326
331
 
327
332
  ```bash
328
333
  tiangong-lca dataset maintenance plan \
@@ -349,21 +354,32 @@ tiangong-lca dataset maintenance verify \
349
354
  --json
350
355
  ```
351
356
 
357
+ For the derivative-only profile, use the same three commands with `--operation rebuild-derivatives`. Its scope must contain exactly one `processes` action with `action: "rebuild_derivatives"`, `target_mode: "owner_draft"`, expected current owner, expected `state_code: 0`, and the exact component set `extracted_md` plus `embedding_ft`.
358
+
359
+ `--page-size` accepts `1-5000` and is only the requested maximum. PostgREST may enforce a lower server-side cap. The CLI requests `Prefer: count=exact`, validates the exact total and returned range from each `Content-Range`, advances the next offset by the number of rows actually returned, and requires strict `id`/`version` ordering without missing or duplicate identities. Each accepted scan records per-table requested/effective page size, page count, rows fetched, exact total, and aggregate entity counts.
360
+
361
+ This completeness proof means the CLI traversed the filtered result while that table's membership and ordering keys remained stable. Because the tables are read through multiple HTTP requests, it is not a transaction-level or MVCC snapshot of one instant; same-cardinality delete/insert churn can evade total and ordering checks. Plan hashes and apply-time drift checks provide the later mutation guard, and operators must avoid concurrent maintenance of the same account while planning or clearing it.
362
+
352
363
  The scope is intentionally narrow:
353
364
 
354
365
  - Each requested row must name its table, exact `id`, exact `version`, expected current owner, and draft `state_code=0` state.
355
- - `--operation` accepts `delete`, `retire`, `redo-import`, `repair-references`, or `merge-support-aliases`; it records the operator's maintenance intent and does not broaden the eligible row actions.
366
+ - `--operation` accepts `delete`, `retire`, `redo-import`, `repair-references`, `merge-support-aliases`, or `rebuild-derivatives`; it records the operator's maintenance intent and does not broaden the eligible row actions.
356
367
  - Only current-user `contacts`, `sources`, `flows`, and `processes` can become `save_draft` or `delete` actions.
357
368
  - `merge-support-aliases` requires top-level `target_mode: "owner_draft"` and accepts only two named batches, `time` and `length_time`. The scope must bind reviewed current-owner draft source and target FP/UG exact ids/versions to 52 `update_json_ordered` actions: 25 time rows (1 FP, 10 flows, 14 processes) and 27 length-time rows (1 FP, 13 flows, 13 processes). Process actions freeze every selected exchange index, internal id, flow id/version, direction, before hash, and both amount strings.
358
369
  - The alias factors are exact decimal strings: `0.00011415525114155251` for time and `1000` for length-time. Planning requires exactly 20 and 39 selected exchanges, preserves exactly 309 other exchanges in the affected processes, and proves the fixed source-zero/target-reference postconditions. The transformation changes references and the selected `meanAmount`/`resultingAmount`; it does not delete the source FP/UG rows.
359
370
  - Source alias support, target FP/UG, and every changed flow/process must all belong to the authenticated account at `state_code=0`. Public/shared, foreign-owner, mixed-visibility, non-draft, lifecyclemodel, and unsupported action/table rows remain protected.
371
+ - `rebuild-derivatives` accepts exactly one exact-version `processes` row and is bidirectionally bound to `action=rebuild_derivatives`, `target_mode=owner_draft`, and components `extracted_md` plus `embedding_ft`. It cannot target a public/shared row, a foreign owner, a non-draft row, another table, multiple actions, or a partial/different component set. It rebuilds derivatives only; the primary process payload, owner/state, and `modified_at` remain unchanged.
360
372
  - The CLI classifies and executes an operator-authored scope; it does not decide whether rows are semantically duplicates, canonical replacements, or safe business-level cleanup targets.
361
373
 
362
- `plan` writes the frozen `maintenance-scope.json`, `rls-visible-snapshot.json`, `protected-rows.jsonl`, `reference-impact-report.json`, `maintenance-plan.json`, and `dry-run-report.json`. Alias plans additionally write `exchange-rewrite-plan.jsonl`, freeze current-owner state-0 target FP, target UG, and source UG snapshots for each batch, derive schema-valid desired payloads with matching embedded UUID/version, and include the exact closure, `modified_at`, hashes, conversion evidence, and postconditions in the approved plan. The plan SHA-256 is the approval identity; do not edit or recompute the plan after review.
374
+ `plan` accepts the account scan only after its exact-count proof is complete, then writes the frozen `maintenance-scope.json`, `rls-visible-snapshot.json`, `protected-rows.jsonl`, `reference-impact-report.json`, `maintenance-plan.json`, and `dry-run-report.json`. The snapshot, dry-run report, and newly generated plan carry the aggregate completeness proof, so it is bound into the plan SHA-256. Alias plans additionally write `exchange-rewrite-plan.jsonl`, freeze current-owner state-0 target FP, target UG, and source UG snapshots for each batch, derive schema-valid desired payloads with matching embedded UUID/version, and include the exact closure, `modified_at`, hashes, conversion evidence, and postconditions in the approved plan. A derivative rebuild plan additionally obtains a database-produced snapshot for only the exact target action and binds its primary and derivative preconditions into the plan; large markdown/vector fields are not added to the account-wide scan. The plan SHA-256 is the approval identity; do not edit or recompute the plan after review.
375
+
376
+ `apply` is write-disabled unless all three commit guards are present: `--commit`, `--approve-plan <sha256>`, and `--confirm <current-account-email>`. Before approval is persisted or any write runs, it requires a fresh complete exact-count account scan and re-checks the whole plan for drift; the current completeness proof is recorded in `approval-record.json`. Ordinary draft updates/deletes use their platform paths. The ordered `time` plus `length_time` alias request is sent once to `cmd_dataset_alias_plan_guarded` with `target_visibility=owner_draft`; the CLI has neither a per-dimension fallback nor a 52-write sequential fallback. The RPC locks and validates the complete 52-row/59-exchange closure before both dimensions commit, so a second-dimension failure rolls back the first. It checks actor ownership, state 0, exact payload/timestamp locks and embedded UUID/version, rejects missing or phantom flow/exchange references, and returns one plan summary audit id plus both batch and per-row audit proofs. The CLI writes `alias-plan-progress.jsonl` together with plan-bound per-batch, per-row, and per-exchange ledgers. A lost response or incomplete derived ledger is repaired only by replaying the same whole plan and matching every returned plan and batch proof.
377
+
378
+ For `rebuild-derivatives`, apply submits the frozen single-action plan only to the authenticated guarded RPC. The database admission envelope must report `queued`; the CLI records that durable admission as an `accepted` action with queued proof. It does not mean markdown or embedding generation has completed. Replay must return the same durable request/proof rather than enqueueing a second rebuild. There is no fallback to a direct Edge call, `admin embedding-run`, a raw queue, SQL, service-role credentials, or raw REST mutation.
363
379
 
364
- `apply` is write-disabled unless all three commit guards are present: `--commit`, `--approve-plan <sha256>`, and `--confirm <current-account-email>`. Before the first write it re-checks the whole plan for drift and persists `approval-record.json`. Ordinary draft updates/deletes use their platform paths. The ordered `time` plus `length_time` alias request is sent once to `cmd_dataset_alias_plan_guarded` with `target_visibility=owner_draft`; the CLI has neither a per-dimension fallback nor a 52-write sequential fallback. The RPC locks and validates the complete 52-row/59-exchange closure before both dimensions commit, so a second-dimension failure rolls back the first. It checks actor ownership, state 0, exact payload/timestamp locks and embedded UUID/version, rejects missing or phantom flow/exchange references, and returns one plan summary audit id plus both batch and per-row audit proofs. The CLI writes `alias-plan-progress.jsonl` together with plan-bound per-batch, per-row, and per-exchange ledgers. A lost response or incomplete derived ledger is repaired only by replaying the same whole plan and matching every returned plan and batch proof.
380
+ `verify` requires another complete exact-count account readback rather than trusting the apply report, records its completeness proof, and writes `readback-verify-report.json` in its own output directory. For alias plans it also requires both successful batch records, all 52 correlated row records, all 59 unique exchange records, unchanged support snapshots, and exact desired row payloads. It validates the RPC-returned audit ids against the local proof chain; it does not independently query `public.command_audit_log`. For derivative rebuild plans, verify reads the durable request plus a fresh action-scoped process snapshot and reports only `pending`, `passed`, or `failed`. Database statuses `queued`, `dispatching`, `markdown_pending`, and `embedding_pending` map to `pending`; `completed` maps to `passed` only when both requested derivatives are current and every frozen primary-field precondition remains unchanged; `stale` and `failed` map to `failed`. The raw status proof, including `phase` and `fence_active`, is preserved in the report. A failed rebuild does not by itself prove that the primary-row write fence has been released.
365
381
 
366
- `verify` performs a fresh remote readback rather than trusting the apply report and writes `readback-verify-report.json` in its own output directory. For alias plans it also requires both successful batch records, all 52 correlated row records, all 59 unique exchange records, unchanged support snapshots, and exact desired row payloads. It validates the RPC-returned audit ids against the local proof chain; it does not independently query `public.command_audit_log`.
382
+ `dataset maintenance clear-account` uses the same exact-count rule for its initial five-table snapshot, per-table commit checks, and a final fresh scan of all five tables. It reports `cleared_account` only when that final aggregate proof exists with `row_count=0`; if the final proof fails after deletions begin, it still writes a `completed_with_failures` audit report. If the initial scan cannot prove completeness, it writes no snapshot or approval artifact and performs zero deletes.
367
383
 
368
384
  Foundry and skills may prepare the scope, invoke these commands, and retain their artifacts. They must not replace the CLI with direct SQL, service-role access, raw REST mutation, or private Supabase delete/update code.
369
385
 
package/dist/src/cli.js CHANGED
@@ -319,8 +319,8 @@ function renderDatasetMaintenanceHelp() {
319
319
  Actions:
320
320
  clear-account Dry-run or delete current authenticated account-owned lifecyclemodels, processes, flows, sources, and contacts.
321
321
  plan Build an immutable maintenance plan from a scope manifest, visible remote snapshot, dependency impact report, and intended operation.
322
- apply Execute an approved plan through current-user RLS and platform dataset command paths; never bypass RLS or delete rows outside the visible scope.
323
- verify Re-fetch affected rows and references, then prove that deleted, updated, skipped, protected, and redone rows match the plan.
322
+ apply Execute or durably admit an approved plan through current-user RLS and platform dataset command paths; never bypass RLS.
323
+ verify Re-fetch rows, references, or derivative request state and report passed, pending, or failed independently of apply.
324
324
 
325
325
  Safety:
326
326
  plan and verify are read-only.
@@ -339,6 +339,7 @@ Examples:
339
339
  tiangong-lca dataset maintenance clear-account --out-dir ./account-clear --json
340
340
  tiangong-lca dataset maintenance clear-account --commit --confirm user@example.com --out-dir ./account-clear
341
341
  tiangong-lca dataset maintenance plan --scope ./maintenance-scope.json --operation merge-support-aliases --out-dir ./dataset-maintenance
342
+ tiangong-lca dataset maintenance plan --scope ./derivative-rebuild-scope.json --operation rebuild-derivatives --out-dir ./derivative-rebuild
342
343
  tiangong-lca dataset maintenance apply --plan ./dataset-maintenance/maintenance-plan.json --commit --approve-plan <sha256> --confirm user@example.com
343
344
  tiangong-lca dataset maintenance verify --plan ./dataset-maintenance/maintenance-plan.json --out-dir ./dataset-maintenance/verify
344
345
  `.trim();
@@ -348,19 +349,20 @@ function renderDatasetMaintenancePlanHelp() {
348
349
  tiangong-lca dataset maintenance plan --scope <file> --operation <operation> --out-dir <dir> [options]
349
350
 
350
351
  Operations:
351
- delete | retire | redo-import | repair-references | merge-support-aliases
352
+ delete | retire | redo-import | repair-references | merge-support-aliases | rebuild-derivatives
352
353
 
353
354
  Options:
354
355
  --scope <file> Maintenance scope manifest
355
356
  --operation <value> Intended row-level maintenance operation
356
357
  --out-dir <dir> Artifact directory
357
- --page-size <n> Snapshot page size
358
+ --page-size <n> Requested snapshot page size, 1-5000 (default: 1000); server caps are followed using exact counts
358
359
  --timeout-ms <n> Request timeout in milliseconds
359
360
  --json Print compact JSON
360
361
  -h, --help
361
362
 
362
363
  Outputs written under --out-dir include the immutable maintenance-plan.json, visible snapshot,
363
- protected-row ledger, reference-impact report, and dry-run report.
364
+ protected-row ledger, reference-impact report, and dry-run report. rebuild-derivatives additionally
365
+ freezes one action-scoped database snapshot without expanding the account scan with vector data.
364
366
  `.trim();
365
367
  }
366
368
  function renderDatasetMaintenanceApplyHelp() {
@@ -370,6 +372,7 @@ function renderDatasetMaintenanceApplyHelp() {
370
372
  Behavior:
371
373
  Commit-only. The command rejects dry-run mode, a missing --commit flag, a plan hash mismatch,
372
374
  or a confirmation email that does not match the current authenticated account.
375
+ For rebuild-derivatives, success means guarded admission is queued; it never means derivatives completed.
373
376
 
374
377
  Options:
375
378
  --plan <file> Immutable maintenance-plan.json
@@ -380,7 +383,7 @@ Options:
380
383
  --json Print compact JSON
381
384
  -h, --help
382
385
 
383
- Outputs include approval-record.json and commit-report.json alongside an append-only action ledger.
386
+ Outputs include approval-record.json and commit-report.json alongside an append-only action or admission ledger.
384
387
  `.trim();
385
388
  }
386
389
  function renderDatasetMaintenanceVerifyHelp() {
@@ -390,12 +393,13 @@ function renderDatasetMaintenanceVerifyHelp() {
390
393
  Options:
391
394
  --plan <file> Immutable maintenance-plan.json
392
395
  --out-dir <dir> Optional verification artifact directory
393
- --page-size <n> Readback page size
396
+ --page-size <n> Requested readback page size, 1-5000 (default: 1000); server caps are followed using exact counts
394
397
  --timeout-ms <n> Request timeout in milliseconds
395
398
  --json Print compact JSON
396
399
  -h, --help
397
400
 
398
- Outputs include readback-verify-report.json with affected-row and reference checks.
401
+ Outputs include readback-verify-report.json. rebuild-derivatives reports pending, passed, or failed;
402
+ pending and failed both return a non-zero exit status.
399
403
  `.trim();
400
404
  }
401
405
  function renderDatasetMaintenanceClearAccountHelp() {
@@ -410,7 +414,7 @@ Behavior:
410
414
  Options:
411
415
  --state-code <n> Optional repeatable state_code filter. Omit to clear all state codes for the current account.
412
416
  --out-dir <dir> Artifact directory (default: ./dataset-maintenance/clear-account)
413
- --page-size <n> Snapshot page size, 1-5000 (default: 1000)
417
+ --page-size <n> Requested snapshot page size, 1-5000 (default: 1000); server caps are followed using exact counts
414
418
  --timeout-ms <n> Request timeout in milliseconds (default: 10000)
415
419
  --commit Execute deletion. Without this flag the command only writes a dry-run report.
416
420
  --dry-run Explicit dry-run mode
@@ -424,6 +428,8 @@ Outputs written under --out-dir:
424
428
  - approval-record.json (commit only)
425
429
  - commit-report.json (commit only)
426
430
  - readback-verify-report.json (commit only)
431
+
432
+ Every snapshot/readback must prove complete exact-count pagination before an artifact or delete is accepted.
427
433
  `.trim();
428
434
  }
429
435
  function renderDatasetContractHelp() {
@@ -3060,8 +3066,15 @@ function parseDatasetMaintenancePlanFlags(args) {
3060
3066
  }
3061
3067
  const rawOperation = typeof values.operation === 'string' ? values.operation : null;
3062
3068
  if (rawOperation !== null &&
3063
- !['delete', 'retire', 'redo-import', 'repair-references', 'merge-support-aliases'].includes(rawOperation)) {
3064
- throw new CliError("--operation must be 'delete', 'retire', 'redo-import', 'repair-references', or 'merge-support-aliases'.", {
3069
+ ![
3070
+ 'delete',
3071
+ 'retire',
3072
+ 'redo-import',
3073
+ 'repair-references',
3074
+ 'merge-support-aliases',
3075
+ 'rebuild-derivatives',
3076
+ ].includes(rawOperation)) {
3077
+ throw new CliError("--operation must be 'delete', 'retire', 'redo-import', 'repair-references', 'merge-support-aliases', or 'rebuild-derivatives'.", {
3065
3078
  code: 'DATASET_MAINTENANCE_OPERATION_INVALID',
3066
3079
  exitCode: 2,
3067
3080
  details: rawOperation,
@@ -5628,7 +5641,7 @@ export async function executeCli(argv, deps) {
5628
5641
  fetchImpl: deps.fetchImpl,
5629
5642
  });
5630
5643
  return {
5631
- exitCode: report.status === 'failed' ? 1 : 0,
5644
+ exitCode: report.status === 'passed' ? 0 : 1,
5632
5645
  stdout: stringifyJson(report, datasetFlags.json),
5633
5646
  stderr: '',
5634
5647
  };