redberry-cli 0.1.0__tar.gz → 0.2.0__tar.gz

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 (31) hide show
  1. {redberry_cli-0.1.0/src/redberry_cli.egg-info → redberry_cli-0.2.0}/PKG-INFO +16 -3
  2. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/README.md +15 -2
  3. redberry_cli-0.2.0/src/redberry_cli/_version.py +1 -0
  4. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/main.py +150 -12
  5. {redberry_cli-0.1.0 → redberry_cli-0.2.0/src/redberry_cli.egg-info}/PKG-INFO +16 -3
  6. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli.egg-info/SOURCES.txt +1 -0
  7. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/tests/test_cli.py +3 -5
  8. redberry_cli-0.2.0/tests/test_cli_extract.py +253 -0
  9. redberry_cli-0.1.0/src/redberry_cli/_version.py +0 -1
  10. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/LICENSE +0 -0
  11. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/NOTICE +0 -0
  12. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/pyproject.toml +0 -0
  13. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/setup.cfg +0 -0
  14. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/__init__.py +0 -0
  15. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/_channel.py +0 -0
  16. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/_cloud.py +0 -0
  17. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/_edits.py +0 -0
  18. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/_scan.py +0 -0
  19. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/_ts_tools.py +0 -0
  20. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/aws_lambda.py +0 -0
  21. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/azure_functions.py +0 -0
  22. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/instrument.py +0 -0
  23. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli/vercel.py +0 -0
  24. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli.egg-info/dependency_links.txt +0 -0
  25. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli.egg-info/entry_points.txt +0 -0
  26. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli.egg-info/requires.txt +0 -0
  27. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/src/redberry_cli.egg-info/top_level.txt +0 -0
  28. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/tests/test_cli_aws.py +0 -0
  29. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/tests/test_cli_azure.py +0 -0
  30. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/tests/test_cli_vercel.py +0 -0
  31. {redberry_cli-0.1.0 → redberry_cli-0.2.0}/tests/test_scan.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: redberry-cli
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: The redberry command: sign in to Redberry and wire an agent's code to send its spans to your Redberry install.
5
5
  Author: Redberry Labs
6
6
  License-Expression: LicenseRef-PolyForm-Shield-1.0.0
@@ -110,8 +110,21 @@ to the form above when you run `connect` again.
110
110
  An app connected by an earlier version with the in-process hasher gets the new `instrumentation.ts`; remove the
111
111
  old variables it set (`REDBERRY_SIGNING_KEY`, `REDBERRY_PROJECT_TOKEN` and the others) from the project.
112
112
 
113
- `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` when that tool is
114
- installed. It is not part of this release; without it, write the manifest by hand.
113
+ ## `connect --extract`: the agent's manifest
114
+
115
+ `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` and the LLM key already
116
+ in your environment (`ANTHROPIC_API_KEY`, else `OPENAI_API_KEY`). The agent's code is sent only to that key's
117
+ provider. Without `--extract`, `connect` asks first.
118
+
119
+ A `redberry-extract` on PATH is used as it is. Otherwise `connect` installs it:
120
+
121
+ - It needs `redberry login` (the extractor is licensed to your company) and
122
+ [uv](https://docs.astral.sh/uv/) (`curl -LsSf https://astral.sh/uv/install.sh | sh` or `pipx install uv`).
123
+ - The Redberry Extractor Licence is shown and accepted once per company, and again when a new version is
124
+ published. `--yes` does not accept it; in a script or with `--json`, pass `--accept-extractor-licence`.
125
+ - It is installed with uv into its own environment in `~/.redberry/extractor/<version>/` (`$REDBERRY_HOME`
126
+ overrides `~/.redberry`), from Redberry's package index with a short-lived download token that is passed to uv
127
+ in its environment only. Later runs reuse that install.
115
128
 
116
129
  ## License
117
130
 
@@ -83,8 +83,21 @@ to the form above when you run `connect` again.
83
83
  An app connected by an earlier version with the in-process hasher gets the new `instrumentation.ts`; remove the
84
84
  old variables it set (`REDBERRY_SIGNING_KEY`, `REDBERRY_PROJECT_TOKEN` and the others) from the project.
85
85
 
86
- `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` when that tool is
87
- installed. It is not part of this release; without it, write the manifest by hand.
86
+ ## `connect --extract`: the agent's manifest
87
+
88
+ `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` and the LLM key already
89
+ in your environment (`ANTHROPIC_API_KEY`, else `OPENAI_API_KEY`). The agent's code is sent only to that key's
90
+ provider. Without `--extract`, `connect` asks first.
91
+
92
+ A `redberry-extract` on PATH is used as it is. Otherwise `connect` installs it:
93
+
94
+ - It needs `redberry login` (the extractor is licensed to your company) and
95
+ [uv](https://docs.astral.sh/uv/) (`curl -LsSf https://astral.sh/uv/install.sh | sh` or `pipx install uv`).
96
+ - The Redberry Extractor Licence is shown and accepted once per company, and again when a new version is
97
+ published. `--yes` does not accept it; in a script or with `--json`, pass `--accept-extractor-licence`.
98
+ - It is installed with uv into its own environment in `~/.redberry/extractor/<version>/` (`$REDBERRY_HOME`
99
+ overrides `~/.redberry`), from Redberry's package index with a short-lived download token that is passed to uv
100
+ in its environment only. Later runs reuse that install.
88
101
 
89
102
  ## License
90
103
 
@@ -0,0 +1 @@
1
+ __version__ = "0.2.0"
@@ -22,8 +22,8 @@ serverless platforms:
22
22
 
23
23
  Each shows the diff and asks before it changes a file (``--yes`` applies without asking), sets
24
24
  ``temperature`` 1 on each model call it finds (pricing needs exactly 1), and never deploys.
25
- ``--extract`` writes the agent's manifest (``.redberry/manifest.json``) with ``redberry-extract`` when that
26
- is installed.
25
+ ``--extract`` writes the agent's manifest (``.redberry/manifest.json``) with ``redberry-extract``: the one on
26
+ PATH, else one installed with uv into ``~/.redberry/extractor`` once the company's extractor licence is accepted.
27
27
 
28
28
  The API is ``$REDBERRY_API_URL`` or ``--api`` (default ``https://api.redberry.sbs``). Every prompt has a
29
29
  flag (``--name``, ``--yes``), and ``--json`` prints the result for scripts and coding assistants.
@@ -241,14 +241,10 @@ _EXTRACT_BACKENDS = (("ANTHROPIC_API_KEY", "Anthropic", []),
241
241
 
242
242
 
243
243
  def _extract_manifest(a, repo: Path, manifest: Path, name: str, out, run) -> None:
244
- """No manifest: write one with redberry-extract and the LLM key already in the environment, when
245
- redberry-extract is installed. It shows the agent's code to that provider, so this asks unless --extract
246
- said yes."""
244
+ """No manifest: write one with redberry-extract and the LLM key already in the environment. It shows the
245
+ agent's code to that provider, so this asks unless --extract said yes. ``redberry-extract`` on PATH is used
246
+ as it is; otherwise it is installed for this machine (``_install_extractor``)."""
247
247
  _require_agent_code(repo)
248
- if not shutil.which("redberry-extract"):
249
- raise CliError(f"no manifest at {manifest}, and the extractor (redberry-extract) is not available in this "
250
- "release. Write the manifest by hand; the tools and model calls connect's own scan finds "
251
- "(redberry_cli._scan) are the place to start.")
252
248
  found = next(((env, who, extra) for env, who, extra in _EXTRACT_BACKENDS if os.environ.get(env)), None)
253
249
  how = f"redberry-extract --repo {repo} --agent-id {name} (needs ANTHROPIC_API_KEY or OPENAI_API_KEY)"
254
250
  if found is None:
@@ -261,12 +257,152 @@ def _extract_manifest(a, repo: Path, manifest: Path, name: str, out, run) -> Non
261
257
  answer = input(f"No manifest yet. Describe this agent with your {env}? Its code is sent to {who}. [y/N] ")
262
258
  if answer.strip().lower() not in ("y", "yes"):
263
259
  raise CliError(f"no manifest. Write it yourself: {how}")
260
+ extractor = "redberry-extract" if shutil.which("redberry-extract") else str(_install_extractor(a, out, run))
264
261
  out(f"Describing the agent with {who} (redberry-extract) ...")
265
- cmd = ["redberry-extract", "--repo", str(repo), "--agent-id", name, "--output", str(manifest), *extra]
262
+ cmd = [extractor, "--repo", str(repo), "--agent-id", name, "--output", str(manifest), *extra]
266
263
  if run(cmd, cwd=repo).returncode != 0 or not manifest.exists():
267
264
  raise CliError(f"redberry-extract did not write {manifest}; run it yourself: {how}")
268
265
 
269
266
 
267
+ # ── the extractor: licence, download token, install with uv ─────────────────────────────────
268
+
269
+ #: The private index's name for uv: its credential is read from ``UV_INDEX_<NAME>_USERNAME`` / ``_PASSWORD``.
270
+ EXTRACTOR_INDEX = "redberry-extractor"
271
+ _INDEX_ENV = "UV_INDEX_" + EXTRACTOR_INDEX.upper().replace("-", "_")
272
+ _INSTALLED = ".redberry-installed"
273
+
274
+
275
+ def _redberry_home() -> Path:
276
+ return Path(os.environ.get("REDBERRY_HOME") or Path.home() / ".redberry")
277
+
278
+
279
+ def _extractor_root() -> Path:
280
+ return _redberry_home() / "extractor"
281
+
282
+
283
+ def _extractor_exe(venv: Path) -> Path:
284
+ if os.name == "nt":
285
+ return venv / "Scripts" / "redberry-extract.exe"
286
+ return venv / "bin" / "redberry-extract"
287
+
288
+
289
+ def _installed_extractor() -> Path | None:
290
+ """The install recorded as current, when it is complete."""
291
+ root = _extractor_root()
292
+ with contextlib.suppress(OSError):
293
+ venv = root / (root / "current").read_text(encoding="utf-8").strip()
294
+ if venv.parent == root and (venv / _INSTALLED).exists() and _extractor_exe(venv).exists():
295
+ return venv
296
+ return None
297
+
298
+
299
+ def _install_extractor(a, out, run) -> Path:
300
+ """redberry-extract for this machine: after the company's extractor licence is accepted, installed with uv
301
+ from Redberry's private index into ``~/.redberry/extractor/<version>/`` (``$REDBERRY_HOME``), or the
302
+ install already there. The download token is short-lived and reaches uv only in its environment."""
303
+ try:
304
+ s = session(a.api)
305
+ except CliError:
306
+ raise CliError("the extractor is installed for your company: sign in first with `redberry login`, "
307
+ "then run connect again") from None
308
+ _accept_licence(a, s, out)
309
+ venv = _installed_extractor()
310
+ if venv is not None:
311
+ return _extractor_exe(venv)
312
+ uv = shutil.which("uv")
313
+ if not uv:
314
+ raise CliError("installing the extractor needs uv: `curl -LsSf https://astral.sh/uv/install.sh | sh` "
315
+ "or `pipx install uv`, then run connect again")
316
+ status, grant = call(a.api, "POST", "/api/cli/extractor-token", token=s["token"])
317
+ if status != 200:
318
+ raise CliError(_token_error(status, grant))
319
+ secret = grant["access_token"]
320
+ pin = str(grant.get("version_pin") or "").strip()
321
+ spec = "" if not pin else pin if pin[0] in "=<>!~" else f"=={pin}"
322
+ version = "".join(c for c in pin.lstrip("=<>!~ ") if c.isalnum() or c in ".-_+") or "latest"
323
+ root = _extractor_root()
324
+ venv = root / version
325
+ shutil.rmtree(venv, ignore_errors=True) # an earlier install that did not finish
326
+ root.mkdir(parents=True, exist_ok=True)
327
+ out(f"Installing the Redberry extractor ({version}) into {venv} ...")
328
+ python = f"{sys.version_info.major}.{sys.version_info.minor}"
329
+ plain = {k: v for k, v in os.environ.items() if not k.startswith(_INDEX_ENV)}
330
+ # The token goes to uv in its environment only: never in argv (visible to other users in ps), a file or a log.
331
+ authed = {**plain, f"{_INDEX_ENV}_USERNAME": grant.get("username") or "oauth2accesstoken",
332
+ f"{_INDEX_ENV}_PASSWORD": secret}
333
+ private = f"{EXTRACTOR_INDEX}={grant['index_url']}"
334
+ steps = [
335
+ ([uv, "venv", str(venv), "--python", python], plain),
336
+ # the extractor itself from the private index alone, so a package of the same name elsewhere never wins
337
+ ([uv, "pip", "install", "--python", str(venv), "--no-deps", "--default-index", private,
338
+ f"redberry-extractor{spec}"], authed),
339
+ # then its dependencies: the private index first, PyPI as the default
340
+ ([uv, "pip", "install", "--python", str(venv), "--index-strategy", "first-index", "--index", private,
341
+ f"redberry-extractor[llm]{spec}"], authed),
342
+ ]
343
+ for cmd, env in steps:
344
+ try:
345
+ r = run(cmd, capture_output=True, text=True, env=env)
346
+ except OSError as e:
347
+ shutil.rmtree(venv, ignore_errors=True)
348
+ raise CliError(f"could not run uv: {str(e).replace(secret, '***')}") from None
349
+ if r.returncode != 0:
350
+ shutil.rmtree(venv, ignore_errors=True)
351
+ detail = ((r.stderr or "") + (r.stdout or "")).replace(secret, "***").strip()[-2000:]
352
+ raise CliError(f"installing the extractor failed (`{' '.join(cmd[1:3])}`): {detail}")
353
+ if not _extractor_exe(venv).exists():
354
+ shutil.rmtree(venv, ignore_errors=True)
355
+ raise CliError(f"the extractor installed without its command ({_extractor_exe(venv).name})")
356
+ (venv / _INSTALLED).write_text(version + "\n", encoding="utf-8")
357
+ (root / "current").write_text(version + "\n", encoding="utf-8")
358
+ return _extractor_exe(venv)
359
+
360
+
361
+ def _accept_licence(a, s: dict[str, Any], out) -> None:
362
+ """The Redberry Extractor Licence, accepted once per company (and again for a new version). ``--yes`` does
363
+ not accept it: without a terminal, ``--accept-extractor-licence`` does."""
364
+ status, lic = call(a.api, "GET", "/api/cli/extractor-licence", token=s["token"])
365
+ if status == 401:
366
+ raise CliError("your Redberry session has ended: run `redberry login`")
367
+ if status != 200:
368
+ raise CliError(f"could not read the extractor licence ({status}): {_detail(lic)}")
369
+ if lic.get("accepted"):
370
+ return
371
+ company = s.get("company") or {}
372
+ company = company.get("name") or company.get("id") or "your company"
373
+ version = lic.get("version")
374
+ if not getattr(a, "accept_extractor_licence", False):
375
+ if not sys.stdin.isatty() or a.json:
376
+ raise CliError(f"the Redberry Extractor Licence (version {version}) is not yet accepted for {company}. "
377
+ "Read it in the Redberry portal or run connect in a terminal, and pass "
378
+ "--accept-extractor-licence to accept it without a prompt (--yes does not)")
379
+ out(lic.get("text") or "")
380
+ answer = input(f"Accept the Redberry Extractor Licence for {company} (version {version})? [y/N] ")
381
+ if answer.strip().lower() not in ("y", "yes"):
382
+ raise CliError("the extractor licence was not accepted; write the manifest by hand instead")
383
+ status, body = call(a.api, "POST", "/api/cli/extractor-licence", {"version": version}, token=s["token"])
384
+ if status == 409:
385
+ raise CliError("the extractor licence changed while you read it: run connect again to see the new version")
386
+ if status != 200:
387
+ raise CliError(f"could not accept the extractor licence ({status}): {_detail(body)}")
388
+ out(f"Accepted the Redberry Extractor Licence (version {version}) for {company}")
389
+
390
+
391
+ def _token_error(status: int, body: dict[str, Any]) -> str:
392
+ detail = body.get("detail")
393
+ message = detail.get("message") if isinstance(detail, dict) else None
394
+ if status == 401:
395
+ return "your Redberry session has ended: run `redberry login`"
396
+ if status == 403:
397
+ return ("the extractor licence is not accepted for your company"
398
+ + (f" ({message})" if message else "") + ": run connect again to accept it")
399
+ if status == 429:
400
+ return "too many extractor downloads for now: try again in a few minutes"
401
+ if status == 502:
402
+ return "Redberry could not issue an extractor download token right now: try again shortly"
403
+ return f"could not get an extractor download token ({status}): {message or _detail(body)}"
404
+
405
+
270
406
  def _require_agent_code(repo: Path) -> None:
271
407
  """Before anything is described: ``repo`` holds an agent. Run from a home folder by mistake, the
272
408
  extractor described it (0 tools, 0 models)."""
@@ -301,8 +437,10 @@ def parser() -> argparse.ArgumentParser:
301
437
  con.add_argument("--manifest", default=".redberry/manifest.json", help="relative to --repo, or absolute")
302
438
  con.add_argument("--yes", action="store_true", help="apply the changes without asking")
303
439
  con.add_argument("--extract", action="store_true",
304
- help="with no manifest, write one with redberry-extract (when installed) and your LLM key, "
305
- "without asking")
440
+ help="with no manifest, write one with redberry-extract and your LLM key, without asking "
441
+ "(installs the extractor with uv when it is not on PATH)")
442
+ con.add_argument("--accept-extractor-licence", action="store_true",
443
+ help="accept the Redberry Extractor Licence for your company without a prompt")
306
444
  con.add_argument("--json", action="store_true", help="print the result as JSON")
307
445
  az = con.add_argument_group("Azure Functions (--platform azure-functions: a Node.js v4 or Python v2 function app)")
308
446
  az.add_argument("--app", help="the function app's name")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: redberry-cli
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: The redberry command: sign in to Redberry and wire an agent's code to send its spans to your Redberry install.
5
5
  Author: Redberry Labs
6
6
  License-Expression: LicenseRef-PolyForm-Shield-1.0.0
@@ -110,8 +110,21 @@ to the form above when you run `connect` again.
110
110
  An app connected by an earlier version with the in-process hasher gets the new `instrumentation.ts`; remove the
111
111
  old variables it set (`REDBERRY_SIGNING_KEY`, `REDBERRY_PROJECT_TOKEN` and the others) from the project.
112
112
 
113
- `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` when that tool is
114
- installed. It is not part of this release; without it, write the manifest by hand.
113
+ ## `connect --extract`: the agent's manifest
114
+
115
+ `--extract` writes the agent's manifest (`.redberry/manifest.json`) with `redberry-extract` and the LLM key already
116
+ in your environment (`ANTHROPIC_API_KEY`, else `OPENAI_API_KEY`). The agent's code is sent only to that key's
117
+ provider. Without `--extract`, `connect` asks first.
118
+
119
+ A `redberry-extract` on PATH is used as it is. Otherwise `connect` installs it:
120
+
121
+ - It needs `redberry login` (the extractor is licensed to your company) and
122
+ [uv](https://docs.astral.sh/uv/) (`curl -LsSf https://astral.sh/uv/install.sh | sh` or `pipx install uv`).
123
+ - The Redberry Extractor Licence is shown and accepted once per company, and again when a new version is
124
+ published. `--yes` does not accept it; in a script or with `--json`, pass `--accept-extractor-licence`.
125
+ - It is installed with uv into its own environment in `~/.redberry/extractor/<version>/` (`$REDBERRY_HOME`
126
+ overrides `~/.redberry`), from Redberry's package index with a short-lived download token that is passed to uv
127
+ in its environment only. Later runs reuse that install.
115
128
 
116
129
  ## License
117
130
 
@@ -23,5 +23,6 @@ src/redberry_cli.egg-info/top_level.txt
23
23
  tests/test_cli.py
24
24
  tests/test_cli_aws.py
25
25
  tests/test_cli_azure.py
26
+ tests/test_cli_extract.py
26
27
  tests/test_cli_vercel.py
27
28
  tests/test_scan.py
@@ -3,7 +3,7 @@
3
3
  The fake holds the state the API's /api/cli routes do: a device code someone approves and a session.
4
4
  Checked: the device-code sign-in (reporting the machine's name) and where the session is kept (0600);
5
5
  logout revoking the session at Redberry; bare `connect` pointing at the portal's install command;
6
- `--extract` with no extractor installed; and `redberry_cli.instrument`'s edits.
6
+ `--extract` with no extractor installed and no session; and `redberry_cli.instrument`'s edits.
7
7
  """
8
8
 
9
9
  from __future__ import annotations
@@ -158,14 +158,12 @@ def test_bare_connect_needs_no_sign_in_and_refuses_platform_flags(api, capsys):
158
158
  _main(api, "connect", "--platform", "foundry")
159
159
 
160
160
 
161
- def test_extract_without_the_extractor_says_to_write_the_manifest(api, repo, monkeypatch, capsys):
161
+ def test_extract_without_the_extractor_or_a_session_says_to_log_in(api, repo, monkeypatch, capsys):
162
162
  monkeypatch.setattr(cli.shutil, "which", lambda name: None)
163
163
  monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-test")
164
- ran = []
165
164
  assert _main(api, "connect", "--repo", str(repo), "--extract") == 1
166
165
  err = capsys.readouterr().err
167
- assert "not available in this release" in err and "by hand" in err and "tools and model calls" in err
168
- assert not ran and not (repo / ".redberry").exists()
166
+ assert "redberry login" in err and not (repo / ".redberry").exists()
169
167
 
170
168
 
171
169
  def test_extract_runs_the_extractor_when_it_is_installed(api, repo, monkeypatch, capsys):
@@ -0,0 +1,253 @@
1
+ """`connect --extract` when redberry-extract is not on PATH: the extractor licence, the download token, and
2
+ the install with uv into ``$REDBERRY_HOME/extractor/<version>``, against a fake Redberry API over real HTTP.
3
+
4
+ The download token must reach uv only in its environment: never in argv, a file or an error message.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ import threading
11
+ import time
12
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
13
+ from pathlib import Path
14
+ from typing import ClassVar
15
+
16
+ import pytest
17
+
18
+ from redberry_cli import main as cli
19
+
20
+ TOKEN = "ya29.secret-download-token"
21
+ INDEX = "https://europe-python.pkg.dev/redberry/extractor/simple/"
22
+ ENV = "UV_INDEX_REDBERRY_EXTRACTOR"
23
+
24
+
25
+ class _Api(BaseHTTPRequestHandler):
26
+ state: ClassVar[dict] = {}
27
+
28
+ def log_message(self, *args): # quiet
29
+ pass
30
+
31
+ def _reply(self, status: int, body: dict) -> None:
32
+ data = json.dumps(body).encode()
33
+ self.send_response(status)
34
+ self.send_header("Content-Type", "application/json")
35
+ self.send_header("Content-Length", str(len(data)))
36
+ self.end_headers()
37
+ self.wfile.write(data)
38
+
39
+ def _authed(self) -> bool:
40
+ return self.headers.get("Authorization") == "Bearer rbcli_session"
41
+
42
+ def do_GET(self):
43
+ s = self.state
44
+ if self.path == "/api/cli/extractor-licence" and self._authed():
45
+ return self._reply(200, {"version": "2026-11", "text": "THE LICENCE TEXT", "accepted": s["accepted"]})
46
+ self._reply(401, {"detail": "Run `redberry login` first"})
47
+
48
+ def do_POST(self):
49
+ body = json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))) or b"{}")
50
+ s = self.state
51
+ if not self._authed():
52
+ return self._reply(401, {"detail": "Run `redberry login` first"})
53
+ if self.path == "/api/cli/extractor-licence":
54
+ s["accept_posts"] = s.get("accept_posts", 0) + 1
55
+ if body.get("version") != "2026-11":
56
+ return self._reply(409, {"detail": {"error": "licence_version_mismatch", "message": "old",
57
+ "version": "2026-11"}})
58
+ s["accepted"] = True
59
+ return self._reply(200, {"version": "2026-11", "accepted_at": "2026-11-17T12:00:00Z"})
60
+ if self.path == "/api/cli/extractor-token":
61
+ s["token_posts"] = s.get("token_posts", 0) + 1
62
+ if s.get("token_status"):
63
+ return self._reply(s["token_status"], {"detail": {"error": "x", "message": "nope", "version": "1"}})
64
+ if not s["accepted"]:
65
+ return self._reply(403, {"detail": {"error": "licence_not_accepted", "message": "accept it",
66
+ "version": "2026-11"}})
67
+ return self._reply(200, {"index_url": INDEX, "username": "oauth2accesstoken", "access_token": TOKEN,
68
+ "expires_at": int(time.time()) + 3600, "version_pin": "0.4.1"})
69
+ self._reply(404, {"detail": "Not Found"})
70
+
71
+
72
+ @pytest.fixture
73
+ def api(tmp_path, monkeypatch):
74
+ monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config"))
75
+ monkeypatch.setenv("REDBERRY_HOME", str(tmp_path / "rbhome"))
76
+ monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-test")
77
+ monkeypatch.delenv("OPENAI_API_KEY", raising=False)
78
+ _Api.state = {"accepted": False}
79
+ server = ThreadingHTTPServer(("127.0.0.1", 0), _Api)
80
+ threading.Thread(target=server.serve_forever, daemon=True).start()
81
+ yield f"http://127.0.0.1:{server.server_port}"
82
+ server.shutdown()
83
+
84
+
85
+ @pytest.fixture
86
+ def repo(tmp_path):
87
+ repo = tmp_path / "agent"
88
+ repo.mkdir()
89
+ (repo / "agent.py").write_text("import anthropic\nclient = anthropic.Anthropic()\n")
90
+ return repo
91
+
92
+
93
+ def _sign_in(api: str) -> None:
94
+ creds = {api: {"token": "rbcli_session", "company": {"id": "co_acme", "name": "Acme Ltd"},
95
+ "expires_at": time.time() + 3600}}
96
+ cli._save_credentials(creds)
97
+
98
+
99
+ class _Run:
100
+ """A fake subprocess.run: uv creates the venv and installs the command; the extractor writes the manifest."""
101
+
102
+ def __init__(self, fail_install: bool = False):
103
+ self.calls: list[tuple[list[str], dict | None]] = []
104
+ self.fail_install = fail_install
105
+
106
+ def __call__(self, cmd, cwd=None, env=None, **kw):
107
+ self.calls.append((list(cmd), env))
108
+ rc = 0
109
+ if cmd[0] == "/fake/uv" and cmd[1] == "venv":
110
+ Path(cmd[2]).mkdir(parents=True)
111
+ elif cmd[0] == "/fake/uv" and "redberry-extractor[llm]==0.4.1" in cmd:
112
+ if self.fail_install:
113
+ return type("R", (), {"returncode": 1, "stderr": f"401 for https://x:{TOKEN}@host", "stdout": ""})()
114
+ exe = cli._extractor_exe(Path(cmd[cmd.index("--python") + 1]))
115
+ exe.parent.mkdir(parents=True, exist_ok=True)
116
+ exe.write_text("#!/bin/sh\n")
117
+ elif "--output" in cmd:
118
+ out = Path(cmd[cmd.index("--output") + 1])
119
+ out.parent.mkdir(parents=True, exist_ok=True)
120
+ out.write_text("{}")
121
+ return type("R", (), {"returncode": rc, "stderr": "", "stdout": ""})()
122
+
123
+ @property
124
+ def uv(self):
125
+ return [(c, e) for c, e in self.calls if c[0] == "/fake/uv"]
126
+
127
+
128
+ @pytest.fixture
129
+ def run(monkeypatch):
130
+ r = _Run()
131
+ monkeypatch.setattr(cli.subprocess, "run", r)
132
+ monkeypatch.setattr(cli.shutil, "which", lambda name: "/fake/uv" if name == "uv" else None)
133
+ return r
134
+
135
+
136
+ def _tty(monkeypatch, answers: list[str]):
137
+ asked = []
138
+ monkeypatch.setattr(cli.sys, "stdin", type("T", (), {"isatty": lambda self: True})())
139
+
140
+ def answer(prompt):
141
+ asked.append(prompt)
142
+ return answers.pop(0)
143
+
144
+ monkeypatch.setattr("builtins.input", answer)
145
+ return asked
146
+
147
+
148
+ def _connect(api, repo, *flags):
149
+ return cli.main(["--api", api, "connect", "--repo", str(repo), "--name", "refunds", "--extract", *flags])
150
+
151
+
152
+ def test_interactive_accept_installs_with_the_token_only_in_the_environment(api, repo, run, monkeypatch, capsys,
153
+ tmp_path):
154
+ _sign_in(api)
155
+ asked = _tty(monkeypatch, ["y"])
156
+ assert _connect(api, repo) == 0
157
+ out = capsys.readouterr().out
158
+ assert "THE LICENCE TEXT" in out and "Accepted the Redberry Extractor Licence" in out
159
+ assert asked == ["Accept the Redberry Extractor Licence for Acme Ltd (version 2026-11)? [y/N] "]
160
+ assert _Api.state["accepted"] and _Api.state["token_posts"] == 1
161
+ venv = tmp_path / "rbhome" / "extractor" / "0.4.1"
162
+ (venv_cmd, venv_env), (own, own_env), (deps, deps_env) = run.uv
163
+ assert venv_cmd[:3] == ["/fake/uv", "venv", str(venv)] and f"{ENV}_PASSWORD" not in venv_env
164
+ assert own[-1] == "redberry-extractor==0.4.1" and "--no-deps" in own
165
+ assert own[own.index("--default-index") + 1] == f"redberry-extractor={INDEX}"
166
+ assert deps[-1] == "redberry-extractor[llm]==0.4.1"
167
+ assert deps[deps.index("--index") + 1] == f"redberry-extractor={INDEX}" and "first-index" in deps
168
+ for env in (own_env, deps_env):
169
+ assert env[f"{ENV}_USERNAME"] == "oauth2accesstoken" and env[f"{ENV}_PASSWORD"] == TOKEN
170
+ assert not any(TOKEN in arg for cmd, _ in run.calls for arg in cmd) # never in argv
171
+ assert not any(TOKEN in p.read_text(errors="ignore") for p in (tmp_path / "rbhome").rglob("*") if p.is_file())
172
+ extract = run.calls[-1][0]
173
+ assert extract[0] == str(cli._extractor_exe(venv)) and extract[1:5] == ["--repo", str(repo.resolve()),
174
+ "--agent-id", "refunds"]
175
+ assert (repo / ".redberry" / "manifest.json").exists()
176
+
177
+
178
+ def test_declining_the_licence_installs_nothing(api, repo, run, monkeypatch, capsys):
179
+ _sign_in(api)
180
+ _tty(monkeypatch, ["n"])
181
+ assert _connect(api, repo) == 1
182
+ assert "not accepted" in capsys.readouterr().err
183
+ assert not _Api.state["accepted"] and not run.calls and "token_posts" not in _Api.state
184
+
185
+
186
+ def test_non_interactive_needs_the_licence_flag_and_yes_is_not_enough(api, repo, run, capsys):
187
+ _sign_in(api)
188
+ assert _connect(api, repo, "--yes") == 1
189
+ err = capsys.readouterr().err
190
+ assert "--accept-extractor-licence" in err and "--yes does not" in err
191
+ assert not run.calls and "accept_posts" not in _Api.state
192
+ assert _connect(api, repo, "--json", "--accept-extractor-licence") == 0
193
+ assert _Api.state["accepted"] and _Api.state["accept_posts"] == 1 and len(run.uv) == 3
194
+
195
+
196
+ def test_an_accepted_licence_is_not_asked_again(api, repo, run, capsys):
197
+ _sign_in(api)
198
+ _Api.state["accepted"] = True
199
+ assert _connect(api, repo) == 0
200
+ assert "accept_posts" not in _Api.state and "THE LICENCE TEXT" not in capsys.readouterr().out
201
+
202
+
203
+ def test_an_existing_install_is_reused_without_a_new_token(api, repo, run, capsys, tmp_path):
204
+ _sign_in(api)
205
+ assert _connect(api, repo, "--accept-extractor-licence") == 0
206
+ (repo / ".redberry" / "manifest.json").unlink()
207
+ run.calls.clear()
208
+ assert _connect(api, repo) == 0
209
+ assert _Api.state["token_posts"] == 1 and not run.uv
210
+ assert run.calls[0][0][0] == str(cli._extractor_exe(tmp_path / "rbhome" / "extractor" / "0.4.1"))
211
+
212
+
213
+ def test_not_signed_in_says_to_log_in(api, repo, run, capsys):
214
+ assert _connect(api, repo, "--accept-extractor-licence") == 1
215
+ assert "redberry login" in capsys.readouterr().err and not run.calls
216
+
217
+
218
+ def test_uv_missing_says_how_to_install_it(api, repo, run, monkeypatch, capsys):
219
+ _sign_in(api)
220
+ monkeypatch.setattr(cli.shutil, "which", lambda name: None)
221
+ assert _connect(api, repo, "--accept-extractor-licence") == 1
222
+ err = capsys.readouterr().err
223
+ assert "astral.sh/uv/install.sh" in err and "pipx install uv" in err
224
+ assert not run.calls and "token_posts" not in _Api.state
225
+
226
+
227
+ @pytest.mark.parametrize("status, says", [(403, "licence is not accepted"), (429, "too many"),
228
+ (502, "could not issue")])
229
+ def test_token_errors_are_explained(api, repo, run, capsys, status, says):
230
+ _sign_in(api)
231
+ _Api.state["token_status"] = status
232
+ assert _connect(api, repo, "--accept-extractor-licence") == 1
233
+ assert says in capsys.readouterr().err and not run.uv
234
+
235
+
236
+ def test_a_failed_install_never_echoes_the_token(api, repo, monkeypatch, capsys, tmp_path):
237
+ r = _Run(fail_install=True)
238
+ monkeypatch.setattr(cli.subprocess, "run", r)
239
+ monkeypatch.setattr(cli.shutil, "which", lambda name: "/fake/uv" if name == "uv" else None)
240
+ _sign_in(api)
241
+ assert _connect(api, repo, "--accept-extractor-licence") == 1
242
+ err = capsys.readouterr().err
243
+ assert "installing the extractor failed" in err and TOKEN not in err and "***" in err
244
+ assert not (tmp_path / "rbhome" / "extractor" / "0.4.1").exists()
245
+ assert cli._installed_extractor() is None
246
+
247
+
248
+ def test_the_extractor_on_path_is_preferred(api, repo, monkeypatch, capsys):
249
+ r = _Run()
250
+ monkeypatch.setattr(cli.subprocess, "run", r)
251
+ monkeypatch.setattr(cli.shutil, "which", lambda name: "/usr/local/bin/" + name)
252
+ assert _connect(api, repo) == 0 # no session, licence or token needed
253
+ assert [c[0][0] for c in r.calls] == ["redberry-extract"] and "accept_posts" not in _Api.state
@@ -1 +0,0 @@
1
- __version__ = "0.1.0"
File without changes
File without changes
File without changes