$npx -y skills add HKUST-KnowComp/DeepRefine-Skill --skill deeprefine_skillAgent-native DeepRefine refinement loop — same control flow as DeepRefine.refine(), graphify search instead of FAISS, session LLM, dry-run review before approved graph writes.
| 1 | # DeepRefine — Agent refinement loop (strict) |
| 2 | |
| 3 | ## Default safety policy: dry-run only |
| 4 | |
| 5 | A normal `/deeprefine` invocation **MUST NEVER** call `deeprefine apply`. |
| 6 | |
| 7 | The default `/deeprefine` workflow must stop after: |
| 8 | |
| 9 | 1. `deeprefine loop validate` |
| 10 | 2. `deeprefine review` |
| 11 | 3. showing the proposed actions and HIGH/MEDIUM/LOW review report to the user |
| 12 | |
| 13 | Then ask the user for explicit approval. |
| 14 | |
| 15 | Only if the user's **next message** explicitly says to approve/apply/write the graph may you run: |
| 16 | |
| 17 | ```bash |
| 18 | deeprefine apply --refresh-wiki --trace-file ... --refinement-file ... |
| 19 | deeprefine loop finish --trace-file ... --refinement-file ... |
| 20 | ``` |
| 21 | |
| 22 | Do not treat generation of `<refinement>` actions as approval. Do not treat a valid trace as approval. Do not apply in the same `/deeprefine` turn. |
| 23 | |
| 24 | --- |
| 25 | |
| 26 | You **MUST** implement the **same control flow** as `DeepRefine.refine()` in DeepRefine (`autorefiner/src/deeprefine.py`). |
| 27 | |
| 28 | | Component | Agent mode | CLI `deeprefine refine` | |
| 29 | |-----------|------------|-------------------------| |
| 30 | | Retrieval | `graphify query` + k-hop from `graph.json` | FAISS retriever | |
| 31 | | LLM | **Your session model** | External API / vLLM | |
| 32 | | Graph writes | Dry-run proposal + `deeprefine review`; `deeprefine apply` only after user approval | Dry-run by default; `--apply` persists | |
| 33 | |
| 34 | --- |
| 35 | |
| 36 | ## FORBIDDEN (hard stop) |
| 37 | |
| 38 | Do **NOT**: |
| 39 | |
| 40 | 1. Run `deeprefine refine` (unless the user explicitly asks for CLI/FAISS mode). |
| 41 | 2. Call `deeprefine apply` without a valid `loop_trace_<query_id>.json` (CLI will reject). |
| 42 | 3. Call `deeprefine apply` before running `deeprefine review` and receiving explicit user approval. |
| 43 | 4. Ignore LOW-confidence review warnings unless the user explicitly requests `--allow-low-confidence`. |
| 44 | 5. Skip any hop’s `<judge>Yes</judge>` / `<judge>No</judge>` judgement. |
| 45 | 6. Skip error abduction when `len(interaction_history) > 1`. |
| 46 | 7. Write `<refinement>` before abduction when refinement is required. |
| 47 | 8. Hand-edit `graph.json` with Python or ad-hoc JSON patches. |
| 48 | 9. Ignore pending history and refine only one latest query when unrefined queries already exist. |
| 49 | 10. Invent a shorter pipeline (“read file → write refinement → apply”). |
| 50 | |
| 51 | If validation fails, **fix the trace and re-run the missing step** — do not bypass with `--skip-trace-check`. |
| 52 | |
| 53 | --- |
| 54 | |
| 55 | ## Constants (match `refine_runner.py` / DeepRefine) |
| 56 | |
| 57 | ```text |
| 58 | MAX_HOPS = 4 |
| 59 | INCREMENT_HOP = 1 |
| 60 | BASE_TOP_K = 10 |
| 61 | MAX_TRIPLE_NUM_BY_STEP = [5, 10, 15, 20] # cap triples per step |
| 62 | HISTORY_HORIZON = 4 # abduction uses last N steps |
| 63 | ``` |
| 64 | |
| 65 | --- |
| 66 | |
| 67 | ## Mandatory artifact |
| 68 | |
| 69 | For each query, maintain: |
| 70 | |
| 71 | `graphify-out/.deeprefine/loop_trace_<query_id>.json` |
| 72 | |
| 73 | Create template: |
| 74 | |
| 75 | ```bash |
| 76 | deeprefine loop init --query "<exact question>" |
| 77 | ``` |
| 78 | |
| 79 | Append each hop to `interaction_history` **before** starting the next hop. |
| 80 | Run `deeprefine loop validate --trace-file ...` after abduction and before `review` / approved `apply`. |
| 81 | |
| 82 | --- |
| 83 | |
| 84 | ## Query queue selection (default behavior of `/deeprefine`) |
| 85 | |
| 86 | `/deeprefine` must process **all unrefined history queries** first, not just the latest one. |
| 87 | |
| 88 | 1. Sync graphify query memory into DeepRefine history: |
| 89 | - run: `deeprefine history sync-memory` |
| 90 | - source dir: `graphify-out/memory/query_*.md` |
| 91 | - target file: `graphify-out/.deeprefine/history.jsonl` |
| 92 | 2. Read pending queue from `graphify-out/.deeprefine/history.jsonl`: |
| 93 | - include rows where `refined != true` |
| 94 | - dedupe by `id` (first occurrence) |
| 95 | - preserve file order |
| 96 | 3. If pending queue is non-empty: set `target_queries = pending_queue`. |
| 97 | 4. If pending queue is empty: set `target_queries = [current session question]`. |
| 98 | 5. Run the full refinement loop for **each** query in `target_queries`, one by one. |
| 99 | 6. For early-exit queries, finish immediately. For refinement-path queries, generate review output and wait for user approval before `apply` + `loop finish`. |
| 100 | |
| 101 | --- |
| 102 | |
| 103 | ## Control flow (must match `DeepRefine.refine()`) |
| 104 | |
| 105 | Pseudocode — follow **exactly**: |
| 106 | |
| 107 | ```text |
| 108 | target_queries = pending_history_queries() # refined != true, dedupe by id, keep order |
| 109 | run "deeprefine history sync-memory" before loading pending history |
| 110 | if target_queries is empty: |
| 111 | target_queries = [current session question] |
| 112 | |
| 113 | for question in target_queries: |
| 114 | interaction_history = [] |
| 115 | for step in 1..MAX_HOPS: |
| 116 | print "[Step: {step}]" # show in chat |
| 117 | |
| 118 | if step == 1: |
| 119 | # Vector-retrieval equivalent: graphify query on full question |
| 120 | RUN: graphify query "<question>" |
| 121 | triples = parse NODE/EDGE → [{subject, relation, object}, ...] |
| 122 | cap = MAX_TRIPLE_NUM_BY_STEP[0] # 5 |
| 123 | record retrieval.method = "graphify_query" |
| 124 | else: |
| 125 | # k-hop expansion from entities in previous hop (NOT a new random search) |
| 126 | entities = uni |