mlai-code 0.0.0-stage → 0.3.1

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.
Files changed (182) hide show
  1. package/README.md +12 -2
  2. package/bin/mlai.mjs +3 -0
  3. package/package.json +18 -3
  4. package/src/browser-setup.mjs +77 -0
  5. package/src/cli-ext.mjs +146 -0
  6. package/src/cli.mjs +534 -0
  7. package/src/cloud.mjs +29 -0
  8. package/src/completion.mjs +159 -0
  9. package/src/credentials.mjs +54 -0
  10. package/src/deploy.mjs +134 -0
  11. package/src/distribution.mjs +373 -0
  12. package/src/engine-key.mjs +4 -0
  13. package/src/engine.mjs +167 -0
  14. package/src/headless.mjs +196 -0
  15. package/src/home.mjs +22 -0
  16. package/src/host.mjs +123 -0
  17. package/src/local-models.mjs +64 -0
  18. package/src/modes.mjs +38 -0
  19. package/src/netbox.mjs +97 -0
  20. package/src/netjail.py +59 -0
  21. package/src/offline.mjs +98 -0
  22. package/src/parity.mjs +86 -0
  23. package/src/project.mjs +68 -0
  24. package/src/relay.mjs +140 -0
  25. package/src/render.mjs +73 -0
  26. package/src/restricted.mjs +47 -0
  27. package/src/review.mjs +70 -0
  28. package/src/runner.mjs +127 -0
  29. package/src/runtime.mjs +75 -0
  30. package/src/sandbox.mjs +135 -0
  31. package/src/sdk.mjs +11 -0
  32. package/src/server.mjs +110 -0
  33. package/src/session.mjs +290 -0
  34. package/src/settings.mjs +135 -0
  35. package/src/shared-data.mjs +3 -0
  36. package/src/tui/acp.mjs +145 -0
  37. package/src/tui/agents-cli.mjs +114 -0
  38. package/src/tui/agents-local.mjs +65 -0
  39. package/src/tui/ansi.mjs +123 -0
  40. package/src/tui/app.mjs +710 -0
  41. package/src/tui/approval.mjs +88 -0
  42. package/src/tui/bg.mjs +172 -0
  43. package/src/tui/bgterm.mjs +79 -0
  44. package/src/tui/chat.mjs +230 -0
  45. package/src/tui/clipboard.mjs +74 -0
  46. package/src/tui/cloud-cli.mjs +57 -0
  47. package/src/tui/cloud-run.mjs +123 -0
  48. package/src/tui/cmd-agents.mjs +130 -0
  49. package/src/tui/cmd-auto-review.mjs +94 -0
  50. package/src/tui/cmd-cloud.mjs +17 -0
  51. package/src/tui/cmd-config.mjs +104 -0
  52. package/src/tui/cmd-core.mjs +160 -0
  53. package/src/tui/cmd-distribution.mjs +15 -0
  54. package/src/tui/cmd-ecosystem.mjs +169 -0
  55. package/src/tui/cmd-extensibility.mjs +333 -0
  56. package/src/tui/cmd-remote.mjs +18 -0
  57. package/src/tui/cmd-review.mjs +58 -0
  58. package/src/tui/cmd-session-extra.mjs +152 -0
  59. package/src/tui/cmd-tui.mjs +200 -0
  60. package/src/tui/commands.mjs +160 -0
  61. package/src/tui/custom-commands.mjs +98 -0
  62. package/src/tui/diff.mjs +60 -0
  63. package/src/tui/editor.mjs +190 -0
  64. package/src/tui/ext-state.mjs +16 -0
  65. package/src/tui/extensions-local.mjs +140 -0
  66. package/src/tui/files.mjs +72 -0
  67. package/src/tui/handoff.mjs +110 -0
  68. package/src/tui/import-setups.mjs +151 -0
  69. package/src/tui/init.mjs +104 -0
  70. package/src/tui/keymap.mjs +70 -0
  71. package/src/tui/keys.mjs +101 -0
  72. package/src/tui/loop.mjs +75 -0
  73. package/src/tui/markdown.mjs +118 -0
  74. package/src/tui/mcp-config.mjs +225 -0
  75. package/src/tui/mcp-oauth.mjs +162 -0
  76. package/src/tui/permissions.mjs +189 -0
  77. package/src/tui/plain.mjs +32 -0
  78. package/src/tui/plugins-local.mjs +103 -0
  79. package/src/tui/remote.mjs +125 -0
  80. package/src/tui/review-cli.mjs +332 -0
  81. package/src/tui/review-github.mjs +59 -0
  82. package/src/tui/screen.mjs +60 -0
  83. package/src/tui/session-ops.mjs +104 -0
  84. package/src/tui/skills-local.mjs +92 -0
  85. package/src/tui/snapshots.mjs +87 -0
  86. package/src/tui/statusline.mjs +98 -0
  87. package/src/tui/transcript.mjs +116 -0
  88. package/src/two-words.mjs +36 -0
  89. package/src/wakeups.mjs +53 -0
  90. package/src/winenv.mjs +94 -0
  91. package/src/winsandbox.py +351 -0
  92. package/src/workspace-api.mjs +621 -0
  93. package/src/worktree.mjs +128 -0
  94. package/vendor/client-sdk/controller.cjs +115 -0
  95. package/vendor/client-sdk/files.cjs +38 -0
  96. package/vendor/client-sdk/index.cjs +133 -0
  97. package/vendor/client-sdk/login.cjs +52 -0
  98. package/vendor/client-sdk/updates.cjs +41 -0
  99. package/vendor/cloud-cli/mlai.mjs +394 -0
  100. package/worker/architecture_parse.cjs +23 -0
  101. package/worker/architecture_worker.py +394 -0
  102. package/worker/ask_first.py +96 -0
  103. package/worker/attachment_reader.py +66 -0
  104. package/worker/background_jobs.py +111 -0
  105. package/worker/brief_questions.py +56 -0
  106. package/worker/browser_local.py +49 -0
  107. package/worker/browser_pane.py +89 -0
  108. package/worker/browser_tool.py +101 -0
  109. package/worker/build_planner.py +187 -0
  110. package/worker/chat_fork.py +277 -0
  111. package/worker/checkpoints.py +133 -0
  112. package/worker/cloud_pty.py +351 -0
  113. package/worker/command_policy.py +39 -0
  114. package/worker/command_safety.py +717 -0
  115. package/worker/computer_card.sh +23 -0
  116. package/worker/computer_setup.sh +86 -0
  117. package/worker/computer_use.py +221 -0
  118. package/worker/connector_search.py +43 -0
  119. package/worker/context_manager.py +60 -0
  120. package/worker/conversation.py +144 -0
  121. package/worker/deploy_pack.py +116 -0
  122. package/worker/design_director.py +240 -0
  123. package/worker/dev_jobs.py +166 -0
  124. package/worker/edit_guard.py +99 -0
  125. package/worker/env_setup.py +129 -0
  126. package/worker/evidence.py +109 -0
  127. package/worker/fast_search.py +24 -0
  128. package/worker/git_sync.py +333 -0
  129. package/worker/goals.py +156 -0
  130. package/worker/handover.py +98 -0
  131. package/worker/harness_memory.py +194 -0
  132. package/worker/image_generation.py +173 -0
  133. package/worker/intelligence.py +149 -0
  134. package/worker/intent_model.py +78 -0
  135. package/worker/intent_weights.json +1 -0
  136. package/worker/ios_simulator.py +87 -0
  137. package/worker/kimi_bridge.py +997 -0
  138. package/worker/kimi_compact.py +93 -0
  139. package/worker/kimi_gates.py +304 -0
  140. package/worker/kimi_hook.py +617 -0
  141. package/worker/lifecycle.py +95 -0
  142. package/worker/lsp.py +77 -0
  143. package/worker/mcp_local.py +293 -0
  144. package/worker/memory.py +99 -0
  145. package/worker/mlai_auto_review.py +177 -0
  146. package/worker/mlai_hooks.py +966 -0
  147. package/worker/mlai_mcp.py +441 -0
  148. package/worker/mlai_permissions.py +283 -0
  149. package/worker/model_stream.py +75 -0
  150. package/worker/nested_repos.py +76 -0
  151. package/worker/offline_guard.py +17 -0
  152. package/worker/preview.py +806 -0
  153. package/worker/proc_watch.py +124 -0
  154. package/worker/project_context.py +72 -0
  155. package/worker/project_rules.py +99 -0
  156. package/worker/quality.py +180 -0
  157. package/worker/reply_language.py +56 -0
  158. package/worker/repo_hygiene.py +132 -0
  159. package/worker/repo_memory.py +101 -0
  160. package/worker/repo_policy.py +269 -0
  161. package/worker/roles.json +3173 -0
  162. package/worker/roles.py +303 -0
  163. package/worker/runner.py +1795 -0
  164. package/worker/runtime_control.py +205 -0
  165. package/worker/secret_scan.py +168 -0
  166. package/worker/session_hub.py +80 -0
  167. package/worker/skill_library.py +129 -0
  168. package/worker/skill_pack.py +533 -0
  169. package/worker/subagents.py +91 -0
  170. package/worker/task_controller.py +139 -0
  171. package/worker/task_skills.py +159 -0
  172. package/worker/team_checkpoint.py +33 -0
  173. package/worker/token_count.py +32 -0
  174. package/worker/tool_catalog.py +38 -0
  175. package/worker/upload_storage.py +531 -0
  176. package/worker/verification.py +725 -0
  177. package/worker/vision.py +84 -0
  178. package/worker/wakeups.py +63 -0
  179. package/worker/web_tools.py +203 -0
  180. package/worker/winproc.py +135 -0
  181. package/worker/work_ledger.py +117 -0
  182. package/worker/worktrees.py +79 -0
@@ -0,0 +1,129 @@
1
+ """Automatic project setup: detect the stack, install dependencies once, redo it only when manifests change.
2
+
3
+ Priority: .mlai/setup.sh (the project's own recipe) > devcontainer postCreateCommand > detected
4
+ package managers (Node by lockfile, Python by uv.lock / requirements / pyproject). State lives in
5
+ ROOT/env-setup.json (manifest hash, steps, results) and output in ROOT/env-setup.log.
6
+ """
7
+ import hashlib, json, os, re, shutil, subprocess, time
8
+ from pathlib import Path
9
+
10
+ MANIFESTS = ('package.json', 'package-lock.json', 'pnpm-lock.yaml', 'yarn.lock', 'bun.lockb', 'bun.lock', 'requirements.txt', 'requirements-dev.txt',
11
+ 'pyproject.toml', 'uv.lock', 'poetry.lock', '.mlai/setup.sh', '.devcontainer/devcontainer.json', 'Pipfile.lock')
12
+ TOTAL_SECONDS, STEP_SECONDS = 420, 300
13
+
14
+
15
+ def manifest_hash(repo):
16
+ h = hashlib.sha256()
17
+ for name in MANIFESTS:
18
+ p = Path(repo) / name
19
+ try:
20
+ if p.is_file() and not p.is_symlink() and p.stat().st_size < 5_000_000: h.update(name.encode() + b'\0' + p.read_bytes() + b'\0')
21
+ except OSError: pass
22
+ return h.hexdigest()[:24]
23
+
24
+
25
+ def _devcontainer_command(repo):
26
+ p = Path(repo) / '.devcontainer' / 'devcontainer.json'
27
+ try: text = p.read_text()
28
+ except OSError: return None
29
+ text = re.sub(r'(?m)^\s*//.*$', '', text)
30
+ text = re.sub(r',(\s*[}\]])', r'\1', text)
31
+ try: data = json.loads(text)
32
+ except ValueError: return None
33
+ cmd = data.get('postCreateCommand') or data.get('onCreateCommand')
34
+ if isinstance(cmd, list) and all(isinstance(c, str) for c in cmd): return ' '.join(cmd)
35
+ if isinstance(cmd, dict): return ' && '.join(v if isinstance(v, str) else ' '.join(v) for v in cmd.values() if isinstance(v, (str, list)))
36
+ return cmd if isinstance(cmd, str) and cmd.strip() else None
37
+
38
+
39
+ def detect(repo):
40
+ """Return [(label, shell command)] in run order."""
41
+ r = Path(repo)
42
+ if (r / '.mlai' / 'setup.sh').is_file(): return [('Project setup script', 'bash .mlai/setup.sh')]
43
+ dev = _devcontainer_command(repo)
44
+ if dev: return [('Dev container setup', dev)]
45
+ steps = []
46
+ if (r / 'package.json').is_file():
47
+ if (r / 'pnpm-lock.yaml').is_file(): steps.append(('Node packages (pnpm)', 'corepack enable >/dev/null 2>&1 || npm install -g pnpm >/dev/null 2>&1; pnpm install --frozen-lockfile --prefer-offline || pnpm install --prefer-offline'))
48
+ elif (r / 'yarn.lock').is_file(): steps.append(('Node packages (yarn)', 'corepack enable >/dev/null 2>&1 || npm install -g yarn >/dev/null 2>&1; yarn install --frozen-lockfile --prefer-offline || yarn install --prefer-offline'))
49
+ elif ((r / 'bun.lockb').is_file() or (r / 'bun.lock').is_file()) and shutil.which('bun'): steps.append(('Node packages (bun)', 'bun install'))
50
+ elif (r / 'package-lock.json').is_file(): steps.append(('Node packages (npm)', 'npm ci --prefer-offline --no-audit --no-fund || npm install --prefer-offline --no-audit --no-fund'))
51
+ else: steps.append(('Node packages (npm)', 'npm install --prefer-offline --no-audit --no-fund')) # writes package-lock.json so later installs are exact and fast
52
+ if (r / 'uv.lock').is_file():
53
+ steps.append(('Python packages (uv)', ('' if shutil.which('uv') else 'python3 -m pip install --quiet --user uv && export PATH=$HOME/.local/bin:$PATH; ') + 'uv sync'))
54
+ elif (r / 'requirements.txt').is_file() or (r / 'pyproject.toml').is_file():
55
+ req = 'requirements.txt' if (r / 'requirements.txt').is_file() else None
56
+ install = ('.venv/bin/pip install --quiet -r requirements.txt' + (' -r requirements-dev.txt' if (r / 'requirements-dev.txt').is_file() else '')) if req else '.venv/bin/pip install --quiet -e . || .venv/bin/pip install --quiet .'
57
+ steps.append(('Python packages (venv)', '[ -x .venv/bin/python ] || python3 -m venv .venv; ' + install))
58
+ return steps
59
+
60
+
61
+ def tools_line(root='/vercel/sandbox/.mini'):
62
+ """The computer card (written when the workspace image is built) or a quick probe on older images."""
63
+ try:
64
+ card = (Path(root) / 'computer.md').read_text().strip()
65
+ if card: return _live_size(card)[:2400]
66
+ except OSError: pass
67
+ return _probe_line(root)
68
+
69
+
70
+ def _live_size(card):
71
+ """The card is baked on the build machine; swap in this computer's real CPUs and RAM."""
72
+ try:
73
+ cpus = os.cpu_count() or 0
74
+ try: cpus = len(os.sched_getaffinity(0)) or cpus
75
+ except (AttributeError, OSError): pass
76
+ kb = next(int(l.split()[1]) for l in open('/proc/meminfo') if l.startswith('MemTotal:'))
77
+ gb = max(1, round(kb / 1048576))
78
+ except (OSError, StopIteration, ValueError):
79
+ return card
80
+ return re.sub(r'\d+ CPUs, \d+ GB RAM', f'{cpus} CPUs, {gb} GB RAM', card, count=1)
81
+
82
+
83
+ def _probe_line(root):
84
+ names = ('node', 'npm', 'pnpm', 'bun', 'python3', 'uv', 'pip', 'git', 'gh', 'jq', 'rg', 'docker', 'go', 'cargo', 'java')
85
+ found = [n for n in names if shutil.which(n)]
86
+ versions = []
87
+ for cmd in (['node', '-v'], ['python3', '-V']):
88
+ try:
89
+ out = subprocess.run(cmd, capture_output=True, text=True, timeout=5)
90
+ versions.append((out.stdout or out.stderr).strip())
91
+ except (OSError, subprocess.TimeoutExpired): pass
92
+ return ('Workspace tools: ' + ', '.join(versions) + (' | on PATH: ' + ' '.join(found) if found else '')
93
+ + '. You have passwordless sudo: install missing system packages with sudo apt-get update && sudo apt-get install -y <pkg>.'
94
+ + (' This computer predates the full toolset; when a task needs several missing toolchains (Go, Rust, Java, Docker, Postgres, Redis, cloud CLIs), start `sudo bash ' + str(Path(root) / 'computer_setup.sh') + ' && bash ' + str(Path(root) / 'computer_card.sh') + '` as a background job (about 4 minutes) instead of installing piecemeal.' if (Path(root) / 'computer_setup.sh').is_file() else ''))
95
+
96
+
97
+ def ensure(root, repo, event=None, env=None, force=False):
98
+ """Run setup if the manifests changed since the last successful setup. Returns a context note for the agent."""
99
+ root, repo = Path(root), Path(repo)
100
+ state_file = root / 'env-setup.json'
101
+ try: prior = json.loads(state_file.read_text())
102
+ except (OSError, ValueError): prior = {}
103
+ digest = manifest_hash(repo)
104
+ steps = detect(repo)
105
+ if not steps:
106
+ return ''
107
+ if not force and prior.get('hash') == digest and prior.get('ok'):
108
+ return 'Project environment is set up (' + ', '.join(prior.get('labels', [])) + '); dependencies are installed.\n'
109
+ if event: event('environment', state='running', content='Setting up the project environment: ' + ', '.join(l for l, _ in steps))
110
+ started = time.time(); results = []; log = root / 'env-setup.log'
111
+ with open(log, 'w') as out:
112
+ for label, cmd in steps:
113
+ remaining = TOTAL_SECONDS - (time.time() - started)
114
+ if remaining < 20: results.append({'label': label, 'code': None, 'note': 'skipped: setup time budget used'}); continue
115
+ out.write('$ ' + cmd + '\n'); out.flush()
116
+ try:
117
+ p = subprocess.run(['bash', '-lc', cmd], cwd=str(repo), stdout=out, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL, timeout=min(STEP_SECONDS, remaining), env=env)
118
+ code = p.returncode
119
+ except subprocess.TimeoutExpired: code = 124
120
+ results.append({'label': label, 'code': code})
121
+ out.write('\n[exit %s]\n' % code); out.flush()
122
+ ok = all(r.get('code') == 0 for r in results)
123
+ state_file.write_text(json.dumps({'hash': digest, 'ok': ok, 'labels': [l for l, _ in steps], 'results': results, 'time': time.time(), 'seconds': round(time.time() - started, 1)}))
124
+ if event: event('environment', state='ready' if ok else 'failed', content=('Project environment ready' if ok else 'Project setup had problems') + ' (' + ', '.join(l + (' ok' if r.get('code') == 0 else ' failed') for (l, _), r in zip(steps, results)) + ') in %.0fs' % (time.time() - started))
125
+ if ok: return 'Project environment was just set up (' + ', '.join(l for l, _ in steps) + '); dependencies are installed.\n'
126
+ try: tail = log.read_text()[-1500:]
127
+ except OSError: tail = ''
128
+ failed = [r['label'] for r in results if r.get('code') != 0]
129
+ return 'Automatic project setup failed for: ' + ', '.join(failed) + '. Fix the setup before relying on these dependencies. Log tail (' + str(log) + '):\n' + tail + '\n'
@@ -0,0 +1,109 @@
1
+ """Per-run source/claim ledger. Provenance and quotes are checked, truth is not inferred."""
2
+ import hashlib,json,re,threading,unicodedata
3
+ from pathlib import Path
4
+ from web_tools import public_url
5
+
6
+ URL=re.compile(r'https://[^\s<>"\\]+')
7
+ def urls(text):
8
+ result=[]
9
+ for match in URL.finditer(text):
10
+ value=match.group().rstrip('.,);]}')
11
+ try:public_url(value)
12
+ except ValueError:continue
13
+ if value not in result:result.append(value)
14
+ return result[:40]
15
+ QUOTES=str.maketrans({'\u2018':"'",'\u2019':"'",'\u201c':'"','\u201d':'"','\u2013':'-','\u2014':'-','\u2212':'-','\u00a0':' '})
16
+ def normalized(text):
17
+ text=unicodedata.normalize('NFKC',text).translate(QUOTES)
18
+ text=re.sub(r'[*_`]+','',text)
19
+ text=re.sub(r'\s+([.,;:!?)\]])',r'\1',' '.join(text.split()))
20
+ return text.casefold()
21
+ def quote_key(quote):return normalized(quote).strip().strip('"\'').rstrip('.,;: ').strip()
22
+
23
+ class EvidenceLedger:
24
+ def __init__(self,root,task_key):
25
+ self.path=Path(root)/'research-evidence.json';self.key=task_key;self.lock=threading.RLock()
26
+ def load(self):
27
+ try:
28
+ if self.path.is_symlink() or self.path.stat().st_size>600000:return self.empty()
29
+ data=json.loads(self.path.read_text())
30
+ if data.get('task_key')==self.key:return data
31
+ except (OSError,ValueError,AttributeError):pass
32
+ return self.empty()
33
+ def empty(self):return {'task_key':self.key,'sources':{},'claims':[]}
34
+ def save(self,data):
35
+ temp=self.path.with_suffix('.tmp');temp.write_text(json.dumps(data));temp.replace(self.path)
36
+ def ingest(self,tool,args,result):
37
+ with self.lock:
38
+ data=self.load();wrapped=json.loads(result);body=wrapped.get('untrusted_web_content','')
39
+ if not isinstance(body,str):return []
40
+ structured={}
41
+ try:
42
+ parsed=json.loads(body)
43
+ if isinstance(parsed,list):
44
+ for entry in parsed:
45
+ if isinstance(entry,dict) and isinstance(entry.get('url'),str) and isinstance(entry.get('text'),str):
46
+ structured[entry['url']]=entry['url']+'\n'+str(entry.get('title',''))+'\n'+entry['text']
47
+ except ValueError:pass
48
+ found=list(structured) or urls(body);ids=[] # structured results: the result pages themselves, not every link inside their markdown
49
+ for url in found:
50
+ ident='src-'+hashlib.sha256(url.encode()).hexdigest()[:12]
51
+ if ident not in data['sources'] and len(data['sources'])>=60:continue
52
+ start=max(0,body.find(url));following=[body.find(u,start+len(url)) for u in found if u!=url];end=min([i for i in following if i>=0] or [len(body)])
53
+ kind='fetched_page' if tool=='web_fetch' and url in args.get('urls',[]) else 'search_excerpt'
54
+ previous=data['sources'].get(ident)
55
+ if not previous or kind=='fetched_page' or previous['kind']!='fetched_page':
56
+ data['sources'][ident]={'id':ident,'url':url,'kind':kind,'provider':wrapped.get('provider'),'retrieved_at':wrapped.get('cached_at'),'excerpt':structured.get(url,body[start:end])[:6000]}
57
+ ids.append(ident)
58
+ self.save(data);return ids
59
+ def run(self,args):
60
+ """Validate claims one by one: valid claims are recorded (merged), invalid ones are returned with the exact reason."""
61
+ with self.lock:
62
+ data=self.load()
63
+ if not args:return data
64
+ if set(args)!={'claims'} or not isinstance(args['claims'],list) or not 1<=len(args['claims'])<=20:raise ValueError('Provide 1–20 claims, or {} to read sources and claims')
65
+ accepted,rejected,notes=[],[],[]
66
+ for index,entry in enumerate(args['claims']):
67
+ try:
68
+ record,note=self.check(data,entry)
69
+ accepted.append(record)
70
+ if note:notes.append({'index':index,'note':note})
71
+ except ValueError as error:
72
+ rejected.append({'index':index,'claim':str(entry.get('claim',''))[:160] if isinstance(entry,dict) else '','reason':str(error)})
73
+ merged={c['claim']:c for c in data['claims']}
74
+ for record in accepted:merged[record['claim']]=record
75
+ data['claims']=list(merged.values())[-40:];self.save(data)
76
+ result={'recorded':len(accepted),'total_claims':len(data['claims']),'rejected':rejected,'notes':notes}
77
+ if rejected:result['next']='Only the rejected claims need fixing. Copy a shorter quote verbatim from the excerpt (read sources with evidence {}), cite the source ID that contains it, or relabel the claim as inference with uncertainty. Accepted claims are already recorded; do not resend them.'
78
+ if not accepted:raise ValueError(json.dumps(result))
79
+ return result
80
+ def check(self,data,entry):
81
+ if not isinstance(entry,dict) or set(entry)-{'claim','kind','source_ids','quote','uncertainty'}:raise ValueError('Invalid evidence claim: use only claim, kind, source_ids, quote, uncertainty')
82
+ claim=entry.get('claim');kind=entry.get('kind');ids=entry.get('source_ids',[]);quote=entry.get('quote','');uncertainty=entry.get('uncertainty','')
83
+ if not isinstance(claim,str) or not 1<=len(claim)<=2000 or kind not in ('fact','inference','assumption'):raise ValueError('Claim needs text and kind fact/inference/assumption')
84
+ if not isinstance(ids,list) or len(ids)>5 or any(not isinstance(i,str) for i in ids):raise ValueError('source_ids must be a list of up to 5 retrieved source IDs')
85
+ unknown=[i for i in ids if i not in data['sources']]
86
+ if unknown:raise ValueError('Unknown source IDs '+', '.join(unknown[:5])+'; use IDs returned by web_search or web_fetch')
87
+ if not isinstance(quote,str) or len(quote)>1000 or not isinstance(uncertainty,str) or len(uncertainty)>2000:raise ValueError('Invalid quote or uncertainty')
88
+ if kind=='fact' and (not ids or not quote.strip()):raise ValueError('Factual claims require retrieved source IDs and a supporting exact quote')
89
+ if kind!='fact' and not uncertainty.strip():raise ValueError('State uncertainty for assumptions and inferences')
90
+ note=''
91
+ if quote.strip():
92
+ key=quote_key(quote)
93
+ if len(key)<12 and kind=='fact':raise ValueError('Quote is too short to support a fact; quote at least a full phrase')
94
+ if not any(key in normalized(data['sources'][i]['excerpt']) for i in ids):
95
+ elsewhere=[i for i,src in data['sources'].items() if key in normalized(src['excerpt'])]
96
+ if elsewhere:
97
+ ids=elsewhere[:5];note='Quote found in '+', '.join(ids)+' (not the cited source); source IDs corrected'
98
+ elif kind=='fact':
99
+ raise ValueError('Quote was not found in '+', '.join(ids)+' or any retrieved source. The stored excerpt may be partial: quote text that appears in the excerpt, fetch the page with web_fetch, or relabel as inference')
100
+ else:
101
+ note='Quote not found in stored excerpts, so it was dropped; the claim is recorded as '+kind;quote=''
102
+ return {'claim':claim,'kind':kind,'source_ids':ids,'quote':quote,'uncertainty':uncertainty},note
103
+ def completion_issue(self,answer):
104
+ data=self.load()
105
+ if not data['sources']:return None
106
+ if not data['claims']:return 'Record the material recommendations with evidence {claims:[{claim,kind,source_ids,quote,uncertainty}]} before finishing. Facts need an exact supporting quote; label inferences and assumptions.'
107
+ missing=[i for c in data['claims'] for i in c['source_ids'] if data['sources'][i]['url'] not in answer]
108
+ if missing:return 'Link the retrieved source URLs for the recorded claims in the final answer: '+', '.join(sorted({data['sources'][i]['url'] for i in missing}))
109
+ return None
@@ -0,0 +1,24 @@
1
+ """Literal repository search through git grep (index-aware, skips ignored files); callers still apply path policy."""
2
+ import subprocess
3
+
4
+ def git_grep(repo, query, limit=100, timeout=15):
5
+ """List of (path, line_number, line) or None when git grep is unavailable."""
6
+ try:
7
+ proc = subprocess.run(['git', '-c', 'core.quotePath=false', 'grep', '-n', '-I', '-F', '--untracked', '--no-color', '-e', query, '--', '.',
8
+ ':(exclude).git', ':(exclude)node_modules', ':(exclude).next', ':(exclude).mini'],
9
+ cwd=repo, capture_output=True, timeout=timeout)
10
+ except Exception: # Missing git, timeouts or a patched process layer all fall back to the Python walker.
11
+ return None
12
+ if not isinstance(getattr(proc, 'stdout', None), bytes) or proc.returncode not in (0, 1) or b'not a git repository' in proc.stderr:
13
+ return None
14
+ hits = []
15
+ for raw in proc.stdout.splitlines():
16
+ line = raw.decode('utf-8', errors='replace')
17
+ path, sep, rest = line.partition(':')
18
+ number, sep2, text = rest.partition(':')
19
+ if not sep or not sep2 or not number.isdigit():
20
+ continue
21
+ hits.append((path, int(number), text))
22
+ if len(hits) >= limit * 3:
23
+ break
24
+ return hits
@@ -0,0 +1,333 @@
1
+ """Two-way GitHub sync for a workspace repository.
2
+
3
+ Runs inside the workspace. The server passes the GitHub token through the
4
+ MINI_GH_TOKEN environment variable; it is handed to git as an HTTP header via
5
+ GIT_CONFIG_* variables, so it never lands in .git/config, the remote URL,
6
+ command arguments or output.
7
+
8
+ Model: `baseline` is the GitHub commit the local work is based on. Local work
9
+ is everything that differs between `baseline` and the working tree, including
10
+ task checkpoint commits. Pull and push replay that difference onto the target
11
+ commit with a three-way apply in a scratch worktree, so the user's working
12
+ tree is only touched after the result is known to be clean.
13
+
14
+ Usage: python3 git_sync.py <action> '<json args>' (prints one JSON line)
15
+ """
16
+ import base64, json, os, re, shutil, subprocess, sys, tempfile
17
+ from pathlib import Path
18
+
19
+ SKIP = ('node_modules', '.next', '.venv', '__pycache__')
20
+ EXCLUDES = [f':(exclude,glob)**/{d}/**' for d in SKIP] + [f':(exclude){d}' for d in SKIP]
21
+ SECRET_FILE = re.compile(r'(^|/)(\.env(\..*)?|.*\.pem|.*\.key|id_rsa|id_ed25519)$')
22
+ BRANCH = re.compile(r'^(?!-)(?!.*\.\.)(?!.*@\{)(?!.*//)[A-Za-z0-9._/-]{1,200}(?<![./])$')
23
+
24
+
25
+ class SyncError(Exception):
26
+ pass
27
+
28
+
29
+ GIT = shutil.which('git', path='/usr/bin:/bin:/usr/local/bin') or 'git'
30
+ # Overrides that beat anything the workspace (or an agent) wrote into .git/config,
31
+ # so hooks, proxies, credential helpers and push redirects cannot see the token.
32
+ HARDEN = [('core.hooksPath', '/dev/null'), ('core.fsmonitor', 'false'), ('http.sslVerify', 'true'), ('http.proxy', ''),
33
+ ('credential.helper', ''), ('core.askPass', ''), ('protocol.ext.allow', 'never'), ('protocol.file.allow', 'user'),
34
+ ('safe.directory', '*')]
35
+
36
+
37
+ def git_env(extra=None):
38
+ env = {k: v for k, v in os.environ.items() if not k.startswith('GIT_')}
39
+ env.update({'GIT_TERMINAL_PROMPT': '0', 'GIT_ASKPASS': '/bin/false', 'GCM_INTERACTIVE': 'never', 'GIT_CONFIG_NOSYSTEM': '1'})
40
+ token = env.pop('MINI_GH_TOKEN', '')
41
+ # Other hosts (GitLab cloud or self-managed): MINI_GIT_HOST is https://host/, MINI_GIT_USER the basic-auth user.
42
+ host = env.pop('MINI_GIT_HOST', '') or 'https://github.com/'
43
+ user = env.pop('MINI_GIT_USER', '') or 'x-access-token'
44
+ env.pop('MINI_GIT_PROVIDER', None)
45
+ if not re.fullmatch(r'https://[A-Za-z0-9.-]+(:\d+)?/', host):
46
+ host = 'https://github.com/'
47
+ pairs = list(HARDEN)
48
+ if token:
49
+ basic = base64.b64encode((user + ':' + token).encode()).decode()
50
+ pairs.append(('http.' + host + '.extraheader', 'AUTHORIZATION: basic ' + basic))
51
+ env['GIT_CONFIG_COUNT'] = str(len(pairs))
52
+ for i, (k, v) in enumerate(pairs):
53
+ env[f'GIT_CONFIG_KEY_{i}'] = k
54
+ env[f'GIT_CONFIG_VALUE_{i}'] = v
55
+ if extra:
56
+ env.update(extra)
57
+ return env
58
+
59
+
60
+ def clean(text):
61
+ token = os.environ.get('MINI_GH_TOKEN', '')
62
+ text = text or ''
63
+ if token:
64
+ text = text.replace(token, '[redacted]')
65
+ return re.sub(r'(AUTHORIZATION: basic )\S+', r'\1[redacted]', text, flags=re.I)
66
+
67
+
68
+ def friendly(stderr, fallback):
69
+ s = stderr.lower()
70
+ if 'could not read username' in s or 'authentication failed' in s or 'invalid username or password' in s or 'terminal prompts disabled' in s:
71
+ return 'GitHub did not accept the connection. Connect GitHub with a token that has Contents read and write access to this repository.'
72
+ if 'repository not found' in s or 'not found' in s and 'remote' in s:
73
+ return 'GitHub could not find this repository, or the connected token cannot see it. Check the URL and the token\'s repository access.'
74
+ if 'protected branch' in s or 'gh006' in s or 'gh013' in s:
75
+ return 'This branch is protected on GitHub. Publish a draft PR instead.'
76
+ if 'permission to' in s and 'denied' in s or 'write access to repository not granted' in s or ' 403' in s:
77
+ return 'The connected token can read this repository but cannot write to it. Give it Contents: Read and write.'
78
+ if 'non-fast-forward' in s or 'fetch first' in s or 'rejected' in s:
79
+ return 'GitHub has newer commits on this branch. Sync first, then push.'
80
+ if 'workflow' in s and 'scope' in s:
81
+ return 'Changing files under .github/workflows needs a token with the Workflows permission.'
82
+ tail = clean(stderr).strip().splitlines()[-3:]
83
+ return fallback + (': ' + ' '.join(tail) if tail else '')
84
+
85
+
86
+ def git(args, cwd, check=True, env=None, input=None, timeout=300):
87
+ r = subprocess.run([GIT, *args], cwd=cwd, capture_output=True, env=env or git_env(), input=input, timeout=timeout)
88
+ if check and r.returncode != 0:
89
+ raise SyncError(friendly(r.stderr.decode(errors='replace'), 'git ' + args[0] + ' failed'))
90
+ return r
91
+
92
+
93
+ def out(args, cwd, **kw):
94
+ return git(args, cwd, **kw).stdout.decode().strip()
95
+
96
+
97
+ def valid_branch(name):
98
+ if not isinstance(name, str) or not BRANCH.match(name) or name.endswith('.lock'):
99
+ raise SyncError('Invalid branch name')
100
+ return name
101
+
102
+
103
+ def remote_url(repository):
104
+ if not isinstance(repository, str) or not re.fullmatch(r'[\w.-]+/[\w.-]+', repository):
105
+ raise SyncError('Invalid repository')
106
+ return 'https://github.com/' + repository + '.git'
107
+
108
+
109
+ def origin_url(a):
110
+ return a.get('remote') or remote_url(a.get('repository'))
111
+
112
+
113
+ def ensure_remote(repo, a):
114
+ """Point origin at the GitHub repository, without credentials."""
115
+ url = origin_url(a)
116
+ current = git(['remote', 'get-url', 'origin'], repo, check=False)
117
+ if current.returncode != 0:
118
+ git(['remote', 'add', 'origin', url], repo)
119
+ elif current.stdout.decode().strip() != url:
120
+ git(['remote', 'set-url', 'origin', url], repo)
121
+
122
+
123
+ def fetch(repo, branch):
124
+ branch = valid_branch(branch)
125
+ git(['fetch', '--no-tags', '--depth=50', 'origin', '+refs/heads/' + branch + ':refs/remotes/origin/' + branch], repo)
126
+ return out(['rev-parse', 'refs/remotes/origin/' + branch], repo)
127
+
128
+
129
+ def has_object(repo, sha):
130
+ return git(['cat-file', '-e', sha + '^{commit}'], repo, check=False).returncode == 0
131
+
132
+
133
+ def ensure_commit(repo, sha):
134
+ if sha and not has_object(repo, sha):
135
+ git(['fetch', '--no-tags', '--depth=1', 'origin', sha], repo)
136
+
137
+
138
+ def snapshot(repo, baseline):
139
+ """Commit object holding the current working tree, parented on baseline. HEAD and the index are not touched."""
140
+ fd, index = tempfile.mkstemp(prefix='mini-sync-index-')
141
+ os.close(fd)
142
+ os.unlink(index)
143
+ env = git_env({'GIT_INDEX_FILE': index, 'GIT_AUTHOR_NAME': 'Mini Code', 'GIT_AUTHOR_EMAIL': 'workspace@mini-code.local',
144
+ 'GIT_COMMITTER_NAME': 'Mini Code', 'GIT_COMMITTER_EMAIL': 'workspace@mini-code.local'})
145
+ try:
146
+ git(['read-tree', baseline], repo, env=env)
147
+ try:
148
+ git(['add', '-A', '--', '.', *EXCLUDES], repo, env=env)
149
+ except Exception:
150
+ # An empty nested repository (forge init, cargo new) cannot be indexed; absorb it and retry once.
151
+ import nested_repos
152
+ if not nested_repos.absorb(repo):
153
+ raise
154
+ git(['add', '-A', '--', '.', *EXCLUDES], repo, env=env)
155
+ tree = out(['write-tree'], repo, env=env)
156
+ finally:
157
+ if os.path.exists(index):
158
+ os.unlink(index)
159
+ return out(['commit-tree', tree, '-p', baseline, '-m', 'Workspace snapshot'], repo, env=env)
160
+
161
+
162
+ def changed(repo, a, b):
163
+ raw = out(['diff', '--name-status', '--no-renames', '-z', a, b], repo)
164
+ parts = [p for p in raw.split('\0') if p]
165
+ return [{'status': parts[i], 'path': parts[i + 1]} for i in range(0, len(parts) - 1, 2)]
166
+
167
+
168
+ def replay(repo, baseline, work, onto):
169
+ """Apply the change baseline->work on top of commit `onto`. Returns (tree, conflicts)."""
170
+ patch = git(['diff', '--binary', '--full-index', '--no-renames', baseline, work], repo).stdout
171
+ if not patch.strip():
172
+ return out(['rev-parse', onto + '^{tree}'], repo), []
173
+ scratch = tempfile.mkdtemp(prefix='mini-sync-')
174
+ try:
175
+ git(['worktree', 'add', '--detach', '--force', scratch, onto], repo)
176
+ r = git(['apply', '--3way', '--binary', '--whitespace=nowarn'], scratch, check=False, input=patch)
177
+ if r.returncode != 0:
178
+ conflicts = [p for p in out(['diff', '--name-only', '--diff-filter=U'], scratch).splitlines() if p]
179
+ if not conflicts:
180
+ conflicts = sorted(set(re.findall(r'(?:patch failed|error): ([^:\n]+):', r.stderr.decode(errors='replace'))))
181
+ return None, conflicts or ['(unknown file)']
182
+ return out(['write-tree'], scratch), []
183
+ finally:
184
+ git(['worktree', 'remove', '--force', scratch], repo, check=False)
185
+ shutil.rmtree(scratch, ignore_errors=True)
186
+ git(['worktree', 'prune'], repo, check=False)
187
+
188
+
189
+ def secrets_added(repo, baseline, work):
190
+ return [c['path'] for c in changed(repo, baseline, work) if c['status'] == 'A' and SECRET_FILE.search(c['path'])]
191
+
192
+
193
+ def identity_env(a):
194
+ name = (a.get('authorName') or '').strip()[:100] or 'Mini Code'
195
+ email = (a.get('authorEmail') or '').strip()[:200] or 'workspace@mini-code.local'
196
+ return git_env({'GIT_AUTHOR_NAME': name, 'GIT_AUTHOR_EMAIL': email, 'GIT_COMMITTER_NAME': name, 'GIT_COMMITTER_EMAIL': email})
197
+
198
+
199
+ # ---------------------------------------------------------------- actions
200
+
201
+ def act_clone(a):
202
+ dest = Path(a['dest'])
203
+ url = a.get('remote') or remote_url(a.get('repository'))
204
+ args = ['clone', '--depth=50', '--no-tags']
205
+ if a.get('branch'):
206
+ args += ['--branch', valid_branch(a['branch'])]
207
+ if dest.exists() and any(dest.iterdir()):
208
+ raise SyncError('Workspace folder is not empty')
209
+ git(args + [url, str(dest)], str(dest.parent), timeout=600)
210
+ branch = out(['branch', '--show-current'], str(dest))
211
+ sha = out(['rev-parse', 'HEAD'], str(dest))
212
+ return {'branch': branch, 'sha': sha}
213
+
214
+
215
+ def act_status(repo, a):
216
+ ensure_remote(repo, a)
217
+ base, branch = a['baseline'], a['branch']
218
+ remote = fetch(repo, branch)
219
+ work = snapshot(repo, base)
220
+ files = changed(repo, base, work)
221
+ behind = None
222
+ if remote != a.get('remoteSha'):
223
+ r = git(['rev-list', '--count', a.get('remoteSha', '') + '..' + remote], repo, check=False) if a.get('remoteSha') and has_object(repo, a['remoteSha']) else None
224
+ behind = int(r.stdout.decode().strip()) if r is not None and r.returncode == 0 else -1
225
+ else:
226
+ behind = 0
227
+ return {'remoteHead': remote, 'behind': behind, 'changes': files[:500], 'changeCount': len(files)}
228
+
229
+
230
+ def act_pull(repo, a):
231
+ ensure_remote(repo, a)
232
+ base, branch = a['baseline'], a['branch']
233
+ remote = fetch(repo, branch)
234
+ if remote == a.get('remoteSha') and remote == base:
235
+ return {'updated': False, 'remoteSha': remote, 'baseline': remote, 'conflicts': []}
236
+ work = snapshot(repo, base)
237
+ tree, conflicts = replay(repo, base, work, remote)
238
+ if conflicts:
239
+ return {'updated': False, 'conflicts': conflicts, 'remoteSha': a.get('remoteSha'), 'baseline': base}
240
+ merged = out(['commit-tree', tree, '-p', remote, '-m', 'Workspace after sync'], repo, env=identity_env(a))
241
+ patch = git(['diff', '--binary', '--full-index', '--no-renames', work, merged], repo).stdout
242
+ if patch.strip():
243
+ r = git(['apply', '--binary', '--whitespace=nowarn'], repo, check=False, input=patch)
244
+ if r.returncode != 0:
245
+ raise SyncError(friendly(r.stderr.decode(errors='replace'), 'Could not update the workspace files'))
246
+ # HEAD becomes the GitHub commit; local work stays as uncommitted changes on top of it.
247
+ git(['reset', '--mixed', '-q', remote], repo)
248
+ pulled = changed(repo, base if has_object(repo, base) else remote, remote) if base != remote else []
249
+ return {'updated': True, 'remoteSha': remote, 'baseline': remote, 'conflicts': [], 'pulledFiles': len(pulled)}
250
+
251
+
252
+ def act_push(repo, a):
253
+ """Push local work as one commit. mode=direct pushes to the base branch; mode=branch to a working branch."""
254
+ ensure_remote(repo, a)
255
+ base, branch, mode = a['baseline'], a['branch'], a.get('mode')
256
+ message = (a.get('message') or '').strip()
257
+ if not message or len(message) > 2000:
258
+ raise SyncError('Enter a commit message')
259
+ remote = fetch(repo, branch)
260
+ work = snapshot(repo, base)
261
+ leaked = secrets_added(repo, base, work)
262
+ if leaked and not a.get('allowSecrets'):
263
+ raise SyncError('Refusing to push files that usually hold secrets: ' + ', '.join(leaked[:5]) + '. Remove them or add them to .gitignore.')
264
+ env = identity_env(a)
265
+ if mode == 'direct':
266
+ if remote != a.get('remoteSha'):
267
+ raise SyncError('GitHub has newer commits on ' + branch + '. Sync first, then push.')
268
+ tree, conflicts = replay(repo, base, work, remote)
269
+ if conflicts:
270
+ raise SyncError('Changes do not apply cleanly to ' + branch + ': ' + ', '.join(conflicts[:5]))
271
+ if tree == out(['rev-parse', remote + '^{tree}'], repo):
272
+ raise SyncError('No changes to push')
273
+ commit = out(['commit-tree', tree, '-p', remote, '-m', message], repo, env=env)
274
+ git(['push', '--porcelain', origin_url(a), commit + ':refs/heads/' + branch], repo, timeout=600)
275
+ git(['update-ref', 'refs/remotes/origin/' + branch, commit], repo)
276
+ git(['reset', '--mixed', '-q', commit], repo)
277
+ return {'sha': commit, 'branch': branch, 'remoteSha': commit, 'baseline': commit}
278
+ if mode == 'branch':
279
+ target = valid_branch(a.get('target'))
280
+ if target == branch:
281
+ raise SyncError('Use direct push for the base branch')
282
+ onto = a.get('remoteSha') or remote
283
+ ensure_commit(repo, onto)
284
+ tree, conflicts = replay(repo, base, work, onto)
285
+ if conflicts:
286
+ raise SyncError('Changes do not apply cleanly: ' + ', '.join(conflicts[:5]) + '. Sync first.')
287
+ parent, force = onto, True
288
+ previous = a.get('previousHead')
289
+ if previous and a.get('previousBase') == onto:
290
+ # Same base as the last publish: stack a new commit so the PR keeps its history.
291
+ r = git(['fetch', '--no-tags', '--depth=1', 'origin', '+refs/heads/' + target + ':refs/remotes/origin/' + target], repo, check=False)
292
+ live = out(['rev-parse', 'refs/remotes/origin/' + target], repo) if r.returncode == 0 else ''
293
+ if live == previous:
294
+ parent, force = previous, False
295
+ if tree == out(['rev-parse', parent + '^{tree}'], repo):
296
+ if parent != onto:
297
+ return {'sha': parent, 'branch': target, 'unchanged': True}
298
+ raise SyncError('No changes to publish')
299
+ commit = out(['commit-tree', tree, '-p', parent, '-m', message], repo, env=env)
300
+ refspec = ('+' if force else '') + commit + ':refs/heads/' + target
301
+ git(['push', '--porcelain', origin_url(a), refspec], repo, timeout=600)
302
+ return {'sha': commit, 'branch': target, 'base': onto, 'files': len(changed(repo, onto, commit))}
303
+ raise SyncError('Unknown push mode')
304
+
305
+
306
+ def main(argv):
307
+ action = argv[1] if len(argv) > 1 else ''
308
+ a = json.loads(argv[2]) if len(argv) > 2 else {}
309
+ try:
310
+ if action == 'clone':
311
+ result = act_clone(a)
312
+ else:
313
+ repo = a.get('repo') or os.getcwd()
314
+ if not (Path(repo) / '.git').exists():
315
+ raise SyncError('This workspace has no git repository')
316
+ if not a.get('baseline') or not a.get('branch'):
317
+ raise SyncError('This workspace is not linked to a GitHub branch. Import the repository again.')
318
+ result = {'status': act_status, 'pull': act_pull, 'push': act_push}[action](repo, a)
319
+ print(json.dumps({'ok': True, **result}))
320
+ except SyncError as e:
321
+ provider = os.environ.get('MINI_GIT_PROVIDER', '')
322
+ message = clean(str(e))
323
+ if provider and provider != 'GitHub':
324
+ message = message.replace('GitHub', provider).replace('a draft PR', 'a merge request')
325
+ print(json.dumps({'ok': False, 'error': message}))
326
+ except KeyError:
327
+ print(json.dumps({'ok': False, 'error': 'Unknown sync action'}))
328
+ except subprocess.TimeoutExpired:
329
+ print(json.dumps({'ok': False, 'error': 'GitHub took too long to respond. Try again.'}))
330
+
331
+
332
+ if __name__ == '__main__':
333
+ main(sys.argv)