8000 feat(scaling): find units Lens lost track of, and let them go on a person's yes by adeelahmad · Pull Request #666 · adeelahmad/lense · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p
[the meter](docs/budgets.md#the-meter-and-the-costs-page); [checks](docs/requirements.md#see-what-it-costs))
- **Scale out.** Scaling groups add workers when work waits and let them go when it doesn't, by target, step and
schedule rules, within min and max and a monthly budget, with every launch, join and hour of cost in the activity
ledger. You add providers and make groups in Settings › Auto scaling, and a group launches nothing until you approve
ledger; a unit a provider runs with Lens's tag that Lens lost track of shows on the provider, for you to let go. You add providers and make groups in Settings › Auto scaling, and a group launches nothing until you approve
its terms there; agents propose and manage groups over the API, MCP and the assistant, and never approve one. It
runs more workers on this machine today; AWS EC2 (spot or on demand), DigitalOcean, Hetzner Cloud, Lambda GPU
instances, Proxmox VE and Linux hosts over SSH providers are in progress (built, with machines that join over the mesh to transcribe and tell
Expand Down
5 changes: 5 additions & 0 deletions changelog.d/scaling-strays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- **Auto scaling finds units Lens lost track of.** Every ten minutes each provider is asked for the units it runs with
Lens's tag; one no unit of Lens's stands for (a launch whose answer never came back, say) shows on the provider in
Settings › Auto scaling and the API, and goes in the activity ledger once. Lens lets none of them go itself: you do,
with Let go (and Undo for a few seconds), and the assistant can only ask you to
([Units Lens lost track of](docs/scaling.md#units-lens-lost-track-of)).
1 change: 1 addition & 0 deletions docs/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ request, to another check that proves the same thing; loosening a requirement or
| Each unit's cost goes into the activity ledger as an estimate, and a group stops at its budget's cap | [test_scaling.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling.py) `test_the_scaler_launches_joins_drains_and_terminates`, `test_at_its_budgets_cap_a_group_stops` |
| Settings › Auto scaling shows each group's terms in words and approves them in one click | [settings-scaling.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/settings-scaling.test.tsx) `shows each group's terms and approves them in one click`, `puts the terms and the state in words` |
| Settings › Auto scaling adds a provider from its driver's fields (credentials sent apart, never shown), shows its check in words, removes it with Undo, and makes a group, with a budget when the provider charges | [settings-scaling.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/settings-scaling.test.tsx) `adds a provider from its driver's fields, with the secret sent apart`, `shows a failed check in words, after saving and in the list`, `makes a group with a budget on a priced provider`, `asks no budget on a free provider and ticks a list's options`, `removes a provider with Undo instead of a question` |
| Units a provider runs with Lens's tag that Lens has no unit for show on the provider and in the activity ledger once; Lens lets none go itself, a person signed in to the web app does (with Undo), and the assistant only asks | [test_scaling.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling.py) `test_units_lens_lost_track_of_show_on_their_provider`, `test_a_person_lets_a_stray_go_and_an_agent_cant`; [test_scaling_agents.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling_agents.py) `test_the_assistant_asks_to_let_a_stray_go_and_the_person_says_yes`; [settings-scaling.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/settings-scaling.test.tsx) `shows the units a provider runs that Lens lost track of, and why it couldn't ask`, `lets a stray go with Undo instead of a question` |
| This machine runs more workers at no cost | [test_scaling.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling.py) `test_this_machine_runs_more_workers_at_no_cost` |
| DigitalOcean and Hetzner Cloud: credentials checked, list prices, machines launched with a one-use join token, watched, found by tag and deleted; a group waits for the mesh's install line before launching | [test_scaling_clouds.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling_clouds.py) `test_a_digitalocean_group_waits_for_the_install_line_then_launches_droplets`, `test_hetzner_prices_in_euros_and_its_errors_read_as_hetzners`, `test_the_cloud_drivers_are_listed_and_need_the_mesh` |
| Lambda: GPU instances launched with the account's SSH key and a one-use join token in their user-data, tagged with Lens's tag and the unit, priced from Lambda's list, watched (an instance Lambda takes back counts as gone), found by tag across pages and terminated; every request and answer held to Lambda's published API document | [test_scaling_clouds.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling_clouds.py) `test_a_lambda_group_launches_gpu_instances_that_join_over_the_mesh`, `test_lambda_needs_an_ssh_key_and_pages_through_instances`, `test_lens_reads_only_fields_the_clouds_answer_with` |
Expand Down
18 changes: 16 additions & 2 deletions docs/scaling.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ carries none of them.
A worker on This machine stops with the process that started it, and the scaler that takes over starts them again where
it runs.

### Units Lens lost track of

While scaling runs, the scaler asks each provider every ten minutes for the units it runs that carry Lens's tag (the
driver's `listing`, a read-only call in the activity ledger). One that no unit of Lens's stands for is a stray: a launch
that started a machine but whose answer never came back, say, or a machine someone made by hand with Lens's tag. A
stray may cost money, so it shows on its provider (`strays` in `GET /api/v1/scaling/providers`, with when Lens first
saw it, and in Settings › Auto scaling) and goes in the activity ledger once, as `scaling.stray`. Lens lets none of them
go itself, since it may not have started them: a person signed in to the web app does, with Let go in Settings (Undo
stays a few seconds) or `POST /api/v1/scaling/providers/{id}/strays/let-go` with the stray's `id`, which terminates it
at the provider. An agent asks for that (`let_go_scaling_stray` in the assistant), and the ask waits for the person's
yes on the Approvals page; an API key or an app gets 403. A unit Lens let go within the last hour isn't counted, since a provider may
still show it while it goes. When the provider can't be asked, `list_error` says why.

### Machines elsewhere

A cloud provider's unit is a new machine. Its first boot runs a script (user-data) that sets `LENS_SCALING_UNIT` (its
Expand Down Expand Up @@ -352,15 +365,16 @@ The same in the assistant (admins, in the server tools) and over MCP: `scaling`
`pause_scaling_group` and `terminate_scaling_unit` act within the approved terms. In the assistant, proposing or
changing a group waits for the person's yes; a yes on the Approvals page in the web app also approves the group's
terms, while a group the assistant makes in a setup conversation, or an agent over MCP, still waits for the person.
Turning scaling on (`change_settings scaling`) always asks.
Turning scaling on (`change_settings scaling`) always asks, and so does letting a stray go (`let_go_scaling_stray`),
which only that yes in the web app does.

## Refine later

Spreading model calls across a group's units, and serving only some kinds of call (chat, decisions); more model
hosts (Lambda instances serving a model); Bedrock's price per unit-hour from AWS, its streamed answers
(`ConverseStream`) and Bedrock API keys as the provider's key; SageMaker's streamed invocations (`InvokeEndpointWithResponseStream`), so a SageMaker answer
streams as it is written; Hugging Face's scale to zero and private endpoints; RunPod network volumes, so a model is downloaded once; Proxmox LXC containers; revoking a unit's mesh
certificate when it goes; reporting machines with Lens's tag that Lens has no unit for (`listing`); a live euro rate;
certificate when it goes; a live euro rate;
preemptible and spot instances at clouds other than AWS and RunPod; draining an AWS spot unit on its two-minute notice
(the node agent reading the instance's metadata) and handing its job back at once; following the spot price as it
changes; AWS sign-in with an instance role or IAM Identity Center instead of keys; mixed instance types per group;
Expand Down
11 changes: 11 additions & 0 deletions fastapi_backend/app/api/v1/routes/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Group,
GroupChange,
GroupIn,
LetGo,
Metrics,
Overview,
Paused,
Expand Down Expand Up @@ -125,6 +126,16 @@ def check_provider(pid: int, user: AdminReader, db: Db, cfg: Cfg) -> dict[str, A
return scaling.check_provider(db, cfg, pid)


@router.post("/providers/{pid}/strays/let-go")
def let_go_stray(pid: int, body: LetGo, user: AdminWriter, db: Db, cfg: Cfg) -> Provider:
"""Let a stray go: a unit the provider runs with Lens's tag that no unit of Lens's stands for, terminated at the
provider. Only a person signed in to the web app (403 to a token or an app)."""
with _errors():
p = scaling.let_go_stray(db, cfg, pid, body.id, user.email, _via(user))
auth.audit(db, user.as_audit(), "scaling.stray.let_go", f"scale_provider:{pid}", {"id": body.id})
return Provider(**p)


@router.delete("/providers/{pid}")
def remove_provider(pid: int, user: AdminWriter, db: Db, cfg: Cfg) -> Ok:
with _errors():
Expand Down
26 changes: 23 additions & 3 deletions fastapi_backend/app/domain/ops_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
ALWAYS_ASK = {"telemetry", "analytics", "sensors", "shares", "files", "web", "secrets", "scaling"}
# who gets in always waits for the admin's approval: a person, or a machine joining the mesh; and so does taking a
# machine off it, which stops what runs there
ASK_TOOLS = {"invite_person", "change_person", "add_mesh_node", "remove_mesh_node"}
ASK_TOOLS = {"invite_person", "change_person", "add_mesh_node", "remove_mesh_node", "let_go_scaling_stray"}
# within a scaling group's approved terms (its min, max and budget), or a change to the mesh that's undone as easily
# (renaming a node, the namespaces it serves, bringing a removed one back, withdrawing a token), so made at once
# unless the answer read outside text
Expand Down Expand Up @@ -148,6 +148,14 @@
["unit_id"],
True,
),
(
"let_go_scaling_stray",
"Ask the person to let a stray go: a unit a provider runs with Lens's tag that Lens has no unit for (a provider's "
"strays in scaling), terminated at the provider once they approve in the web app.",
{"provider_id": {"type": "integer"}, "stray_id": _S},
["provider_id", "stray_id"],
True,
),
]

_NAMESPACES = {"type": "array", "items": _S, "description": "the namespaces it serves (empty: every one)"}
Expand Down Expand Up @@ -427,8 +435,9 @@ def apply(db, cfg, base, tool, args, user, editable, admin, via="agent"):
if not admin:
raise PermissionError("only admins manage scaling")
out = scaling.apply_tool(db, cfg, tool, args, user, via)
what = out.get("group") or out.get("unit") or {}
table = "scale_group" if "group" in out else "scale_node"
table, what = next(
((t, out[k]) for k, t in (("group", "scale_group"), ("unit", "scale_node"), ("provider", "scale_provider")) if k in out)
)
auth.audit(db, user, f"scaling.{tool}", f"{table}:{what.get('id')}", {"via": via, "assistant": True})
return out
if tool in MESH:
Expand Down Expand Up @@ -716,6 +725,17 @@ def t_terminate_scaling_unit(self, unit_id, why=None):
args = store.clean({"unit_id": n["id"], "why": why})
return self._change("terminate_scaling_unit", args, f"Let the unit {n['name']} go")

def t_let_go_scaling_stray(self, provider_id, stray_id):
try:
p = scaling._provider(self.db, int(provider_id))
except (KeyError, TypeError, ValueError):
raise ValueError("there's no scaling provider with that id: scaling lists them") from None
s = next((x for x in p.get("strays") or [] if x.get("id") == str(stray_id)), None)
if s is None:
raise ValueError(f"{p['name']} has no stray with that id: scaling lists each provider's strays")
args = {"provider_id": p["id"], "stray_id": s["id"]}
return self._change("let_go_scaling_stray", args, f"Let {s.get('name') or s['id']} go at {p['name']}")

def t_mesh(self):
v = mesh.agent_view(self.db, self.cfg)
if not v["enabled"]:
Expand Down
Loading
Loading
0