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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
charm.land/lipgloss/v2 v2.1.1
filippo.io/age v1.3.2
github.com/charmbracelet/x/ansi v0.11.9
github.com/coder/websocket v1.8.14
github.com/jmespath/go-jmespath v0.4.0
github.com/modelcontextprotocol/go-sdk v1.8.0
golang.org/x/crypto v0.59.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSE
github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
Expand Down
38 changes: 35 additions & 3 deletions internal/computers/handoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,46 @@ func (c *Catalog) TakeRequest(ctx context.Context, botID int64, requestID, viewI
if session.botID != botID || session.service != card.Takeover.Service {
return rpc.NewError("FORBIDDEN", "viewer 不属于该请求。")
}
// A grant already settling reports its real outcome, like views.ts take
// awaiting view.taking.
session.mu.Lock()
for session.grant != nil {
grant := session.grant
session.mu.Unlock()
<-grant.done
session.mu.Lock()
if grant.err != nil {
session.mu.Unlock()
return grant.err
}
}
if session.lease == 0 {
lease, err := c.svcs.takeover(ctx, session.service, botID)
grant := &pendingGrant{done: make(chan struct{})}
session.grant = grant
lease := c.svcs.nextLease()
session.lease = lease
session.mu.Unlock()
err := c.svcs.takeover(ctx, session.service, botID, lease)
session.mu.Lock()
grant.err = err
close(grant.done)
session.grant = nil
if err != nil {
session.lease = 0
session.mu.Unlock()
return err
}
session.lease = lease
session.mu.Unlock()
// A viewer that closed during the grant released nothing it never
// saw; give the claimed lane back.
if c.vw.get(viewID) == nil {
c.svcs.release(session.service, botID, lease)
return rpc.NewError("GONE", "画面连接已断开。")
}
session.mu.Lock()
session.controller = true
}
session.mu.Unlock()
// Concurrent requests from the owning viewer reuse the live grant.
session.taking = true
session.requestID = requestID
Expand All @@ -149,7 +181,7 @@ func (c *Catalog) FinishRequest(_ context.Context, botID int64, requestID string
owner.taking = false
owner.requestID = ""
if owner.lease != 0 {
c.svcs.release(owner.service, owner.lease)
c.svcs.release(owner.service, owner.botID, owner.lease)
owner.lease = 0
owner.controller = false
}
Expand Down
85 changes: 61 additions & 24 deletions internal/computers/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package computers

import (
"context"
"fmt"
"sync"

"github.com/Personed/Persona/internal/computer"
Expand Down Expand Up @@ -55,7 +56,8 @@ type services struct {
workers map[serviceKey]*serviceWorker
records map[serviceKey]string // key -> record id
controls map[string]*control
leased int64 // next viewer lease
isolated map[int64]bool // Bots whose browser does not share the desktop (services.ts isolatedBrowsers)
leased int64 // next viewer lease
}

func newServices(c *Catalog) *services {
Expand All @@ -64,6 +66,7 @@ func newServices(c *Catalog) *services {
workers: map[serviceKey]*serviceWorker{},
records: map[serviceKey]string{},
controls: map[string]*control{},
isolated: map[int64]bool{},
leased: 1,
}
}
Expand Down Expand Up @@ -132,17 +135,39 @@ func (s *services) workerFor(ctx context.Context, botID int64, service string) (
}

// controlOf returns the service control state, creating it owned by the Bot.
func (s *services) controlOf(service string) *control {
func (s *services) controlOf(service string, botID int64) *control {
s.mu.Lock()
defer s.mu.Unlock()
ctl := s.controls[service]
return s.control(service, botID)
}

// control resolves the lane as services.ts control(): an isolated browser owns
// its lane per Bot, and every other service shares the desktop lane.
func (s *services) control(service string, botID int64) *control {
key := "desktop"
if service == "browser" && s.isolated[botID] {
key = fmt.Sprintf("browser:%d", botID)
}
ctl := s.controls[key]
if ctl == nil {
ctl = &control{owner: true}
s.controls[service] = ctl
s.controls[key] = ctl
}
return ctl
}

// setIsolated records whether the Bot's browser shares the desktop, as the
// prepare result's sharedDesktop does in services.ts.
func (s *services) setIsolated(botID int64, on bool) {
s.mu.Lock()
defer s.mu.Unlock()
if on {
s.isolated[botID] = true
} else {
delete(s.isolated, botID)
}
}

// closeRecord ends every worker for the record (disable/close, services.ts
// close: control by the caller's computer is destroyed).
func (s *services) closeRecord(recordID string) {
Expand Down Expand Up @@ -175,30 +200,42 @@ func (s *services) close() {
}
}

// takeover reserves a viewer lease for the service (services.ts takeover):
// serializes on the lane, waits for the service to settle, then records the
// lease so viewAccess is reserved before the grant waits.
func (s *services) takeover(ctx context.Context, service string, botID int64) (int64, error) {
w, dir, err := s.workerFor(ctx, botID, service)
if err != nil {
return 0, err
}
// The worker settles dispatched work before control changes.
if _, err := w.ComputerRequest(ctx, worker.ComputerInput{Action: "settle", Service: service, Dir: dir}); err != nil {
return 0, err
}
// nextLease issues a viewer lease ID (services.ts takeover's lease argument).
func (s *services) nextLease() int64 {
s.mu.Lock()
defer s.mu.Unlock()
ctl := s.controls[service]
if ctl == nil {
ctl = &control{owner: true}
s.controls[service] = ctl
}
lease := s.leased
s.leased++
return lease
}

// takeover claims a viewer lease for the service before the service settles
// (services.ts takeover): the reservation is visible to a closing viewer, one
// viewer holds a lane at a time, and a failed settle gives it back.
func (s *services) takeover(ctx context.Context, service string, botID int64, lease int64) error {
s.mu.Lock()
ctl := s.control(service, botID)
if ctl.lease != 0 {
s.mu.Unlock()
return rpc.NewError("CONFLICT", "用户正在接管这台电脑。")
}
ctl.lease = lease
ctl.owner = false
return lease, nil
s.mu.Unlock()
w, dir, err := s.workerFor(ctx, botID, service)
if err == nil {
// The worker settles dispatched work before control changes.
_, err = w.ComputerRequest(ctx, worker.ComputerInput{Action: "settle", Service: service, Dir: dir})
}
if err != nil {
s.mu.Lock()
if ctl.lease == lease {
ctl.lease = 0
}
s.mu.Unlock()
return err
}
return nil
}

// Workspace dials the worker of the Bot's bound work computer and returns it
Expand Down Expand Up @@ -257,8 +294,8 @@ func (c *Catalog) Workspace(ctx context.Context, botID int64) (computer.Target,
}

// release returns control to the Bot (services.ts release).
func (s *services) release(service string, lease int64) {
ctl := s.controlOf(service)
func (s *services) release(service string, botID int64, lease int64) {
ctl := s.controlOf(service, botID)
if ctl.lease != lease || ctl.releasing {
return
}
Expand Down
8 changes: 4 additions & 4 deletions internal/computers/surface.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,12 @@ func (c *Catalog) Watch(ctx context.Context, botID int64, service string, emit f
if service != "browser" && service != "desktop" {
return rpc.NewError("BAD_REQUEST", "服务无效。")
}
session, first, err := c.vw.open(ctx, botID, service, "")
session, err := c.vw.open(ctx, botID, service, "")
if err != nil {
return err
}
defer c.vw.close(context.Background(), session.id)
if err := emit(frame{Type: "opened", ViewID: session.id, Message: first}); err != nil {
if err := emit(frame{Type: "ready", ID: session.id}); err != nil {
return err
}
for {
Expand All @@ -374,10 +374,10 @@ func (c *Catalog) Watch(ctx context.Context, botID int64, service string, emit f
return err
}
if batch.Closed {
return emit(frame{Type: "closed", ViewID: session.id})
return nil
}
for _, message := range batch.Messages {
if err := emit(frame{Type: "message", ViewID: session.id, Message: message}); err != nil {
if err := emit(frame{Type: "frame", Frame: message}); err != nil {
return err
}
}
Expand Down
Loading