Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(models): Use switches local models — stops the one running, starts the new one

bitcoinbuilderd1a4b41c8b0e
diff --git a/cli/src/lib/localModels.spec.ts b/cli/src/lib/localModels.spec.ts
index 45952c595..3348ee856 100644
--- a/cli/src/lib/localModels.spec.ts
+++ b/cli/src/lib/localModels.spec.ts
@@ -1398,6 +1398,17 @@ describe('models other apps downloaded, started in their own app', () => {
expect(scans).toBe(2)
})
+ it('starts another right after a stop, without a list read between to clear the one that ran', async () => {
+ const models = appService()
+ await models.act('home', ollama.id, 'start'); await models.settled()
+ await models.list('home', true)
+ // A switch: stop, then start at once — the list read above still says llama3.2:3b runs.
+ await models.act('home', ollama.id, 'stop'); await models.settled()
+ await models.act('home', studio.id, 'start'); await models.settled()
+ expect(ops.start).toHaveBeenLastCalledWith(studio, 131072, join(stateDir, 'logs'))
+ expect((await models.list('home', true)).models.find(m => m.id === studio.id)).toMatchObject({ state: 'running', operation: { phase: 'done' } })
+ })
+
it('takes it down again, and says so, when the app loaded it with less than 64K', async () => {
window = 16384
const models = appService()
diff --git a/cli/src/lib/localModels.ts b/cli/src/lib/localModels.ts
index 9c8bce2c7..2f26a1c06 100644
--- a/cli/src/lib/localModels.ts
+++ b/cli/src/lib/localModels.ts
@@ -935,6 +935,11 @@ export class LocalModels {
if (record) { await takeDown(record); await without(record) }
} else if (operation.action === 'start' && !record) {
if (!app || !ops) throw new ModelError('This model could not be checked. Refresh and try again.')
+ // Asked now, not taken from the last list: Use stops the model running and starts this one at
+ // once, and the list read before that stop still named it.
+ await this.owned(grid)
+ const running = records.find(r => r.grid === grid && r.modelId !== app.id)
+ if (running) throw new ModelError(`Stop ${cleanName(running.name)} first to start another local model.`)
if (this.blockers.has(grid)) throw new ModelError(this.blockers.get(grid)!)
const device = obj(await this.json(['device-info', '--json']))
const ctx = appContext(app, num(device.usable_bytes))
diff --git a/desktop/lib/screens/swarm_screen.dart b/desktop/lib/screens/swarm_screen.dart
index 3de1e0883..531a4518c 100644
--- a/desktop/lib/screens/swarm_screen.dart
+++ b/desktop/lib/screens/swarm_screen.dart
@@ -23,7 +23,8 @@ import '../logging/debug_surface.dart';
import '../models/api_connections_controller.dart' show agentOnApiModel;
import '../models/models_panel.dart';
import '../models/model_search_catalog.dart';
-import '../widgets/resting_section.dart' show confirmSwitchAnyway;
+import '../widgets/resting_section.dart'
+ show confirmStopInUse, confirmSwitchAnyway;
import '../settings/settings_screen.dart';
import '../settings/settings_section.dart';
import '../shared/theme/app_theme.dart' as grid;
@@ -5385,6 +5386,35 @@ class _SwarmScreenState extends State<SwarmScreen> {
return;
}
var selected = search.selectableGridModel(choice.destination);
+ // Use and Get stop the local model running on that machine to start this one in its place. The
+ // harness being switched is moving off it; any other harness on it is asked about first.
+ if (selected == null &&
+ (search.canGetModelForUse(choice.destination) ||
+ search.canStartModelForUse(choice.destination))) {
+ if (search.otherRunningModel(choice.destination) case final other?) {
+ final users = _harnessesOn(other.name, except: chosenFor?.agentId);
+ if (users.isNotEmpty) {
+ _pickerModalChanged(true);
+ bool proceed;
+ try {
+ proceed = await confirmStopInUse(
+ context,
+ model: other.name,
+ users: users,
+ );
+ } finally {
+ _pickerModalChanged(false);
+ // Back to the picker's field: closed, the dialog left focus on no row, and Enter did nothing.
+ if (mounted && _search != null) _focusSearch();
+ }
+ if (!proceed) return;
+ // The screen reads its route as current again only at its next frame: checked before
+ // that, the switch the person just confirmed was dropped as if the picker had closed.
+ await WidgetsBinding.instance.endOfFrame;
+ if (!mounted || !current() || _search == null) return;
+ }
+ }
+ }
if (selected == null && search.canGetModelForUse(choice.destination)) {
selected = await search.getModelForUse(
choice.destination,
@@ -5410,8 +5440,11 @@ class _SwarmScreenState extends State<SwarmScreen> {
);
} finally {
_pickerModalChanged(false);
+ if (mounted && _search != null) _focusSearch();
}
- if (!proceed || !mounted || !current() || _search == null) return;
+ if (!proceed) return;
+ await WidgetsBinding.instance.endOfFrame;
+ if (!mounted || !current() || _search == null) return;
}
final now = WorkspacePaneContext.focused(app)!;
_closeSearch();
@@ -6841,6 +6874,16 @@ class _SwarmScreenState extends State<SwarmScreen> {
});
}
+ /// The harnesses on grid model [model] besides [except], by name — what a stop would leave without
+ /// a model to answer with.
+ List<String> _harnessesOn(String model, {String? except}) => [
+ for (final machine in app.machineStates.values)
+ for (final agent in machine.agents)
+ if (agent.id != except &&
+ agent.gridModel?.toLowerCase() == model.toLowerCase())
+ agent.displayName,
+ ];
+
bool _canSwitchFocusedModel(WorkspacePaneContext focused) {
if (!_shortcutsEnabled || !modelPickerSupports(focused.engine)) {
return false;
diff --git a/desktop/lib/state/swarm_search.dart b/desktop/lib/state/swarm_search.dart
index b02eb2460..81cfa3402 100644
--- a/desktop/lib/state/swarm_search.dart
+++ b/desktop/lib/state/swarm_search.dart
@@ -617,17 +617,14 @@ class SwarmSearchController extends ChangeNotifier {
}
/// Whether Get on [row] can go on to put the picker's harness on the model: a download of yours,
- /// chosen for a harness that can run on a local model. Otherwise Get only downloads.
- ///
- /// Not while another model runs on that machine: its daemon runs one local model at a time and
- /// refuses the start, so Get would download and then fail. It only downloads, and says why.
+ /// chosen for a harness that can run on a local model. Otherwise Get only downloads. Another model
+ /// running on that machine is stopped first ([otherRunningModel]): it runs one at a time.
bool canGetModelForUse(SwarmDestination? row) =>
canGetModel(row) &&
modelSelectionEngine != null &&
_modelChoices?.reachable == true &&
_modelChoices?.canRunLocally(modelSelectionEngine) == true &&
- models!.entries[row!.modelId]!.own &&
- otherRunningModel(row) == null;
+ models!.entries[row!.modelId]!.own;
/// Another model of yours running on [row]'s machine — the one to stop before [row]'s can run.
LocalModel? otherRunningModel(SwarmDestination? row) {
@@ -656,11 +653,15 @@ class SwarmSearchController extends ChangeNotifier {
);
}
+ /// The model Use or Get is stopping to make room — its machine runs one local model at a time.
+ String? stoppingOther;
+
/// What the picker is doing for Use or Get, for its hint: `Downloading 42%…`, `Starting…`.
String? get usingLabel {
final entry = models?.entries[usingModelId];
final local = entry?.local;
if (usingModelId == null) return null;
+ if (stoppingOther case final other?) return 'Stopping $other…';
if (entry == null || local == null) return 'Starting…';
final owner = entry.controller ?? models!.manager;
final operation = owner.operationFor(local);
@@ -710,6 +711,54 @@ class SwarmSearchController extends ChangeNotifier {
LocalModel? model() =>
owner.localModels.where((model) => model.id == modelId).firstOrNull;
+ /// The model running on that machine besides this one, stopped and gone before this one starts:
+ /// the machine runs one local model at a time, and Use is a switch, not two steps to make.
+ Future<bool> stopOther() async {
+ final other = otherRunningModel(row);
+ if (other == null) return true;
+ stoppingOther = other.name;
+ notifyListeners();
+ try {
+ await owner.control(other, 'stop');
+ final deadline = DateTime.now().add(timeout);
+ while (current()) {
+ if (hostGone()) {
+ fail('Reconnect to ${entry.node} to use this model.');
+ return false;
+ }
+ final now = owner.localModels
+ .where((model) => model.id == other.id)
+ .firstOrNull;
+ final operation = now == null ? null : owner.operationFor(now);
+ if (operation?.failed == true ||
+ (owner.error != null && !owner.busy)) {
+ fail(
+ operation?.error ??
+ owner.error ??
+ 'Could not stop ${other.name}. Try again.',
+ );
+ return false;
+ }
+ if (now?.canStop != true &&
+ operation?.active != true &&
+ !owner.busy) {
+ return true;
+ }
+ if (DateTime.now().isAfter(deadline)) {
+ fail('${other.name} is still stopping. Try again in a moment.');
+ return false;
+ }
+ await _modelUsePause();
+ if (!current()) return false;
+ await owner.refresh();
+ }
+ return false;
+ } finally {
+ stoppingOther = null;
+ if (!_disposed) notifyListeners();
+ }
+ }
+
try {
if (download) {
await owner.control(
@@ -738,7 +787,10 @@ class SwarmSearchController extends ChangeNotifier {
got.downloaded &&
operation?.active != true &&
!owner.busy) {
- if (!got.running) await owner.control(got, 'start');
+ if (!got.running) {
+ if (!await stopOther()) return null;
+ await owner.control(model() ?? got, 'start');
+ }
break;
}
await _modelUsePause();
@@ -746,7 +798,8 @@ class SwarmSearchController extends ChangeNotifier {
await owner.refresh();
}
} else {
- await owner.control(entry.local!, 'start');
+ if (!await stopOther()) return null;
+ await owner.control(model() ?? entry.local!, 'start');
}
final deadline = DateTime.now().add(timeout);
while (current()) {
diff --git a/desktop/lib/widgets/resting_section.dart b/desktop/lib/widgets/resting_section.dart
index 9b3e3df24..30b3f406c 100644
--- a/desktop/lib/widgets/resting_section.dart
+++ b/desktop/lib/widgets/resting_section.dart
@@ -157,6 +157,87 @@ class _WakeRow extends StatelessWidget {
);
}
+/// "Stop [model]?" before Use stops the local model other harnesses are on, to start another in its
+/// place (one runs at a time). Only asked when [users] — those harnesses' names — is not empty: the
+/// harness being switched is moving off it anyway. True only for the button that says so.
+Future<bool> confirmStopInUse(
+ BuildContext context, {
+ required String model,
+ required List<String> users,
+}) async =>
+ await showAppDialog<bool>(
+ context: context,
+ builder: (_) => _StopInUsePrompt(model: model, users: users),
+ ) ??
+ false;
+
+class _StopInUsePrompt extends StatefulWidget {
+ const _StopInUsePrompt({required this.model, required this.users});
+
+ final String model;
+ final List<String> users;
+
+ @override
+ State<_StopInUsePrompt> createState() => _StopInUsePromptState();
+}
+
+class _StopInUsePromptState extends State<_StopInUsePrompt> {
+ final _cancel = FocusNode(debugLabel: 'Cancel stopping a model in use');
+
+ @override
+ void initState() {
+ super.initState();
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (mounted && ModalRoute.of(context)?.isCurrent != false) {
+ _cancel.requestFocus();
+ }
+ });
+ }
+
+ @override
+ void dispose() {
+ _cancel.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ grid.AppTheme.watch(context);
+ final users = widget.users;
+ final who = users.length == 1
+ ? users.single
+ : '${users.first} and ${users.length - 1} more';
+ return DesktopPromptSurface(
+ body: DesktopPromptScrollBody(
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ Text('Stop ${widget.model}?', style: DesktopChrome.heading()),
+ const SizedBox(height: DesktopChrome.groupGap),
+ Text(
+ '$who ${users.length == 1 ? 'uses' : 'use'} it, and will stop answering until '
+ '${users.length == 1 ? 'it moves' : 'they move'} to another model. One local model '
+ 'runs at a time on this computer.',
+ style: DesktopChrome.text(size: 13),
+ ),
+ ],
+ ),
+ ),
+ actions: [
+ TextButton(
+ focusNode: _cancel,
+ onPressed: () => Navigator.pop(context, false),
+ child: const Text('Cancel'),
+ ),
+ FilledButton(
+ onPressed: () => Navigator.pop(context, true),
+ child: const Text('Stop and switch'),
+ ),
+ ],
+ );
+ }
+}
+
/// "Switch anyway?" before moving an agent onto [model], every computer serving which seems
/// offline. True only for the button that says so; Cancel, Escape and a click outside all leave
/// the agent where it is. The move itself is never refused — the daemon can be wrong about a
diff --git a/desktop/lib/widgets/swarm_resource_preview.dart b/desktop/lib/widgets/swarm_resource_preview.dart
index 82f6ece5d..be63c8253 100644
--- a/desktop/lib/widgets/swarm_resource_preview.dart
+++ b/desktop/lib/widgets/swarm_resource_preview.dart
@@ -1425,10 +1425,12 @@ class _SwarmResourcePreviewState extends State<SwarmResourcePreview> {
final search = widget.search;
final selected = row;
if (selected == null || search.modelRowInUse(selected)) return null;
- // One local model runs at a time: a second one waits for the first to stop.
+ // One local model runs at a time: Use and Get stop the one running, then start this one.
final other = search.otherRunningModel(selected)?.name;
if (search.canGetModelForUse(selected)) {
- return 'Get downloads it, starts it, and moves this harness onto it.';
+ return other == null
+ ? 'Get downloads it, starts it, and moves this harness onto it.'
+ : 'Get downloads it, stops $other, starts it, and moves this harness onto it.';
}
if (search.canGetModel(selected)) {
return other == null
@@ -1440,7 +1442,7 @@ class _SwarmResourcePreviewState extends State<SwarmResourcePreview> {
? 'Use moves this harness onto it.'
: other == null
? 'Use starts it and moves this harness onto it.'
- : 'Stop $other first: one local model runs at a time.';
+ : 'Use stops $other, starts this one, and moves this harness onto it.';
}
return null;
}
diff --git a/desktop/test/local_model_picker_test.dart b/desktop/test/local_model_picker_test.dart
index 814a0a08e..a8c6f9251 100644
--- a/desktop/test/local_model_picker_test.dart
+++ b/desktop/test/local_model_picker_test.dart
@@ -97,6 +97,9 @@ class _LocalApp extends ModelManagerTestApp {
/// Downloads and starts finish at once, as a fast machine's would; left false, they stay under way.
bool completeDownloads = false, completeStarts = false;
+ /// Stops finish at once: the model is down and off the grid, as a fast machine's would be.
+ bool completeStops = false;
+
/// A started model runs at once but the grid lists it only on [listHeld]: the gap between running
/// here and being a model a harness can move onto, which a busy machine took 16s to close.
bool holdListing = false;
@@ -165,6 +168,35 @@ class _LocalApp extends ModelManagerTestApp {
modelId,
start: start,
);
+ if (!start && completeStops) {
+ late String stopped;
+ setInventoryFor(
+ machineId,
+ _with(modelId, (model) {
+ stopped = model['name'] as String;
+ return {
+ ...model,
+ 'state': 'downloaded',
+ 'canStart': true,
+ 'canStop': false,
+ 'operation': null,
+ };
+ }),
+ );
+ inventory = GridModels(
+ gridName: 'home',
+ models: [
+ for (final model in inventory.models)
+ if (model.id != stopped) model,
+ ],
+ );
+ return {
+ 'operation': {
+ ...answer['operation'] as Map<String, dynamic>,
+ 'phase': 'done',
+ },
+ };
+ }
if (!start || !completeStarts) return answer;
late String name;
setInventoryFor(
@@ -211,7 +243,11 @@ class _LocalApp extends ModelManagerTestApp {
}
}
-Future<_LocalApp> _localFixture({String? onModel, bool running = true}) async {
+Future<_LocalApp> _localFixture({
+ String? onModel,
+ bool running = true,
+ String? alsoOn,
+}) async {
final app = _LocalApp()
..inventory = const GridModels(
gridName: 'home',
@@ -237,6 +273,18 @@ Future<_LocalApp> _localFixture({String? onModel, bool running = true}) async {
gridModel: onModel,
),
);
+ // Another harness, on [alsoOn]: a stop of that model leaves it without one.
+ if (alsoOn != null) {
+ machine.agents.add(
+ Agent(
+ id: 'b70',
+ name: 'Review harness',
+ engine: 'claude',
+ terminalAvailable: true,
+ gridModel: alsoOn,
+ ),
+ );
+ }
await app.modelManager.refresh(force: true);
return app;
}
@@ -377,11 +425,11 @@ void main() {
expect(_inPreview('256K'), findsOneWidget);
expect(_inPreview('9B'), findsOneWidget);
expect(_inPreview('Q4_K_XL'), findsOneWidget);
- // One local model runs at a time: with Qwen3.6 running, Get only downloads, and says why.
- expect(picker.canGetModelForUse(download), isFalse);
+ // One local model runs at a time: with Qwen3.6 running, Get stops it to start this one.
+ expect(picker.canGetModelForUse(download), isTrue);
expect(
_inPreview(
- 'Get downloads it. Stop Qwen3.6-35B-A3B to run it: one local model runs at a time.',
+ 'Get downloads it, stops Qwen3.6-35B-A3B, starts it, and moves this harness onto it.',
),
findsOneWidget,
);
@@ -398,7 +446,7 @@ void main() {
expect(_inPreview('Download'), findsNothing);
expect(
_inPreview(
- 'Stop Qwen3.6-35B-A3B first: one local model runs at a time.',
+ 'Use stops Qwen3.6-35B-A3B, starts this one, and moves this harness onto it.',
),
findsOneWidget,
);
@@ -494,12 +542,13 @@ void main() {
testWidgets(
'a model another app downloaded is one of yours, named with that app, and Use starts it there',
(tester) async {
- final app = (await _localFixture())..completeStarts = true;
+ // Nothing else running: this is about the one model, not a switch.
+ final app = (await _localFixture(running: false))..completeStarts = true;
try {
app.localInventory = {
- ..._inventory(),
+ ..._inventory(running: false),
'models': [
- ...(_inventory()['models'] as List),
+ ...(_inventory(running: false)['models'] as List),
{
'id': 'app:ollama:llama3.2:3b',
'name': 'llama3.2:3b',
@@ -538,7 +587,7 @@ void main() {
testWidgets(
'one Use: the row says Starting until the harness is on the model, then it moves',
(tester) async {
- final app = (await _localFixture())
+ final app = (await _localFixture(running: false))
..completeStarts = true
..holdListing = true;
try {
@@ -565,4 +614,85 @@ void main() {
}
},
);
+
+ testWidgets(
+ 'Use on another model stops the one running, starts this one and moves the harness, in one step',
+ (tester) async {
+ final app = (await _localFixture(onModel: 'Qwen3.6-35B-A3B'))
+ ..completeStarts = true
+ ..completeStops = true;
+ try {
+ final picker = await _open(tester, app);
+ final gemma = _row(picker, 'gemma-4-12B');
+ picker.move(picker.rows.indexOf(gemma) - picker.cursor);
+ await tester.pumpAndSettle();
+ expect(
+ _inPreview(
+ 'Use stops Qwen3.6-35B-A3B, starts this one, and moves this harness onto it.',
+ ),
+ findsOneWidget,
+ );
+ await key(tester, LogicalKeyboardKey.enter);
+ await tester.pump(const Duration(seconds: 3));
+ await tester.pump(const Duration(seconds: 3));
+ await tester.pumpAndSettle();
+ // The harness on it was this one, so nobody is asked: stop, start, move.
+ expect(app.actions, [
+ (
+ machine: 'm',
+ model: 'local:Qwen3.6-35B-A3B-UD-Q5_K_XL.gguf',
+ start: false,
+ ),
+ (machine: 'm', model: 'gemma', start: true),
+ ]);
+ expect(app.selections, [(agent: 'a69', model: 'gemma-4-12B')]);
+ } finally {
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ }
+ },
+ );
+
+ testWidgets(
+ 'another harness on the running model is asked about before it stops; Cancel leaves both',
+ (tester) async {
+ final app = (await _localFixture(alsoOn: 'Qwen3.6-35B-A3B'))
+ ..completeStarts = true
+ ..completeStops = true;
+ try {
+ final picker = await _open(tester, app);
+ final gemma = _row(picker, 'gemma-4-12B');
+ picker.move(picker.rows.indexOf(gemma) - picker.cursor);
+ await tester.pumpAndSettle();
+ await key(tester, LogicalKeyboardKey.enter);
+ await tester.pumpAndSettle();
+ expect(find.text('Stop Qwen3.6-35B-A3B?'), findsOneWidget);
+ expect(
+ find.textContaining(
+ 'Review harness uses it, and will stop answering',
+ ),
+ findsOneWidget,
+ );
+ // Cancel holds the focus: a stray Return stops nothing.
+ await key(tester, LogicalKeyboardKey.enter);
+ await tester.pumpAndSettle();
+ expect(find.text('Stop Qwen3.6-35B-A3B?'), findsNothing);
+ expect(app.actions, isEmpty);
+ expect(app.selections, isEmpty);
+
+ // Focus is back on the picker: Enter asks again.
+ await key(tester, LogicalKeyboardKey.enter);
+ await tester.pumpAndSettle();
+ await tester.tap(find.text('Stop and switch'));
+ await tester.pump(const Duration(seconds: 3));
+ await tester.pump(const Duration(seconds: 3));
+ await tester.pumpAndSettle();
+ expect(app.actions.map((action) => action.start), [false, true]);
+ expect(app.selections, [(agent: 'a69', model: 'gemma-4-12B')]);
+ } finally {
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ }
+ },
+ );
}