v0.9.4: a turn keeps running when the app is not on screen

A phone freezes an app it cannot see, and the front end had nothing holding
it: a turn that took minutes -- a long shell command, a provider that thinks,
a build -- stopped quietly the moment the user pressed home, and the answer
was only there if the window happened to come back. The engine's own /attach
flow made this visible: an APK turn survived, a pocket did not.

- app/android/src/xyz/whcself/cstagent/CstService.java: a foreground service
  started with an explicit component (nothing outside the app can reach it,
  no intent filter) for as long as a turn runs. Being a foreground service
  makes the process one Android neither freezes nor kills; a partial wake
  lock (30 min ceiling) keeps the CPU awake with the screen off. Its ongoing
  notification says what is running -- "CST-agent is working" plus the first
  line of the prompt -- on the same cst-agent channel the notification_send
  tool uses, and taps back into the app. A phone that refuses the foreground
  start (Android 12+ does, from the background) loses the protection, not the
  turn: the failure is caught and the service stops itself.
- Manifest: FOREGROUND_SERVICE, FOREGROUND_SERVICE_DATA_SYNC (API 34+ wants
  the typed one) and WAKE_LOCK, plus the <service> with
  foregroundServiceType="dataSync". The first build of this failed with
  "Expected '>', but got ' '" -- a manifest comment cannot contain "--".
- Front end: app::holdForeground(text) / app::releaseForeground() in
  ToolProvider.h, one call for every platform -- Android starts and stops the
  service through JNI, a desktop has nothing to hold (its window is the app).
  AgentCore::send() calls it when the turn starts (firstLine() of the prompt
  is what the notification says) and finishTurn() releases it, so the
  notification exists exactly as long as the work does. CST_APP_NO_SERVICE=1
  keeps a test run from touching the phone.
- On the NX789J, with the app sent home and the screen off: the service read
  back isForeground=true with its notification, the wake lock cst-agent:turn
  was taken at the start of the turn and dropped at its end, run_shell
  "sleep 60" finished with the screen dark, and the service was gone after.

Desktop: build clean, --uitest all green, --selftest and cst-app-selftest OK
(the hold is a no-op there, so nothing about the Qt side changed).

Version 0.9.4.
This commit is contained in:
WHC
2026-10-11 16:51:39 +08:00
parent d4f65b17cf
commit 7299f39245
9 changed files with 304 additions and 5 deletions
+19
View File
@@ -188,6 +188,18 @@ void AgentCore::close() {
/* --- turns --------------------------------------------------------------- */
namespace {
/* The first line of a prompt, cut short: the line the phone's "working"
* notification carries while the turn runs (app::holdForeground). */
QString firstLine(const QString &s) {
const int nl = s.indexOf(QLatin1Char('\n'));
QString line = (nl < 0 ? s : s.left(nl)).trimmed();
if (line.size() > 120)
line = line.left(119) + QStringLiteral("…");
return line;
}
} // namespace
void AgentCore::send(const QString &prompt) {
if (!m_agent || m_running || prompt.trimmed().isEmpty())
return;
@@ -195,6 +207,12 @@ void AgentCore::send(const QString &prompt) {
* message the composer just appended, before the turn can say anything. */
const QString sending = pendingImages();
setRunning(true);
/* A turn can take minutes: hold the process while it runs, or a phone
* freezes the app the moment it leaves the screen. "" on a desktop and
* whenever the hold is in place. */
const QString held = app::holdForeground(firstLine(prompt));
if (!held.isEmpty())
emit statusChanged(held);
if (!sending.isEmpty())
emit promptImages(sending);
m_turn = new Turn(this, prompt);
@@ -210,6 +228,7 @@ void AgentCore::finishTurn(int rc, const QString &err) {
Turn *t = static_cast<Turn *>(sender());
post([this, rc, err] {
setRunning(false);
app::releaseForeground(); /* the turn is over: let the phone sleep again */
/* The prompt carried whatever was pending; the engine dropped it. */
m_attachNames.clear();
emit pendingChanged();
+10
View File
@@ -67,6 +67,16 @@ QString openLocalFile(const QString &path, const QString &title, QString *status
* desktop has no share sheet, so the path goes to the clipboard. */
QString shareLocalFile(const QString &path, const QString &title,
QString *status);
/* Hold the machine while a turn runs, so it survives the app leaving the
* screen: a phone starts a foreground service with a partial wake lock (the
* process is one Android will not freeze or kill, and the CPU keeps going
* with the screen off) and shows a notification naming the work; a desktop
* does nothing -- the window *is* the app there. Returns "" when the hold is
* in place (or when the platform has nothing to do), a reason otherwise; the
* turn runs either way. `text` is what the phone's notification says. */
QString holdForeground(const QString &text);
/* Drop it again, at the end of the turn (a phone: the notification goes). */
void releaseForeground();
} // namespace app