oficina / base Public

Abilities phase 1: Astra round 3 fixes (2 P1, 2 P2)

P1: a client's record, and undoing it after a failure, run on a bounded
context the client going away can't cancel. P1: a member's capabilities
are worked out under the same lock their records take, so never while
one is half made. P2: a hold on a client Base couldn't record lasts the
30-day client window and no longer. P2: with links off, an address in
a lookup's answer is text whatever it is, in Go, ask.js and the app: no
URL check decides how it is drawn (shared sample added).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 1659aa1b4e76fefd8ad79955ecc3260a75817d22 parent ab3a197
6 files changed, +101 −19
  • internal/look/static/ask.js +6 −3
  • internal/web/ask_caps.go +34 −8
  • internal/web/ask_caps_internal_test.go +44 −0
  • internal/web/ask_cards.go +8 −5
  • internal/web/testdata/lookup.json +4 −0
  • native/apple/Sources/BaseKit/Ask/Markdown/PersonMarkdown.swift +5 −3
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js
index d6b59f3..211aad3 100644
--- a/internal/look/static/ask.js
+++ b/internal/look/static/ask.js
@@ -336,14 +336,17 @@ function start(root) {
end = m.index + target.length;
label = target;
}
- const href = webURL(target);
- if (!href) continue;
- emphasis(el, text.slice(last, m.index));
if (!links) {
+ // An address is text, whatever it is: no URL check decides how
+ // it is drawn.
+ emphasis(el, text.slice(last, m.index));
el.append(text.slice(m.index, end));
last = end;
continue;
}
+ const href = webURL(target);
+ if (!href) continue;
+ emphasis(el, text.slice(last, m.index));
el.append(link(href, label));
last = end;
}
diff --git a/internal/web/ask_caps.go b/internal/web/ask_caps.go
index e5bc48d..c7b8b2a 100644
--- a/internal/web/ask_caps.go
+++ b/internal/web/ask_caps.go
@@ -55,6 +55,11 @@ func clientCaps(r *http.Request) (native bool, caps []string) {
// the last 30 days last called without. A read error withholds them all,
// and so does a client of theirs Base couldn't record (clientsDown).
func (a *app) askCaps(ctx context.Context, member int64) []string {
+ // Worked out under the member's lock, so no record of theirs is
+ // half made meanwhile.
+ m := a.memberLock(member)
+ m.Lock()
+ defer m.Unlock()
if a.clientsDown(member) {
return nil
}
@@ -90,7 +95,7 @@ func (a *app) askCaps(ctx context.Context, member int64) []string {
// the client's next request records it.
type clientsUnrecorded struct {
mu sync.Mutex
- held map[int64]map[string]bool
+ held map[int64]map[string]time.Time
// locks holds a *sync.Mutex per member number (recordClient).
locks sync.Map
}
@@ -111,9 +116,13 @@ func (a *app) recordClient(ctx context.Context, r *http.Request, member int64, s
return
}
client := sessions.HashToken(c.Value)
- m, _ := a.unrecorded.locks.LoadOrStore(member, &sync.Mutex{})
- m.(*sync.Mutex).Lock()
- defer m.(*sync.Mutex).Unlock()
+ m := a.memberLock(member)
+ m.Lock()
+ defer m.Unlock()
+ // A client going away mid-request doesn't stop its record (nor its
+ // undoing on a failure), which stays bounded.
+ ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), recordWait)
+ defer cancel()
if !recordsClient(r) {
if err := store.TouchAskClient(ctx, a.db, member, client); err != nil {
a.logger.Error("ask: see client", "err", err)
@@ -132,6 +141,16 @@ func (a *app) recordClient(ctx context.Context, r *http.Request, member int64, s
a.clientRecorded(member, client, true)
}
+// recordWait bounds recording one client's request.
+const recordWait = 5 * time.Second
+
+// memberLock is the member numbered member's lock: their clients are
+// recorded, and their capabilities worked out, one at a time.
+func (a *app) memberLock(member int64) *sync.Mutex {
+ m, _ := a.unrecorded.locks.LoadOrStore(member, &sync.Mutex{})
+ return m.(*sync.Mutex)
+}
+
// clientRecorded notes whether client's request was recorded for the
// member numbered member.
func (a *app) clientRecorded(member int64, client string, ok bool) {
@@ -146,12 +165,12 @@ func (a *app) clientRecorded(member int64, client string, ok bool) {
return
}
if u.held == nil {
- u.held = map[int64]map[string]bool{}
+ u.held = map[int64]map[string]time.Time{}
}
if u.held[member] == nil {
- u.held[member] = map[string]bool{}
+ u.held[member] = map[string]time.Time{}
}
- u.held[member][client] = true
+ u.held[member][client] = time.Now()
}
// clientsDown reports whether a native client of the member numbered
@@ -159,7 +178,14 @@ func (a *app) clientRecorded(member int64, client string, ok bool) {
func (a *app) clientsDown(member int64) bool {
a.unrecorded.mu.Lock()
defer a.unrecorded.mu.Unlock()
- return len(a.unrecorded.held[member]) > 0
+ for client, at := range a.unrecorded.held[member] {
+ // A hold lasts as long as a client's word does.
+ if time.Since(at) < clientWindow {
+ return true
+ }
+ delete(a.unrecorded.held[member], client)
+ }
+ return false
}
// recordsClient reports whether r is one of the two reads on which an
diff --git a/internal/web/ask_caps_internal_test.go b/internal/web/ask_caps_internal_test.go
new file mode 100644
index 0000000..c0ff8f4
--- /dev/null
+++ b/internal/web/ask_caps_internal_test.go
@@ -0,0 +1,44 @@
+package web
+
+import (
+ "context"
+ "log/slog"
+ "testing"
+ "time"
+)
+
+// Capabilities are worked out under the member's lock: never while one of
+// their clients is half recorded (Astra round 3).
+func TestAskCapsWaitsForRecord(t *testing.T) {
+ a := &app{logger: slog.New(slog.DiscardHandler)}
+ a.clientRecorded(7, "client", false)
+ m := a.memberLock(7)
+ m.Lock()
+ done := make(chan []string, 1)
+ go func() { done <- a.askCaps(context.Background(), 7) }()
+ select {
+ case <-done:
+ t.Fatal("askCaps didn't wait for the member's record")
+ case <-time.After(50 * time.Millisecond):
+ }
+ m.Unlock()
+ if got := <-done; got != nil {
+ t.Errorf("a held member's capabilities: %v", got)
+ }
+}
+
+// A hold on a client Base couldn't record lasts as long as a client's
+// word does, 30 days, and no longer (Astra round 3).
+func TestAskCapsHoldExpires(t *testing.T) {
+ a := &app{logger: slog.New(slog.DiscardHandler)}
+ a.clientRecorded(7, "client", false)
+ if !a.clientsDown(7) || a.clientsDown(8) {
+ t.Fatal("hold not kept for its member alone")
+ }
+ a.unrecorded.mu.Lock()
+ a.unrecorded.held[7]["client"] = time.Now().Add(-clientWindow - time.Minute)
+ a.unrecorded.mu.Unlock()
+ if a.clientsDown(7) {
+ t.Error("a hold outlived the client window")
+ }
+}
diff --git a/internal/web/ask_cards.go b/internal/web/ask_cards.go
index 528ae05..9bd0cf6 100644
--- a/internal/web/ask_cards.go
+++ b/internal/web/ask_cards.go
@@ -134,16 +134,19 @@ func inline(b *strings.Builder, text string, strict, links bool) {
target = strings.TrimRight(text[m[0]:m[1]], ".,;:!?")
end, label = m[0]+len(target), target
}
- href := equipo.WebURL(target)
- if href == "" {
- continue
- }
- emphasis(b, text[last:m[0]])
if !links {
+ // An address is text, whatever it is: no URL check decides
+ // how it is drawn.
+ emphasis(b, text[last:m[0]])
b.WriteString(html.EscapeString(text[m[0]:end]))
last = end
continue
}
+ href := equipo.WebURL(target)
+ if href == "" {
+ continue
+ }
+ emphasis(b, text[last:m[0]])
b.WriteString(`<a href="` + html.EscapeString(href) + `" target="_blank" rel="noopener">` + html.EscapeString(label) + `</a>`)
last = end
}
diff --git a/internal/web/testdata/lookup.json b/internal/web/testdata/lookup.json
index 50ad0cd..af8e8a8 100644
--- a/internal/web/testdata/lookup.json
+++ b/internal/web/testdata/lookup.json
@@ -34,5 +34,9 @@
{
"text": "Go https://a.test/x *now* or [z](https://c.test/ q)",
"html": "Go https://a.test/x *now* or [z](https://c.test/ q)"
+ },
+ {
+ "text": "See https://a.test/%zz/*x* ok and _y_",
+ "html": "See https://a.test/%zz/*x* ok and <em>y</em>"
}
]
diff --git a/native/apple/Sources/BaseKit/Ask/Markdown/PersonMarkdown.swift b/native/apple/Sources/BaseKit/Ask/Markdown/PersonMarkdown.swift
index 0a0ba7a..ee128e3 100644
--- a/native/apple/Sources/BaseKit/Ask/Markdown/PersonMarkdown.swift
+++ b/native/apple/Sources/BaseKit/Ask/Markdown/PersonMarkdown.swift
@@ -91,14 +91,16 @@ enum PersonMarkdown {
while let c = bare.last, ".,;:!?".contains(c) { bare.removeLast() }
target = bare; label = bare; end = m.range.location + (bare as NSString).length
}
- guard ChatMarkdown.allowsLink(target) else { continue }
- out += emphasis(ns.substring(with: NSRange(location: last, length: m.range.location - last)))
guard links else {
- // An address as text, untouched by emphasis.
+ // An address is text, whatever it is, untouched by
+ // emphasis: no URL check decides how it is drawn.
+ out += emphasis(ns.substring(with: NSRange(location: last, length: m.range.location - last)))
out.append(node("text", text: ns.substring(with: NSRange(location: m.range.location, length: end - m.range.location))))
last = end
continue
}
+ guard ChatMarkdown.allowsLink(target) else { continue }
+ out += emphasis(ns.substring(with: NSRange(location: last, length: m.range.location - last)))
out.append(node("link", target: target, children: [node("text", text: label)]))
last = end
}