feat(desktop): add monitor scheduler, Playwright CDP layer, and account health

Move monitor-task scheduling authority onto the client with a durable
file-backed queue that survives restart, drops stale cross-day tasks,
enforces per-platform serialism, and adapts global concurrency from
Electron process metrics. Publish tasks keep their existing FIFO.

Add a hidden Playwright CDP manager that attaches to Electron Chromium
on account session partitions, lets adapters opt into `executionMode:
"playwright"`, and leaves the existing hidden WebContentsView path in
place for current adapters.

Introduce an account-health subsystem with silent probes, projected
health/auth states, and IPC invalidation events so the renderer can
show accurate auth/probe status and verification timestamps.

Server-side, derive and forward title/business_date/scheduler_group_key/
question_text metadata on desktop task events so the local scheduler
can defer same-question fan-out before leasing.
This commit is contained in:
2026-04-20 17:16:15 +08:00
parent 07881a66d0
commit 55e1c2e74b
26 changed files with 3001 additions and 153 deletions
+101 -10
View File
@@ -2,23 +2,29 @@ package app
import (
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"time"
"github.com/geo-platform/tenant-api/internal/shared/messaging/rabbitmq"
)
type DesktopTaskEvent struct {
Type string `json:"type"`
TaskID string `json:"task_id"`
JobID string `json:"job_id"`
WorkspaceID int64 `json:"workspace_id"`
TargetAccountID string `json:"target_account_id"`
TargetClientID string `json:"target_client_id"`
Platform string `json:"platform"`
Status string `json:"status"`
Kind string `json:"kind"`
UpdatedAt time.Time `json:"updated_at"`
Type string `json:"type"`
TaskID string `json:"task_id"`
JobID string `json:"job_id"`
WorkspaceID int64 `json:"workspace_id"`
TargetAccountID string `json:"target_account_id"`
TargetClientID string `json:"target_client_id"`
Platform string `json:"platform"`
Title *string `json:"title,omitempty"`
BusinessDate *string `json:"business_date,omitempty"`
SchedulerGroupKey *string `json:"scheduler_group_key,omitempty"`
QuestionText *string `json:"question_text,omitempty"`
Status string `json:"status"`
Kind string `json:"kind"`
UpdatedAt time.Time `json:"updated_at"`
}
func publishDesktopTaskEvent(ctx context.Context, client *rabbitmq.Client, event DesktopTaskEvent) error {
@@ -33,3 +39,88 @@ func publishDesktopTaskEvent(ctx context.Context, client *rabbitmq.Client, event
return client.PublishDesktopTaskEvent(ctx, payload)
}
func deriveDesktopTaskEventMetadataFromPayload(
kind string,
payload []byte,
) (title *string, businessDate *string, schedulerGroupKey *string, questionText *string) {
if len(payload) == 0 {
return nil, nil, nil, nil
}
var envelope map[string]any
if err := json.Unmarshal(payload, &envelope); err != nil {
return nil, nil, nil, nil
}
title = firstDesktopTaskEventString(envelope,
"title",
"question_title",
"questionTitle",
)
businessDate = firstDesktopTaskEventString(envelope,
"business_date",
"businessDate",
"metric_date",
"date",
)
questionText = firstDesktopTaskEventString(envelope,
"question_text",
"questionText",
"query",
"question",
"prompt",
"content",
)
if explicit := firstDesktopTaskEventString(envelope,
"scheduler_group_key",
"schedulerGroupKey",
); explicit != nil {
return title, businessDate, explicit, questionText
}
if questionHash := firstDesktopTaskEventString(envelope, "question_hash", "questionHash"); questionHash != nil {
return title, businessDate, questionHash, questionText
}
if questionID := firstDesktopTaskEventString(envelope, "question_id", "questionId"); questionID != nil {
value := fmt.Sprintf("qid:%s", *questionID)
return title, businessDate, &value, questionText
}
if kind == "monitor" && questionText != nil {
sum := sha1.Sum([]byte(*questionText))
value := fmt.Sprintf("qtxt:%x", sum)
return title, businessDate, &value, questionText
}
return title, businessDate, nil, questionText
}
func firstDesktopTaskEventString(payload map[string]any, keys ...string) *string {
for _, key := range keys {
value, exists := payload[key]
if !exists {
continue
}
switch typed := value.(type) {
case string:
if typed != "" {
return &typed
}
case float64:
text := fmt.Sprintf("%.0f", typed)
return &text
case int64:
text := fmt.Sprintf("%d", typed)
return &text
case int:
text := fmt.Sprintf("%d", typed)
return &text
}
}
return nil
}
@@ -725,17 +725,22 @@ func (s *DesktopTaskService) publishTaskEvent(ctx context.Context, task *reposit
return
}
title, businessDate, schedulerGroupKey, questionText := deriveDesktopTaskEventMetadataFromPayload(task.Kind, task.Payload)
err := publishDesktopTaskEvent(ctx, s.messaging, DesktopTaskEvent{
Type: eventType,
TaskID: task.DesktopID.String(),
JobID: task.JobID.String(),
WorkspaceID: task.WorkspaceID,
TargetAccountID: task.TargetAccountID.String(),
TargetClientID: task.TargetClientID.String(),
Platform: task.Platform,
Status: task.Status,
Kind: task.Kind,
UpdatedAt: task.UpdatedAt,
Type: eventType,
TaskID: task.DesktopID.String(),
JobID: task.JobID.String(),
WorkspaceID: task.WorkspaceID,
TargetAccountID: task.TargetAccountID.String(),
TargetClientID: task.TargetClientID.String(),
Platform: task.Platform,
Title: title,
BusinessDate: businessDate,
SchedulerGroupKey: schedulerGroupKey,
QuestionText: questionText,
Status: task.Status,
Kind: task.Kind,
UpdatedAt: task.UpdatedAt,
})
if err != nil {
s.logWarn("desktop task event publish failed", err, zap.String("task_id", task.DesktopID.String()), zap.String("event_type", eventType))
@@ -213,17 +213,22 @@ func (s *PublishJobService) Create(ctx context.Context, actor auth.Actor, req Cr
invalidateArticleCaches(ctx, s.cache, actor.TenantID, &articleID)
for _, task := range createdTasks {
title, businessDate, schedulerGroupKey, questionText := deriveDesktopTaskEventMetadataFromPayload(task.Kind, task.Payload)
if publishErr := publishDesktopTaskEvent(context.Background(), s.messaging, DesktopTaskEvent{
Type: "task_available",
TaskID: task.DesktopID.String(),
JobID: task.JobID.String(),
WorkspaceID: task.WorkspaceID,
TargetAccountID: task.TargetAccountID.String(),
TargetClientID: task.TargetClientID.String(),
Platform: task.Platform,
Status: task.Status,
Kind: task.Kind,
UpdatedAt: task.UpdatedAt,
Type: "task_available",
TaskID: task.DesktopID.String(),
JobID: task.JobID.String(),
WorkspaceID: task.WorkspaceID,
TargetAccountID: task.TargetAccountID.String(),
TargetClientID: task.TargetClientID.String(),
Platform: task.Platform,
Title: title,
BusinessDate: businessDate,
SchedulerGroupKey: schedulerGroupKey,
QuestionText: questionText,
Status: task.Status,
Kind: task.Kind,
UpdatedAt: task.UpdatedAt,
}); publishErr != nil {
s.logWarn("desktop task available event publish failed", publishErr, zap.String("task_id", task.DesktopID.String()))
}