-
Notifications
You must be signed in to change notification settings - Fork 0
feat: refine — graded work items to training data with provenance (#14) #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OriNachum
wants to merge
6
commits into
main
Choose a base branch
from
spec/291-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae7ac62
t21(291/S6): refine dataset + lineage — graded work items to sloth-re…
OriNachum 1e6b77f
chore: bump version 0.8.0 -> 0.10.0 (refine dataset + lineage, issue …
OriNachum fb6b9ce
fix: address Qodo review on #15 (UTF-8 errors, Sonar S2245, changelog)
OriNachum a0386f4
Merge origin/main into spec/291-integration
OriNachum 84b8295
ci: waive Sonar python:S2245 on the refine shuffle via sonar-project.…
OriNachum 392337d
refactor: clear 5 SonarCloud code-smells in the refine surface
OriNachum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| """``data-refinery refine`` — graded colleague work items to sloth-ready training data. | ||
|
|
||
| The S6 leg of the colleague/data-refinery/unsloth "refine" arc | ||
| (agentculture/data-refinery-cli#14, owner decision recorded on | ||
| agentculture/colleague#291): turn a `colleague feedback export` JSONL stream | ||
| (one graded work item per line) into a sloth-ready chat-schema train/eval split | ||
| plus a provenance ``lineage.json``. Pure library logic lives in | ||
| :mod:`data_refinery.refine.dataset`; this module is the CLI mirror, following | ||
| the same shell-or-import pattern as ``store``/``validate``/``dedup``/etc. | ||
|
|
||
| Contract (agent-first): ``--json`` on every verb; results to stdout, | ||
| diagnostics/errors to stderr; a malformed input line is skipped with **one** | ||
| stderr diagnostic plus a counted total — never silently dropped; a missing | ||
| ``--from``/``--dataset`` path exits ``1`` (user-input error) with a ``hint:``, | ||
| never a traceback. Stdlib only: no network, no LLM. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| from pathlib import Path | ||
|
|
||
| from data_refinery.cli._errors import EXIT_USER_ERROR, CliError | ||
| from data_refinery.cli._output import emit_diagnostic, emit_result | ||
| from data_refinery.refine import dataset | ||
|
|
||
| _DEFAULT_MIN_RATING = 4 | ||
| _DEFAULT_SPLIT = "90/10" | ||
| _DEFAULT_SEED = 0 | ||
|
|
||
|
|
||
| def _add_json_flag(p: argparse.ArgumentParser) -> None: | ||
| p.add_argument("--json", action="store_true", help="Emit structured JSON.") | ||
|
|
||
|
|
||
| def cmd_refine_dataset(args: argparse.Namespace) -> int: | ||
| json_mode = bool(getattr(args, "json", False)) | ||
| from_path = Path(args.from_path) | ||
| if not from_path.is_file(): | ||
| raise CliError( | ||
| code=EXIT_USER_ERROR, | ||
| message=f"--from file not found: {from_path}", | ||
| remediation=( | ||
| "pass --from pointing at a JSONL file produced by 'colleague feedback export'" | ||
| ), | ||
| ) | ||
|
|
||
| result = dataset.run_refine_dataset( | ||
| from_path, | ||
| schema=args.schema, | ||
| min_rating=args.min_rating, | ||
| split=args.split, | ||
| seed=args.seed, | ||
| out_dir=Path(args.out), | ||
| ) | ||
| for diag in result["diagnostics"]: | ||
| emit_diagnostic(diag) | ||
|
|
||
| payload = result["payload"] | ||
| if json_mode: | ||
| emit_result(payload, json_mode=True) | ||
| else: | ||
| agg = payload["aggregate"] | ||
| emit_result( | ||
| f"refine dataset: kept {agg['kept_count']} " | ||
| f"(train {agg['train_size']} / eval {agg['eval_size']}) from " | ||
| f"{agg['input_count']} input line(s) — {agg['malformed_count']} malformed, " | ||
| f"{agg['filtered_count']} filtered, {agg['duplicates_removed']} duplicate(s) " | ||
| f"removed\nwrote {payload['train_file']}, {payload['eval_file']}, " | ||
| f"{payload['lineage_file']}", | ||
| json_mode=False, | ||
| ) | ||
| return 0 | ||
|
|
||
|
|
||
| def cmd_refine_lineage(args: argparse.Namespace) -> int: | ||
| json_mode = bool(getattr(args, "json", False)) | ||
| payload = dataset.read_lineage(Path(args.dataset)) | ||
| if json_mode: | ||
| emit_result(payload, json_mode=True) | ||
| else: | ||
| agg = payload.get("aggregate", {}) | ||
| dist = agg.get("grade_distribution", {}) | ||
| lines = [ | ||
| f"lineage: {args.dataset}", | ||
| f"kept: {agg.get('kept_count')} " | ||
| f"(train {agg.get('train_size')} / eval {agg.get('eval_size')})", | ||
| f"input: {agg.get('input_count')} " | ||
| f"(malformed {agg.get('malformed_count')}, filtered {agg.get('filtered_count')}, " | ||
| f"duplicates removed {agg.get('duplicates_removed')})", | ||
| f"min_rating: {agg.get('min_rating')}, split: {agg.get('split')}, " | ||
| f"seed: {agg.get('seed')}", | ||
| "grade distribution: " + ", ".join(f"{k}={v}" for k, v in sorted(dist.items())), | ||
| f"train_sha256: {agg.get('train_sha256')}", | ||
| f"eval_sha256: {agg.get('eval_sha256')}", | ||
| ] | ||
| emit_result("\n".join(lines), json_mode=False) | ||
| return 0 | ||
|
|
||
|
|
||
| def _refine_overview(args: argparse.Namespace) -> int: | ||
| """`data-refinery refine` with no sub-verb prints the noun's overview.""" | ||
| from data_refinery.cli._commands.overview import emit_overview | ||
|
|
||
| sections = [ | ||
| { | ||
| "title": "Verbs", | ||
| "items": [ | ||
| "refine dataset --from <graded.jsonl> --out <dir> — graded colleague work " | ||
| "items -> sloth-ready chat-schema train/eval JSONL + lineage.json", | ||
| "refine lineage --dataset <dir> — read back a dataset's lineage.json " | ||
| "(aggregates + grade distribution)", | ||
| ], | ||
| }, | ||
| { | ||
| "title": "Input line shape (colleague `feedback export`)", | ||
| "items": [ | ||
| "{task_id, request, summary, rating(int 1-5 | null), notes, status, at, " | ||
| "stats{steps,files_changed,bytes_written}}", | ||
| "one JSON object per line; a malformed line is skipped with a stderr " | ||
| "diagnostic, never silently dropped", | ||
| ], | ||
| }, | ||
| { | ||
| "title": "Filter + split rules", | ||
| "items": [ | ||
| "kept iff status == 'ok' AND rating is graded (non-null) AND " | ||
| ">= --min-rating (default 4) — ungraded/below-threshold never enters silently", | ||
| "deduped by content hash before splitting (reuses the quality dedup primitive)", | ||
| "train/eval are disjoint by construction: a deterministic seeded shuffle " | ||
| "(--seed, default 0) then a straight slice by --split (default 90/10)", | ||
| ], | ||
| }, | ||
| { | ||
| "title": "Conventions", | ||
| "items": [ | ||
| "every verb supports --json", | ||
| "stdlib-only: no network, no LLM", | ||
| "--schema chat is the only supported schema today", | ||
| ], | ||
| }, | ||
| ] | ||
| emit_overview("data-refinery refine", sections, json_mode=bool(getattr(args, "json", False))) | ||
| return 0 | ||
|
|
||
|
|
||
| def register(sub: argparse._SubParsersAction) -> None: | ||
| p = sub.add_parser( | ||
| "refine", | ||
| help="Turn graded colleague work items into sloth-ready train/eval training data.", | ||
| ) | ||
| _add_json_flag(p) | ||
| p.set_defaults(func=_refine_overview, json=False) | ||
| # Propagate the structured-error parser_class to nested verbs. | ||
| verb = p.add_subparsers(dest="refine_command", parser_class=type(p)) | ||
|
|
||
| ds = verb.add_parser( | ||
| "dataset", | ||
| help="Build a train/eval JSONL split + lineage.json from graded colleague work items.", | ||
| ) | ||
| ds.add_argument( | ||
| "--from", | ||
| dest="from_path", | ||
| required=True, | ||
| help="Path to a JSONL file produced by 'colleague feedback export'.", | ||
| ) | ||
| ds.add_argument( | ||
| "--schema", | ||
| default="chat", | ||
| choices=sorted(dataset.KNOWN_SCHEMAS), | ||
| help="Output example schema (default: chat; the only schema supported today).", | ||
| ) | ||
| ds.add_argument( | ||
| "--min-rating", | ||
| type=int, | ||
| default=_DEFAULT_MIN_RATING, | ||
| help=f"Minimum feedback rating to keep (default: {_DEFAULT_MIN_RATING}).", | ||
| ) | ||
| ds.add_argument( | ||
| "--split", | ||
| default=_DEFAULT_SPLIT, | ||
| help=f"train/eval ratio as A/B (default: {_DEFAULT_SPLIT}).", | ||
| ) | ||
| ds.add_argument( | ||
| "--seed", | ||
| type=int, | ||
| default=_DEFAULT_SEED, | ||
| help=f"Seed for the deterministic train/eval shuffle (default: {_DEFAULT_SEED}).", | ||
| ) | ||
| ds.add_argument( | ||
| "--out", | ||
| required=True, | ||
| help="Output directory for train.jsonl/eval.jsonl/lineage.json.", | ||
| ) | ||
| _add_json_flag(ds) | ||
| ds.set_defaults(func=cmd_refine_dataset) | ||
|
|
||
| lin = verb.add_parser( | ||
| "lineage", | ||
| help="Read back a dataset directory's lineage.json (aggregates + grade distribution).", | ||
| ) | ||
| lin.add_argument( | ||
| "--dataset", | ||
| required=True, | ||
| help="Dataset output directory (as produced by 'refine dataset --out').", | ||
| ) | ||
| _add_json_flag(lin) | ||
| lin.set_defaults(func=cmd_refine_lineage) | ||
|
|
||
| ov = verb.add_parser("overview", help="Describe the refine noun.") | ||
| _add_json_flag(ov) | ||
| ov.set_defaults(func=_refine_overview) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.