package calls import ( "context" "encoding/hex" "encoding/json" "strings" access "amadan.net/oficina/agent-access.git" "github.com/google/uuid" "gorm.io/gorm" ) // Only a keyed fingerprint is retained. Each destination's atomic create // receipt owns progress; the connection service retains no workflow data. func (a *app) delegatedReserveSchedule(ctx context.Context, p access.Principal, person Person, raw json.RawMessage) (any, error) { var in struct { Team string `json:"team"` ClientID string `json:"client_id"` Fingerprint string `json:"fingerprint"` } if json.Unmarshal(raw, &in) != nil { return nil, access.Fail("invalid", "Invalid schedule reservation.") } id, err := uuid.Parse(in.ClientID) digest, e := hex.DecodeString(in.Fingerprint) if err != nil || id == uuid.Nil || id.String() != in.ClientID || e != nil || len(digest) != 32 || strings.ToLower(in.Fingerprint) != in.Fingerprint { return nil, access.Fail("invalid", "Supply a canonical UUID and SHA-256 fingerprint.") } coordinates, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, p.Subject, p.Client, in.Team, "calls_reserve_schedule", in.ClientID}) key, err := a.sealer.Fingerprint("connection.schedule.coordinate.v1", string(coordinates)) if err != nil { return nil, err } content, err := a.sealer.Fingerprint("connection.schedule.content.v1", in.Fingerprint) if err != nil { return nil, err } err = a.store.g.WithContext(ctx).Transaction(func(tx *gorm.DB) error { if err := a.bindDelegated(tx); err != nil { return err } if err := a.checkDelegatedWriter(ctx, tx, p, person); err != nil { return err } current := *a.store current.g = tx return current.guard(ctx, func() error { if err := tx.Exec("INSERT INTO calls_agent_schedules(coordinate,content) VALUES(?,?) ON CONFLICT(coordinate) DO NOTHING", key, content).Error; err != nil { return err } var saved string if err := tx.Raw("SELECT content FROM calls_agent_schedules WHERE coordinate=?", key).Row().Scan(&saved); err != nil { return err } if saved != content { return access.Fail("conflict", "This schedule client_id was used with different arguments.") } return a.checkDelegatedWriter(ctx, tx, p, person) }) }) if err != nil { return nil, err } return map[string]any{"reserved": true}, nil } func (a *app) delegatedScheduleOperation(team string) access.Operation { return access.Operation{Name: "calls_reserve_schedule", Description: "Reserve a fingerprint for a resumable schedule. Creates no meeting or event and sends no invitations. Reuse the UUID and fingerprint only for the same workflow.", Scope: "calls:write", Idempotent: true, Input: delegatedInput(map[string]any{"client_id": delegatedString(36), "fingerprint": map[string]any{"type": "string", "pattern": "^[0-9a-f]{64}$"}}, "client_id", "fingerprint"), Output: delegatedObject(map[string]any{"reserved": map[string]any{"const": true}}, "reserved"), Invoke: a.delegatedGuard(team, "calls:write", a.delegatedReserveSchedule)} }