Text Generation
Transformers
Safetensors
English
counsel
legal
law
reasoning
tool-use
conversational
custom_code
Instructions to use judicialmind/Counsel-2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use judicialmind/Counsel-2B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="judicialmind/Counsel-2B", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("judicialmind/Counsel-2B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use judicialmind/Counsel-2B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "judicialmind/Counsel-2B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "judicialmind/Counsel-2B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/judicialmind/Counsel-2B
- SGLang
How to use judicialmind/Counsel-2B with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "judicialmind/Counsel-2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "judicialmind/Counsel-2B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "judicialmind/Counsel-2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "judicialmind/Counsel-2B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use judicialmind/Counsel-2B with Docker Model Runner:
docker model run hf.co/judicialmind/Counsel-2B
Add attribution to LICENSE and gates
Browse files- LICENSE +13 -0
- gates/README.md +14 -0
- gates/gates.py +361 -0
LICENSE
CHANGED
|
@@ -200,3 +200,16 @@
|
|
| 200 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
See the License for the specific language governing permissions and
|
| 202 |
limitations under the License.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
See the License for the specific language governing permissions and
|
| 202 |
limitations under the License.
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
--------------------------------------------------------------------------------
|
| 206 |
+
ATTRIBUTION
|
| 207 |
+
|
| 208 |
+
Counsel-2B is a modified version of MiniCPM5-2B by OpenBMB
|
| 209 |
+
(https://huggingface.co/openbmb/MiniCPM5-2B), which is licensed under the
|
| 210 |
+
Apache License, Version 2.0. All rights in the original model belong to its
|
| 211 |
+
authors.
|
| 212 |
+
|
| 213 |
+
Modifications by JudicialMind (2026): supervised fine-tuning, preference
|
| 214 |
+
optimisation and reinforcement learning for legal work; renamed model classes,
|
| 215 |
+
configuration, chat template and documentation.
|
gates/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gates
|
| 2 |
+
|
| 3 |
+
Gates make an agent loop enforce a careful work process, so Counsel-2B finishes long, multi-document tasks fully. The model
|
| 4 |
+
must read every document, plan, draft, check, and build a real deliverable before it can stop.
|
| 5 |
+
|
| 6 |
+
| `HL_SCAFFOLD` | Mode | What it does |
|
| 7 |
+
|---|---|---|
|
| 8 |
+
| `1` | Strict gate | Every document is read end to end before writing. Plan comes before draft, and check before build. The run cannot end while any check fails. |
|
| 9 |
+
| `2` | Guided (recommended) | The loop leads one step at a time: read (chunk by chunk) → plan → draft → check → build and validate. |
|
| 10 |
+
|
| 11 |
+
Settings: `HL_MIN_WORDS` (minimum deliverable length, default 500), `HL_GUIDE_CHUNK_LINES` (lines per reading chunk,
|
| 12 |
+
default 150), `HL_ELIDE_AT` (context size at which old tool outputs are shortened).
|
| 13 |
+
|
| 14 |
+
On legal agent tasks, the guided gates **doubled** Counsel-2B's score.
|
gates/gates.py
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Counsel-2B gates: make an agent loop enforce a careful work process for a small model.
|
| 2 |
+
|
| 3 |
+
_Gate (HL_SCAFFOLD=1): every document is read end to end before writing; plan before draft; check before build;
|
| 4 |
+
the run cannot end while any check fails.
|
| 5 |
+
_Guide (HL_SCAFFOLD=2): the loop leads step by step: read (chunk by chunk) -> plan -> draft -> check -> build/validate.
|
| 6 |
+
_elide_old_tool_results: shortens old tool outputs so long tasks fit the context window.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import zipfile as _zipfile
|
| 10 |
+
import re as _re
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _docx_words(path) -> int:
|
| 14 |
+
try:
|
| 15 |
+
xml = _zipfile.ZipFile(path).read("word/document.xml").decode("utf8", "ignore")
|
| 16 |
+
return len(_re.sub(r"<[^>]+>", " ", xml).split())
|
| 17 |
+
except Exception:
|
| 18 |
+
return -1
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _scaffold_gaps(response, tool_executor) -> list[str]:
|
| 22 |
+
"""Why the run may not finish yet: unread documents, missing / invalid / thin deliverables, a cut-off step."""
|
| 23 |
+
gaps = []
|
| 24 |
+
if getattr(response, "finish_reason", None) == "length":
|
| 25 |
+
gaps.append("Your last step was cut off at the length limit before it finished, so nothing in it was executed. "
|
| 26 |
+
"Continue, and split long content into several smaller tool calls.")
|
| 27 |
+
if "<function" in (response.text or "") or "<tool_call" in (response.text or ""):
|
| 28 |
+
gaps.append("Your last message contains a tool call as plain text; it was NOT executed. Issue it again as a proper tool call.")
|
| 29 |
+
try:
|
| 30 |
+
m = tool_executor.get_metrics()
|
| 31 |
+
unread = [f for f in (m.get("documents_skipped_list") or [])]
|
| 32 |
+
if m.get("documents_read", 0) < m.get("total_documents", 0):
|
| 33 |
+
gaps.append("You have not read every document yet (%d of %d). Still unread: %s. Read them completely."
|
| 34 |
+
% (m.get("documents_read", 0), m.get("total_documents", 0), ", ".join(unread[:20]) or "see documents/"))
|
| 35 |
+
except Exception:
|
| 36 |
+
pass
|
| 37 |
+
try:
|
| 38 |
+
names = json.loads(_os.environ.get("HL_EXPECTED_DELIVERABLES", "[]"))
|
| 39 |
+
except Exception:
|
| 40 |
+
names = []
|
| 41 |
+
out = Path(tool_executor.output_dir)
|
| 42 |
+
for n in names:
|
| 43 |
+
p = out / n
|
| 44 |
+
if not p.exists():
|
| 45 |
+
gaps.append("The deliverable /workspace/output/%s does not exist yet. Build it with the docx skill and save it there." % n)
|
| 46 |
+
elif n.lower().endswith((".docx", ".xlsx", ".pptx")) and p.read_bytes()[:2] != b"PK":
|
| 47 |
+
gaps.append("/workspace/output/%s is not a real Office file (it is plain text). Rebuild it with the docx skill script." % n)
|
| 48 |
+
elif n.lower().endswith(".docx") and _docx_words(p) < int(_os.environ.get("HL_MIN_WORDS", "300")):
|
| 49 |
+
gaps.append("/workspace/output/%s has only %d words; it is not the complete deliverable. Finish the full content and rebuild it."
|
| 50 |
+
% (n, max(_docx_words(p), 0)))
|
| 51 |
+
return gaps
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class _Gate:
|
| 55 |
+
"""Strict careful-work gate (HL_SCAFFOLD=1). The model cannot skip the work:
|
| 56 |
+
every document must be read end to end with the read tool (line coverage is tracked across offset/limit calls)
|
| 57 |
+
before anything but notes.md can be written or any non-listing shell command run; plan.md comes before drafting;
|
| 58 |
+
check.md (requirements vs. draft) comes before building; Office files can only be built with the docx script;
|
| 59 |
+
and the run cannot end while any check fails (no nudge cap; only max_turns ends it)."""
|
| 60 |
+
LISTING = _re.compile(r"^\s*(ls|wc|file|stat|pwd|find|du|tree)\b[^>|;&`$<]*$")
|
| 61 |
+
|
| 62 |
+
def __init__(self, ex):
|
| 63 |
+
from harness import tools as _T
|
| 64 |
+
self.ex, self.DOC = ex, _T.DOCUMENTS_PATH + "/"
|
| 65 |
+
self.cov, self.total = {}, {}
|
| 66 |
+
|
| 67 |
+
def docs(self):
|
| 68 |
+
root = Path(self.ex.documents_dir)
|
| 69 |
+
return sorted(str(f.relative_to(root)) for f in root.rglob("*") if f.is_file())
|
| 70 |
+
|
| 71 |
+
def note_read(self, args):
|
| 72 |
+
try:
|
| 73 |
+
sb = self.ex._resolve_read_path(str(args.get("file_path", "")))
|
| 74 |
+
except Exception:
|
| 75 |
+
return
|
| 76 |
+
if not sb.startswith(self.DOC):
|
| 77 |
+
return
|
| 78 |
+
rel = sb[len(self.DOC):]
|
| 79 |
+
if rel not in self.total:
|
| 80 |
+
try:
|
| 81 |
+
full = self.ex._read_and_parse(sb)
|
| 82 |
+
except Exception:
|
| 83 |
+
return
|
| 84 |
+
self.total[rel] = max(1, len(full.rstrip("\n").split("\n")))
|
| 85 |
+
n = self.total[rel]
|
| 86 |
+
try:
|
| 87 |
+
off = int(args.get("offset") or 0)
|
| 88 |
+
lim = int(args["limit"]) if args.get("limit") else None
|
| 89 |
+
except Exception:
|
| 90 |
+
return
|
| 91 |
+
end = n if lim is None else min(n, off + lim)
|
| 92 |
+
self.cov.setdefault(rel, set()).update(range(max(0, off), end))
|
| 93 |
+
|
| 94 |
+
def unread(self):
|
| 95 |
+
out = []
|
| 96 |
+
for d in self.docs():
|
| 97 |
+
n, c = self.total.get(d), len(self.cov.get(d, ()))
|
| 98 |
+
if n is None or c < n:
|
| 99 |
+
out.append("%s (%s)" % (d, "not opened" if n is None else "read %d of %d lines; continue with offset=%d" % (c, n, min(set(range(n)) - self.cov.get(d, set())))))
|
| 100 |
+
return out
|
| 101 |
+
|
| 102 |
+
def _ws_words(self, name):
|
| 103 |
+
f = Path(self.ex.workspace_dir) / name
|
| 104 |
+
try:
|
| 105 |
+
return len(f.read_text(errors="ignore").split()) if f.is_file() else -1
|
| 106 |
+
except Exception:
|
| 107 |
+
return -1
|
| 108 |
+
|
| 109 |
+
def block(self, name, args):
|
| 110 |
+
"""Return a refusal message if this tool call is not allowed yet, else None."""
|
| 111 |
+
un = self.unread()
|
| 112 |
+
p = str(args.get("file_path", "")); base = p.split("/workspace/", 1)[-1].lstrip("/")
|
| 113 |
+
if name in ("write", "edit"):
|
| 114 |
+
if base.lower().endswith((".docx", ".xlsx", ".pptx")):
|
| 115 |
+
return ("BLOCKED: never create or edit Office files with the write/edit tool; that makes a fake file. Write the "
|
| 116 |
+
"content in markdown, then build it: python3 /workspace/skills/docx/scripts/generate_from_md.py "
|
| 117 |
+
"/workspace/draft.md /workspace/output/<name>.docx")
|
| 118 |
+
if "output/output" in p:
|
| 119 |
+
return "BLOCKED: save deliverables directly in /workspace/output/, never in a nested output/output/ folder."
|
| 120 |
+
if un and base != "notes.md":
|
| 121 |
+
return ("BLOCKED: you must read every document completely before writing anything except /workspace/notes.md. "
|
| 122 |
+
"Still unread: " + "; ".join(un) + ". Read them with the read tool (use offset/limit to reach the last line).")
|
| 123 |
+
if base.startswith(("draft", "output/")) and self._ws_words("plan.md") < 80:
|
| 124 |
+
return "BLOCKED: write /workspace/plan.md first (each deliverable, its sections, and the document behind each point)."
|
| 125 |
+
if name == "bash":
|
| 126 |
+
cmd = str(args.get("command", ""))
|
| 127 |
+
if un and not self.LISTING.match(cmd):
|
| 128 |
+
return ("BLOCKED: until every document is read completely you may only run ls/find/wc/file/stat. Read the documents "
|
| 129 |
+
"with the read tool. Still unread: " + "; ".join(un) + ".")
|
| 130 |
+
if "generate_from_md" in cmd and self._ws_words("check.md") < 80:
|
| 131 |
+
return ("BLOCKED: before building the Word file, write /workspace/check.md: every requirement in the task "
|
| 132 |
+
"instructions, one per line, with the draft section that covers it. Fix the draft for anything missing.")
|
| 133 |
+
return None
|
| 134 |
+
|
| 135 |
+
def gaps(self, response):
|
| 136 |
+
g = []
|
| 137 |
+
if getattr(response, "finish_reason", None) == "length":
|
| 138 |
+
g.append("Your last step was cut off at the length limit, so nothing in it ran. Continue in smaller tool calls.")
|
| 139 |
+
txt = response.text or ""
|
| 140 |
+
if "<function" in txt or "<tool_call" in txt:
|
| 141 |
+
g.append("Your last message contains a tool call written as plain text; it did NOT run. Issue it as a real tool call.")
|
| 142 |
+
un = self.unread()
|
| 143 |
+
if un:
|
| 144 |
+
g.append("You have not read every document completely. Still unread: " + "; ".join(un) + ".")
|
| 145 |
+
for f, why in (("plan.md", "the plan"), ("check.md", "the requirements check")):
|
| 146 |
+
if self._ws_words(f) < 80:
|
| 147 |
+
g.append("/workspace/%s (%s) is missing or too short." % (f, why))
|
| 148 |
+
try:
|
| 149 |
+
names = json.loads(_os.environ.get("HL_EXPECTED_DELIVERABLES", "[]"))
|
| 150 |
+
except Exception:
|
| 151 |
+
names = []
|
| 152 |
+
out = Path(self.ex.output_dir)
|
| 153 |
+
for n in names:
|
| 154 |
+
q = out / n
|
| 155 |
+
if not q.exists():
|
| 156 |
+
g.append("The deliverable /workspace/output/%s does not exist. Build it with generate_from_md.py." % n)
|
| 157 |
+
elif n.lower().endswith((".docx", ".xlsx", ".pptx")) and q.read_bytes()[:2] != b"PK":
|
| 158 |
+
g.append("/workspace/output/%s is not a real Office file. Rebuild it with generate_from_md.py." % n)
|
| 159 |
+
elif n.lower().endswith(".docx"):
|
| 160 |
+
w = _docx_words(q)
|
| 161 |
+
if w < int(_os.environ.get("HL_MIN_WORDS", "500")):
|
| 162 |
+
g.append("/workspace/output/%s has only %d words; it is not the complete deliverable. Finish it and rebuild." % (n, max(w, 0)))
|
| 163 |
+
else:
|
| 164 |
+
try:
|
| 165 |
+
r = self.ex.sandbox.exec("python3 /workspace/skills/docx/scripts/validate.py /workspace/output/%s" % n, timeout=60)
|
| 166 |
+
if r.returncode not in (0, None):
|
| 167 |
+
g.append("/workspace/output/%s fails validation: %s" % (n, (r.stdout + r.stderr)[-400:]))
|
| 168 |
+
except Exception:
|
| 169 |
+
pass
|
| 170 |
+
return g
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
class _Guide:
|
| 174 |
+
"""Guided mode (HL_SCAFFOLD=2) for small models that cannot run a long process on their own.
|
| 175 |
+
The harness leads one step at a time and turns plain-text replies into progress instead of letting the model escape:
|
| 176 |
+
read - the harness shows every document chunk by chunk until every line has been shown; each plain-text reply is
|
| 177 |
+
appended to notes.md;
|
| 178 |
+
plan - reply text becomes plan.md; draft - replies are appended to the draft file(s) until the model says DONE and
|
| 179 |
+
the draft is long enough; check - reply becomes check.md;
|
| 180 |
+
build - the harness builds each .docx with generate_from_md.py, word-counts and validates it; any problem sends the
|
| 181 |
+
model back to the draft with the exact reason. The run ends only when every check passes (or max_turns)."""
|
| 182 |
+
MARK = "[DOCUMENT CHUNK]"
|
| 183 |
+
|
| 184 |
+
def __init__(self, ex):
|
| 185 |
+
self.g, self.ex, self.phase, self.i = _Gate(ex), ex, "read", 0
|
| 186 |
+
self.chunk = int(_os.environ.get("HL_GUIDE_CHUNK_LINES", "150"))
|
| 187 |
+
self.minw = int(_os.environ.get("HL_MIN_WORDS", "500"))
|
| 188 |
+
try:
|
| 189 |
+
self.names = json.loads(_os.environ.get("HL_EXPECTED_DELIVERABLES", "[]")) or ["deliverable.docx"]
|
| 190 |
+
except Exception:
|
| 191 |
+
self.names = ["deliverable.docx"]
|
| 192 |
+
self.drafts = ["draft.md"] if len(self.names) == 1 else ["draft-%s.md" % Path(n).stem for n in self.names]
|
| 193 |
+
self.fails = 0
|
| 194 |
+
|
| 195 |
+
def _ws(self, name):
|
| 196 |
+
return Path(self.ex.workspace_dir) / name
|
| 197 |
+
|
| 198 |
+
def _notes(self, limit=24000):
|
| 199 |
+
f = self._ws("notes.md")
|
| 200 |
+
t = f.read_text(errors="ignore") if f.is_file() else ""
|
| 201 |
+
return t if len(t) <= limit else t[:limit] + "\n...[notes truncated]"
|
| 202 |
+
|
| 203 |
+
@staticmethod
|
| 204 |
+
def _clean(text):
|
| 205 |
+
t = _re.sub(r"<think>.*?</think>", "", text or "", flags=_re.S)
|
| 206 |
+
t = _re.sub(r"<(tool_call|function)[^>]*>.*?(</\1>|$)", "", t, flags=_re.S)
|
| 207 |
+
return t.strip()
|
| 208 |
+
|
| 209 |
+
CAPS = {"read": 400, "plan": 1500, "draft": 1500, "check": 1200}
|
| 210 |
+
|
| 211 |
+
@staticmethod
|
| 212 |
+
def _repetitive(t):
|
| 213 |
+
w = t.split()
|
| 214 |
+
if len(w) < 120:
|
| 215 |
+
return False
|
| 216 |
+
grams = list(zip(w, w[1:], w[2:], w[3:]))
|
| 217 |
+
return len(set(grams)) / max(1, len(grams)) < 0.6
|
| 218 |
+
|
| 219 |
+
def _save(self, text, done_word=False):
|
| 220 |
+
self.rejected = False
|
| 221 |
+
if self.phase not in self.CAPS:
|
| 222 |
+
return 0
|
| 223 |
+
t = self._clean(text)
|
| 224 |
+
if done_word:
|
| 225 |
+
t = _re.sub(r"(?im)^\s*\**DONE\**\.?\s*$", "", t).strip()
|
| 226 |
+
if len(t.split()) < 25:
|
| 227 |
+
return 0
|
| 228 |
+
if self._repetitive(t):
|
| 229 |
+
self.rejected = True
|
| 230 |
+
return 0
|
| 231 |
+
w = t.split()
|
| 232 |
+
if len(w) > self.CAPS[self.phase]:
|
| 233 |
+
t = " ".join(w[:self.CAPS[self.phase]]) + " [cut: keep replies shorter]"
|
| 234 |
+
target = {"read": "notes.md", "plan": "plan.md", "check": "check.md"}.get(self.phase) or self.drafts[self.i]
|
| 235 |
+
mode = "a" if self.phase in ("read", "draft") else "w"
|
| 236 |
+
with open(self._ws(target), mode) as f:
|
| 237 |
+
f.write(("\n\n" if mode == "a" else "") + t + "\n")
|
| 238 |
+
return len(t.split())
|
| 239 |
+
|
| 240 |
+
def _feed(self):
|
| 241 |
+
for d in self.g.docs():
|
| 242 |
+
n, cov = self.g.total.get(d), self.g.cov.get(d, set())
|
| 243 |
+
if n is not None and len(cov) >= n:
|
| 244 |
+
continue
|
| 245 |
+
off = 0 if n is None else min(set(range(n)) - cov)
|
| 246 |
+
args = {"file_path": self.g.DOC + d, "offset": off, "limit": self.chunk}
|
| 247 |
+
content = self.ex.execute("read", args)
|
| 248 |
+
self.g.note_read(args)
|
| 249 |
+
n = self.g.total.get(d, 0); k = self.g.docs().index(d) + 1
|
| 250 |
+
return ("%s %s, lines %d-%d of %d (document %d of %d)\n\n%s\n\n---\nReply with your notes on these lines as plain "
|
| 251 |
+
"text bullets (at most 15 bullets): parties, dates, amounts, section numbers, defined terms, obligations, deadlines, and any "
|
| 252 |
+
"problem or inconsistency you notice. The harness appends your reply to /workspace/notes.md. Do not start "
|
| 253 |
+
"the deliverable yet; the next part follows." % (self.MARK, d, off + 1, min(n, off + self.chunk), n, k,
|
| 254 |
+
len(self.g.docs()), content))
|
| 255 |
+
return None
|
| 256 |
+
|
| 257 |
+
def _draft_msg(self, extra=""):
|
| 258 |
+
name, d = self.names[self.i], self.drafts[self.i]
|
| 259 |
+
have = max(self.g._ws_words(d), 0)
|
| 260 |
+
return ("%sNow write the deliverable %s (part %d of %d) in markdown, following the task instructions and your plan.\n"
|
| 261 |
+
"Your notes:\n%s\n\nYour plan:\n%s\n\n---\nReply with the next part of the deliverable as markdown text (headings, "
|
| 262 |
+
"numbered sections, full sentences; real names, dates and section references from the documents). The harness "
|
| 263 |
+
"appends each reply to /workspace/%s (%d words so far). Write at most 1-3 sections (about 800 words) per reply and keep going "
|
| 264 |
+
"section by section until the whole deliverable is complete, then reply with only the word DONE."
|
| 265 |
+
% (extra, name, self.i + 1, len(self.names), self._notes(), self._ws("plan.md").read_text(errors="ignore")
|
| 266 |
+
if self._ws("plan.md").is_file() else "(none)", d, have))
|
| 267 |
+
|
| 268 |
+
def _build(self):
|
| 269 |
+
probs = []
|
| 270 |
+
for name, d in zip(self.names, self.drafts):
|
| 271 |
+
if not name.lower().endswith(".docx"):
|
| 272 |
+
continue
|
| 273 |
+
r = self.ex.sandbox.exec("python3 /workspace/skills/docx/scripts/generate_from_md.py /workspace/%s /workspace/output/%s"
|
| 274 |
+
% (d, name), timeout=180)
|
| 275 |
+
q = Path(self.ex.output_dir) / name
|
| 276 |
+
if not q.exists() or q.read_bytes()[:2] != b"PK":
|
| 277 |
+
probs.append("Building %s from %s failed: %s" % (name, d, ((r.stdout or "") + (r.stderr or ""))[-500:]))
|
| 278 |
+
continue
|
| 279 |
+
w = _docx_words(q)
|
| 280 |
+
if w < self.minw:
|
| 281 |
+
probs.append("%s has only %d words; the deliverable is not complete. Add the missing sections." % (name, max(w, 0)))
|
| 282 |
+
v = self.ex.sandbox.exec("python3 /workspace/skills/docx/scripts/validate.py /workspace/output/%s" % name, timeout=60)
|
| 283 |
+
if v.returncode not in (0, None):
|
| 284 |
+
probs.append("%s fails validation: %s" % (name, ((v.stdout or "") + (v.stderr or ""))[-400:]))
|
| 285 |
+
return probs
|
| 286 |
+
|
| 287 |
+
def after_turn(self, response, had_tools, model_read):
|
| 288 |
+
"""Return the next instruction for the model, or None (no message; when the model also made no tool call, the run ends)."""
|
| 289 |
+
txt = response.text or ""
|
| 290 |
+
saved = 0 if had_tools else self._save(txt, done_word=(self.phase == "draft"))
|
| 291 |
+
if getattr(self, "rejected", False):
|
| 292 |
+
return ("Your last reply repeated itself and was discarded. Write it again, concisely and without repetition: "
|
| 293 |
+
"for notes at most 15 bullets; for the deliverable at most about 800 words per reply.")
|
| 294 |
+
if self.phase == "read":
|
| 295 |
+
if had_tools and model_read:
|
| 296 |
+
return None
|
| 297 |
+
m = self._feed()
|
| 298 |
+
if m:
|
| 299 |
+
return m
|
| 300 |
+
self.phase = "plan"
|
| 301 |
+
return ("You have now seen every line of all %d documents. Your notes:\n%s\n\n---\nReply with the plan as plain text: "
|
| 302 |
+
"each deliverable (%s), its sections in order, and which document and section supports each point. The "
|
| 303 |
+
"harness saves your reply as /workspace/plan.md." % (len(self.g.docs()), self._notes(), ", ".join(self.names)))
|
| 304 |
+
if self.phase == "plan":
|
| 305 |
+
if self.g._ws_words("plan.md") >= 80:
|
| 306 |
+
self.phase = "draft"
|
| 307 |
+
return self._draft_msg()
|
| 308 |
+
return None if had_tools else "Reply with the plan as plain text (at least a few sections, with the supporting documents)."
|
| 309 |
+
if self.phase == "draft":
|
| 310 |
+
if had_tools:
|
| 311 |
+
return None
|
| 312 |
+
w = self.g._ws_words(self.drafts[self.i])
|
| 313 |
+
said_done = bool(_re.search(r"\bDONE\b", txt))
|
| 314 |
+
if w >= self.minw and (said_done or saved == 0):
|
| 315 |
+
if self.i + 1 < len(self.names):
|
| 316 |
+
self.i += 1
|
| 317 |
+
return self._draft_msg()
|
| 318 |
+
self.phase = "check"
|
| 319 |
+
return ("Now check your work. Reply as plain text with every requirement in the task instructions, one per line, "
|
| 320 |
+
"and for each the draft section that covers it, or MISSING. The harness saves it as /workspace/check.md.")
|
| 321 |
+
return ("Continue the deliverable %s: reply with the next sections in markdown (%d words so far; not complete yet). "
|
| 322 |
+
"Reply DONE only when every section of the plan is fully written." % (self.names[self.i], max(w, 0)))
|
| 323 |
+
if self.phase == "check":
|
| 324 |
+
if had_tools and self.g._ws_words("check.md") < 80:
|
| 325 |
+
return None
|
| 326 |
+
if self.g._ws_words("check.md") < 80:
|
| 327 |
+
return "Reply with the requirements check as plain text: every requirement, one per line, with its draft section."
|
| 328 |
+
chk = self._ws("check.md").read_text(errors="ignore")
|
| 329 |
+
if _re.search(r"\bMISSING\b", chk) and self.fails < 3:
|
| 330 |
+
self.fails += 1; self.phase = "draft"; self.i = 0
|
| 331 |
+
return self._draft_msg("Your check found MISSING requirements:\n%s\n\nAdd them now. " %
|
| 332 |
+
"\n".join(l for l in chk.splitlines() if "MISSING" in l)[:3000])
|
| 333 |
+
self.phase = "build"
|
| 334 |
+
if self.phase == "build":
|
| 335 |
+
probs = self._build()
|
| 336 |
+
if probs:
|
| 337 |
+
self.fails += 1; self.phase = "draft"; self.i = 0
|
| 338 |
+
return self._draft_msg("Building and checking the Word file found problems:\n- " + "\n- ".join(probs) +
|
| 339 |
+
"\n\nFix the draft. ")
|
| 340 |
+
self.phase = "done"
|
| 341 |
+
return ("The deliverables are built and validated in /workspace/output/ (%s). Reply with a two-sentence summary and stop."
|
| 342 |
+
% ", ".join(self.names))
|
| 343 |
+
return None
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
def _elide_old_tool_results(messages, keep_last: int = 6, keep_chars: int = 400) -> int:
|
| 347 |
+
"""Shrink old tool outputs (keeps the newest `keep_last`) so long tasks fit the context window. Chat-format only."""
|
| 348 |
+
guided = _os.environ.get("HL_SCAFFOLD") == "2"
|
| 349 |
+
idx = [i for i, m in enumerate(messages) if isinstance(m, dict) and (m.get("role") == "tool" or (
|
| 350 |
+
m.get("role") == "user" and str(m.get("content", "")).startswith(_Guide.MARK)) or (
|
| 351 |
+
guided and m.get("role") == "assistant" and isinstance(m.get("content"), str)))]
|
| 352 |
+
n = 0
|
| 353 |
+
for i in idx[:-keep_last] if len(idx) > keep_last else []:
|
| 354 |
+
c = messages[i].get("content")
|
| 355 |
+
if isinstance(c, str) and len(c) > keep_chars + 200 and not c.endswith("[elided]"):
|
| 356 |
+
messages[i] = dict(messages[i], content=c[:keep_chars] + "\n...[earlier tool output elided to save context; "
|
| 357 |
+
"your notes are in /workspace/notes.md, re-read the file if you need it][elided]")
|
| 358 |
+
n += 1
|
| 359 |
+
return n
|
| 360 |
+
|
| 361 |
+
|