feat: add prompt history plugin Host prerequisites (#3588)
Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
@@ -214,7 +214,7 @@ jobs:
|
||||
# (the build job has the Go toolchain) and shipped to the shards as its
|
||||
# own artifact; global-setup fails fast if it is missing.
|
||||
- name: Build E2E fixture plugin package
|
||||
run: make -C apps/backend e2e-plugin-package
|
||||
run: make -C apps/backend e2e-plugin-ui e2e-plugin-package
|
||||
|
||||
# Linux/amd64 helper binaries (agentctl + mock-agent) bind-mounted into
|
||||
# containers by the Docker E2E project. Cheap to build (~1s each) so we
|
||||
@@ -393,7 +393,9 @@ jobs:
|
||||
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
|
||||
with:
|
||||
name: e2e-plugin-package
|
||||
path: apps/backend/.build/kandev-plugin-e2e-1.0.0.tar.gz
|
||||
path: |
|
||||
apps/backend/.build/kandev-plugin-e2e-1.0.0.tar.gz
|
||||
apps/backend/.build/e2e-plugin-identity.json
|
||||
retention-days: 1
|
||||
|
||||
# Tar the Vite dist directory so shards can reuse the same static web build.
|
||||
|
||||
+13
-2
@@ -186,7 +186,7 @@ endif
|
||||
DEADCODE_VERSION ?= v0.48.0
|
||||
DEADCODE_GO_TOOLCHAIN ?= go1.26.0
|
||||
|
||||
.PHONY: all build build-dev build-runtime build-kandev build-all build-agentctl build-agentctl-dev build-agentctl-remote build-agentctl-linux build-agentctl-linux-arm64 build-agentctl-darwin-arm64 build-agentctl-darwin-amd64 build-acpdbg acpdbg build-mock-agent build-mock-agent-linux build-preview build-winjob e2e-plugin-package clean run dev start-debug test test-e2e test-sprites-e2e test-lifecycle-goleak lint deadcode fmt vet sqlguard check-make-shells help
|
||||
.PHONY: all build build-dev build-runtime build-kandev build-all build-agentctl build-agentctl-dev build-agentctl-remote build-agentctl-linux build-agentctl-linux-arm64 build-agentctl-darwin-arm64 build-agentctl-darwin-amd64 build-acpdbg acpdbg build-mock-agent build-mock-agent-linux build-preview build-winjob e2e-plugin-ui e2e-plugin-package clean run dev start-debug test test-e2e test-sprites-e2e test-lifecycle-goleak lint deadcode fmt vet sqlguard check-make-shells help
|
||||
|
||||
## Default target
|
||||
all: build
|
||||
@@ -311,11 +311,16 @@ build-mock-agent-linux:
|
||||
FIXTURE_PACKAGE_DIR := cmd/plugin-fixture/fixture-package
|
||||
FIXTURE_STAGE_DIR := .build/fixture-stage
|
||||
FIXTURE_PACKAGE_OUT := .build/kandev-plugin-e2e-1.0.0.tar.gz
|
||||
FIXTURE_IDENTITY_OUT := .build/e2e-plugin-identity.json
|
||||
HOST_GOOS := $(shell $(GO) env GOOS)
|
||||
HOST_GOARCH := $(shell $(GO) env GOARCH)
|
||||
|
||||
.PHONY: e2e-plugin-ui
|
||||
e2e-plugin-ui:
|
||||
cd ../web && pnpm run build:e2e-plugin -- --outDir ../../apps/backend/$(FIXTURE_PACKAGE_DIR)/ui
|
||||
|
||||
.PHONY: e2e-plugin-package
|
||||
e2e-plugin-package:
|
||||
e2e-plugin-package: e2e-plugin-ui
|
||||
@echo "Packaging plugin-fixture for e2e ($(HOST_GOOS)-$(HOST_GOARCH))..."
|
||||
@$(RMDIR) $(FIXTURE_STAGE_DIR)
|
||||
@$(call MKDIR,$(FIXTURE_STAGE_DIR)/server)
|
||||
@@ -323,6 +328,12 @@ e2e-plugin-package:
|
||||
cp -r $(FIXTURE_PACKAGE_DIR)/ui $(FIXTURE_STAGE_DIR)/ui
|
||||
$(GO) build $(GOFLAGS) -o $(FIXTURE_STAGE_DIR)/server/plugin-$(HOST_GOOS)-$(HOST_GOARCH)$(EXE) ./cmd/plugin-fixture
|
||||
$(GO) run ./cmd/plugin-pack -dir $(FIXTURE_STAGE_DIR) -out $(FIXTURE_PACKAGE_OUT) -platform-only
|
||||
cd ../web && node scripts/write-e2e-plugin-identity.mjs \
|
||||
e2e/fixtures/plugins/prompt-history-plugin/bundle.js \
|
||||
../backend/cmd/plugin-fixture/fixture-package/ui/bundle.js \
|
||||
../backend/cmd/plugin-fixture/fixture-package/manifest.yaml \
|
||||
../backend/$(FIXTURE_PACKAGE_OUT) \
|
||||
../backend/$(FIXTURE_IDENTITY_OUT)
|
||||
@$(RMDIR) $(FIXTURE_STAGE_DIR)
|
||||
@echo "Wrote $(FIXTURE_PACKAGE_OUT)"
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
# cmd/plugin-pack into apps/backend/.build/kandev-plugin-e2e-1.0.0.tar.gz.
|
||||
id: "kandev-plugin-e2e"
|
||||
api_version: 2
|
||||
min_kandev_version: "0.91.1"
|
||||
version: "1.0.0"
|
||||
display_name: "Kandev E2E Fixture Plugin"
|
||||
description: "SDK-based plugin backend used by backend integration tests and Playwright e2e tests"
|
||||
@@ -23,6 +24,7 @@ runtime:
|
||||
windows-amd64: "server/plugin-windows-amd64.exe"
|
||||
|
||||
capabilities:
|
||||
api_read: ["messages"]
|
||||
events: ["task.created"]
|
||||
api_write: ["tasks"]
|
||||
agent_invoke: true
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
* a tiny listener set, so it survives across route navigations (the page
|
||||
* component unmounts/remounts as the user navigates away and back).
|
||||
*/
|
||||
/* eslint-disable max-lines, max-lines-per-function, sonarjs/no-duplicate-string -- The fixture is the literal self-contained, dependency-free browser bundle exercised and hashed by E2E. */
|
||||
(function () {
|
||||
var moduleCount = 0;
|
||||
var listeners = new Set();
|
||||
@@ -368,6 +369,179 @@
|
||||
});
|
||||
}
|
||||
|
||||
function promptDuration(startedAt, completedAt, translated) {
|
||||
var started = Date.parse(startedAt);
|
||||
var completed = Date.parse(completedAt);
|
||||
if (Number.isNaN(started) || Number.isNaN(completed)) return "";
|
||||
var seconds = Math.max(0, Math.floor((completed - started) / 1000));
|
||||
var hours = Math.floor(seconds / 3600);
|
||||
var minutes = Math.floor((seconds % 3600) / 60);
|
||||
var remaining = seconds % 60;
|
||||
if (hours > 0) {
|
||||
return (
|
||||
hours +
|
||||
translated.t("durationHours") +
|
||||
" " +
|
||||
minutes +
|
||||
translated.t("durationMinutes") +
|
||||
" " +
|
||||
remaining +
|
||||
translated.t("durationSeconds")
|
||||
);
|
||||
}
|
||||
if (minutes > 0) {
|
||||
return (
|
||||
minutes +
|
||||
translated.t("durationMinutes") +
|
||||
" " +
|
||||
remaining +
|
||||
translated.t("durationSeconds")
|
||||
);
|
||||
}
|
||||
return remaining + translated.t("durationSeconds");
|
||||
}
|
||||
|
||||
function PromptHistoryPluginRow(props) {
|
||||
var message = props.message;
|
||||
var turn = props.turn;
|
||||
var translated = props.translated;
|
||||
var duration =
|
||||
turn && turn.completedAt
|
||||
? promptDuration(turn.startedAt, turn.completedAt, translated)
|
||||
: "";
|
||||
var favorite = props.conversation.history.useMessageFavorite(props.sessionId, message.id);
|
||||
var isExpanded = props.expanded === message.id;
|
||||
return jsx(
|
||||
"article",
|
||||
{
|
||||
"data-testid": "fixture-prompt-history-row",
|
||||
"data-message-id": message.id,
|
||||
"data-favorite": favorite ? "true" : "false",
|
||||
className: "border-b p-3",
|
||||
},
|
||||
jsx(
|
||||
"button",
|
||||
{
|
||||
type: "button",
|
||||
className: "min-h-11 w-full text-left",
|
||||
onClick: function () {
|
||||
props.setExpanded(isExpanded ? null : message.id);
|
||||
},
|
||||
},
|
||||
translated.t("prompt", { values: { index: message.promptIndex || 0 } }),
|
||||
message.senderTaskId ? " · " + translated.t("sentByAgent") : "",
|
||||
duration
|
||||
? jsx("span", { "data-testid": "fixture-prompt-history-duration" }, " · " + duration)
|
||||
: "",
|
||||
),
|
||||
isExpanded
|
||||
? jsx(
|
||||
"div",
|
||||
{ "data-testid": "fixture-prompt-history-detail" },
|
||||
jsx(ui.PromptMentionText, { text: message.content, interactive: true }),
|
||||
jsx(
|
||||
"button",
|
||||
{
|
||||
type: "button",
|
||||
className: "min-h-11",
|
||||
onClick: function () {
|
||||
props.conversation.openMessage(message.id);
|
||||
},
|
||||
},
|
||||
translated.t("openPrompt"),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
function PromptHistoryPluginPanel(props) {
|
||||
var translated = host.i18n.useTranslation();
|
||||
var messages = props.conversation.history.useSessionMessages({
|
||||
sessionId: props.sessionId,
|
||||
taskId: props.taskId,
|
||||
authorTypes: ["user"],
|
||||
sort: "desc",
|
||||
pageSize: 2,
|
||||
});
|
||||
var turns = props.conversation.history.useSessionTurns(props.sessionId, props.taskId);
|
||||
var expandedState = React.useState(null);
|
||||
var expanded = expandedState[0];
|
||||
var setExpanded = expandedState[1];
|
||||
|
||||
if (messages.loading || turns.loading) {
|
||||
return jsx(
|
||||
"div",
|
||||
{ "data-testid": "fixture-prompt-history-loading" },
|
||||
translated.t("loading"),
|
||||
);
|
||||
}
|
||||
if (messages.error || turns.error) {
|
||||
return jsx(
|
||||
"button",
|
||||
{
|
||||
type: "button",
|
||||
"data-testid": "fixture-prompt-history-error",
|
||||
onClick: function () {
|
||||
messages.retry();
|
||||
turns.retry();
|
||||
},
|
||||
},
|
||||
translated.t("retry"),
|
||||
);
|
||||
}
|
||||
if (messages.messages.length === 0) {
|
||||
return jsx(
|
||||
"div",
|
||||
{ "data-testid": "fixture-prompt-history-empty" },
|
||||
translated.t("empty"),
|
||||
);
|
||||
}
|
||||
var rows = messages.messages.map(function (message) {
|
||||
var turn = turns.turns.find(function (candidate) {
|
||||
return candidate.id === message.turnId;
|
||||
});
|
||||
return jsx(PromptHistoryPluginRow, {
|
||||
key: message.id,
|
||||
message: message,
|
||||
turn: turn,
|
||||
translated: translated,
|
||||
conversation: props.conversation,
|
||||
sessionId: props.sessionId,
|
||||
expanded: expanded,
|
||||
setExpanded: setExpanded,
|
||||
});
|
||||
});
|
||||
if (messages.hasMore) {
|
||||
rows.push(
|
||||
jsx(
|
||||
"button",
|
||||
{
|
||||
key: "load-more",
|
||||
type: "button",
|
||||
className: "min-h-11 w-full",
|
||||
"data-testid": "fixture-prompt-history-load-more",
|
||||
disabled: messages.loadingMore,
|
||||
onClick: function () {
|
||||
messages.loadMore();
|
||||
},
|
||||
},
|
||||
translated.t("loadOlder"),
|
||||
),
|
||||
);
|
||||
}
|
||||
return jsx(
|
||||
"div",
|
||||
{
|
||||
"data-testid": "fixture-prompt-history-panel",
|
||||
"data-removed": messages.removed ? "true" : "false",
|
||||
"data-presentation": props.presentation,
|
||||
className: "h-full min-w-0 overflow-y-auto overflow-x-hidden",
|
||||
},
|
||||
rows,
|
||||
);
|
||||
}
|
||||
|
||||
function CardIndicator(props) {
|
||||
var slotProps = props.slotProps || {};
|
||||
return jsx(
|
||||
@@ -691,6 +865,46 @@
|
||||
Selector: FixtureReviewSelector,
|
||||
});
|
||||
|
||||
registry.registerTranslations({
|
||||
en: {
|
||||
promptHistoryTitle: "Prompt history fixture",
|
||||
loading: "Loading prompt history",
|
||||
retry: "Retry",
|
||||
empty: "No prompts yet",
|
||||
prompt: "Prompt {{index}}",
|
||||
sentByAgent: "Sent by agent",
|
||||
openPrompt: "Open prompt",
|
||||
loadOlder: "Load older prompts",
|
||||
durationSeconds: "s",
|
||||
durationMinutes: "m",
|
||||
durationHours: "h",
|
||||
},
|
||||
pseudo: {
|
||||
promptHistoryTitle: "Ƥřǿɱƥŧ ħīşŧǿřẏ ƒīẋŧŭřḗ",
|
||||
loading: "Ŀǿȧḓīƞɠ ƥřǿɱƥŧ ħīşŧǿřẏ",
|
||||
retry: "Řḗŧřẏ",
|
||||
empty: "Ƞǿ ƥřǿɱƥŧş ẏḗŧ",
|
||||
prompt: "Ƥřǿɱƥŧ {{index}}",
|
||||
sentByAgent: "Şḗƞŧ ƀẏ ȧɠḗƞŧ",
|
||||
openPrompt: "Ǿƥḗƞ ƥřǿɱƥŧ",
|
||||
loadOlder: "Ŀǿȧḓ ǿŀḓḗř ƥřǿɱƥŧş",
|
||||
durationSeconds: "ş",
|
||||
durationMinutes: "ɱ",
|
||||
durationHours: "ħ",
|
||||
},
|
||||
});
|
||||
registry.registerTaskPanel({
|
||||
id: "prompt-history-plugin",
|
||||
title: "Prompt history fixture",
|
||||
titleKey: "promptHistoryTitle",
|
||||
icon: "history",
|
||||
Component: PromptHistoryPluginPanel,
|
||||
mobileEnabled: true,
|
||||
visible: function (context) {
|
||||
return context.sessionKind === "managed";
|
||||
},
|
||||
});
|
||||
|
||||
registry.registerTaskPanel({
|
||||
id: "notes",
|
||||
title: "Notes",
|
||||
|
||||
@@ -1518,13 +1518,24 @@ func registerSecondaryRoutes(
|
||||
}
|
||||
|
||||
if p.services.Plugins != nil {
|
||||
p.gateway.SetPluginConversationService(p.services.Plugins)
|
||||
if p.authSvc != nil {
|
||||
// Lets an auth-capable plugin complete OIDC/SAML SSO: it asserts a
|
||||
// validated external identity on its webhook response and the host
|
||||
// mints + sets the session cookie (the plugin never sees the token).
|
||||
p.services.Plugins.SetAuthLoginBridge(pluginSSOBridge{auth: p.authSvc})
|
||||
}
|
||||
plugins.RegisterRoutes(p.router, p.services.Plugins, p.services.Plugins.Deliverer(), p.log)
|
||||
conversationReaders := make([]plugins.ConversationReader, 0, 1)
|
||||
if p.services.Task != nil {
|
||||
conversationReaders = append(conversationReaders, p.services.Task)
|
||||
}
|
||||
plugins.RegisterRoutes(
|
||||
p.router,
|
||||
p.services.Plugins,
|
||||
p.services.Plugins.Deliverer(),
|
||||
p.log,
|
||||
conversationReaders...,
|
||||
)
|
||||
if p.features.Canvases {
|
||||
plugins.RegisterWebAppRuntimeRoutes(p.router, p.services.Plugins.WebRuntime())
|
||||
registerCanvasRoutes(p)
|
||||
|
||||
@@ -419,6 +419,9 @@ func startServices( //nolint:cyclop
|
||||
log.Error("Failed to initialize services", zap.Error(err))
|
||||
return false
|
||||
}
|
||||
if services.PluginsCleanup != nil {
|
||||
addCleanup(services.PluginsCleanup)
|
||||
}
|
||||
agentRegistry.SetManagedRuntimeSelectionStore(services.ManagedRuntimeSelections)
|
||||
if services.Workflow != nil {
|
||||
addCleanup(services.Workflow.Close)
|
||||
|
||||
@@ -96,7 +96,10 @@ func newTestPluginsService(t *testing.T) *plugins.Service {
|
||||
t.Fatalf("load registry: %v", err)
|
||||
}
|
||||
svc := plugins.NewService(fsStore, registry, nil, testPluginsLogger(t))
|
||||
svc.SetPluginsDir(dir)
|
||||
if err := svc.SetPluginsDir(dir); err != nil {
|
||||
t.Fatalf("SetPluginsDir: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = svc.Close() })
|
||||
svc.SetRuntime(alwaysUpRuntime{})
|
||||
return svc
|
||||
}
|
||||
|
||||
@@ -134,6 +134,9 @@ func provideTestServices(t *testing.T, version string) (*Services, *config.Confi
|
||||
if err != nil {
|
||||
t.Fatalf("provideServices: %v", err)
|
||||
}
|
||||
if services.PluginsCleanup != nil {
|
||||
t.Cleanup(func() { _ = services.PluginsCleanup() })
|
||||
}
|
||||
if services.Workflow != nil {
|
||||
t.Cleanup(func() { _ = services.Workflow.Close() })
|
||||
}
|
||||
|
||||
@@ -87,6 +87,9 @@ func TestPostgresBootInitializesRepositories(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("provide services with postgres: %v", err)
|
||||
}
|
||||
if services.PluginsCleanup != nil {
|
||||
t.Cleanup(func() { _ = services.PluginsCleanup() })
|
||||
}
|
||||
if services.GitHub == nil {
|
||||
t.Fatal("GitHub service is unavailable after PostgreSQL boot")
|
||||
}
|
||||
|
||||
@@ -296,8 +296,11 @@ func provideServices(cfg *config.Config, log *logger.Logger, repos *Repositories
|
||||
if recordErr := recordRequiredStore(storeTracker, "workflow-sync", workflowSyncErr); recordErr != nil {
|
||||
return nil, nil, fmt.Errorf("initialize workflow sync: %w", recordErr)
|
||||
}
|
||||
pluginsSvc, _, pluginStoreErrors := initPluginsServiceRequired(cfg, dbPool, eventBus, repos.Secrets, log)
|
||||
pluginsSvc, pluginsCleanup, pluginStoreErrors := initPluginsServiceRequired(cfg, dbPool, eventBus, repos.Secrets, log)
|
||||
if recordErr := recordPluginStores(storeTracker, pluginStoreErrors); recordErr != nil {
|
||||
if pluginsCleanup != nil {
|
||||
_ = pluginsCleanup()
|
||||
}
|
||||
return nil, nil, fmt.Errorf("initialize plugins: %w", recordErr)
|
||||
}
|
||||
if pluginsSvc != nil {
|
||||
@@ -434,6 +437,7 @@ func provideServices(cfg *config.Config, log *logger.Logger, repos *Repositories
|
||||
Share: shareHTTP,
|
||||
Automation: automationComponents,
|
||||
Plugins: pluginsSvc,
|
||||
PluginsCleanup: pluginsCleanup,
|
||||
Canvas: canvasSvc,
|
||||
CanvasDistribution: canvasDistributionSvc,
|
||||
GitCredentials: gitCredentialBroker,
|
||||
|
||||
@@ -131,7 +131,8 @@ type Services struct {
|
||||
// Plugins is the extensible plugin system service (registration
|
||||
// registry, event delivery, health monitoring). Always constructed
|
||||
// (non-nil) when initialization succeeds.
|
||||
Plugins *plugins.Service
|
||||
Plugins *plugins.Service
|
||||
PluginsCleanup func() error
|
||||
// Canvas is the gated lifecycle service for agent-authored plugin web
|
||||
// applications. It is nil while features.canvases is disabled.
|
||||
Canvas *canvasservice.Service
|
||||
|
||||
@@ -74,6 +74,17 @@ func DateTimeOf(driver, expr string) string {
|
||||
return fmt.Sprintf("datetime(%s)", expr)
|
||||
}
|
||||
|
||||
// RFC3339Millis renders an ISO-8601 timestamp with millisecond precision.
|
||||
//
|
||||
// SQLite: strftime('%Y-%m-%dT%H:%M:%fZ', expr)
|
||||
// Postgres: to_char(expr, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')
|
||||
func RFC3339Millis(driver, expr string) string {
|
||||
if IsPostgres(driver) {
|
||||
return fmt.Sprintf(`to_char(%s, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')`, expr)
|
||||
}
|
||||
return fmt.Sprintf(`strftime('%%Y-%%m-%%dT%%H:%%M:%%fZ', %s)`, expr)
|
||||
}
|
||||
|
||||
// NaiveUTCTimestampOf normalizes a naive timestamp expression — one with NO
|
||||
// embedded zone information, but KNOWN to always hold UTC wall-clock values
|
||||
// (every `TIMESTAMP`-typed column in this codebase, written via
|
||||
|
||||
@@ -76,6 +76,7 @@ const (
|
||||
MessageAdded = "message.added"
|
||||
MessageUpdated = "message.updated"
|
||||
MessageDeleted = "message.deleted"
|
||||
SessionRemoved = "session.removed"
|
||||
)
|
||||
|
||||
// Event types for message queue
|
||||
@@ -142,6 +143,7 @@ const (
|
||||
const (
|
||||
TurnStarted = "turn.started"
|
||||
TurnCompleted = "turn.completed"
|
||||
TurnRemoved = "turn.removed"
|
||||
)
|
||||
|
||||
// Event types for repositories
|
||||
|
||||
@@ -3,13 +3,16 @@ package websocket
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/kandev/kandev/internal/auth/authn"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/plugins"
|
||||
"github.com/kandev/kandev/internal/user/store"
|
||||
ws "github.com/kandev/kandev/pkg/websocket"
|
||||
"go.uber.org/zap"
|
||||
@@ -32,7 +35,14 @@ const (
|
||||
// Control frames (RPC responses and errors) use a separate bounded queue so
|
||||
// high-volume session notifications cannot fill the queue and make a user
|
||||
// action appear to time out.
|
||||
controlSendBufferSize = 256
|
||||
controlSendBufferSize = 256
|
||||
orderedConsumerPlugin = "plugin"
|
||||
orderedConsumerCore = "core"
|
||||
responseErrorKey = "error"
|
||||
sessionIDPayloadKey = "session_id"
|
||||
eventTypePayloadKey = "type"
|
||||
eventSequencePayloadKey = "sequence"
|
||||
taskIDPayloadKey = "task_id"
|
||||
)
|
||||
|
||||
// Client represents a single WebSocket connection
|
||||
@@ -42,20 +52,21 @@ type Client struct {
|
||||
// anonymous connections (auth disabled and no synthetic identity set by
|
||||
// the HTTP middleware — e.g. direct hub tests); synthetic in disabled
|
||||
// mode; a real user when auth is enabled.
|
||||
identity authn.Identity
|
||||
conn *websocket.Conn
|
||||
hub *Hub
|
||||
send chan []byte
|
||||
controlSend chan []byte
|
||||
subscriptions map[string]bool // Task IDs this client is subscribed to
|
||||
sessionSubscriptions map[string]bool // Session IDs this client is subscribed to
|
||||
sessionFocus map[string]bool // Session IDs this client has focused (a strict subset of subscriptions, conceptually — see hub_session_mode.go)
|
||||
userSubscriptions map[string]bool // User IDs this client is subscribed to
|
||||
runSubscriptions map[string]bool // Office run IDs this client is subscribed to (for run.event.appended)
|
||||
systemMetricsSubscribed bool
|
||||
mu sync.RWMutex
|
||||
closed bool
|
||||
logger *logger.Logger
|
||||
identity authn.Identity
|
||||
conn *websocket.Conn
|
||||
hub *Hub
|
||||
send chan []byte
|
||||
controlSend chan []byte
|
||||
subscriptions map[string]bool // Task IDs this client is subscribed to
|
||||
sessionSubscriptions map[string]bool // Session IDs this client is subscribed to
|
||||
sessionFocus map[string]bool // Session IDs this client has focused (a strict subset of subscriptions, conceptually — see hub_session_mode.go)
|
||||
orderedSessionSubscriptions map[string]map[string]plugins.SessionDeliveryCursorKey
|
||||
userSubscriptions map[string]bool // User IDs this client is subscribed to
|
||||
runSubscriptions map[string]bool // Office run IDs this client is subscribed to (for run.event.appended)
|
||||
systemMetricsSubscribed bool
|
||||
mu sync.RWMutex
|
||||
closed bool
|
||||
logger *logger.Logger
|
||||
|
||||
// Replaceable session.message.updated traffic is scheduled separately from
|
||||
// semantic notifications so one noisy session cannot fill the shared FIFO.
|
||||
@@ -81,22 +92,23 @@ type Client struct {
|
||||
// NewClient creates a new WebSocket client
|
||||
func NewClient(id string, identity authn.Identity, conn *websocket.Conn, hub *Hub, log *logger.Logger) *Client {
|
||||
return &Client{
|
||||
ID: id,
|
||||
identity: identity,
|
||||
conn: conn,
|
||||
hub: hub,
|
||||
send: make(chan []byte, 256),
|
||||
controlSend: make(chan []byte, controlSendBufferSize),
|
||||
subscriptions: make(map[string]bool),
|
||||
sessionSubscriptions: make(map[string]bool),
|
||||
sessionFocus: make(map[string]bool),
|
||||
userSubscriptions: make(map[string]bool),
|
||||
runSubscriptions: make(map[string]bool),
|
||||
replaceableByKey: make(map[queuedReplaceableKey]outboundNotification),
|
||||
replaceableBySession: make(map[string][]sessionNotificationQueueItem),
|
||||
replaceableCurrentByKey: make(map[replaceableNotificationKey]queuedReplaceableKey),
|
||||
notificationWake: make(chan struct{}, 1),
|
||||
logger: log.WithFields(zap.String("client_id", id)),
|
||||
ID: id,
|
||||
identity: identity,
|
||||
conn: conn,
|
||||
hub: hub,
|
||||
send: make(chan []byte, 256),
|
||||
controlSend: make(chan []byte, controlSendBufferSize),
|
||||
subscriptions: make(map[string]bool),
|
||||
sessionSubscriptions: make(map[string]bool),
|
||||
sessionFocus: make(map[string]bool),
|
||||
orderedSessionSubscriptions: make(map[string]map[string]plugins.SessionDeliveryCursorKey),
|
||||
userSubscriptions: make(map[string]bool),
|
||||
runSubscriptions: make(map[string]bool),
|
||||
replaceableByKey: make(map[queuedReplaceableKey]outboundNotification),
|
||||
replaceableBySession: make(map[string][]sessionNotificationQueueItem),
|
||||
replaceableCurrentByKey: make(map[replaceableNotificationKey]queuedReplaceableKey),
|
||||
notificationWake: make(chan struct{}, 1),
|
||||
logger: log.WithFields(zap.String("client_id", id)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,6 +205,12 @@ func (c *Client) handleMessage(msg *ws.Message) {
|
||||
case ws.ActionSessionUnsubscribe:
|
||||
c.handleSessionUnsubscribe(msg)
|
||||
return
|
||||
case ws.ActionSessionAck:
|
||||
c.handleSessionAck(msg)
|
||||
return
|
||||
case ws.ActionSessionPoisonRequeue:
|
||||
c.handleSessionPoisonRequeue(msg)
|
||||
return
|
||||
case ws.ActionSessionFocus:
|
||||
c.handleSessionFocus(msg)
|
||||
return
|
||||
@@ -303,7 +321,16 @@ type UserSubscribeRequest struct {
|
||||
}
|
||||
|
||||
type SessionSubscribeRequest struct {
|
||||
SessionID string `json:"session_id"`
|
||||
SessionID string `json:"session_id"`
|
||||
ConsumerKind string `json:"consumer_kind,omitempty"`
|
||||
PluginID string `json:"plugin_id,omitempty"`
|
||||
Generation int64 `json:"generation,omitempty"`
|
||||
BindingToken string `json:"binding_token,omitempty"`
|
||||
LastSeenSequence *uint64 `json:"last_seen_sequence,omitempty"`
|
||||
ResumeToken string `json:"resume_token,omitempty"`
|
||||
ReplaceCursor bool `json:"replace_cursor,omitempty"`
|
||||
ConsumerID string `json:"consumer_id,omitempty"`
|
||||
WireID string `json:"wire_id,omitempty"`
|
||||
}
|
||||
|
||||
// ownUserTopic resolves the user-topic this client may subscribe to: its own
|
||||
@@ -351,6 +378,10 @@ func (c *Client) handleSessionSubscribe(msg *ws.Message) {
|
||||
c.sendError(msg.ID, msg.Action, ws.ErrorCodeValidation, "session_id is required", nil)
|
||||
return
|
||||
}
|
||||
if req.ConsumerKind != "" {
|
||||
c.handleOrderedSessionSubscribe(msg, req)
|
||||
return
|
||||
}
|
||||
|
||||
if !c.maySubscribeSession(msg, req.SessionID) {
|
||||
return
|
||||
@@ -370,6 +401,500 @@ func (c *Client) handleSessionSubscribe(msg *ws.Message) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) handleOrderedSessionSubscribe(msg *ws.Message, req SessionSubscribeRequest) {
|
||||
service := c.hub.pluginConversationService
|
||||
if service == nil {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "upstream_failure", "session stream unavailable", true)
|
||||
return
|
||||
}
|
||||
userID := c.ownUserTopic()
|
||||
if req.ConsumerKind == orderedConsumerPlugin {
|
||||
lastSeen := uint64(0)
|
||||
if req.LastSeenSequence != nil {
|
||||
lastSeen = *req.LastSeenSequence
|
||||
}
|
||||
candidate := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: req.SessionID, ConsumerKind: req.ConsumerKind, ConsumerID: req.ConsumerID,
|
||||
WireID: req.WireID, PluginID: req.PluginID, Generation: req.Generation, UserID: userID,
|
||||
}
|
||||
if req.ResumeToken == "" || service.ValidateSessionResume(req.ResumeToken, candidate, lastSeen) != nil {
|
||||
req.ConsumerID = uuid.NewString()
|
||||
}
|
||||
}
|
||||
key := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: req.SessionID, ConsumerKind: req.ConsumerKind,
|
||||
ConsumerID: req.ConsumerID, WireID: req.WireID, PluginID: req.PluginID,
|
||||
Generation: req.Generation, UserID: userID,
|
||||
}
|
||||
if !c.authorizeOrderedSessionConsumer(msg, req, service, userID) {
|
||||
return
|
||||
}
|
||||
if !c.maySubscribeOrderedSession(msg, req, key, service) {
|
||||
return
|
||||
}
|
||||
c.hub.orderedSessionMu.Lock()
|
||||
defer c.hub.orderedSessionMu.Unlock()
|
||||
committedEvents, err := service.SyncCommittedSessionEvents(context.Background(), req.SessionID)
|
||||
if err != nil {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "upstream_failure", "session stream unavailable", true)
|
||||
return
|
||||
}
|
||||
for _, event := range committedEvents {
|
||||
c.hub.broadcastCommittedOrderedSessionEvent(service, event)
|
||||
}
|
||||
replay := resolveOrderedSessionReplay(service, req, key)
|
||||
if replay.terminal && (replay.resumeValid || service.SessionEvents().HasCursor(key)) {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "session_removed", "session was removed", false)
|
||||
return
|
||||
}
|
||||
c.acceptOrderedSessionSubscription(msg, req, key, userID, replay)
|
||||
}
|
||||
|
||||
type orderedSessionReplay struct {
|
||||
events []plugins.SessionEvent
|
||||
watermark uint64
|
||||
cursorSequence uint64
|
||||
result string
|
||||
resumeValid bool
|
||||
terminal bool
|
||||
}
|
||||
|
||||
//nolint:goconst // Result values are protocol literals.
|
||||
func resolveOrderedSessionReplay(
|
||||
service *plugins.Service,
|
||||
req SessionSubscribeRequest,
|
||||
key plugins.SessionDeliveryCursorKey,
|
||||
) orderedSessionReplay {
|
||||
lastSeen := uint64(0)
|
||||
lastSeenSupplied := req.LastSeenSequence != nil
|
||||
if lastSeenSupplied {
|
||||
lastSeen = *req.LastSeenSequence
|
||||
}
|
||||
events, watermark, terminal := service.SessionEvents().ReplayState(req.SessionID, lastSeen)
|
||||
// A retained replay that starts past lastSeen+1 - or has no retained rows
|
||||
// at all while the partition watermark is still ahead of the cursor -
|
||||
// means intermediate rows aged out on both sides; replaying it as if
|
||||
// contiguous would hide lost history. Rebind to the watermark instead
|
||||
// (same replacement-cursor machinery as invalid_resume) so the client
|
||||
// reconciles from the authoritative snapshot boundary.
|
||||
retainedGap := lastSeenSupplied &&
|
||||
lastSeen < watermark &&
|
||||
(len(events) == 0 || events[0].Sequence > lastSeen+1)
|
||||
if retainedGap {
|
||||
return orderedSessionReplay{
|
||||
watermark: watermark, cursorSequence: watermark,
|
||||
result: "invalid_resume", terminal: terminal,
|
||||
}
|
||||
}
|
||||
if req.ResumeToken != "" {
|
||||
if err := service.ValidateSessionResume(req.ResumeToken, key, lastSeen); err != nil {
|
||||
return orderedSessionReplay{
|
||||
watermark: watermark, cursorSequence: watermark,
|
||||
result: "invalid_resume", terminal: terminal,
|
||||
}
|
||||
}
|
||||
result := "fresh"
|
||||
if lastSeenSupplied && len(events) > 0 {
|
||||
result = "replay"
|
||||
}
|
||||
return orderedSessionReplay{
|
||||
events: events, watermark: watermark, cursorSequence: lastSeen,
|
||||
result: result, resumeValid: true, terminal: terminal,
|
||||
}
|
||||
}
|
||||
if !lastSeenSupplied {
|
||||
return orderedSessionReplay{
|
||||
watermark: watermark, cursorSequence: watermark,
|
||||
result: "fresh", terminal: terminal,
|
||||
}
|
||||
}
|
||||
result := "fresh"
|
||||
if len(events) > 0 {
|
||||
result = "replay"
|
||||
}
|
||||
return orderedSessionReplay{
|
||||
events: events, watermark: watermark, cursorSequence: lastSeen,
|
||||
result: result, terminal: terminal,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) acceptOrderedSessionSubscription(
|
||||
msg *ws.Message,
|
||||
req SessionSubscribeRequest,
|
||||
key plugins.SessionDeliveryCursorKey,
|
||||
userID string,
|
||||
replay orderedSessionReplay,
|
||||
) bool {
|
||||
service := c.hub.pluginConversationService
|
||||
replay, claims, err := prepareOrderedReplayDelivery(service, replay, time.Now().UTC())
|
||||
if err != nil {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "upstream_failure", "cannot claim session event delivery", true)
|
||||
return false
|
||||
}
|
||||
cursorSequence := replay.cursorSequence
|
||||
if req.ReplaceCursor || replay.result == "invalid_resume" {
|
||||
cursorSequence = replay.watermark
|
||||
}
|
||||
snapshotToken, resumeToken, expiresAt, err := service.MintSessionStreamGrant(
|
||||
req.PluginID,
|
||||
userID,
|
||||
req.Generation,
|
||||
req.SessionID,
|
||||
req.ConsumerID,
|
||||
req.WireID,
|
||||
replay.watermark,
|
||||
cursorSequence,
|
||||
)
|
||||
if err != nil {
|
||||
c.releaseOrderedReplayClaims(service, claims)
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_binding", "cannot mint session stream grant", true)
|
||||
return false
|
||||
}
|
||||
var cursorErr error
|
||||
if req.ReplaceCursor || replay.result == "invalid_resume" {
|
||||
cursorErr = service.SessionEvents().ReplaceCursor(key, cursorSequence)
|
||||
} else {
|
||||
cursorErr = service.SessionEvents().RegisterCursor(key, cursorSequence)
|
||||
}
|
||||
if cursorErr != nil {
|
||||
c.releaseOrderedReplayClaims(service, claims)
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "upstream_failure", "cannot register session cursor", true)
|
||||
return false
|
||||
}
|
||||
payload := orderedSessionSubscribePayload(req, replay, snapshotToken, resumeToken, expiresAt)
|
||||
c.storeOrderedSessionSubscription(req, key)
|
||||
response, _ := ws.NewResponse(msg.ID, msg.Action, payload)
|
||||
c.sendMessage(response)
|
||||
for _, event := range replay.events {
|
||||
queued := c.sendOrderedSessionEvent(event)
|
||||
if claim, ok := claims[event.ID]; ok {
|
||||
if err := service.SessionDelivery().Complete(claim, queued, time.Now().UTC()); err != nil && c.logger != nil {
|
||||
c.logger.Error("complete ordered replay poison", zap.String("event_id", event.ID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Client) releaseOrderedReplayClaims(
|
||||
service *plugins.Service,
|
||||
claims map[string]plugins.SessionDeliveryClaim,
|
||||
) {
|
||||
if err := releaseOrderedReplayClaims(service, claims); err != nil && c.logger != nil {
|
||||
c.logger.Error("release ordered replay claims", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
func prepareOrderedReplayDelivery(
|
||||
service *plugins.Service,
|
||||
replay orderedSessionReplay,
|
||||
now time.Time,
|
||||
) (orderedSessionReplay, map[string]plugins.SessionDeliveryClaim, error) {
|
||||
claims := make(map[string]plugins.SessionDeliveryClaim)
|
||||
for _, event := range replay.events {
|
||||
claim, err := service.SessionDelivery().Claim(event.SessionID, event.ID, now)
|
||||
if err != nil {
|
||||
releaseErr := releaseOrderedReplayClaims(service, claims)
|
||||
return replay, nil, errors.Join(err, releaseErr)
|
||||
}
|
||||
switch claim.Disposition {
|
||||
case plugins.SessionDeliveryUntracked:
|
||||
case plugins.SessionDeliveryClaimed:
|
||||
claims[event.ID] = claim
|
||||
default:
|
||||
if err := releaseOrderedReplayClaims(service, claims); err != nil {
|
||||
return replay, nil, err
|
||||
}
|
||||
replay.events = nil
|
||||
replay.result = "invalid_resume"
|
||||
replay.cursorSequence = replay.watermark
|
||||
return replay, nil, nil
|
||||
}
|
||||
}
|
||||
return replay, claims, nil
|
||||
}
|
||||
|
||||
func releaseOrderedReplayClaims(
|
||||
service *plugins.Service,
|
||||
claims map[string]plugins.SessionDeliveryClaim,
|
||||
) error {
|
||||
now := time.Now().UTC()
|
||||
var result error
|
||||
for _, claim := range claims {
|
||||
result = errors.Join(result, service.SessionDelivery().Complete(claim, false, now))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func orderedSessionSubscribePayload(
|
||||
req SessionSubscribeRequest,
|
||||
replay orderedSessionReplay,
|
||||
snapshotToken string,
|
||||
resumeToken string,
|
||||
expiresAt time.Time,
|
||||
) map[string]any {
|
||||
payload := map[string]any{
|
||||
"success": true, sessionIDPayloadKey: req.SessionID, "result": replay.result,
|
||||
"event_watermark": replay.watermark, "snapshot_cutoff": replay.watermark,
|
||||
"snapshot_token": snapshotToken, "resume_token": resumeToken, "expires_at": expiresAt,
|
||||
}
|
||||
if req.ConsumerKind == orderedConsumerPlugin {
|
||||
payload["consumer_id"] = req.ConsumerID
|
||||
} else {
|
||||
payload["wire_id"] = req.WireID
|
||||
}
|
||||
if replay.result == "replay" && len(replay.events) > 0 {
|
||||
payload["replay_from"] = replay.events[0].Sequence
|
||||
payload["replay_to"] = replay.events[len(replay.events)-1].Sequence
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func (c *Client) storeOrderedSessionSubscription(
|
||||
req SessionSubscribeRequest,
|
||||
key plugins.SessionDeliveryCursorKey,
|
||||
) {
|
||||
subscriptionID := req.ConsumerID
|
||||
if subscriptionID == "" {
|
||||
subscriptionID = req.WireID
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
byConsumer := c.orderedSessionSubscriptions[req.SessionID]
|
||||
if byConsumer == nil {
|
||||
byConsumer = make(map[string]plugins.SessionDeliveryCursorKey)
|
||||
c.orderedSessionSubscriptions[req.SessionID] = byConsumer
|
||||
}
|
||||
byConsumer[subscriptionID] = key
|
||||
}
|
||||
|
||||
func (c *Client) authorizeOrderedSessionConsumer(
|
||||
msg *ws.Message,
|
||||
req SessionSubscribeRequest,
|
||||
service *plugins.Service,
|
||||
userID string,
|
||||
) bool {
|
||||
switch req.ConsumerKind {
|
||||
case orderedConsumerPlugin:
|
||||
return c.authorizeOrderedPluginConsumer(msg, req, service, userID)
|
||||
case orderedConsumerCore:
|
||||
if req.WireID != "" && req.ConsumerID == "" && req.PluginID == "" &&
|
||||
req.Generation == 0 && req.BindingToken == "" {
|
||||
return true
|
||||
}
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_request", "invalid core consumer identity", false)
|
||||
default:
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_request", "unsupported consumer kind", false)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Client) authorizeOrderedPluginConsumer(
|
||||
msg *ws.Message,
|
||||
req SessionSubscribeRequest,
|
||||
service *plugins.Service,
|
||||
userID string,
|
||||
) bool {
|
||||
if req.ConsumerID == "" || req.WireID != "" || req.PluginID == "" || req.Generation == 0 {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_request", "invalid plugin consumer identity", false)
|
||||
return false
|
||||
}
|
||||
err := service.AuthorizeConversationConsumer(
|
||||
req.PluginID,
|
||||
userID,
|
||||
req.Generation,
|
||||
req.BindingToken,
|
||||
)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
code := "invalid_binding"
|
||||
retryable := false
|
||||
if errors.Is(err, plugins.ErrConversationGenerationSuperseded) {
|
||||
code = "generation_superseded"
|
||||
retryable = true
|
||||
}
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, code, "plugin conversation binding rejected", retryable)
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Client) sendSessionStreamFailure(
|
||||
msg *ws.Message,
|
||||
sessionID string,
|
||||
code string,
|
||||
message string,
|
||||
retryable bool,
|
||||
) {
|
||||
payload := map[string]any{
|
||||
"success": false,
|
||||
responseErrorKey: map[string]any{"code": code, "message": message, "retryable": retryable},
|
||||
}
|
||||
if sessionID != "" {
|
||||
payload["session_id"] = sessionID
|
||||
}
|
||||
response, _ := ws.NewResponse(msg.ID, msg.Action, payload)
|
||||
c.sendMessage(response)
|
||||
}
|
||||
|
||||
func (c *Client) sendOrderedSessionEvent(event plugins.SessionEvent) bool {
|
||||
frame, err := sessionEventFrame(event)
|
||||
if err != nil {
|
||||
c.closeSend()
|
||||
return false
|
||||
}
|
||||
if c.sendNotification(frame, "session.event") {
|
||||
return true
|
||||
}
|
||||
// An ordered frame that is not queued cannot be acknowledged safely. End
|
||||
// this connection so the client reconnects and replays from its cursor.
|
||||
c.closeSend()
|
||||
return false
|
||||
}
|
||||
|
||||
type SessionAckRequest struct {
|
||||
SessionID string `json:"session_id"`
|
||||
ConsumerKind string `json:"consumer_kind"`
|
||||
PluginID string `json:"plugin_id,omitempty"`
|
||||
Generation int64 `json:"generation,omitempty"`
|
||||
Sequence uint64 `json:"sequence"`
|
||||
ResumeToken string `json:"resume_token,omitempty"`
|
||||
ConsumerID string `json:"consumer_id,omitempty"`
|
||||
WireID string `json:"wire_id,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) handleSessionAck(msg *ws.Message) {
|
||||
var req SessionAckRequest
|
||||
if err := msg.ParsePayload(&req); err != nil {
|
||||
c.sendSessionStreamFailure(msg, "", "invalid_request", "invalid acknowledgement", false)
|
||||
return
|
||||
}
|
||||
service := c.hub.pluginConversationService
|
||||
identity, validIdentity := sessionAckSubscriptionID(req)
|
||||
if !validIdentity {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_request", "invalid session consumer identity", false)
|
||||
return
|
||||
}
|
||||
c.mu.RLock()
|
||||
key, ok := c.orderedSessionSubscriptions[req.SessionID][identity]
|
||||
c.mu.RUnlock()
|
||||
if service == nil || !ok || !sessionAckMatchesKey(req, key) {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_request", "unknown session consumer", false)
|
||||
return
|
||||
}
|
||||
if err := service.ValidateSessionResume(req.ResumeToken, key, req.Sequence); err != nil {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_binding", "invalid resume token", false)
|
||||
return
|
||||
}
|
||||
if err := service.SessionEvents().Acknowledge(key, req.Sequence); err != nil {
|
||||
code := "forward_gap"
|
||||
retryable := true
|
||||
if errors.Is(err, plugins.ErrPoisonEvent) {
|
||||
code = "invalid_request"
|
||||
retryable = false
|
||||
}
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, code, "acknowledgement rejected", retryable)
|
||||
return
|
||||
}
|
||||
_, resumeToken, _, err := service.MintSessionStreamGrant(
|
||||
key.PluginID,
|
||||
key.UserID,
|
||||
key.Generation,
|
||||
key.SessionID,
|
||||
key.ConsumerID,
|
||||
key.WireID,
|
||||
req.Sequence,
|
||||
req.Sequence,
|
||||
)
|
||||
if err != nil {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_binding", "cannot mint resume token", true)
|
||||
return
|
||||
}
|
||||
payload := map[string]any{
|
||||
"success": true, "session_id": req.SessionID,
|
||||
"acknowledged_sequence": req.Sequence, "resume_token": resumeToken,
|
||||
}
|
||||
if key.ConsumerKind == orderedConsumerPlugin {
|
||||
payload["consumer_id"] = key.ConsumerID
|
||||
} else {
|
||||
payload["wire_id"] = key.WireID
|
||||
}
|
||||
response, _ := ws.NewResponse(msg.ID, msg.Action, payload)
|
||||
c.sendMessage(response)
|
||||
}
|
||||
|
||||
func sessionAckSubscriptionID(req SessionAckRequest) (string, bool) {
|
||||
switch req.ConsumerKind {
|
||||
case orderedConsumerPlugin:
|
||||
return req.ConsumerID, req.ConsumerID != "" && req.WireID == ""
|
||||
case orderedConsumerCore:
|
||||
return req.WireID, req.WireID != "" && req.ConsumerID == ""
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
func sessionAckMatchesKey(req SessionAckRequest, key plugins.SessionDeliveryCursorKey) bool {
|
||||
if req.SessionID != key.SessionID || req.ConsumerKind != key.ConsumerKind {
|
||||
return false
|
||||
}
|
||||
switch req.ConsumerKind {
|
||||
case orderedConsumerPlugin:
|
||||
return req.ConsumerID == key.ConsumerID &&
|
||||
req.PluginID == key.PluginID &&
|
||||
req.Generation == key.Generation
|
||||
case orderedConsumerCore:
|
||||
return req.WireID == key.WireID && req.PluginID == "" && req.Generation == 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
type sessionPoisonRequeueRequest struct {
|
||||
SessionID string `json:"session_id"`
|
||||
EventID string `json:"event_id"`
|
||||
ExpectedOwnerEpoch uint64 `json:"expected_owner_epoch"`
|
||||
}
|
||||
|
||||
const sessionPoisonStateKey = "state"
|
||||
|
||||
func (c *Client) handleSessionPoisonRequeue(msg *ws.Message) {
|
||||
var req sessionPoisonRequeueRequest
|
||||
if err := msg.ParsePayload(&req); err != nil ||
|
||||
req.SessionID == "" ||
|
||||
req.EventID == "" ||
|
||||
req.ExpectedOwnerEpoch == 0 {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_request", "invalid poison requeue request", false)
|
||||
return
|
||||
}
|
||||
if !canRequeueSessionEvents(c.identity) || c.hub.pluginConversationService == nil {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "unauthorized", "session_events:requeue required", false)
|
||||
return
|
||||
}
|
||||
err := c.hub.pluginConversationService.SessionDelivery().Requeue(
|
||||
req.SessionID,
|
||||
req.EventID,
|
||||
req.ExpectedOwnerEpoch,
|
||||
c.ownUserTopic(),
|
||||
time.Now().UTC(),
|
||||
)
|
||||
if err != nil {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_request", "poison requeue rejected", false)
|
||||
return
|
||||
}
|
||||
response, _ := ws.NewResponse(msg.ID, msg.Action, map[string]any{
|
||||
"success": true, "session_id": req.SessionID, "event_id": req.EventID,
|
||||
sessionPoisonStateKey: plugins.SessionPoisonPending,
|
||||
})
|
||||
c.sendMessage(response)
|
||||
}
|
||||
|
||||
func canRequeueSessionEvents(identity authn.Identity) bool {
|
||||
if identity.UserID == "" {
|
||||
return false
|
||||
}
|
||||
return identity.Instance || identity.Synthetic
|
||||
}
|
||||
|
||||
// maySubscribeSession applies the per-user session scoping check, emitting the
|
||||
// forbidden error itself. Returns true when the subscription may proceed.
|
||||
func (c *Client) maySubscribeSession(msg *ws.Message, sessionID string) bool {
|
||||
@@ -384,6 +909,27 @@ func (c *Client) maySubscribeSession(msg *ws.Message, sessionID string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Client) maySubscribeOrderedSession(
|
||||
msg *ws.Message,
|
||||
req SessionSubscribeRequest,
|
||||
key plugins.SessionDeliveryCursorKey,
|
||||
service *plugins.Service,
|
||||
) bool {
|
||||
check := c.hub.authPolicy.Subscriptions.Session
|
||||
if check == nil {
|
||||
return true
|
||||
}
|
||||
if err := check(c.dispatchContext(), req.SessionID); err == nil {
|
||||
return true
|
||||
}
|
||||
_, _, terminal := service.SessionEvents().ReplayState(req.SessionID, 0)
|
||||
if terminal && service.SessionEvents().HasCursor(key) {
|
||||
return true
|
||||
}
|
||||
c.sendError(msg.ID, msg.Action, ws.ErrorCodeForbidden, "cannot subscribe to this session", nil)
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Client) handleUserUnsubscribe(msg *ws.Message) {
|
||||
var req UserSubscribeRequest
|
||||
if err := msg.ParsePayload(&req); err != nil {
|
||||
@@ -550,7 +1096,6 @@ func (c *Client) handleSystemMetricsSubscribe(msg *ws.Message) {
|
||||
})
|
||||
c.sendMessage(resp)
|
||||
}
|
||||
|
||||
func (c *Client) handleSystemMetricsUnsubscribe(msg *ws.Message) {
|
||||
c.hub.UnsubscribeFromSystemMetrics(c)
|
||||
resp, _ := ws.NewResponse(msg.ID, msg.Action, map[string]interface{}{
|
||||
@@ -566,14 +1111,15 @@ func (c *Client) handleSessionUnsubscribe(msg *ws.Message) {
|
||||
c.sendError(msg.ID, msg.Action, ws.ErrorCodeBadRequest, "Invalid payload: "+err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if req.SessionID == "" {
|
||||
c.sendError(msg.ID, msg.Action, ws.ErrorCodeValidation, "session_id is required", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if req.ConsumerKind != "" {
|
||||
c.handleOrderedSessionUnsubscribe(msg, req)
|
||||
return
|
||||
}
|
||||
c.hub.UnsubscribeFromSession(c, req.SessionID)
|
||||
|
||||
resp, _ := ws.NewResponse(msg.ID, msg.Action, map[string]interface{}{
|
||||
"success": true,
|
||||
"session_id": req.SessionID,
|
||||
@@ -581,6 +1127,55 @@ func (c *Client) handleSessionUnsubscribe(msg *ws.Message) {
|
||||
c.sendMessage(resp)
|
||||
}
|
||||
|
||||
func (c *Client) handleOrderedSessionUnsubscribe(msg *ws.Message, req SessionSubscribeRequest) {
|
||||
key, removed := c.removeOrderedSessionSubscription(req)
|
||||
if !removed {
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "invalid_request", "ordered subscription not found", false)
|
||||
return
|
||||
}
|
||||
if service := c.hub.pluginConversationService; service != nil {
|
||||
if err := service.SessionEvents().ReleaseCursor(key); err != nil {
|
||||
c.storeOrderedSessionSubscription(req, key)
|
||||
c.sendSessionStreamFailure(msg, req.SessionID, "upstream_failure", "cannot release session cursor", true)
|
||||
return
|
||||
}
|
||||
}
|
||||
payload := map[string]any{"success": true, sessionIDPayloadKey: req.SessionID}
|
||||
if req.ConsumerKind == orderedConsumerPlugin {
|
||||
payload["consumer_id"] = req.ConsumerID
|
||||
} else {
|
||||
payload["wire_id"] = req.WireID
|
||||
}
|
||||
response, _ := ws.NewResponse(msg.ID, msg.Action, payload)
|
||||
c.sendMessage(response)
|
||||
}
|
||||
|
||||
func (c *Client) removeOrderedSessionSubscription(
|
||||
req SessionSubscribeRequest,
|
||||
) (plugins.SessionDeliveryCursorKey, bool) {
|
||||
subscriptionID := req.ConsumerID
|
||||
if req.ConsumerKind == orderedConsumerCore {
|
||||
subscriptionID = req.WireID
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
byConsumer := c.orderedSessionSubscriptions[req.SessionID]
|
||||
key, subscribed := byConsumer[subscriptionID]
|
||||
if !subscribed ||
|
||||
key.ConsumerKind != req.ConsumerKind ||
|
||||
key.ConsumerID != req.ConsumerID ||
|
||||
key.WireID != req.WireID ||
|
||||
key.PluginID != req.PluginID ||
|
||||
key.Generation != req.Generation {
|
||||
return plugins.SessionDeliveryCursorKey{}, false
|
||||
}
|
||||
delete(byConsumer, subscriptionID)
|
||||
if len(byConsumer) == 0 {
|
||||
delete(c.orderedSessionSubscriptions, req.SessionID)
|
||||
}
|
||||
return key, true
|
||||
}
|
||||
|
||||
// handleSessionFocus handles session.focus — marks the session as actively
|
||||
// viewed by this client, lifting backend polling to fast mode for the workspace.
|
||||
// It intentionally sends only the control acknowledgement. Snapshot/data
|
||||
|
||||
@@ -0,0 +1,484 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/auth/authn"
|
||||
"github.com/kandev/kandev/internal/plugins"
|
||||
ws "github.com/kandev/kandev/pkg/websocket"
|
||||
)
|
||||
|
||||
func TestSessionAckIdentityMatchesExactConsumerBranch(t *testing.T) {
|
||||
pluginKey := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: "session-1", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1",
|
||||
}
|
||||
coreKey := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: "session-1", ConsumerKind: orderedConsumerCore, WireID: "wire-1",
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
req SessionAckRequest
|
||||
key plugins.SessionDeliveryCursorKey
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "plugin identity",
|
||||
req: SessionAckRequest{SessionID: "session-1", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1"},
|
||||
key: pluginKey, want: true,
|
||||
},
|
||||
{
|
||||
name: "plugin cannot also send wire identity",
|
||||
req: SessionAckRequest{SessionID: "session-1", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1", WireID: "wire-1"},
|
||||
key: pluginKey,
|
||||
},
|
||||
{
|
||||
name: "plugin id must match",
|
||||
req: SessionAckRequest{SessionID: "session-1", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "other", Generation: 7, ConsumerID: "consumer-1"},
|
||||
key: pluginKey,
|
||||
},
|
||||
{
|
||||
name: "core identity",
|
||||
req: SessionAckRequest{SessionID: "session-1", ConsumerKind: orderedConsumerCore,
|
||||
WireID: "wire-1"},
|
||||
key: coreKey, want: true,
|
||||
},
|
||||
{
|
||||
name: "core cannot send plugin fields",
|
||||
req: SessionAckRequest{SessionID: "session-1", ConsumerKind: orderedConsumerCore,
|
||||
WireID: "wire-1", PluginID: "plugin-1", Generation: 7},
|
||||
key: coreKey,
|
||||
},
|
||||
{
|
||||
name: "core wire must match",
|
||||
req: SessionAckRequest{SessionID: "session-1", ConsumerKind: orderedConsumerCore,
|
||||
WireID: "other"},
|
||||
key: coreKey,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
identity, valid := sessionAckSubscriptionID(test.req)
|
||||
if test.want {
|
||||
if !valid || identity == "" || !sessionAckMatchesKey(test.req, test.key) {
|
||||
t.Fatalf("valid ACK identity rejected: valid=%v identity=%q", valid, identity)
|
||||
}
|
||||
return
|
||||
}
|
||||
if valid && sessionAckMatchesKey(test.req, test.key) {
|
||||
t.Fatal("malformed or conflicting ACK identity accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPoisonRequeueRequiresOperatorCapability(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
identity authn.Identity
|
||||
want bool
|
||||
}{
|
||||
{name: "instance operator", identity: authn.Identity{UserID: "operator-1", Instance: true}, want: true},
|
||||
{name: "single user operator", identity: authn.Identity{UserID: "default-user", Role: authn.RoleAdmin, Synthetic: true}, want: true},
|
||||
{name: "organization admin has no operator capability", identity: authn.Identity{UserID: "admin-1", Role: authn.RoleAdmin}},
|
||||
{name: "anonymous instance bit is insufficient", identity: authn.Identity{Instance: true}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := canRequeueSessionEvents(test.identity); got != test.want {
|
||||
t.Fatalf("canRequeueSessionEvents() = %v, want %v", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedReplayIsExplicitOnly(t *testing.T) {
|
||||
service := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
_, err := service.SessionEvents().Append(
|
||||
"session-1",
|
||||
nil,
|
||||
"message.deleted",
|
||||
json.RawMessage(`{"type":"message.deleted","session_id":"session-1","message_id":"message-1"}`),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("append event: %v", err)
|
||||
}
|
||||
key := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: "session-1", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1", UserID: "user-1",
|
||||
}
|
||||
|
||||
fresh := resolveOrderedSessionReplay(service, SessionSubscribeRequest{
|
||||
SessionID: "session-1", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1",
|
||||
}, key)
|
||||
if fresh.result != "fresh" || fresh.watermark != 1 || len(fresh.events) != 0 {
|
||||
t.Fatalf("fresh result = %+v, want no replay at watermark 1", fresh)
|
||||
}
|
||||
|
||||
zero := uint64(0)
|
||||
replay := resolveOrderedSessionReplay(service, SessionSubscribeRequest{
|
||||
SessionID: "session-1", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1",
|
||||
LastSeenSequence: &zero,
|
||||
}, key)
|
||||
if replay.result != "replay" || len(replay.events) != 1 {
|
||||
t.Fatalf("explicit replay result = %+v, want one replay event", replay)
|
||||
}
|
||||
|
||||
invalid := resolveOrderedSessionReplay(service, SessionSubscribeRequest{
|
||||
SessionID: "session-1", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1",
|
||||
LastSeenSequence: &zero, ResumeToken: "invalid",
|
||||
}, key)
|
||||
if invalid.result != "invalid_resume" || len(invalid.events) != 0 {
|
||||
t.Fatalf("invalid resume result = %+v, want replacement without replay", invalid)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedReplayGrantAcceptsEveryContiguousAck(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
consumerKind string
|
||||
consumerID string
|
||||
wireID string
|
||||
pluginID string
|
||||
generation int64
|
||||
}{
|
||||
{name: "core", consumerKind: orderedConsumerCore, wireID: "wire-replay"},
|
||||
{
|
||||
name: "plugin",
|
||||
consumerKind: orderedConsumerPlugin,
|
||||
consumerID: "consumer-replay",
|
||||
pluginID: "plugin-replay",
|
||||
generation: 7,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
service := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
for sequence := 1; sequence <= 12; sequence++ {
|
||||
_, err := service.SessionEvents().Append(
|
||||
"session-replay",
|
||||
nil,
|
||||
"message.deleted",
|
||||
json.RawMessage(`{"type":"message.deleted","session_id":"session-replay","message_id":"message"}`),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("append event %d: %v", sequence, err)
|
||||
}
|
||||
}
|
||||
|
||||
client := newTestClient("replay-client-" + test.name)
|
||||
client.hub = newTestHub(t)
|
||||
client.hub.SetPluginConversationService(service)
|
||||
client.controlSend = make(chan []byte, 16)
|
||||
client.orderedSessionSubscriptions = make(map[string]map[string]plugins.SessionDeliveryCursorKey)
|
||||
userID := client.ownUserTopic()
|
||||
key := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: "session-replay", ConsumerKind: test.consumerKind,
|
||||
ConsumerID: test.consumerID, WireID: test.wireID, PluginID: test.pluginID,
|
||||
Generation: test.generation, UserID: userID,
|
||||
}
|
||||
lastSeen := uint64(10)
|
||||
req := SessionSubscribeRequest{
|
||||
SessionID: "session-replay", ConsumerKind: test.consumerKind,
|
||||
PluginID: test.pluginID, Generation: test.generation,
|
||||
ConsumerID: test.consumerID, WireID: test.wireID,
|
||||
LastSeenSequence: &lastSeen,
|
||||
}
|
||||
_, initialResume, _, err := service.MintSessionStreamGrant(
|
||||
test.pluginID,
|
||||
userID,
|
||||
test.generation,
|
||||
"session-replay",
|
||||
test.consumerID,
|
||||
test.wireID,
|
||||
10,
|
||||
10,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("mint initial resume token: %v", err)
|
||||
}
|
||||
req.ResumeToken = initialResume
|
||||
if err := service.SessionEvents().RegisterCursor(key, 10); err != nil {
|
||||
t.Fatalf("register replay cursor: %v", err)
|
||||
}
|
||||
replay := resolveOrderedSessionReplay(service, req, key)
|
||||
if replay.result != "replay" || replay.watermark != 12 || replay.cursorSequence != 10 {
|
||||
t.Fatalf("replay = %+v, want cursor 10 and watermark 12", replay)
|
||||
}
|
||||
msg := &ws.Message{ID: "subscribe-replay", Type: ws.MessageTypeRequest, Action: "session.subscribe"}
|
||||
if !client.acceptOrderedSessionSubscription(msg, req, key, userID, replay) {
|
||||
t.Fatal("replay subscription was rejected")
|
||||
}
|
||||
var subscribeResponse struct {
|
||||
Payload struct {
|
||||
SnapshotCutoff uint64 `json:"snapshot_cutoff"`
|
||||
ResumeToken string `json:"resume_token"`
|
||||
} `json:"payload"`
|
||||
}
|
||||
if err := json.Unmarshal(<-client.controlSend, &subscribeResponse); err != nil {
|
||||
t.Fatalf("decode replay response: %v", err)
|
||||
}
|
||||
if subscribeResponse.Payload.SnapshotCutoff != 12 {
|
||||
t.Fatalf("snapshot cutoff = %d, want 12", subscribeResponse.Payload.SnapshotCutoff)
|
||||
}
|
||||
|
||||
ack := func(sequence uint64, resumeToken string) string {
|
||||
raw, err := json.Marshal(SessionAckRequest{
|
||||
SessionID: "session-replay", ConsumerKind: test.consumerKind,
|
||||
PluginID: test.pluginID, Generation: test.generation,
|
||||
Sequence: sequence, ResumeToken: resumeToken,
|
||||
ConsumerID: test.consumerID, WireID: test.wireID,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal ACK %d: %v", sequence, err)
|
||||
}
|
||||
client.handleSessionAck(&ws.Message{
|
||||
ID: "ack-replay", Type: ws.MessageTypeRequest, Action: "session.ack", Payload: raw,
|
||||
})
|
||||
var response struct {
|
||||
Type ws.MessageType `json:"type"`
|
||||
Payload struct {
|
||||
Success bool `json:"success"`
|
||||
ResumeToken string `json:"resume_token"`
|
||||
Acknowledged uint64 `json:"acknowledged_sequence"`
|
||||
ErrorCode string `json:"code"`
|
||||
} `json:"payload"`
|
||||
}
|
||||
if err := json.Unmarshal(<-client.controlSend, &response); err != nil {
|
||||
t.Fatalf("decode ACK %d response: %v", sequence, err)
|
||||
}
|
||||
if !response.Payload.Success {
|
||||
t.Fatalf("ACK %d failed with %s", sequence, response.Payload.ErrorCode)
|
||||
}
|
||||
if response.Payload.Acknowledged != sequence {
|
||||
t.Fatalf("ACK %d acknowledged %d", sequence, response.Payload.Acknowledged)
|
||||
}
|
||||
return response.Payload.ResumeToken
|
||||
}
|
||||
|
||||
resumeToken := subscribeResponse.Payload.ResumeToken
|
||||
resumeToken = ack(11, resumeToken)
|
||||
resumeToken = ack(12, resumeToken)
|
||||
if _, err := service.SessionEvents().Append(
|
||||
"session-replay",
|
||||
nil,
|
||||
"message.deleted",
|
||||
json.RawMessage(`{"type":"message.deleted","session_id":"session-replay","message_id":"message-13"}`),
|
||||
); err != nil {
|
||||
t.Fatalf("append live event: %v", err)
|
||||
}
|
||||
_ = ack(13, resumeToken)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedReplayGrantRejectsInvalidAck(t *testing.T) {
|
||||
service := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
for sequence := 1; sequence <= 2; sequence++ {
|
||||
if _, err := service.SessionEvents().Append(
|
||||
"session-invalid-ack",
|
||||
nil,
|
||||
"message.deleted",
|
||||
json.RawMessage(`{"type":"message.deleted","session_id":"session-invalid-ack","message_id":"message"}`),
|
||||
); err != nil {
|
||||
t.Fatalf("append event %d: %v", sequence, err)
|
||||
}
|
||||
}
|
||||
|
||||
client := newTestClient("invalid-ack-client")
|
||||
client.hub = newTestHub(t)
|
||||
client.hub.SetPluginConversationService(service)
|
||||
client.controlSend = make(chan []byte, 8)
|
||||
client.orderedSessionSubscriptions = make(map[string]map[string]plugins.SessionDeliveryCursorKey)
|
||||
userID := client.ownUserTopic()
|
||||
key := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: "session-invalid-ack", ConsumerKind: orderedConsumerCore,
|
||||
WireID: "wire-invalid-ack", UserID: userID,
|
||||
}
|
||||
if err := service.SessionEvents().RegisterCursor(key, 1); err != nil {
|
||||
t.Fatalf("register ACK cursor: %v", err)
|
||||
}
|
||||
_, resumeToken, _, err := service.MintSessionStreamGrant(
|
||||
"", userID, 0, key.SessionID, "", key.WireID, 1, 1,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("mint resume token: %v", err)
|
||||
}
|
||||
client.orderedSessionSubscriptions[key.SessionID] = map[string]plugins.SessionDeliveryCursorKey{
|
||||
key.WireID: key,
|
||||
}
|
||||
|
||||
ack := func(sequence uint64, token string) (success bool, code string) {
|
||||
raw, err := json.Marshal(SessionAckRequest{
|
||||
SessionID: key.SessionID, ConsumerKind: orderedConsumerCore,
|
||||
Sequence: sequence, ResumeToken: token, WireID: key.WireID,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal ACK %d: %v", sequence, err)
|
||||
}
|
||||
client.handleSessionAck(&ws.Message{
|
||||
ID: "ack-invalid", Type: ws.MessageTypeRequest, Action: "session.ack", Payload: raw,
|
||||
})
|
||||
var response struct {
|
||||
Payload struct {
|
||||
Success bool `json:"success"`
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
} `json:"error"`
|
||||
} `json:"payload"`
|
||||
}
|
||||
if err := json.Unmarshal(<-client.controlSend, &response); err != nil {
|
||||
t.Fatalf("decode ACK %d response: %v", sequence, err)
|
||||
}
|
||||
return response.Payload.Success, response.Payload.Error.Code
|
||||
}
|
||||
|
||||
if success, code := ack(3, resumeToken); success || code != "forward_gap" {
|
||||
t.Fatalf("forward-gap ACK result = success %v, code %q; want forward_gap", success, code)
|
||||
}
|
||||
if success, code := ack(2, resumeToken+"tampered"); success || code != "invalid_binding" {
|
||||
t.Fatalf("tampered ACK result = success %v, code %q; want invalid_binding", success, code)
|
||||
}
|
||||
if success, code := ack(2, resumeToken); !success || code != "" {
|
||||
t.Fatalf("valid ACK after rejected requests = success %v, code %q; want success", success, code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedReplayRebindsWhenPoisonDeliveryIsAlreadyClaimed(t *testing.T) {
|
||||
service := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
poison, err := service.SessionEvents().Append(
|
||||
"session-poison-replay",
|
||||
nil,
|
||||
"unknown.event",
|
||||
json.RawMessage(`{"type":"unknown.event","session_id":"session-poison-replay"}`),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("append poison event: %v", err)
|
||||
}
|
||||
claim, err := service.SessionDelivery().Claim(
|
||||
poison.SessionID,
|
||||
poison.ID,
|
||||
time.Now().UTC(),
|
||||
)
|
||||
if err != nil || claim.Disposition != plugins.SessionDeliveryClaimed {
|
||||
t.Fatalf("initial poison claim = %q, %v", claim.Disposition, err)
|
||||
}
|
||||
zero := uint64(0)
|
||||
req := SessionSubscribeRequest{
|
||||
SessionID: "session-poison-replay", ConsumerKind: orderedConsumerCore,
|
||||
WireID: "wire-poison", LastSeenSequence: &zero,
|
||||
}
|
||||
key := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: "session-poison-replay", ConsumerKind: orderedConsumerCore, WireID: "wire-poison",
|
||||
}
|
||||
replay := resolveOrderedSessionReplay(service, req, key)
|
||||
hub := newTestHub(t)
|
||||
hub.SetPluginConversationService(service)
|
||||
client := newTestClient("poison-replay-client")
|
||||
client.hub = hub
|
||||
client.controlSend = make(chan []byte, 1)
|
||||
client.orderedSessionSubscriptions = make(map[string]map[string]plugins.SessionDeliveryCursorKey)
|
||||
msg := &ws.Message{ID: "subscribe-poison", Type: ws.MessageTypeRequest, Action: "session.subscribe"}
|
||||
|
||||
if !client.acceptOrderedSessionSubscription(msg, req, key, "", replay) {
|
||||
t.Fatal("poison replay replacement subscription was rejected")
|
||||
}
|
||||
|
||||
var response struct {
|
||||
Payload struct {
|
||||
Result string `json:"result"`
|
||||
} `json:"payload"`
|
||||
}
|
||||
if err := json.Unmarshal(<-client.controlSend, &response); err != nil {
|
||||
t.Fatalf("decode subscription response: %v", err)
|
||||
}
|
||||
if response.Payload.Result != "invalid_resume" {
|
||||
t.Fatalf("poison replay result = %q, want invalid_resume", response.Payload.Result)
|
||||
}
|
||||
if err := service.SessionEvents().Acknowledge(key, poison.Sequence); err != nil {
|
||||
t.Fatalf("replacement cursor did not advance past poison: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOrderedReplayRebindsOnRetainedGap pins that a replay whose first
|
||||
// retained row starts past lastSeen+1 (intermediate rows aged out on both
|
||||
// sides) returns the replacement-cursor result instead of masquerading as a
|
||||
// contiguous replay that would hide the lost history.
|
||||
func TestOrderedReplayRebindsOnRetainedGap(t *testing.T) {
|
||||
service := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
// A fresh durable-log partition with only a high sequence present mirrors
|
||||
// the state after both sides pruned the intermediate rows.
|
||||
appended, err := service.SessionEvents().AppendCommitted(plugins.SessionEvent{
|
||||
SessionID: "session-gap", ID: "session-gap:10", Sequence: 10,
|
||||
EventType: "message.deleted",
|
||||
Payload: json.RawMessage(`{"type":"message.deleted","session_id":"session-gap","message_id":"message-1"}`),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("mirror event: %v", err)
|
||||
}
|
||||
if !appended {
|
||||
t.Fatal("expected the gap heal to accept the retained row")
|
||||
}
|
||||
key := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: "session-gap", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1", UserID: "user-1",
|
||||
}
|
||||
lastSeen := uint64(5)
|
||||
replay := resolveOrderedSessionReplay(service, SessionSubscribeRequest{
|
||||
SessionID: "session-gap", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1",
|
||||
LastSeenSequence: &lastSeen,
|
||||
}, key)
|
||||
if replay.result != "invalid_resume" || len(replay.events) != 0 || replay.cursorSequence != replay.watermark {
|
||||
t.Fatalf("retained-gap result = %+v, want replacement cursor at watermark without replay", replay)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOrderedReplayRebindsWhenPartitionWatermarkAheadWithNoRows pins the
|
||||
// fully-pruned case: the partition watermark is ahead of the cursor but every
|
||||
// retained event row aged out, so the reconnect must rebind rather than treat
|
||||
// the empty replay as a fresh success.
|
||||
func TestOrderedReplayRebindsWhenPartitionWatermarkAheadWithNoRows(t *testing.T) {
|
||||
service := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
base := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
appended, err := service.SessionEvents().AppendCommitted(plugins.SessionEvent{
|
||||
SessionID: "session-pruned", ID: "session-pruned:10", Sequence: 10,
|
||||
EventType: "message.added", ProtocolVersion: 1,
|
||||
Payload: json.RawMessage(`{"type":"message.added","session_id":"session-pruned","message_id":"m1","author_type":"user","content":"x","created_at":"2026-09-07T12:00:00Z"}`),
|
||||
CreatedAt: base,
|
||||
})
|
||||
if err != nil || !appended {
|
||||
t.Fatalf("mirror event appended=%v err=%v", appended, err)
|
||||
}
|
||||
// Age the only retained row out: the partition keeps its watermark.
|
||||
if err := service.SessionEvents().CollectExpired(base.Add(plugins.SessionEventRetention + time.Hour)); err != nil {
|
||||
t.Fatalf("collect expired: %v", err)
|
||||
}
|
||||
key := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: "session-pruned", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1", UserID: "user-1",
|
||||
}
|
||||
lastSeen := uint64(5)
|
||||
replay := resolveOrderedSessionReplay(service, SessionSubscribeRequest{
|
||||
SessionID: "session-pruned", ConsumerKind: orderedConsumerPlugin,
|
||||
PluginID: "plugin-1", Generation: 7, ConsumerID: "consumer-1",
|
||||
LastSeenSequence: &lastSeen,
|
||||
}, key)
|
||||
if replay.result != "invalid_resume" || len(replay.events) != 0 || replay.cursorSequence != replay.watermark {
|
||||
t.Fatalf("pruned-gap result = %+v, want replacement cursor at watermark without replay", replay)
|
||||
}
|
||||
if replay.watermark != 10 {
|
||||
t.Fatalf("watermark = %d, want 10", replay.watermark)
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/plugins"
|
||||
ws "github.com/kandev/kandev/pkg/websocket"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -53,6 +54,7 @@ type Hub struct {
|
||||
sessionDataProvider SessionDataProvider
|
||||
sessionGitDataProvider SessionGitDataProvider
|
||||
userSubscriptionListeners []func(userID string)
|
||||
pluginConversationService *plugins.Service
|
||||
|
||||
// clientDisconnectListener releases connection-bound resources after a
|
||||
// client is removed from the hub. It runs asynchronously so durable cleanup
|
||||
@@ -75,8 +77,11 @@ type Hub struct {
|
||||
// value = unscoped, today's behavior. See access.go.
|
||||
authPolicy AuthPolicy
|
||||
|
||||
mu sync.RWMutex
|
||||
logger *logger.Logger
|
||||
// orderedSessionMu closes the replay/register/append race for the ordered
|
||||
// session stream. Never acquire it while holding h.mu or a client mutex.
|
||||
orderedSessionMu sync.Mutex
|
||||
mu sync.RWMutex
|
||||
logger *logger.Logger
|
||||
}
|
||||
|
||||
// NewHub creates a new WebSocket hub
|
||||
@@ -98,6 +103,19 @@ func NewHub(dispatcher *ws.Dispatcher, log *logger.Logger) *Hub {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) SetPluginConversationService(service *plugins.Service) {
|
||||
h.mu.Lock()
|
||||
h.pluginConversationService = service
|
||||
h.mu.Unlock()
|
||||
if service != nil {
|
||||
service.SetSessionEventSink(func(event plugins.SessionEvent) {
|
||||
h.orderedSessionMu.Lock()
|
||||
defer h.orderedSessionMu.Unlock()
|
||||
h.broadcastCommittedOrderedSessionEvent(service, event)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type SystemMetricsInterestTracker interface {
|
||||
MetricsSubscribe(clientID string)
|
||||
MetricsUnsubscribe(clientID string)
|
||||
@@ -515,12 +533,22 @@ func (h *Hub) getSessionRecipientsLocked(sessionID string) []*Client {
|
||||
// BroadcastToSession sends a notification to clients subscribed to OR focused on
|
||||
// a specific session. See getSessionRecipientsLocked for why focus is included.
|
||||
func (h *Hub) BroadcastToSession(sessionID string, msg *ws.Message) {
|
||||
h.appendAndBroadcastOrderedSessionEvent(sessionID, msg)
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
h.logger.Error("Failed to marshal message", zap.Error(err))
|
||||
return
|
||||
}
|
||||
clients := h.authorizedSessionRecipients(sessionID)
|
||||
if _, ordered := orderedEventTypeByAction[msg.Action]; ordered {
|
||||
legacyClients := clients[:0]
|
||||
for _, client := range clients {
|
||||
if !client.hasOrderedSessionSubscription(sessionID) {
|
||||
legacyClients = append(legacyClients, client)
|
||||
}
|
||||
}
|
||||
clients = legacyClients
|
||||
}
|
||||
h.logger.Debug("BroadcastToSession",
|
||||
zap.String("session_id", sessionID),
|
||||
zap.String("action", msg.Action),
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/auth/authn"
|
||||
"github.com/kandev/kandev/internal/plugins"
|
||||
ws "github.com/kandev/kandev/pkg/websocket"
|
||||
)
|
||||
|
||||
@@ -119,6 +120,31 @@ func TestBroadcastToSession_DeliversOnceToSubscribedAndFocusedClient(t *testing.
|
||||
}
|
||||
}
|
||||
|
||||
func TestBroadcastToSession_SuppressesLegacyCopyForOrderedCoreConsumer(t *testing.T) {
|
||||
h := newTestHub(t)
|
||||
c := newTestClient("c1")
|
||||
registerTestClient(h, c)
|
||||
h.SubscribeToSession(c, "sess-1")
|
||||
c.orderedSessionSubscriptions = map[string]map[string]plugins.SessionDeliveryCursorKey{
|
||||
"sess-1": {
|
||||
"core-1": {SessionID: "sess-1", ConsumerKind: "core", WireID: "core-1"},
|
||||
},
|
||||
}
|
||||
|
||||
msg, err := ws.NewNotification(
|
||||
ws.ActionSessionMessageAdded,
|
||||
map[string]any{"session_id": "sess-1"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("notification: %v", err)
|
||||
}
|
||||
h.BroadcastToSession("sess-1", msg)
|
||||
|
||||
if clientReceived(c) {
|
||||
t.Fatal("ordered core consumer received a duplicate legacy notification")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBroadcastToUserReportsWhetherAFrameWasQueued(t *testing.T) {
|
||||
h := newTestHub(t)
|
||||
msg, err := ws.NewNotification("system.update_available", map[string]any{"occurrence_id": "v1.2.3"})
|
||||
@@ -197,3 +223,110 @@ func TestSendToIdentityTargetsConnectedClientsWithoutSubscription(t *testing.T)
|
||||
t.Fatal("a different identity must not receive the notification")
|
||||
}
|
||||
}
|
||||
|
||||
// TestBroadcastCommittedPoisonFrameDeliversAndCountsAttempt pins the unified
|
||||
// poison contract: the mirrored frame IS delivered to live subscribers (their
|
||||
// recovery depends on seeing it) and the delivery attempt is counted only when
|
||||
// a recipient exists.
|
||||
func TestBroadcastCommittedPoisonFrameDeliversAndCountsAttempt(t *testing.T) {
|
||||
h := newTestHub(t)
|
||||
svc := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
h.SetPluginConversationService(svc)
|
||||
|
||||
// message.added missing author/content -> projection poison.
|
||||
event, err := svc.SessionEvents().Append(
|
||||
"sess-poison", newStringPointer("task-poison"), "message.added",
|
||||
[]byte(`{"type":"message.added","session_id":"sess-poison","task_id":"task-poison"}`),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("append poison event: %v", err)
|
||||
}
|
||||
if _, ok := svc.SessionEvents().Poison("sess-poison", event.ID); !ok {
|
||||
t.Fatal("append did not poison the malformed event")
|
||||
}
|
||||
|
||||
c := newTestClient("c-poison")
|
||||
registerTestClient(h, c)
|
||||
c.orderedSessionSubscriptions = map[string]map[string]plugins.SessionDeliveryCursorKey{
|
||||
"sess-poison": {
|
||||
"core-1": {SessionID: "sess-poison", ConsumerKind: "core", WireID: "core-1"},
|
||||
},
|
||||
}
|
||||
|
||||
h.broadcastCommittedOrderedSessionEvent(svc, event)
|
||||
|
||||
if !clientReceived(c) {
|
||||
t.Fatal("live subscriber did not receive the poison frame")
|
||||
}
|
||||
record, ok := svc.SessionEvents().Poison("sess-poison", event.ID)
|
||||
if !ok {
|
||||
t.Fatal("poison record missing after broadcast")
|
||||
}
|
||||
if record.Attempts != 1 {
|
||||
t.Fatalf("poison attempts = %d, want 1 (one delivery with a recipient)", record.Attempts)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBroadcastCommittedPoisonFrameWithoutRecipientsSkipsAttempt pins that a
|
||||
// mirrored poison with nobody subscribed does not burn a delivery attempt.
|
||||
func TestBroadcastCommittedPoisonFrameWithoutRecipientsSkipsAttempt(t *testing.T) {
|
||||
h := newTestHub(t)
|
||||
svc := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
h.SetPluginConversationService(svc)
|
||||
|
||||
event, err := svc.SessionEvents().Append(
|
||||
"sess-poison-idle", newStringPointer("task-poison"), "message.added",
|
||||
[]byte(`{"type":"message.added","session_id":"sess-poison-idle","task_id":"task-poison"}`),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("append poison event: %v", err)
|
||||
}
|
||||
|
||||
h.broadcastCommittedOrderedSessionEvent(svc, event)
|
||||
|
||||
record, ok := svc.SessionEvents().Poison("sess-poison-idle", event.ID)
|
||||
if !ok {
|
||||
t.Fatal("poison record missing after broadcast")
|
||||
}
|
||||
if record.Attempts != 0 {
|
||||
t.Fatalf("poison attempts = %d, want 0 with no recipients", record.Attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBroadcastCommittedPoisonQueueFailureDoesNotCountAttempt(t *testing.T) {
|
||||
h := newTestHub(t)
|
||||
svc := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
h.SetPluginConversationService(svc)
|
||||
event, err := svc.SessionEvents().Append(
|
||||
"sess-poison-full", newStringPointer("task-poison"), "message.added",
|
||||
[]byte(`{"type":"message.added","session_id":"sess-poison-full","task_id":"task-poison"}`),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("append poison event: %v", err)
|
||||
}
|
||||
c := newTestClient("c-poison-full")
|
||||
for range cap(c.send) {
|
||||
c.send <- []byte("occupied")
|
||||
}
|
||||
registerTestClient(h, c)
|
||||
c.orderedSessionSubscriptions = map[string]map[string]plugins.SessionDeliveryCursorKey{
|
||||
"sess-poison-full": {
|
||||
"core-1": {SessionID: "sess-poison-full", ConsumerKind: "core", WireID: "core-1"},
|
||||
},
|
||||
}
|
||||
|
||||
h.broadcastCommittedOrderedSessionEvent(svc, event)
|
||||
|
||||
record, ok := svc.SessionEvents().Poison("sess-poison-full", event.ID)
|
||||
if !ok {
|
||||
t.Fatal("poison record missing after queue failure")
|
||||
}
|
||||
if record.Attempts != 0 || record.State != plugins.SessionPoisonPending {
|
||||
t.Fatalf("poison after queue failure = attempts %d state %q, want pending at zero", record.Attempts, record.State)
|
||||
}
|
||||
if !c.closed {
|
||||
t.Fatal("ordered queue failure must close the client for replay")
|
||||
}
|
||||
}
|
||||
|
||||
func newStringPointer(value string) *string { return &value }
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/plugins"
|
||||
"github.com/kandev/kandev/internal/sysprompt"
|
||||
ws "github.com/kandev/kandev/pkg/websocket"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
orderedMessageAddedEvent = "message.added"
|
||||
orderedMessageUpdatedEvent = "message.updated"
|
||||
orderedMessageDeletedEvent = "message.deleted"
|
||||
orderedTurnStartedEvent = "session.turn.started"
|
||||
orderedTurnCompletedEvent = "session.turn.completed"
|
||||
orderedTurnRemovedEvent = "session.turn.removed"
|
||||
orderedSessionRemovedEvent = "session.removed"
|
||||
)
|
||||
|
||||
var orderedEventTypeByAction = map[string]string{
|
||||
ws.ActionSessionMessageAdded: orderedMessageAddedEvent,
|
||||
ws.ActionSessionMessageUpdated: orderedMessageUpdatedEvent,
|
||||
ws.ActionSessionMessageDeleted: orderedMessageDeletedEvent,
|
||||
ws.ActionSessionTurnStarted: orderedTurnStartedEvent,
|
||||
ws.ActionSessionTurnCompleted: orderedTurnCompletedEvent,
|
||||
ws.ActionSessionTurnRemoved: orderedTurnRemovedEvent,
|
||||
ws.ActionSessionRemoved: orderedSessionRemovedEvent,
|
||||
}
|
||||
|
||||
const orderedContentPayloadKey = "content"
|
||||
|
||||
//nolint:cyclop // This boundary coordinates journal sync, poison recording, and live fan-out.
|
||||
func (h *Hub) appendAndBroadcastOrderedSessionEvent(sessionID string, message *ws.Message) {
|
||||
h.orderedSessionMu.Lock()
|
||||
defer h.orderedSessionMu.Unlock()
|
||||
service := h.pluginConversationService
|
||||
eventType := orderedEventTypeByAction[message.Action]
|
||||
if service == nil || eventType == "" {
|
||||
return
|
||||
}
|
||||
if service.HasConversationJournal() {
|
||||
events, err := service.SyncCommittedSessionEvents(context.Background(), sessionID)
|
||||
if err != nil {
|
||||
if h.logger != nil {
|
||||
h.logger.Error("mirror committed ordered session events", zap.Error(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
for _, event := range events {
|
||||
h.broadcastCommittedOrderedSessionEvent(service, event)
|
||||
}
|
||||
return
|
||||
}
|
||||
var source map[string]any
|
||||
if err := json.Unmarshal(message.Payload, &source); err != nil {
|
||||
source = map[string]any{"raw": message.Payload}
|
||||
}
|
||||
payload := sanitizedOrderedSessionPayload(eventType, source)
|
||||
encoded, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var taskID *string
|
||||
if value, ok := payload["task_id"].(string); ok && value != "" {
|
||||
taskID = &value
|
||||
}
|
||||
event, err := service.SessionEvents().Append(sessionID, taskID, eventType, encoded)
|
||||
if err != nil {
|
||||
if h.logger != nil {
|
||||
h.logger.Error("append ordered session event", zap.Error(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
h.deliverOrderedSessionEvent(service, event, h.orderedSessionRecipients(sessionID))
|
||||
}
|
||||
|
||||
// broadcastCommittedOrderedSessionEvent fans a mirrored primary-journal event
|
||||
// out through the same atomic delivery claim as locally appended events.
|
||||
func (h *Hub) broadcastCommittedOrderedSessionEvent(service *plugins.Service, event plugins.SessionEvent) {
|
||||
h.deliverOrderedSessionEvent(service, event, h.orderedSessionRecipients(event.SessionID))
|
||||
}
|
||||
|
||||
func (h *Hub) deliverOrderedSessionEvent(
|
||||
service *plugins.Service,
|
||||
event plugins.SessionEvent,
|
||||
recipients []*Client,
|
||||
) {
|
||||
if len(recipients) == 0 {
|
||||
return
|
||||
}
|
||||
claim, err := service.SessionDelivery().Claim(event.SessionID, event.ID, time.Now().UTC())
|
||||
if err != nil {
|
||||
if h.logger != nil {
|
||||
h.logger.Error("claim ordered session poison", zap.String("event_id", event.ID), zap.Error(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
if claim.Disposition != plugins.SessionDeliveryUntracked &&
|
||||
claim.Disposition != plugins.SessionDeliveryClaimed {
|
||||
return
|
||||
}
|
||||
queued := false
|
||||
for _, client := range recipients {
|
||||
queued = client.sendOrderedSessionEvent(event) || queued
|
||||
}
|
||||
if claim.Disposition == plugins.SessionDeliveryClaimed {
|
||||
if err := service.SessionDelivery().Complete(claim, queued, time.Now().UTC()); err != nil && h.logger != nil {
|
||||
h.logger.Error("complete ordered session poison", zap.String("event_id", event.ID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sanitizedOrderedSessionPayload(eventType string, source map[string]any) map[string]any {
|
||||
payload := map[string]any{eventTypePayloadKey: eventType}
|
||||
keys := []string{sessionIDPayloadKey, taskIDPayloadKey}
|
||||
switch eventType {
|
||||
case orderedMessageAddedEvent, orderedMessageUpdatedEvent:
|
||||
keys = append(keys,
|
||||
"message_id", "turn_id", "author_type", orderedContentPayloadKey,
|
||||
"created_at", "updated_at", "prompt_index", "requests_input",
|
||||
)
|
||||
if messageType, exists := source[eventTypePayloadKey]; exists {
|
||||
payload["message_type"] = messageType
|
||||
}
|
||||
if metadata, ok := source["metadata"].(map[string]any); ok {
|
||||
if sanitized := plugins.SanitizeConversationMessageMetadata(metadata); len(sanitized) > 0 {
|
||||
payload["metadata"] = sanitized
|
||||
}
|
||||
}
|
||||
if senderTaskID, ok := source["sender_task_id"].(string); ok && senderTaskID != "" {
|
||||
payload["sender_task_id"] = senderTaskID
|
||||
if metadata, ok := payload["metadata"].(map[string]any); !ok {
|
||||
payload["metadata"] = map[string]any{"sender_task_id": senderTaskID}
|
||||
} else {
|
||||
metadata["sender_task_id"] = senderTaskID
|
||||
}
|
||||
}
|
||||
case orderedMessageDeletedEvent:
|
||||
keys = append(keys, "message_id")
|
||||
case orderedTurnStartedEvent, orderedTurnCompletedEvent, orderedTurnRemovedEvent:
|
||||
keys = append(keys, "id", "started_at", "completed_at", "updated_at", "had_output")
|
||||
}
|
||||
for _, key := range keys {
|
||||
if value, exists := source[key]; exists {
|
||||
if key == orderedContentPayloadKey {
|
||||
if content, ok := value.(string); ok {
|
||||
value = sysprompt.StripSystemContent(content)
|
||||
}
|
||||
}
|
||||
payload[key] = value
|
||||
}
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func (h *Hub) orderedSessionRecipients(sessionID string) []*Client {
|
||||
h.mu.RLock()
|
||||
clients := make([]*Client, 0)
|
||||
for client := range h.clients {
|
||||
if client.hasOrderedSessionSubscription(sessionID) {
|
||||
clients = append(clients, client)
|
||||
}
|
||||
}
|
||||
h.mu.RUnlock()
|
||||
// Re-authorize at fanout time, mirroring the legacy session recipients: a
|
||||
// client whose workspace/session membership was revoked after subscribing
|
||||
// must not keep receiving ordered frames. Denied clients lose the ordered
|
||||
// subscription itself AND its durable cursors (same revocation semantics
|
||||
// as the legacy path), so the partition stops being pinned by them and
|
||||
// retention can reclaim it.
|
||||
allowed, denied := h.partitionAuthorized(clients, sessionID, h.authPolicy.Subscriptions.Session)
|
||||
service := h.pluginConversationService
|
||||
for _, client := range denied {
|
||||
client.mu.Lock()
|
||||
revoked := make([]plugins.SessionDeliveryCursorKey, 0, len(client.orderedSessionSubscriptions[sessionID]))
|
||||
for _, key := range client.orderedSessionSubscriptions[sessionID] {
|
||||
revoked = append(revoked, key)
|
||||
}
|
||||
delete(client.orderedSessionSubscriptions, sessionID)
|
||||
client.mu.Unlock()
|
||||
if service == nil {
|
||||
continue
|
||||
}
|
||||
for _, key := range revoked {
|
||||
if err := service.SessionEvents().ReleaseCursor(key); err != nil && h.logger != nil {
|
||||
h.logger.Warn(
|
||||
"release revoked ordered session cursor",
|
||||
zap.String("session_id", sessionID),
|
||||
zap.Error(err),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
return allowed
|
||||
}
|
||||
|
||||
func (c *Client) hasOrderedSessionSubscription(sessionID string) bool {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return len(c.orderedSessionSubscriptions[sessionID]) > 0
|
||||
}
|
||||
|
||||
func sessionEventFrame(event plugins.SessionEvent) ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
eventTypePayloadKey: "session.event", "protocol_version": event.ProtocolVersion,
|
||||
"event_type": event.EventType, sessionIDPayloadKey: event.SessionID,
|
||||
taskIDPayloadKey: event.TaskID, eventSequencePayloadKey: event.Sequence,
|
||||
"event_id": event.ID, "payload": event.Payload,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/auth/authn"
|
||||
"github.com/kandev/kandev/internal/plugins"
|
||||
ws "github.com/kandev/kandev/pkg/websocket"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSanitizedOrderedSessionPayloadStripsSystemContent(t *testing.T) {
|
||||
payload := sanitizedOrderedSessionPayload("message.added", map[string]any{
|
||||
"session_id": "session-1",
|
||||
"task_id": "task-1",
|
||||
"message_id": "message-1",
|
||||
orderedContentPayloadKey: "visible <kandev-system>secret</kandev-system>",
|
||||
})
|
||||
|
||||
require.Equal(t, "visible", payload["content"])
|
||||
}
|
||||
func TestTurnRemovedUsesOrderedSessionEventMapping(t *testing.T) {
|
||||
require.Equal(t, "session.turn.removed", orderedEventTypeByAction[ws.ActionSessionTurnRemoved])
|
||||
payload := sanitizedOrderedSessionPayload(orderedTurnRemovedEvent, map[string]any{
|
||||
"id": "turn-1",
|
||||
"session_id": "session-1",
|
||||
"task_id": "task-1",
|
||||
})
|
||||
require.Equal(t, "turn-1", payload["id"])
|
||||
}
|
||||
|
||||
// TestOrderedRecipientsRevocationReleasesDurableCursor pins the round-9 fix:
|
||||
// when authorization denies a previously-subscribed client at fanout time, the
|
||||
// denied client is removed and its durable cursor is released so retention can
|
||||
// reclaim the partition.
|
||||
func TestOrderedRecipientsRevocationReleasesDurableCursor(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
h := NewHub(ws.NewDispatcher(), testLogger())
|
||||
go h.Run(ctx)
|
||||
svc := plugins.NewService(nil, plugins.NewRegistry(), nil, testLogger())
|
||||
h.SetPluginConversationService(svc)
|
||||
key := plugins.SessionDeliveryCursorKey{
|
||||
SessionID: "sess-revoked", ConsumerKind: "core", WireID: "wire-revoked", UserID: "user-1",
|
||||
}
|
||||
require.NoError(t, svc.SessionEvents().RegisterCursor(key, 0))
|
||||
|
||||
c := NewClient("c-revoked", authn.Identity{}, nil, h, testLogger())
|
||||
registerTestClient(h, c)
|
||||
c.orderedSessionSubscriptions = map[string]map[string]plugins.SessionDeliveryCursorKey{
|
||||
"sess-revoked": {"wire-revoked": key},
|
||||
}
|
||||
deny := func(context.Context, string) error { return errors.New("membership revoked") }
|
||||
h.authPolicy.Subscriptions.Session = deny
|
||||
|
||||
recipients := h.orderedSessionRecipients("sess-revoked")
|
||||
require.Len(t, recipients, 0)
|
||||
require.False(t, c.hasOrderedSessionSubscription("sess-revoked"))
|
||||
require.False(t, svc.SessionEvents().HasCursor(key))
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/kandev/kandev/internal/agent/runtime/lifecycle"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/common/scripts"
|
||||
"github.com/kandev/kandev/internal/plugins"
|
||||
ws "github.com/kandev/kandev/pkg/websocket"
|
||||
)
|
||||
|
||||
@@ -40,6 +41,10 @@ func NewGateway(log *logger.Logger) *Gateway {
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gateway) SetPluginConversationService(service *plugins.Service) {
|
||||
g.Hub.SetPluginConversationService(service)
|
||||
}
|
||||
|
||||
// SetLifecycleManager enables the dedicated terminal WebSocket handler for passthrough mode.
|
||||
// This must be called before SetupRoutes if terminal passthrough is needed.
|
||||
func (g *Gateway) SetLifecycleManager(lifecycleMgr *lifecycle.Manager, userService UserService, scriptService scripts.ScriptService) {
|
||||
|
||||
@@ -95,6 +95,8 @@ func RegisterTaskNotifications(ctx context.Context, eventBus bus.EventBus, hub *
|
||||
b.subscribe(eventBus, events.AgentctlError, ws.ActionSessionAgentctlError)
|
||||
b.subscribe(eventBus, events.TurnStarted, ws.ActionSessionTurnStarted)
|
||||
b.subscribe(eventBus, events.TurnCompleted, ws.ActionSessionTurnCompleted)
|
||||
b.subscribe(eventBus, events.TurnRemoved, ws.ActionSessionTurnRemoved)
|
||||
b.subscribe(eventBus, events.SessionRemoved, ws.ActionSessionRemoved)
|
||||
b.subscribe(eventBus, events.MessageQueueStatusChanged, ws.ActionMessageQueueStatusChanged)
|
||||
b.subscribe(eventBus, events.GitHubTaskPRUpdated, ws.ActionGitHubTaskPRUpdated)
|
||||
b.subscribe(eventBus, events.GitHubTaskPRDeleted, ws.ActionGitHubTaskPRDeleted)
|
||||
@@ -277,11 +279,17 @@ func (b *TaskEventBroadcaster) routeBroadcast(
|
||||
// the owning workspace's user when auth is enabled.
|
||||
b.hub.BroadcastToWorkspace(workspaceID, msg)
|
||||
return nil
|
||||
case ws.ActionSessionMessageAdded, ws.ActionSessionMessageUpdated, ws.ActionSessionMessageDeleted:
|
||||
case ws.ActionSessionMessageAdded, ws.ActionSessionMessageUpdated, ws.ActionSessionMessageDeleted,
|
||||
ws.ActionSessionTurnStarted, ws.ActionSessionTurnCompleted, ws.ActionSessionTurnRemoved:
|
||||
if sessionID != "" {
|
||||
b.hub.BroadcastToSession(sessionID, msg)
|
||||
return nil
|
||||
}
|
||||
case ws.ActionSessionRemoved:
|
||||
if sessionID != "" {
|
||||
b.hub.appendAndBroadcastOrderedSessionEvent(sessionID, msg)
|
||||
return nil
|
||||
}
|
||||
case ws.ActionSessionWorkspaceSourcesUpdated:
|
||||
if sessionID != "" {
|
||||
b.hub.BroadcastToSession(sessionID, msg)
|
||||
|
||||
@@ -112,7 +112,7 @@ func TestTaskEventBroadcaster_NoDuplicateSubscriptions(t *testing.T) {
|
||||
//
|
||||
// Update this number when adding or removing event subscriptions in
|
||||
// RegisterTaskNotifications — it is intentionally exact.
|
||||
const wantSubscriptions = 75
|
||||
const wantSubscriptions = 77
|
||||
if got := len(b.subscriptions); got != wantSubscriptions {
|
||||
t.Errorf("RegisterTaskNotifications created %d subscriptions, want %d — "+
|
||||
"did an event get subscribed twice?", got, wantSubscriptions)
|
||||
|
||||
@@ -3651,12 +3651,22 @@ func (h *Handlers) deleteTaskMessageRollbackSession(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if attachmentSvc := h.taskSvc.AttachmentService(); attachmentSvc != nil {
|
||||
attachmentSvc.RemoveBytes(attachments)
|
||||
if h.taskSvc != nil {
|
||||
if attachmentSvc := h.taskSvc.AttachmentService(); attachmentSvc != nil {
|
||||
attachmentSvc.RemoveBytes(attachments)
|
||||
}
|
||||
}
|
||||
} else if err := repo.DeleteTaskSession(ctx, session); err != nil {
|
||||
return err
|
||||
}
|
||||
if h.eventBus == nil {
|
||||
return nil
|
||||
}
|
||||
return repo.DeleteTaskSession(ctx, session)
|
||||
return h.eventBus.Publish(ctx, events.SessionRemoved, bus.NewEvent(
|
||||
events.SessionRemoved,
|
||||
"mcp-handlers",
|
||||
map[string]interface{}{"session_id": session.ID, "task_id": session.TaskID},
|
||||
))
|
||||
}
|
||||
|
||||
func (r taskMessageReviewRollback) primarySessionID() string {
|
||||
|
||||
@@ -51,10 +51,6 @@ const errInvalidJSONPrefix = "invalid JSON: "
|
||||
// duplicated again.
|
||||
const respKeyError = "error"
|
||||
|
||||
// messageCreatedAtKey is the WS event payload key for a seeded message's
|
||||
// creation timestamp (see publishMessageAddedFallback).
|
||||
const messageCreatedAtKey = "created_at"
|
||||
|
||||
// errJSON writes a `{"error": msg}` body with the given status code.
|
||||
func errJSON(c *gin.Context, code int, msg string) {
|
||||
c.JSON(code, gin.H{respKeyError: msg})
|
||||
@@ -105,7 +101,11 @@ func RegisterRoutes(
|
||||
})
|
||||
g.POST("/tasks", seedTaskHandler(repo, log))
|
||||
g.POST("/task-sessions", seedTaskSessionHandler(repo, eventBus, log))
|
||||
g.DELETE("/task-sessions/:id", deleteTaskSessionHandler(repo, eventBus, log))
|
||||
g.POST("/messages", seedMessageHandler(repo, taskSvc, eventBus, log))
|
||||
g.PATCH("/messages/:id", updateMessageHandler(repo, eventBus, log))
|
||||
g.DELETE("/messages/:id", deleteMessageHandler(repo, eventBus, log))
|
||||
g.POST("/turns/:id/complete", completeTurnHandler(repo, eventBus, log))
|
||||
g.POST("/workflows", seedWorkflowHandler(repo, log))
|
||||
g.PUT("/repositories/:id/git-remote", configureGitRemoteHandler(repo, log))
|
||||
g.DELETE("/repositories/:id/git-remote", configureGitRemoteHandler(repo, log))
|
||||
@@ -409,6 +409,34 @@ func statusForSeedSessionUpdateError(err error) int {
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
|
||||
func deleteTaskSessionHandler(
|
||||
repo *sqliterepo.Repository,
|
||||
eventBus bus.EventBus,
|
||||
log *logger.Logger,
|
||||
) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
sessionID := c.Param("id")
|
||||
session, err := repo.GetTaskSession(c.Request.Context(), sessionID)
|
||||
if err != nil || session == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "task session not found"})
|
||||
return
|
||||
}
|
||||
if err := repo.DeleteTaskSession(c.Request.Context(), session); err != nil {
|
||||
log.Error("delete seeded task session", zap.Error(err), zap.String("session_id", sessionID))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete task session"})
|
||||
return
|
||||
}
|
||||
if eventBus != nil {
|
||||
_ = eventBus.Publish(c.Request.Context(), events.SessionRemoved, bus.NewEvent(
|
||||
events.SessionRemoved,
|
||||
"office-testharness",
|
||||
map[string]interface{}{testSessionIDKey: session.ID, testTaskIDKey: session.TaskID},
|
||||
))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
}
|
||||
|
||||
func getExistingSeedSession(ctx context.Context, repo *sqliterepo.Repository, req *seedTaskSessionRequest) *models.TaskSession {
|
||||
if req.SessionID != "" {
|
||||
existing, _ := repo.GetTaskSession(ctx, req.SessionID)
|
||||
@@ -654,6 +682,16 @@ func publishSessionStateChanged(ctx context.Context, eventBus bus.EventBus, sess
|
||||
}
|
||||
}
|
||||
|
||||
// Event key constants keep the test mutation routes aligned with seed payloads.
|
||||
const (
|
||||
messageCreatedAtKey = "created_at"
|
||||
testSessionIDKey = "session_id"
|
||||
testMessageIDKey = "message_id"
|
||||
testCompletedAtKey = "completed_at"
|
||||
testTaskIDKey = "task_id"
|
||||
testTurnIDKey = "turn_id"
|
||||
)
|
||||
|
||||
type seedMessageRequest struct {
|
||||
SessionID string `json:"session_id"`
|
||||
Type string `json:"type"`
|
||||
@@ -806,23 +844,15 @@ func seedMessageHandler(
|
||||
}
|
||||
}
|
||||
|
||||
// Prefer routing through the task service's own publish path (rather
|
||||
// than a hand-rolled bus.Publish) so a seeded clarification/permission
|
||||
// message gets the same session-scoped pending_action projection a
|
||||
// real agent turn triggers (AC-34/AC-51) — the mobile session
|
||||
// switcher's live icon reads that projection, not the raw message.
|
||||
// Callers with no service handle (older/lighter-weight route tests)
|
||||
// still get the plain message.added event via the fallback so their
|
||||
// coverage of the raw event payload (prompt_index, created_at
|
||||
// precision) keeps working.
|
||||
if taskSvc != nil {
|
||||
if err := taskSvc.PublishMessageEvent(ctx, events.MessageAdded, msg); err != nil {
|
||||
log.Warn("test harness: publish message added failed", zap.Error(err))
|
||||
log.Warn("test harness: publish message added failed; using fallback", zap.Error(err))
|
||||
publishMessageAddedFallback(ctx, eventBus, msg, log)
|
||||
}
|
||||
} else {
|
||||
publishMessageAddedFallback(ctx, eventBus, msg, log)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message_id": msg.ID})
|
||||
c.JSON(http.StatusOK, gin.H{testMessageIDKey: msg.ID, testTurnIDKey: msg.TurnID})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -836,10 +866,10 @@ func publishMessageAddedFallback(ctx context.Context, eventBus bus.EventBus, msg
|
||||
return
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"message_id": msg.ID,
|
||||
"session_id": msg.TaskSessionID,
|
||||
"task_id": msg.TaskID,
|
||||
"turn_id": msg.TurnID,
|
||||
testMessageIDKey: msg.ID,
|
||||
testSessionIDKey: msg.TaskSessionID,
|
||||
testTaskIDKey: msg.TaskID,
|
||||
testTurnIDKey: msg.TurnID,
|
||||
"author_type": string(msg.AuthorType),
|
||||
"content": msg.Content,
|
||||
"type": string(msg.Type),
|
||||
@@ -847,6 +877,7 @@ func publishMessageAddedFallback(ctx context.Context, eventBus bus.EventBus, msg
|
||||
// RFC3339Nano preserves fractional precision so seeded user prompts
|
||||
// order deterministically on the client.
|
||||
messageCreatedAtKey: msg.CreatedAt.Format(time.RFC3339Nano),
|
||||
"updated_at": msg.UpdatedAt.Format(time.RFC3339Nano),
|
||||
}
|
||||
// User rows carry their stable prompt ordinal in the live WS event.
|
||||
if msg.PromptIndex > 0 {
|
||||
@@ -855,8 +886,128 @@ func publishMessageAddedFallback(ctx context.Context, eventBus bus.EventBus, msg
|
||||
if msg.Metadata != nil {
|
||||
data["metadata"] = msg.Metadata
|
||||
}
|
||||
if err := eventBus.Publish(ctx, events.MessageAdded, bus.NewEvent(events.MessageAdded, "e2e-mock", data)); err != nil {
|
||||
log.Warn("test harness: publish message added failed", zap.Error(err))
|
||||
publishMessageEvent(ctx, eventBus, events.MessageAdded, data, log)
|
||||
}
|
||||
|
||||
func publishMessageEvent(
|
||||
ctx context.Context,
|
||||
eventBus bus.EventBus,
|
||||
eventType string,
|
||||
data map[string]interface{},
|
||||
log *logger.Logger,
|
||||
) {
|
||||
if eventBus == nil {
|
||||
return
|
||||
}
|
||||
if err := eventBus.Publish(ctx, eventType, bus.NewEvent(eventType, "e2e-mock", data)); err != nil {
|
||||
log.Warn("test harness: publish message event failed", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
func updateMessageHandler(
|
||||
repo *sqliterepo.Repository,
|
||||
eventBus bus.EventBus,
|
||||
log *logger.Logger,
|
||||
) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var request struct {
|
||||
Content string `json:"content" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
message, err := repo.GetMessage(c.Request.Context(), c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "message not found"})
|
||||
return
|
||||
}
|
||||
message.Content = request.Content
|
||||
message.UpdatedAt = time.Now().UTC()
|
||||
if err := repo.UpdateMessage(c.Request.Context(), message); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
data := messageEventData(message)
|
||||
publishMessageEvent(c.Request.Context(), eventBus, events.MessageUpdated, data, log)
|
||||
c.JSON(http.StatusOK, gin.H{"message_id": message.ID, "updated_at": data["updated_at"]})
|
||||
}
|
||||
}
|
||||
|
||||
func deleteMessageHandler(
|
||||
repo *sqliterepo.Repository,
|
||||
eventBus bus.EventBus,
|
||||
log *logger.Logger,
|
||||
) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
message, err := repo.GetMessage(c.Request.Context(), c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "message not found"})
|
||||
return
|
||||
}
|
||||
if err := repo.DeleteMessage(c.Request.Context(), message.ID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
data := messageEventData(message)
|
||||
publishMessageEvent(c.Request.Context(), eventBus, events.MessageDeleted, data, log)
|
||||
c.JSON(http.StatusOK, gin.H{testMessageIDKey: message.ID})
|
||||
}
|
||||
}
|
||||
|
||||
func messageEventData(message *models.Message) map[string]interface{} {
|
||||
data := map[string]interface{}{
|
||||
testMessageIDKey: message.ID, testSessionIDKey: message.TaskSessionID,
|
||||
testTaskIDKey: message.TaskID, testTurnIDKey: message.TurnID,
|
||||
"author_type": string(message.AuthorType), "content": message.Content,
|
||||
"type": string(message.Type),
|
||||
messageCreatedAtKey: message.CreatedAt.Format(time.RFC3339Nano),
|
||||
"updated_at": message.UpdatedAt.Format(time.RFC3339Nano),
|
||||
}
|
||||
if message.PromptIndex > 0 {
|
||||
data["prompt_index"] = message.PromptIndex
|
||||
}
|
||||
if message.Metadata != nil {
|
||||
data["metadata"] = message.Metadata
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func completeTurnHandler(
|
||||
repo *sqliterepo.Repository,
|
||||
eventBus bus.EventBus,
|
||||
log *logger.Logger,
|
||||
) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
turnID := c.Param("id")
|
||||
if err := repo.CompleteTurn(c.Request.Context(), turnID); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "turn not found"})
|
||||
return
|
||||
}
|
||||
turn, err := repo.GetTurn(c.Request.Context(), turnID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"id": turn.ID, testSessionIDKey: turn.TaskSessionID, testTaskIDKey: turn.TaskID,
|
||||
"started_at": turn.StartedAt.UTC().Format(time.RFC3339Nano),
|
||||
testCompletedAtKey: nil,
|
||||
"updated_at": turn.UpdatedAt.UTC().Format(time.RFC3339Nano),
|
||||
}
|
||||
if turn.CompletedAt != nil {
|
||||
data[testCompletedAtKey] = turn.CompletedAt.UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
if eventBus != nil {
|
||||
if err := eventBus.Publish(
|
||||
c.Request.Context(),
|
||||
events.TurnCompleted,
|
||||
bus.NewEvent(events.TurnCompleted, "e2e-mock", data),
|
||||
); err != nil {
|
||||
log.Warn("test harness: publish turn completion failed", zap.Error(err))
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{testTurnIDKey: turn.ID, testCompletedAtKey: turn.CompletedAt})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -924,8 +924,11 @@ func (s *Service) handleAgentReady(ctx context.Context, data watcher.AgentEventD
|
||||
return
|
||||
}
|
||||
|
||||
// Explicit agent-requested moves (move_task_kandev) take precedence over pending clarifications.
|
||||
// Agent.ready is the authoritative turn boundary. Detach the request before
|
||||
// publishing WAITING_FOR_INPUT so a client that immediately reloads sees the
|
||||
// deferred-answer metadata in its first snapshot.
|
||||
if s.sessionHasPendingClarification(ctx, data.SessionID) {
|
||||
s.detachClarificationWaiters(ctx, data.SessionID)
|
||||
s.logger.Info("deferring on_turn_complete while clarification is pending",
|
||||
zap.String("task_id", data.TaskID),
|
||||
zap.String("session_id", data.SessionID))
|
||||
|
||||
@@ -108,6 +108,9 @@ func TestPauseForClarificationInput_SameTurnBarrierUnchanged(t *testing.T) {
|
||||
agentMgr := &mockAgentManager{repoForExecutionLookup: repo}
|
||||
svc := createEngineService(t, repo, stepGetter, agentMgr)
|
||||
svc.turnService = &repoBackedTurnService{repo: repo}
|
||||
svc.SetClarificationCanceller(
|
||||
clarification.NewCanceller(clarification.NewStore(time.Minute), repo, nil, testLogger()),
|
||||
)
|
||||
|
||||
// Manually call handleAgentReady without ever going through
|
||||
// PauseForClarificationInput — the same-turn barrier must hold.
|
||||
@@ -117,6 +120,13 @@ func TestPauseForClarificationInput_SameTurnBarrierUnchanged(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("get task: %v", err)
|
||||
}
|
||||
clarificationMessage, err := repo.GetMessage(ctx, "clarification-s1")
|
||||
if err != nil {
|
||||
t.Fatalf("get clarification: %v", err)
|
||||
}
|
||||
if detached, _ := clarificationMessage.Metadata["agent_disconnected"].(bool); !detached {
|
||||
t.Fatalf("clarification metadata = %#v, want agent_disconnected=true", clarificationMessage.Metadata)
|
||||
}
|
||||
if task.WorkflowStepID != "step1" {
|
||||
t.Fatalf("same-turn barrier broken: workflow step = %q, want step1", task.WorkflowStepID)
|
||||
}
|
||||
|
||||
@@ -3555,7 +3555,7 @@ func (s *Service) prepareWorkflowReplacementSession(
|
||||
launchProfileID, err := s.resolveDynamicLaunchExecution(ctx, newSession, newAgentProfileID, true)
|
||||
if err != nil {
|
||||
resolutionErr := fmt.Errorf("failed to resolve workflow replacement profile: %w", err)
|
||||
if deleteErr := s.deleteSessionAndCleanAttachments(ctx, newSession); deleteErr != nil {
|
||||
if deleteErr := s.deleteSessionAndPublishRemoval(ctx, taskID, sessionID); deleteErr != nil {
|
||||
s.logger.Warn("failed to delete workflow replacement after profile resolution failure",
|
||||
zap.String("task_id", taskID),
|
||||
zap.String("session_id", sessionID),
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
"github.com/kandev/kandev/internal/orchestrator/executor"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
@@ -72,3 +74,36 @@ func TestDeleteSession_PreservesTaskWorkspaceAndNeverEnqueuesCleanup(t *testing.
|
||||
t.Fatalf("cleanup jobs = %d, want 0 — session deletion must not reserve cleanup", jobCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSessionPublishesTerminalOrderedEvent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := setupTestRepo(t)
|
||||
seedTaskAndSession(t, repo, "task-terminal", "session-terminal", models.TaskSessionStateCompleted)
|
||||
svc := createTestServiceWithAgent(
|
||||
repo,
|
||||
newMockStepGetter(),
|
||||
newMockTaskRepo(),
|
||||
&mockAgentManager{},
|
||||
)
|
||||
eventBus := bus.NewMemoryEventBus(testLogger())
|
||||
t.Cleanup(func() { eventBus.Close() })
|
||||
svc.eventBus = eventBus
|
||||
var received map[string]interface{}
|
||||
if _, err := svc.eventBus.Subscribe(
|
||||
events.SessionRemoved,
|
||||
func(_ context.Context, event *bus.Event) error {
|
||||
received, _ = event.Data.(map[string]interface{})
|
||||
return nil
|
||||
},
|
||||
); err != nil {
|
||||
t.Fatalf("subscribe: %v", err)
|
||||
}
|
||||
|
||||
if err := svc.DeleteSession(ctx, "session-terminal"); err != nil {
|
||||
t.Fatalf("DeleteSession: %v", err)
|
||||
}
|
||||
|
||||
if received["session_id"] != "session-terminal" || received["task_id"] != "task-terminal" {
|
||||
t.Fatalf("terminal event payload = %#v", received)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1837,11 +1837,7 @@ func (s *Service) prepareSessionForStartWithWorkflowRoute(
|
||||
// session row. Compensate before returning so callers never observe a
|
||||
// partial sibling session when the required parent/group workspace is
|
||||
// unavailable.
|
||||
createdSession, lookupErr := s.repo.GetTaskSession(ctx, sessionID)
|
||||
if lookupErr != nil {
|
||||
s.logger.Warn("failed to load inherited workspace session for compensation",
|
||||
zap.String("session_id", sessionID), zap.Error(lookupErr))
|
||||
} else if deleteErr := s.deleteSessionAndCleanAttachments(ctx, createdSession); deleteErr != nil {
|
||||
if deleteErr := s.deleteSessionAndPublishRemoval(ctx, task.ID, sessionID); deleteErr != nil {
|
||||
s.logger.Warn("failed to compensate inherited workspace session",
|
||||
zap.String("session_id", sessionID), zap.Error(deleteErr))
|
||||
}
|
||||
@@ -4309,6 +4305,32 @@ func (s *Service) StopSessionSynchronously(ctx context.Context, sessionID string
|
||||
return s.executor.StopSessionSynchronously(ctx, sessionID, reason, force)
|
||||
}
|
||||
|
||||
// deleteSessionAndPublishRemoval commits a session deletion before publishing
|
||||
// the terminal event consumed by ordered conversation subscribers.
|
||||
func (s *Service) deleteSessionAndPublishRemoval(ctx context.Context, taskID, sessionID string) error {
|
||||
session, err := s.repo.GetTaskSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.deleteSessionAndCleanAttachments(ctx, session); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.eventBus != nil {
|
||||
if err := s.eventBus.Publish(ctx, events.SessionRemoved, bus.NewEvent(
|
||||
events.SessionRemoved,
|
||||
"orchestrator",
|
||||
map[string]interface{}{metaKeySessionID: sessionID, metaKeyTaskID: taskID},
|
||||
)); err != nil {
|
||||
s.logger.Warn("session deleted but removal event publish failed",
|
||||
zap.String("task_id", taskID),
|
||||
zap.String("session_id", sessionID),
|
||||
zap.Error(err))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// DeleteSession deletes a session that is not currently running.
|
||||
func (s *Service) DeleteSession(ctx context.Context, sessionID string) error {
|
||||
if err := s.authorizeSession(ctx, sessionID); err != nil {
|
||||
@@ -4420,7 +4442,7 @@ func (s *Service) deleteSessionAndPublishError(ctx context.Context, session *mod
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
if err := s.deleteSessionAndCleanAttachments(ctx, session); err != nil {
|
||||
if err := s.deleteSessionAndPublishRemoval(ctx, session.TaskID, session.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
s.publishDeletedSessionError(ctx, session.TaskID, session.ID)
|
||||
@@ -4457,7 +4479,6 @@ func (s *Service) publishDeletedSessionError(ctx context.Context, taskID, sessio
|
||||
zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) newestRetainedSessionError(
|
||||
ctx context.Context,
|
||||
taskID string,
|
||||
|
||||
@@ -0,0 +1,685 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/kandev/kandev/internal/auth/authn"
|
||||
"github.com/kandev/kandev/internal/plugins/store"
|
||||
"github.com/kandev/kandev/internal/sysprompt"
|
||||
taskmodels "github.com/kandev/kandev/internal/task/models"
|
||||
taskservice "github.com/kandev/kandev/internal/task/service"
|
||||
)
|
||||
|
||||
const conversationNoStore = "no-store"
|
||||
|
||||
// ConversationReader is the narrow task-service surface needed by browser
|
||||
// plugin conversation reads. The task service applies workspace authorization.
|
||||
type ConversationReader interface {
|
||||
GetTaskSession(context.Context, string) (*taskmodels.TaskSession, error)
|
||||
ListMessagesPaginated(
|
||||
context.Context,
|
||||
taskservice.ListMessagesRequest,
|
||||
) ([]*taskmodels.Message, bool, error)
|
||||
ListTurnsBySession(context.Context, string) ([]*taskmodels.Turn, error)
|
||||
}
|
||||
|
||||
type conversationError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Retryable bool `json:"retryable"`
|
||||
}
|
||||
|
||||
type conversationErrorEnvelope struct {
|
||||
Error conversationError `json:"error"`
|
||||
}
|
||||
|
||||
type conversationBindingResponse struct {
|
||||
BindingToken string `json:"bindingToken"`
|
||||
Generation int64 `json:"generation"`
|
||||
ExpiresAt time.Time `json:"expiresAt"`
|
||||
}
|
||||
|
||||
type conversationMessageDTO struct {
|
||||
ID string `json:"id"`
|
||||
TaskID *string `json:"taskId"`
|
||||
SessionID string `json:"sessionId"`
|
||||
TurnID *string `json:"turnId,omitempty"`
|
||||
AuthorType string `json:"authorType"`
|
||||
Type string `json:"type"`
|
||||
Content string `json:"content"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
PromptIndex *int `json:"promptIndex,omitempty"`
|
||||
SenderTaskID *string `json:"senderTaskId,omitempty"`
|
||||
}
|
||||
|
||||
func loadFallbackTurns(ctx context.Context, reader ConversationReader, sessionID, taskID string) ([]*taskmodels.Turn, error) {
|
||||
turns, err := reader.ListTurnsBySession(ctx, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return filterTurnsByTask(turns, taskID), nil
|
||||
}
|
||||
|
||||
func filterTurnsByTask(turns []*taskmodels.Turn, taskID string) []*taskmodels.Turn {
|
||||
if taskID == "" {
|
||||
return turns
|
||||
}
|
||||
filtered := make([]*taskmodels.Turn, 0, len(turns))
|
||||
for _, turn := range turns {
|
||||
if turn.TaskID == taskID {
|
||||
filtered = append(filtered, turn)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
type conversationMessagesResponse struct {
|
||||
Messages []conversationMessageDTO `json:"messages"`
|
||||
HasMore bool `json:"hasMore"`
|
||||
Cursor *string `json:"cursor"`
|
||||
}
|
||||
|
||||
type conversationTurnDTO struct {
|
||||
ID string `json:"id"`
|
||||
TaskID *string `json:"taskId"`
|
||||
SessionID string `json:"sessionId"`
|
||||
StartedAt string `json:"startedAt"`
|
||||
CompletedAt *string `json:"completedAt,omitempty"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type conversationTurnsResponse struct {
|
||||
Turns []conversationTurnDTO `json:"turns"`
|
||||
}
|
||||
|
||||
type conversationContinuationRenewRequest struct {
|
||||
Cursor string `json:"cursor"`
|
||||
SnapshotToken string `json:"snapshot_token"`
|
||||
}
|
||||
|
||||
type conversationContinuationRenewResponse struct {
|
||||
Cursor string `json:"cursor"`
|
||||
SnapshotToken string `json:"snapshot_token"`
|
||||
Cutoff int64 `json:"cutoff"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
type conversationMessageQuery struct {
|
||||
limit int
|
||||
sort string
|
||||
authorType string
|
||||
authors []string
|
||||
taskID *string
|
||||
cursor string
|
||||
}
|
||||
|
||||
func registerConversationRoutes(api *gin.RouterGroup, ctrl *Controller) {
|
||||
api.GET("/:id/conversation/binding", ctrl.conversationBinding)
|
||||
api.GET("/:id/conversation/task-sessions/:sessionId/messages", ctrl.conversationMessages)
|
||||
api.GET("/:id/conversation/task-sessions/:sessionId/turns", ctrl.conversationTurns)
|
||||
api.POST("/:id/conversation/continuation/renew", ctrl.conversationContinuationRenew)
|
||||
}
|
||||
|
||||
func (c *Controller) conversationBinding(ctx *gin.Context) {
|
||||
ctx.Header("Cache-Control", conversationNoStore)
|
||||
identity, ok := authn.IdentityFromContext(ctx.Request.Context())
|
||||
if !ok {
|
||||
writeConversationError(ctx, http.StatusUnauthorized, "unauthenticated", "authentication required", false)
|
||||
return
|
||||
}
|
||||
record, ok := c.conversationRecord(ctx.Param("id"))
|
||||
if !ok {
|
||||
writeConversationError(ctx, http.StatusNotFound, "not_found", "plugin not found", false)
|
||||
return
|
||||
}
|
||||
generation := conversationGeneration(record.InstalledAt)
|
||||
token, expiresAt, err := c.conversationTokens.mintBinding(record.ID, identity.UserID, generation)
|
||||
if err != nil {
|
||||
c.log.Error("mint plugin conversation binding", zap.Error(err))
|
||||
writeConversationError(ctx, http.StatusInternalServerError, "upstream_failure", "conversation service unavailable", true)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, conversationBindingResponse{
|
||||
BindingToken: token,
|
||||
Generation: generation,
|
||||
ExpiresAt: expiresAt,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) conversationRecord(pluginID string) (*store.Record, bool) {
|
||||
record, err := c.svc.Get(pluginID)
|
||||
if err != nil || record.Status != StatusActive || !record.Capabilities.CanRead("messages") {
|
||||
return nil, false
|
||||
}
|
||||
return record, true
|
||||
}
|
||||
|
||||
//nolint:cyclop,goconst // This handler owns authorization, snapshot validation, pagination, and cursor minting.
|
||||
func (c *Controller) conversationMessages(ctx *gin.Context) {
|
||||
ctx.Header("Cache-Control", conversationNoStore)
|
||||
record, identity, ok := c.authorizeConversationRequest(ctx)
|
||||
if !ok || !c.validBinding(ctx, record, identity.UserID) {
|
||||
return
|
||||
}
|
||||
query, ok := parseConversationMessageQuery(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if c.conversationReader == nil {
|
||||
writeConversationError(ctx, http.StatusInternalServerError, "upstream_failure", "conversation service unavailable", true)
|
||||
return
|
||||
}
|
||||
sessionID := ctx.Param("sessionId")
|
||||
snapshot, ok := c.validSnapshot(ctx, record, identity.UserID, sessionID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
session, err := c.conversationReader.GetTaskSession(ctx.Request.Context(), sessionID)
|
||||
if err != nil || session == nil || session.ID != sessionID {
|
||||
if !c.svc.isSessionRemoved(sessionID) {
|
||||
writeConversationError(ctx, http.StatusNotFound, "not_found", "task session not found", false)
|
||||
return
|
||||
}
|
||||
} else if query.taskID != nil && *query.taskID != session.TaskID {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "task_id does not match the task session", false)
|
||||
return
|
||||
}
|
||||
request := taskservice.ListMessagesRequest{
|
||||
TaskSessionID: sessionID,
|
||||
Limit: query.limit,
|
||||
Sort: query.sort,
|
||||
AuthorType: query.authorType,
|
||||
// Parity with the journal branch: multi-author filters and an
|
||||
// explicit task_id must narrow the fallback page the same way.
|
||||
AuthorTypes: query.authors,
|
||||
TaskID: optionalStringValue(query.taskID),
|
||||
}
|
||||
if !c.applyConversationMessageCursor(ctx, record, identity.UserID, sessionID, query, snapshot, &request) {
|
||||
return
|
||||
}
|
||||
var messages []*taskmodels.Message
|
||||
var hasMore bool
|
||||
if c.svc.HasConversationJournal() {
|
||||
cursorID := request.After
|
||||
if query.sort == "desc" {
|
||||
cursorID = request.Before
|
||||
}
|
||||
messages, hasMore, err = c.svc.conversationMessagesAt(
|
||||
ctx.Request.Context(), sessionID, uint64(snapshot.Cutoff),
|
||||
query.taskID, query.authors, query.sort, cursorID, query.limit,
|
||||
)
|
||||
} else {
|
||||
messages, hasMore, err = c.conversationReader.ListMessagesPaginated(ctx.Request.Context(), request)
|
||||
}
|
||||
if err != nil {
|
||||
writeConversationError(ctx, http.StatusInternalServerError, "upstream_failure", "conversation service unavailable", true)
|
||||
return
|
||||
}
|
||||
response, ok := c.buildConversationMessagesResponse(
|
||||
ctx,
|
||||
record,
|
||||
identity.UserID,
|
||||
sessionID,
|
||||
query,
|
||||
snapshot,
|
||||
messages,
|
||||
hasMore,
|
||||
)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
func (c *Controller) applyConversationMessageCursor(
|
||||
ctx *gin.Context,
|
||||
record *store.Record,
|
||||
userID string,
|
||||
sessionID string,
|
||||
query conversationMessageQuery,
|
||||
snapshot conversationTokenClaims,
|
||||
request *taskservice.ListMessagesRequest,
|
||||
) bool {
|
||||
if query.cursor == "" {
|
||||
return true
|
||||
}
|
||||
claims, err := c.conversationTokens.parse(query.cursor)
|
||||
if err != nil || !validConversationMessageCursor(claims, record, userID, sessionID, query, snapshot) {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "invalid cursor", false)
|
||||
return false
|
||||
}
|
||||
if query.sort == "asc" {
|
||||
request.After = claims.LastID
|
||||
} else {
|
||||
request.Before = claims.LastID
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func validConversationMessageCursor(
|
||||
claims conversationTokenClaims,
|
||||
record *store.Record,
|
||||
userID string,
|
||||
sessionID string,
|
||||
query conversationMessageQuery,
|
||||
snapshot conversationTokenClaims,
|
||||
) bool {
|
||||
return claims.Kind == tokenKindCursor &&
|
||||
claims.PluginID == record.ID &&
|
||||
claims.UserID == userID &&
|
||||
claims.Generation == conversationGeneration(record.InstalledAt) &&
|
||||
claims.SessionID == sessionID &&
|
||||
equalOptionalString(claims.TaskID, query.taskID) &&
|
||||
claims.Sort == query.sort &&
|
||||
equalStrings(claims.Authors, query.authors) &&
|
||||
claims.LastID != "" &&
|
||||
claims.Cutoff == snapshot.Cutoff &&
|
||||
claims.Fingerprint == snapshot.Fingerprint
|
||||
}
|
||||
|
||||
func (c *Controller) buildConversationMessagesResponse(
|
||||
ctx *gin.Context,
|
||||
record *store.Record,
|
||||
userID string,
|
||||
sessionID string,
|
||||
query conversationMessageQuery,
|
||||
snapshot conversationTokenClaims,
|
||||
messages []*taskmodels.Message,
|
||||
hasMore bool,
|
||||
) (conversationMessagesResponse, bool) {
|
||||
response := conversationMessagesResponse{
|
||||
Messages: make([]conversationMessageDTO, 0, len(messages)),
|
||||
HasMore: hasMore,
|
||||
}
|
||||
for _, message := range messages {
|
||||
response.Messages = append(response.Messages, conversationMessageModelToDTO(message))
|
||||
}
|
||||
if !hasMore {
|
||||
return response, true
|
||||
}
|
||||
if len(messages) == 0 {
|
||||
writeConversationError(ctx, http.StatusInternalServerError, "upstream_failure", "conversation service returned an invalid page", true)
|
||||
return conversationMessagesResponse{}, false
|
||||
}
|
||||
cursor, err := c.conversationTokens.mintCursor(
|
||||
record.ID,
|
||||
userID,
|
||||
conversationGeneration(record.InstalledAt),
|
||||
sessionID,
|
||||
query.taskID,
|
||||
query.sort,
|
||||
query.authors,
|
||||
messages[len(messages)-1].ID,
|
||||
snapshot.Cutoff,
|
||||
snapshot.Fingerprint,
|
||||
)
|
||||
if err != nil {
|
||||
writeConversationError(ctx, http.StatusInternalServerError, "upstream_failure", "conversation service unavailable", true)
|
||||
return conversationMessagesResponse{}, false
|
||||
}
|
||||
response.Cursor = &cursor
|
||||
return response, true
|
||||
}
|
||||
|
||||
//nolint:cyclop // Turn lookup supports primary, task-scoped, and fallback sources.
|
||||
func (c *Controller) conversationTurns(ctx *gin.Context) {
|
||||
ctx.Header("Cache-Control", conversationNoStore)
|
||||
record, identity, ok := c.authorizeConversationRequest(ctx)
|
||||
if !ok || !c.validBinding(ctx, record, identity.UserID) {
|
||||
return
|
||||
}
|
||||
if c.conversationReader == nil {
|
||||
writeConversationError(ctx, http.StatusInternalServerError, "upstream_failure", "conversation service unavailable", true)
|
||||
return
|
||||
}
|
||||
sessionID := ctx.Param("sessionId")
|
||||
snapshot, ok := c.validSnapshot(ctx, record, identity.UserID, sessionID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var taskID *string
|
||||
if rawTaskID, exists := ctx.GetQuery("task_id"); exists && rawTaskID != nullJSONValue {
|
||||
if rawTaskID == "" {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "task_id does not match the task session", false)
|
||||
return
|
||||
}
|
||||
taskID = &rawTaskID
|
||||
}
|
||||
session, err := c.conversationReader.GetTaskSession(ctx.Request.Context(), sessionID)
|
||||
sessionFound := err == nil && session != nil && session.ID == sessionID
|
||||
if !sessionFound {
|
||||
if !c.svc.isSessionRemoved(sessionID) {
|
||||
writeConversationError(ctx, http.StatusNotFound, "not_found", "task session not found", false)
|
||||
return
|
||||
}
|
||||
} else if taskID != nil && *taskID != session.TaskID {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "task_id does not match the task session", false)
|
||||
return
|
||||
}
|
||||
var scopedTaskID *string
|
||||
if taskID != nil {
|
||||
scopedTaskID = taskID
|
||||
} else if sessionFound {
|
||||
scopedTaskID = &session.TaskID
|
||||
}
|
||||
var turns []*taskmodels.Turn
|
||||
if c.svc.HasConversationJournal() {
|
||||
turns, err = c.svc.conversationTurnsAt(ctx.Request.Context(), sessionID, uint64(snapshot.Cutoff), scopedTaskID)
|
||||
} else {
|
||||
// The no-journal fallback must honor the requested/inherited task
|
||||
// scope like the journal branch: a session that ever accumulated
|
||||
// mixed-task turn rows must not leak turns outside the validated task.
|
||||
turns, err = loadFallbackTurns(ctx.Request.Context(), c.conversationReader, sessionID, *scopedTaskID)
|
||||
}
|
||||
if err != nil {
|
||||
writeConversationError(ctx, http.StatusInternalServerError, "upstream_failure", "conversation service unavailable", true)
|
||||
return
|
||||
}
|
||||
response := conversationTurnsResponse{Turns: make([]conversationTurnDTO, 0, len(turns))}
|
||||
for _, turn := range turns {
|
||||
response.Turns = append(response.Turns, conversationTurnModelToDTO(turn))
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (c *Controller) conversationContinuationRenew(ctx *gin.Context) {
|
||||
ctx.Header("Cache-Control", conversationNoStore)
|
||||
record, identity, ok := c.authorizeConversationRequest(ctx)
|
||||
if !ok || !c.validBinding(ctx, record, identity.UserID) {
|
||||
return
|
||||
}
|
||||
var request conversationContinuationRenewRequest
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil ||
|
||||
request.Cursor == "" ||
|
||||
request.SnapshotToken == "" {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "cursor and snapshot_token are required", false)
|
||||
return
|
||||
}
|
||||
cursorClaims, cursorErr := c.conversationTokens.parse(request.Cursor)
|
||||
snapshotClaims, snapshotErr := c.conversationTokens.parse(request.SnapshotToken)
|
||||
if !validContinuationClaims(cursorClaims, snapshotClaims, cursorErr, snapshotErr) {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "invalid continuation", false)
|
||||
return
|
||||
}
|
||||
if cursorClaims.PluginID != record.ID ||
|
||||
cursorClaims.UserID != identity.UserID ||
|
||||
cursorClaims.Generation != conversationGeneration(record.InstalledAt) {
|
||||
writeConversationError(ctx, http.StatusConflict, "upstream_failure", "conversation generation changed", true)
|
||||
return
|
||||
}
|
||||
cursor, expiresAt, err := c.conversationTokens.renew(cursorClaims)
|
||||
if err != nil {
|
||||
writeConversationError(ctx, http.StatusInternalServerError, "upstream_failure", "conversation service unavailable", true)
|
||||
return
|
||||
}
|
||||
snapshotToken, _, err := c.conversationTokens.renew(snapshotClaims)
|
||||
if err != nil {
|
||||
writeConversationError(ctx, http.StatusInternalServerError, "upstream_failure", "conversation service unavailable", true)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, conversationContinuationRenewResponse{
|
||||
Cursor: cursor,
|
||||
SnapshotToken: snapshotToken,
|
||||
Cutoff: cursorClaims.Cutoff,
|
||||
Fingerprint: cursorClaims.Fingerprint,
|
||||
ExpiresAt: expiresAt,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) authorizeConversationRequest(
|
||||
ctx *gin.Context,
|
||||
) (*store.Record, authn.Identity, bool) {
|
||||
identity, ok := authn.IdentityFromContext(ctx.Request.Context())
|
||||
if !ok {
|
||||
writeConversationError(ctx, http.StatusUnauthorized, "unauthenticated", "authentication required", false)
|
||||
return nil, authn.Identity{}, false
|
||||
}
|
||||
record, ok := c.conversationRecord(ctx.Param("id"))
|
||||
if !ok {
|
||||
writeConversationError(ctx, http.StatusNotFound, "not_found", "plugin not found", false)
|
||||
return nil, authn.Identity{}, false
|
||||
}
|
||||
return record, identity, true
|
||||
}
|
||||
|
||||
func (c *Controller) validBinding(ctx *gin.Context, record *store.Record, userID string) bool {
|
||||
claims, err := c.conversationTokens.parse(ctx.GetHeader("X-Kandev-Plugin-Binding"))
|
||||
if err != nil ||
|
||||
claims.Kind != tokenKindBinding ||
|
||||
claims.PluginID != record.ID ||
|
||||
claims.UserID != userID ||
|
||||
claims.Generation != conversationGeneration(record.InstalledAt) {
|
||||
writeConversationError(ctx, http.StatusUnauthorized, "unauthenticated", "invalid conversation binding", false)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Controller) validSnapshot(
|
||||
ctx *gin.Context,
|
||||
record *store.Record,
|
||||
userID string,
|
||||
sessionID string,
|
||||
) (conversationTokenClaims, bool) {
|
||||
claims, err := c.conversationTokens.parse(ctx.GetHeader("X-Kandev-Snapshot-Token"))
|
||||
if err != nil ||
|
||||
claims.Kind != tokenKindSnapshot ||
|
||||
claims.PluginID != record.ID ||
|
||||
claims.UserID != userID ||
|
||||
claims.Generation != conversationGeneration(record.InstalledAt) ||
|
||||
claims.SessionID != sessionID {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "invalid snapshot token", false)
|
||||
return conversationTokenClaims{}, false
|
||||
}
|
||||
return claims, true
|
||||
}
|
||||
|
||||
func parseConversationMessageQuery(ctx *gin.Context) (conversationMessageQuery, bool) {
|
||||
limit, ok := parseConversationMessageLimit(ctx)
|
||||
if !ok {
|
||||
return conversationMessageQuery{}, false
|
||||
}
|
||||
sortOrder, ok := parseConversationMessageSort(ctx)
|
||||
if !ok {
|
||||
return conversationMessageQuery{}, false
|
||||
}
|
||||
authors, authorType, ok := parseConversationMessageAuthors(ctx)
|
||||
if !ok {
|
||||
return conversationMessageQuery{}, false
|
||||
}
|
||||
taskID, ok := parseOptionalConversationQuery(ctx, "task_id")
|
||||
if !ok {
|
||||
return conversationMessageQuery{}, false
|
||||
}
|
||||
cursor, ok := parseOptionalConversationQuery(ctx, "cursor")
|
||||
if !ok {
|
||||
return conversationMessageQuery{}, false
|
||||
}
|
||||
if taskID != nil && *taskID == nullJSONValue {
|
||||
taskID = nil
|
||||
}
|
||||
return conversationMessageQuery{
|
||||
limit: limit, sort: sortOrder, authorType: authorType,
|
||||
authors: authors, taskID: taskID, cursor: optionalStringValue(cursor),
|
||||
}, true
|
||||
}
|
||||
|
||||
func parseConversationMessageLimit(ctx *gin.Context) (int, bool) {
|
||||
raw, exists := ctx.GetQuery("limit")
|
||||
if !exists {
|
||||
return 20, true
|
||||
}
|
||||
limit, err := strconv.Atoi(raw)
|
||||
if err != nil || limit < 1 || limit > 100 {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "limit must be between 1 and 100", false)
|
||||
return 0, false
|
||||
}
|
||||
return limit, true
|
||||
}
|
||||
|
||||
func parseConversationMessageSort(ctx *gin.Context) (string, bool) {
|
||||
raw, exists := ctx.GetQuery("sort")
|
||||
if !exists {
|
||||
return "desc", true
|
||||
}
|
||||
if raw != "asc" && raw != "desc" {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "sort must be asc or desc", false)
|
||||
return "", false
|
||||
}
|
||||
return raw, true
|
||||
}
|
||||
|
||||
func parseConversationMessageAuthors(ctx *gin.Context) ([]string, string, bool) {
|
||||
authors := ctx.QueryArray("author_type")
|
||||
if len(authors) > 2 {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "unsupported author_type", false)
|
||||
return nil, "", false
|
||||
}
|
||||
seen := make(map[string]struct{}, len(authors))
|
||||
for _, author := range authors {
|
||||
if author != string(taskmodels.MessageAuthorUser) && author != string(taskmodels.MessageAuthorAgent) {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "unsupported author_type", false)
|
||||
return nil, "", false
|
||||
}
|
||||
if _, duplicate := seen[author]; duplicate {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", "duplicate author_type", false)
|
||||
return nil, "", false
|
||||
}
|
||||
seen[author] = struct{}{}
|
||||
}
|
||||
if len(authors) == 1 {
|
||||
return authors, authors[0], true
|
||||
}
|
||||
return authors, "", true
|
||||
}
|
||||
|
||||
func parseOptionalConversationQuery(ctx *gin.Context, name string) (*string, bool) {
|
||||
raw, exists := ctx.GetQuery(name)
|
||||
if !exists {
|
||||
return nil, true
|
||||
}
|
||||
if raw == "" {
|
||||
writeConversationError(ctx, http.StatusBadRequest, "invalid_query", name+" must not be empty", false)
|
||||
return nil, false
|
||||
}
|
||||
return &raw, true
|
||||
}
|
||||
|
||||
func optionalStringValue(value *string) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return *value
|
||||
}
|
||||
|
||||
func conversationMessageModelToDTO(message *taskmodels.Message) conversationMessageDTO {
|
||||
messageType := string(message.Type)
|
||||
if messageType == "" {
|
||||
messageType = string(taskmodels.MessageTypeMessage)
|
||||
}
|
||||
updatedAt := message.UpdatedAt
|
||||
if updatedAt.IsZero() {
|
||||
updatedAt = message.CreatedAt
|
||||
}
|
||||
dto := conversationMessageDTO{
|
||||
ID: message.ID,
|
||||
TaskID: nonEmptyStringPointer(message.TaskID),
|
||||
SessionID: message.TaskSessionID,
|
||||
TurnID: nonEmptyStringPointer(message.TurnID),
|
||||
AuthorType: string(message.AuthorType),
|
||||
Type: messageType,
|
||||
Content: sysprompt.StripSystemContent(message.Content),
|
||||
CreatedAt: message.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
UpdatedAt: updatedAt.UTC().Format(time.RFC3339Nano),
|
||||
}
|
||||
if message.PromptIndex > 0 {
|
||||
dto.PromptIndex = &message.PromptIndex
|
||||
}
|
||||
if senderTaskID, ok := message.Metadata["sender_task_id"].(string); ok && senderTaskID != "" {
|
||||
dto.SenderTaskID = &senderTaskID
|
||||
}
|
||||
return dto
|
||||
}
|
||||
|
||||
func conversationTurnModelToDTO(turn *taskmodels.Turn) conversationTurnDTO {
|
||||
updatedAt := turn.UpdatedAt
|
||||
if updatedAt.IsZero() {
|
||||
updatedAt = turn.StartedAt
|
||||
}
|
||||
dto := conversationTurnDTO{
|
||||
ID: turn.ID,
|
||||
TaskID: nonEmptyStringPointer(turn.TaskID),
|
||||
SessionID: turn.TaskSessionID,
|
||||
StartedAt: turn.StartedAt.UTC().Format(time.RFC3339Nano),
|
||||
UpdatedAt: updatedAt.UTC().Format(time.RFC3339Nano),
|
||||
}
|
||||
if turn.CompletedAt != nil {
|
||||
completedAt := turn.CompletedAt.UTC().Format(time.RFC3339Nano)
|
||||
dto.CompletedAt = &completedAt
|
||||
}
|
||||
return dto
|
||||
}
|
||||
|
||||
func validContinuationClaims(
|
||||
cursor conversationTokenClaims,
|
||||
snapshot conversationTokenClaims,
|
||||
cursorErr error,
|
||||
snapshotErr error,
|
||||
) bool {
|
||||
return cursorErr == nil &&
|
||||
snapshotErr == nil &&
|
||||
cursor.Kind == tokenKindCursor &&
|
||||
snapshot.Kind == tokenKindSnapshot &&
|
||||
matchingContinuationClaims(cursor, snapshot)
|
||||
}
|
||||
|
||||
func matchingContinuationClaims(
|
||||
cursor conversationTokenClaims,
|
||||
snapshot conversationTokenClaims,
|
||||
) bool {
|
||||
return cursor.PluginID == snapshot.PluginID &&
|
||||
cursor.UserID == snapshot.UserID &&
|
||||
cursor.Generation == snapshot.Generation &&
|
||||
cursor.SessionID == snapshot.SessionID &&
|
||||
cursor.Cutoff == snapshot.Cutoff &&
|
||||
cursor.Fingerprint != "" &&
|
||||
cursor.Fingerprint == snapshot.Fingerprint
|
||||
}
|
||||
|
||||
func nonEmptyStringPointer(value string) *string {
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
return &value
|
||||
}
|
||||
|
||||
func equalOptionalString(left, right *string) bool {
|
||||
if left == nil || right == nil {
|
||||
return left == nil && right == nil
|
||||
}
|
||||
return *left == *right
|
||||
}
|
||||
|
||||
func equalStrings(left, right []string) bool {
|
||||
if len(left) != len(right) {
|
||||
return false
|
||||
}
|
||||
for i := range left {
|
||||
if left[i] != right[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func writeConversationError(ctx *gin.Context, status int, code, message string, retryable bool) {
|
||||
ctx.JSON(status, conversationErrorEnvelope{Error: conversationError{
|
||||
Code: code, Message: message, Retryable: retryable,
|
||||
}})
|
||||
}
|
||||
@@ -0,0 +1,606 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/kandev/kandev/internal/auth/authn"
|
||||
"github.com/kandev/kandev/internal/plugins/manifest"
|
||||
"github.com/kandev/kandev/internal/plugins/store"
|
||||
taskmodels "github.com/kandev/kandev/internal/task/models"
|
||||
taskservice "github.com/kandev/kandev/internal/task/service"
|
||||
)
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.7
|
||||
func TestConversationBindingRequiresAuthentication(t *testing.T) {
|
||||
router, _ := newTestRouter(t)
|
||||
|
||||
response := doRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/kandev-plugin-history/conversation/binding",
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
|
||||
require.Equal(t, http.StatusUnauthorized, response.Code)
|
||||
require.JSONEq(t, `{"error":{"code":"unauthenticated","message":"authentication required","retryable":false}}`, response.Body.String())
|
||||
require.Equal(t, "no-store", response.Header().Get("Cache-Control"))
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.7
|
||||
func TestConversationBindingHidesUnavailablePlugins(t *testing.T) {
|
||||
router, service := newTestRouter(t)
|
||||
service.registry.Add(&store.Record{
|
||||
Manifest: manifest.Manifest{
|
||||
ID: "kandev-plugin-history",
|
||||
Capabilities: manifest.Capabilities{},
|
||||
},
|
||||
Status: StatusActive,
|
||||
InstalledAt: time.Now().UTC(),
|
||||
})
|
||||
|
||||
response := doAuthedRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/kandev-plugin-history/conversation/binding",
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
|
||||
require.Equal(t, http.StatusNotFound, response.Code)
|
||||
require.JSONEq(t, `{"error":{"code":"not_found","message":"plugin not found","retryable":false}}`, response.Body.String())
|
||||
require.Equal(t, "no-store", response.Header().Get("Cache-Control"))
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.13
|
||||
func TestConversationBindingReturnsShortLivedGenerationGrant(t *testing.T) {
|
||||
router, service := newTestRouter(t)
|
||||
installedAt := time.Date(2026, 9, 7, 12, 0, 0, 123, time.UTC)
|
||||
service.registry.Add(&store.Record{
|
||||
Manifest: manifest.Manifest{
|
||||
ID: "kandev-plugin-history",
|
||||
Capabilities: manifest.Capabilities{
|
||||
APIRead: []string{"messages"},
|
||||
},
|
||||
},
|
||||
Status: StatusActive,
|
||||
InstalledAt: installedAt,
|
||||
})
|
||||
|
||||
response := doAuthedRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/kandev-plugin-history/conversation/binding",
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
|
||||
require.Equal(t, http.StatusOK, response.Code)
|
||||
var body struct {
|
||||
BindingToken string `json:"bindingToken"`
|
||||
Generation int64 `json:"generation"`
|
||||
ExpiresAt time.Time `json:"expiresAt"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(response.Body.Bytes(), &body))
|
||||
require.NotEmpty(t, body.BindingToken)
|
||||
require.Equal(t, conversationGeneration(installedAt), body.Generation)
|
||||
require.LessOrEqual(t, body.Generation, int64(1<<53-1))
|
||||
require.WithinDuration(t, time.Now().UTC().Add(10*time.Minute), body.ExpiresAt, time.Second)
|
||||
require.LessOrEqual(t, body.ExpiresAt.Sub(time.Now().UTC()), 10*time.Minute)
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.7
|
||||
func TestConversationReadsRequireAuthenticationBeforeLookup(t *testing.T) {
|
||||
router, _ := newTestRouter(t)
|
||||
|
||||
for _, path := range []string{
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/messages",
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/turns",
|
||||
} {
|
||||
response := doRequest(router, http.MethodGet, path, "", nil)
|
||||
require.Equal(t, http.StatusUnauthorized, response.Code, path)
|
||||
require.JSONEq(t, `{"error":{"code":"unauthenticated","message":"authentication required","retryable":false}}`, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.7
|
||||
func TestConversationReadsRejectJournalFallbackForUnauthorizedLiveSession(t *testing.T) {
|
||||
_, service := newTestRouter(t)
|
||||
service.SetConversationJournalDB(newTestPool(t).Writer())
|
||||
service.registry.Add(conversationPluginRecord("kandev-plugin-history", time.Now().UTC()))
|
||||
router := registerPluginRoutesWithIdentity(
|
||||
t,
|
||||
service,
|
||||
authn.Identity{UserID: "user_1", Role: authn.RoleMember},
|
||||
&fakeConversationReader{},
|
||||
)
|
||||
headers := conversationReadHeaders(t, service, router, "kandev-plugin-history", "session-1")
|
||||
|
||||
for _, path := range []string{
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/messages",
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/turns",
|
||||
} {
|
||||
response := doAuthedRequest(router, http.MethodGet, path, "", headers)
|
||||
require.Equal(t, http.StatusNotFound, response.Code, path)
|
||||
require.JSONEq(t, `{"error":{"code":"not_found","message":"task session not found","retryable":false}}`, response.Body.String(), path)
|
||||
}
|
||||
}
|
||||
|
||||
type fakeConversationReader struct {
|
||||
session *taskmodels.TaskSession
|
||||
messages []*taskmodels.Message
|
||||
turns []*taskmodels.Turn
|
||||
hasMore bool
|
||||
request taskservice.ListMessagesRequest
|
||||
}
|
||||
|
||||
func (f *fakeConversationReader) GetTaskSession(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
) (*taskmodels.TaskSession, error) {
|
||||
return f.session, nil
|
||||
}
|
||||
|
||||
func (f *fakeConversationReader) ListMessagesPaginated(
|
||||
_ context.Context,
|
||||
request taskservice.ListMessagesRequest,
|
||||
) ([]*taskmodels.Message, bool, error) {
|
||||
f.request = request
|
||||
return f.messages, f.hasMore, nil
|
||||
}
|
||||
|
||||
func (f *fakeConversationReader) ListTurnsBySession(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
) ([]*taskmodels.Turn, error) {
|
||||
return f.turns, nil
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.1
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.2
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.3
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.4
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.12
|
||||
func TestConversationMessagesReturnSanitizedNarrowDTOs(t *testing.T) {
|
||||
created := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
reader := &fakeConversationReader{
|
||||
session: &taskmodels.TaskSession{ID: "session-1", TaskID: "task-1"},
|
||||
messages: []*taskmodels.Message{{
|
||||
ID: "message-1",
|
||||
TaskSessionID: "session-1",
|
||||
TaskID: "task-1",
|
||||
TurnID: "turn-1",
|
||||
AuthorType: taskmodels.MessageAuthorUser,
|
||||
Content: "<kandev-system>hidden</kandev-system>Visible prompt.",
|
||||
Metadata: map[string]any{
|
||||
"sender_task_id": "sender-task",
|
||||
"sender_task_title": "must not leak",
|
||||
"raw_content": "must not leak",
|
||||
},
|
||||
CreatedAt: created,
|
||||
PromptIndex: 3,
|
||||
}},
|
||||
}
|
||||
_, service := newTestRouter(t)
|
||||
service.registry.Add(conversationPluginRecord("kandev-plugin-history", created))
|
||||
router := registerPluginRoutesWithIdentity(
|
||||
t,
|
||||
service,
|
||||
authn.Identity{UserID: "user_1", Role: authn.RoleMember},
|
||||
reader,
|
||||
)
|
||||
headers := conversationReadHeaders(t, service, router, "kandev-plugin-history", "session-1")
|
||||
|
||||
response := doAuthedRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/messages?task_id=task-1&author_type=user&sort=asc&limit=25",
|
||||
"",
|
||||
headers,
|
||||
)
|
||||
|
||||
require.Equal(t, http.StatusOK, response.Code, response.Body.String())
|
||||
require.JSONEq(t, `{
|
||||
"messages":[{
|
||||
"id":"message-1",
|
||||
"taskId":"task-1",
|
||||
"sessionId":"session-1",
|
||||
"turnId":"turn-1",
|
||||
"authorType":"user",
|
||||
"type":"message",
|
||||
"content":"Visible prompt.",
|
||||
"createdAt":"2026-09-07T12:00:00Z",
|
||||
"updatedAt":"2026-09-07T12:00:00Z",
|
||||
"promptIndex":3,
|
||||
"senderTaskId":"sender-task"
|
||||
}],
|
||||
"hasMore":false,
|
||||
"cursor":null
|
||||
}`, response.Body.String())
|
||||
require.Equal(t, "session-1", reader.request.TaskSessionID)
|
||||
require.Equal(t, "asc", reader.request.Sort)
|
||||
require.Equal(t, "user", reader.request.AuthorType)
|
||||
require.Equal(t, 25, reader.request.Limit)
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.2
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.5
|
||||
func TestConversationMessageCursorIsOpaqueAndQueryBound(t *testing.T) {
|
||||
created := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
reader := &fakeConversationReader{
|
||||
session: &taskmodels.TaskSession{ID: "session-1", TaskID: "task-1"},
|
||||
messages: []*taskmodels.Message{{
|
||||
ID: "message-1",
|
||||
TaskSessionID: "session-1",
|
||||
TaskID: "task-1",
|
||||
AuthorType: taskmodels.MessageAuthorUser,
|
||||
Content: "Visible prompt.",
|
||||
CreatedAt: created,
|
||||
}},
|
||||
hasMore: true,
|
||||
}
|
||||
_, service := newTestRouter(t)
|
||||
service.registry.Add(conversationPluginRecord("kandev-plugin-history", created))
|
||||
router := registerPluginRoutesWithIdentity(
|
||||
t,
|
||||
service,
|
||||
authn.Identity{UserID: "user_1", Role: authn.RoleMember},
|
||||
reader,
|
||||
)
|
||||
headers := conversationReadHeaders(t, service, router, "kandev-plugin-history", "session-1")
|
||||
|
||||
first := doAuthedRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/messages?task_id=task-1&author_type=user&sort=desc&limit=1",
|
||||
"",
|
||||
headers,
|
||||
)
|
||||
require.Equal(t, http.StatusOK, first.Code, first.Body.String())
|
||||
var page conversationMessagesResponse
|
||||
require.NoError(t, json.Unmarshal(first.Body.Bytes(), &page))
|
||||
require.True(t, page.HasMore)
|
||||
require.NotNil(t, page.Cursor)
|
||||
require.NotContains(t, *page.Cursor, "message-1")
|
||||
|
||||
changedQuery := doAuthedRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/messages?task_id=task-1&author_type=agent&sort=desc&limit=1&cursor="+*page.Cursor,
|
||||
"",
|
||||
headers,
|
||||
)
|
||||
require.Equal(t, http.StatusBadRequest, changedQuery.Code)
|
||||
require.JSONEq(t, `{"error":{"code":"invalid_query","message":"invalid cursor","retryable":false}}`, changedQuery.Body.String())
|
||||
|
||||
tampered := doAuthedRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/messages?task_id=task-1&author_type=user&sort=desc&limit=1&cursor="+*page.Cursor+"x",
|
||||
"",
|
||||
headers,
|
||||
)
|
||||
require.Equal(t, http.StatusBadRequest, tampered.Code)
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.4
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.13
|
||||
func TestConversationStreamGrantFirstPageRenewsQueryBoundCursor(t *testing.T) {
|
||||
installedAt := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
reader := &fakeConversationReader{
|
||||
session: &taskmodels.TaskSession{ID: "session-1", TaskID: "task-1"},
|
||||
messages: []*taskmodels.Message{{
|
||||
ID: "message-1",
|
||||
TaskSessionID: "session-1",
|
||||
TaskID: "task-1",
|
||||
AuthorType: taskmodels.MessageAuthorUser,
|
||||
Content: "Visible prompt.",
|
||||
CreatedAt: installedAt,
|
||||
}},
|
||||
hasMore: true,
|
||||
}
|
||||
_, service := newTestRouter(t)
|
||||
service.registry.Add(conversationPluginRecord("kandev-plugin-history", installedAt))
|
||||
router := registerPluginRoutesWithIdentity(
|
||||
t,
|
||||
service,
|
||||
authn.Identity{UserID: "user_1", Role: authn.RoleMember},
|
||||
reader,
|
||||
)
|
||||
binding := conversationBindingToken(t, router, "kandev-plugin-history")
|
||||
snapshot, _, _, err := service.MintSessionStreamGrant(
|
||||
"kandev-plugin-history",
|
||||
"user_1",
|
||||
conversationGeneration(installedAt),
|
||||
"session-1",
|
||||
"consumer-1",
|
||||
"",
|
||||
0,
|
||||
0,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
headers := map[string]string{
|
||||
"X-Kandev-Plugin-Binding": binding,
|
||||
"X-Kandev-Snapshot-Token": snapshot,
|
||||
}
|
||||
|
||||
first := doAuthedRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/messages?task_id=task-1&author_type=user&sort=desc&limit=1",
|
||||
"",
|
||||
headers,
|
||||
)
|
||||
require.Equal(t, http.StatusOK, first.Code, first.Body.String())
|
||||
var page conversationMessagesResponse
|
||||
require.NoError(t, json.Unmarshal(first.Body.Bytes(), &page))
|
||||
require.NotNil(t, page.Cursor)
|
||||
|
||||
renewed := doAuthedRequest(
|
||||
router,
|
||||
http.MethodPost,
|
||||
"/api/plugins/kandev-plugin-history/conversation/continuation/renew",
|
||||
`{"cursor":"`+*page.Cursor+`","snapshot_token":"`+snapshot+`"}`,
|
||||
map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
"X-Kandev-Plugin-Binding": binding,
|
||||
},
|
||||
)
|
||||
require.Equal(t, http.StatusOK, renewed.Code, renewed.Body.String())
|
||||
var continuation conversationContinuationRenewResponse
|
||||
require.NoError(t, json.Unmarshal(renewed.Body.Bytes(), &continuation))
|
||||
require.NotEqual(t, *page.Cursor, continuation.Cursor)
|
||||
require.NotEqual(t, snapshot, continuation.SnapshotToken)
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.1
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.3
|
||||
func TestConversationTurnsReturnNarrowNullableDTOs(t *testing.T) {
|
||||
started := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
completed := started.Add(time.Minute)
|
||||
reader := &fakeConversationReader{
|
||||
session: &taskmodels.TaskSession{ID: "session-1", TaskID: "task-1"},
|
||||
turns: []*taskmodels.Turn{{
|
||||
ID: "turn-1",
|
||||
TaskSessionID: "session-1",
|
||||
TaskID: "task-1",
|
||||
StartedAt: started,
|
||||
CompletedAt: &completed,
|
||||
Metadata: map[string]any{"secret": "must not leak"},
|
||||
}},
|
||||
}
|
||||
_, service := newTestRouter(t)
|
||||
service.registry.Add(conversationPluginRecord("kandev-plugin-history", started))
|
||||
router := registerPluginRoutesWithIdentity(
|
||||
t,
|
||||
service,
|
||||
authn.Identity{UserID: "user_1", Role: authn.RoleMember},
|
||||
reader,
|
||||
)
|
||||
headers := conversationReadHeaders(t, service, router, "kandev-plugin-history", "session-1")
|
||||
|
||||
response := doAuthedRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/kandev-plugin-history/conversation/task-sessions/session-1/turns",
|
||||
"",
|
||||
headers,
|
||||
)
|
||||
|
||||
require.Equal(t, http.StatusOK, response.Code, response.Body.String())
|
||||
require.JSONEq(t, `{"turns":[{
|
||||
"id":"turn-1",
|
||||
"taskId":"task-1",
|
||||
"sessionId":"session-1",
|
||||
"startedAt":"2026-09-07T12:00:00Z",
|
||||
"completedAt":"2026-09-07T12:01:00Z",
|
||||
"updatedAt":"2026-09-07T12:00:00Z"
|
||||
}]}`, response.Body.String())
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.6
|
||||
func TestConversationContinuationRenewPreservesSnapshot(t *testing.T) {
|
||||
installedAt := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
_, service := newTestRouter(t)
|
||||
service.registry.Add(conversationPluginRecord("kandev-plugin-history", installedAt))
|
||||
tokens := newConversationTokenManager()
|
||||
binding, _, err := tokens.mintBinding(
|
||||
"kandev-plugin-history",
|
||||
"user_1",
|
||||
conversationGeneration(installedAt),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
cursor, err := tokens.mintCursor(
|
||||
"kandev-plugin-history",
|
||||
"user_1",
|
||||
conversationGeneration(installedAt),
|
||||
"session-1",
|
||||
stringPtr("task-1"),
|
||||
"desc",
|
||||
[]string{"user"},
|
||||
"message-1",
|
||||
47,
|
||||
"fingerprint-1",
|
||||
)
|
||||
require.NoError(t, err)
|
||||
snapshot, err := tokens.mintSnapshot(
|
||||
"kandev-plugin-history",
|
||||
"user_1",
|
||||
conversationGeneration(installedAt),
|
||||
"session-1",
|
||||
stringPtr("task-1"),
|
||||
"desc",
|
||||
[]string{"user"},
|
||||
47,
|
||||
"fingerprint-1",
|
||||
)
|
||||
require.NoError(t, err)
|
||||
router := gin.New()
|
||||
ctrl := &Controller{
|
||||
svc: service, log: testLogger(t), conversationTokens: tokens,
|
||||
}
|
||||
registerConversationRoutes(router.Group("/api/plugins"), ctrl)
|
||||
|
||||
response := doAuthedRequest(
|
||||
router,
|
||||
http.MethodPost,
|
||||
"/api/plugins/kandev-plugin-history/conversation/continuation/renew",
|
||||
`{"cursor":"`+cursor+`","snapshot_token":"`+snapshot+`"}`,
|
||||
map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
"X-Kandev-Plugin-Binding": binding,
|
||||
},
|
||||
)
|
||||
|
||||
require.Equal(t, http.StatusOK, response.Code, response.Body.String())
|
||||
require.Equal(t, "no-store", response.Header().Get("Cache-Control"))
|
||||
var renewed conversationContinuationRenewResponse
|
||||
require.NoError(t, json.Unmarshal(response.Body.Bytes(), &renewed))
|
||||
require.NotEqual(t, cursor, renewed.Cursor)
|
||||
require.NotEqual(t, snapshot, renewed.SnapshotToken)
|
||||
require.Equal(t, int64(47), renewed.Cutoff)
|
||||
require.Equal(t, "fingerprint-1", renewed.Fingerprint)
|
||||
require.WithinDuration(t, time.Now().UTC().Add(10*time.Minute), renewed.ExpiresAt, time.Second)
|
||||
}
|
||||
|
||||
// @covers AC-PLUGINS-PROMPT-HISTORY-HOST-002.7
|
||||
func TestConversationContinuationRenewRejectsExpiredTokens(t *testing.T) {
|
||||
current := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
_, service := newTestRouter(t)
|
||||
service.conversationTokens.now = func() time.Time { return current }
|
||||
installedAt := current
|
||||
service.registry.Add(conversationPluginRecord("kandev-plugin-history", installedAt))
|
||||
generation := conversationGeneration(installedAt)
|
||||
|
||||
cursor, err := service.conversationTokens.mintCursor(
|
||||
"kandev-plugin-history",
|
||||
"user_1",
|
||||
generation,
|
||||
"session-1",
|
||||
stringPtr("task-1"),
|
||||
"desc",
|
||||
[]string{"user"},
|
||||
"message-1",
|
||||
47,
|
||||
"fingerprint-1",
|
||||
)
|
||||
require.NoError(t, err)
|
||||
snapshot, err := service.conversationTokens.mintSnapshot(
|
||||
"kandev-plugin-history",
|
||||
"user_1",
|
||||
generation,
|
||||
"session-1",
|
||||
stringPtr("task-1"),
|
||||
"desc",
|
||||
[]string{"user"},
|
||||
47,
|
||||
"fingerprint-1",
|
||||
)
|
||||
require.NoError(t, err)
|
||||
current = current.Add(9 * time.Minute)
|
||||
binding, _, err := service.conversationTokens.mintBinding(
|
||||
"kandev-plugin-history",
|
||||
"user_1",
|
||||
generation,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
current = current.Add(2 * time.Minute)
|
||||
|
||||
router := registerPluginRoutesWithIdentity(
|
||||
t,
|
||||
service,
|
||||
authn.Identity{UserID: "user_1", Role: authn.RoleMember},
|
||||
&fakeConversationReader{},
|
||||
)
|
||||
response := doAuthedRequest(
|
||||
router,
|
||||
http.MethodPost,
|
||||
"/api/plugins/kandev-plugin-history/conversation/continuation/renew",
|
||||
`{"cursor":"`+cursor+`","snapshot_token":"`+snapshot+`"}`,
|
||||
map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
"X-Kandev-Plugin-Binding": binding,
|
||||
},
|
||||
)
|
||||
|
||||
require.Equal(t, http.StatusBadRequest, response.Code)
|
||||
require.JSONEq(t, `{"error":{"code":"invalid_query","message":"invalid continuation","retryable":false}}`, response.Body.String())
|
||||
}
|
||||
|
||||
func conversationPluginRecord(id string, installedAt time.Time) *store.Record {
|
||||
return &store.Record{
|
||||
Manifest: manifest.Manifest{
|
||||
ID: id,
|
||||
Capabilities: manifest.Capabilities{
|
||||
APIRead: []string{"messages"},
|
||||
},
|
||||
},
|
||||
Status: StatusActive,
|
||||
InstalledAt: installedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func conversationBindingToken(t *testing.T, router *gin.Engine, pluginID string) string {
|
||||
t.Helper()
|
||||
response := doAuthedRequest(
|
||||
router,
|
||||
http.MethodGet,
|
||||
"/api/plugins/"+pluginID+"/conversation/binding",
|
||||
"",
|
||||
nil,
|
||||
)
|
||||
require.Equal(t, http.StatusOK, response.Code, response.Body.String())
|
||||
var binding conversationBindingResponse
|
||||
require.NoError(t, json.Unmarshal(response.Body.Bytes(), &binding))
|
||||
return binding.BindingToken
|
||||
}
|
||||
|
||||
func conversationReadHeaders(
|
||||
t *testing.T,
|
||||
service *Service,
|
||||
router *gin.Engine,
|
||||
pluginID string,
|
||||
sessionID string,
|
||||
) map[string]string {
|
||||
t.Helper()
|
||||
binding := conversationBindingToken(t, router, pluginID)
|
||||
record, err := service.Get(pluginID)
|
||||
require.NoError(t, err)
|
||||
snapshot, _, _, err := service.MintSessionStreamGrant(
|
||||
pluginID,
|
||||
"user_1",
|
||||
conversationGeneration(record.InstalledAt),
|
||||
sessionID,
|
||||
"test-consumer",
|
||||
"",
|
||||
service.SessionEvents().Watermark(sessionID),
|
||||
service.SessionEvents().Watermark(sessionID),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
return map[string]string{
|
||||
"X-Kandev-Plugin-Binding": binding,
|
||||
"X-Kandev-Snapshot-Token": snapshot,
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterTurnsByTaskNarrowsMixedTaskSession(t *testing.T) {
|
||||
turns := []*taskmodels.Turn{
|
||||
{ID: "turn-a", TaskID: "task-session"},
|
||||
{ID: "turn-b", TaskID: "task-other"},
|
||||
{ID: "turn-c", TaskID: "task-session"},
|
||||
}
|
||||
filtered := filterTurnsByTask(turns, "task-session")
|
||||
if len(filtered) != 2 || filtered[0].ID != "turn-a" || filtered[1].ID != "turn-c" {
|
||||
t.Fatalf("filtered = %+v, want only the session task's turns", filtered)
|
||||
}
|
||||
if filtered := filterTurnsByTask(turns, ""); len(filtered) != 3 {
|
||||
t.Fatalf("empty task filter must be a no-op, got %d", len(filtered))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,721 @@
|
||||
//nolint:revive // The journal projection and sanitization contract is intentionally co-located.
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/sysprompt"
|
||||
taskmodels "github.com/kandev/kandev/internal/task/models"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// errConversationCursorGone reports a page cursor whose message was
|
||||
// deleted/tombstoned before the walk reached it; the page would otherwise
|
||||
// silently truncate.
|
||||
var errConversationCursorGone = errors.New("conversation page cursor message no longer exists")
|
||||
|
||||
const (
|
||||
conversationContentKey = "content"
|
||||
conversationTypeKey = "type"
|
||||
conversationSessionIDKey = "session_id"
|
||||
conversationTaskIDKey = "task_id"
|
||||
conversationMessageIDKey = "message_id"
|
||||
conversationAuthorTypeKey = "author_type"
|
||||
conversationTurnCompleted = "session.turn.completed"
|
||||
conversationTurnRemoved = "session.turn.removed"
|
||||
conversationTurnStarted = "session.turn.started"
|
||||
)
|
||||
|
||||
//nolint:goconst // These keys are an explicit privacy allowlist.
|
||||
var conversationMessageMetadataKeys = map[string]struct{}{
|
||||
"action_details": {}, "action_type": {}, "action_visibility": {},
|
||||
"actions": {}, "agent_disconnected": {},
|
||||
"attempt": {}, "attachments": {}, "auth_methods": {}, "auto_start": {},
|
||||
"base_branch": {}, "context": {}, "context_files": {}, "decision_id": {},
|
||||
"effective_model": {}, "entity_references": {}, "error_output": {},
|
||||
"failure_code": {}, "failure_details": {}, "failure_kind": {},
|
||||
"fallback_model": {}, "has_hidden_prompts": {}, "has_resume_token": {},
|
||||
"has_review_comments": {}, "is_auth_error": {}, "kind": {}, "max_attempts": {},
|
||||
"message": {}, "missing_branch": {}, "model_id": {}, "new_branch": {},
|
||||
"original_branch": {}, "options": {}, "pending_id": {}, "plan_mode": {}, "progress": {},
|
||||
"provider_name": {}, "question": {}, "question_id": {}, "question_index": {},
|
||||
"question_total": {}, "recovery_actions": {}, "remediation": {},
|
||||
"remediation_url": {}, "requested_model": {}, "request_id": {},
|
||||
"requests_input": {}, "response": {}, "reset_at": {}, "retry_at": {},
|
||||
"retry_in_seconds": {},
|
||||
"retrying": {}, "sender_session_id": {}, "sender_session_name": {},
|
||||
"sender_task_id": {}, "sender_task_title": {}, "stage": {}, "status": {},
|
||||
"script_type": {}, "agent_name": {}, "command": {}, "exit_code": {},
|
||||
"is_resuming": {}, "started_at": {}, "completed_at": {}, "error": {},
|
||||
"task_id": {}, "text": {}, "tool_call_id": {}, "variant": {}, "workflow_message": {},
|
||||
"workflow_step_color": {}, "workflow_step_id": {}, "workflow_step_name": {},
|
||||
}
|
||||
|
||||
// SetConversationJournalDB configures the primary database that owns source
|
||||
// mutations and their immutable conversation journal rows.
|
||||
func (s *Service) SetConversationJournalDB(database *sqlx.DB) {
|
||||
s.conversationJournal = database
|
||||
}
|
||||
|
||||
func (s *Service) HasConversationJournal() bool {
|
||||
return s.conversationJournal != nil
|
||||
}
|
||||
|
||||
func (s *Service) conversationJournalTablesExist(ctx context.Context) (bool, error) {
|
||||
return s.conversationTableExists(ctx, "conversation_message_versions")
|
||||
}
|
||||
|
||||
func toAnySlice(values []string) []any {
|
||||
out := make([]any, len(values))
|
||||
for index, value := range values {
|
||||
out[index] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Service) conversationTableExists(ctx context.Context, table string) (bool, error) {
|
||||
present, err := db.TableExistsContext(ctx, s.conversationJournal, table)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("check conversation journal schema (%s): %w", table, err)
|
||||
}
|
||||
return present, nil
|
||||
}
|
||||
|
||||
// SyncCommittedSessionEvents mirrors primary-journal rows into the durable
|
||||
// delivery log without allocating a second identity or sequence.
|
||||
func (s *Service) SyncCommittedSessionEvents(ctx context.Context, sessionID string) ([]SessionEvent, error) {
|
||||
if s.conversationJournal == nil || s.sessionEvents == nil {
|
||||
return nil, nil
|
||||
}
|
||||
watermark := s.sessionEvents.Watermark(sessionID)
|
||||
query := s.conversationJournal.Rebind(`
|
||||
SELECT session_id, event_id, sequence, protocol_version, event_type, task_id, payload, created_at
|
||||
FROM conversation_session_events
|
||||
WHERE session_id = ? AND sequence > ?
|
||||
ORDER BY sequence ASC`)
|
||||
rows, err := s.conversationJournal.QueryxContext(ctx, query, sessionID, watermark)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query committed conversation events: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
mirrored := make([]SessionEvent, 0)
|
||||
expected := watermark
|
||||
for rows.Next() {
|
||||
var event SessionEvent
|
||||
var taskID sql.NullString
|
||||
var payload string
|
||||
if err := rows.Scan(&event.SessionID, &event.ID, &event.Sequence, &event.ProtocolVersion, &event.EventType, &taskID, &payload, &event.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan committed conversation event: %w", err)
|
||||
}
|
||||
event.Payload = sanitizeConversationEventPayload(event.EventType, json.RawMessage(payload))
|
||||
if taskID.Valid {
|
||||
event.TaskID = &taskID.String
|
||||
}
|
||||
appended, err := s.sessionEvents.AppendCommitted(event)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mirror committed conversation event: %w", err)
|
||||
}
|
||||
if !appended {
|
||||
continue
|
||||
}
|
||||
mirrored = append(mirrored, event)
|
||||
if event.Sequence > expected+1 && s.log != nil {
|
||||
s.log.Warn("journal mirror healed a retention gap",
|
||||
zap.String("session_id", sessionID),
|
||||
zap.Uint64("expected_sequence", expected+1),
|
||||
zap.Uint64("sequence", event.Sequence),
|
||||
)
|
||||
}
|
||||
expected = event.Sequence
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate committed conversation events: %w", err)
|
||||
}
|
||||
return mirrored, nil
|
||||
}
|
||||
|
||||
// emptyConversationPayload is the durable payload persisted for a
|
||||
// primary-journal row whose JSON is malformed. It carries no raw bytes yet is
|
||||
// non-nil, so the BLOB NOT NULL session_events column accepts the write and
|
||||
// the mirrored row is retained as a poison record instead of failing the
|
||||
// append (the projection rejects it because no type/identity fields exist).
|
||||
var emptyConversationPayload = json.RawMessage("{}")
|
||||
|
||||
func sanitizeConversationEventPayload(eventType string, raw json.RawMessage) json.RawMessage {
|
||||
var source map[string]any
|
||||
if err := json.Unmarshal(raw, &source); err != nil {
|
||||
return emptyConversationPayload
|
||||
}
|
||||
payload := map[string]any{conversationTypeKey: eventType}
|
||||
copyConversationPayloadFields(payload, source, conversationSessionIDKey, conversationTaskIDKey)
|
||||
switch eventType {
|
||||
case events.MessageAdded, events.MessageUpdated:
|
||||
copyConversationPayloadFields(payload, source,
|
||||
conversationMessageIDKey, "turn_id", conversationAuthorTypeKey,
|
||||
"created_at", "updated_at", "prompt_index", "requests_input",
|
||||
"message_type")
|
||||
sanitizeConversationMessagePayload(payload, source)
|
||||
case events.MessageDeleted:
|
||||
copyConversationPayloadFields(payload, source, conversationMessageIDKey)
|
||||
case conversationTurnStarted, conversationTurnCompleted, conversationTurnRemoved:
|
||||
copyConversationPayloadFields(payload, source, "id", "started_at", "completed_at", "updated_at", "had_output", "metadata")
|
||||
case events.SessionRemoved:
|
||||
// The common identity fields above are the complete public removal DTO.
|
||||
}
|
||||
sanitized, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return sanitized
|
||||
}
|
||||
|
||||
func copyConversationPayloadFields(target, source map[string]any, keys ...string) {
|
||||
for _, key := range keys {
|
||||
if value, exists := source[key]; exists {
|
||||
target[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SanitizeConversationMessageMetadata returns the presentation metadata that
|
||||
// the web client needs for a conversation message. Internal agent/tool
|
||||
// metadata is deliberately excluded from ordered session events.
|
||||
func SanitizeConversationMessageMetadata(source map[string]any) map[string]any {
|
||||
if len(source) == 0 {
|
||||
return nil
|
||||
}
|
||||
target := make(map[string]any)
|
||||
for key := range conversationMessageMetadataKeys {
|
||||
value, exists := source[key]
|
||||
if !exists || value == nil {
|
||||
continue
|
||||
}
|
||||
target[key] = sanitizeConversationMetadataValue(value)
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
func sanitizeConversationMetadataValue(value any) any {
|
||||
switch typed := value.(type) {
|
||||
case string:
|
||||
return sysprompt.StripSystemContent(typed)
|
||||
case map[string]any:
|
||||
target := make(map[string]any, len(typed))
|
||||
for key, nested := range typed {
|
||||
target[key] = sanitizeConversationMetadataValue(nested)
|
||||
}
|
||||
return target
|
||||
case []any:
|
||||
target := make([]any, len(typed))
|
||||
for index, nested := range typed {
|
||||
target[index] = sanitizeConversationMetadataValue(nested)
|
||||
}
|
||||
return target
|
||||
default:
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
func sanitizeConversationMessagePayload(target, source map[string]any) {
|
||||
if content, ok := source[conversationContentKey].(string); ok {
|
||||
target[conversationContentKey] = sysprompt.StripSystemContent(content)
|
||||
}
|
||||
metadata, _ := source["metadata"].(map[string]any)
|
||||
sanitizedMetadata := SanitizeConversationMessageMetadata(metadata)
|
||||
if senderTaskID, ok := source["sender_task_id"].(string); ok && senderTaskID != "" {
|
||||
target["sender_task_id"] = senderTaskID
|
||||
if sanitizedMetadata == nil {
|
||||
sanitizedMetadata = map[string]any{}
|
||||
}
|
||||
sanitizedMetadata["sender_task_id"] = senderTaskID
|
||||
}
|
||||
if len(sanitizedMetadata) > 0 {
|
||||
target["metadata"] = sanitizedMetadata
|
||||
if senderTaskID, ok := sanitizedMetadata["sender_task_id"].(string); ok && senderTaskID != "" {
|
||||
target["sender_task_id"] = senderTaskID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) syncAllCommittedSessionEvents(ctx context.Context) ([]SessionEvent, error) {
|
||||
if s.conversationJournal == nil || s.sessionEvents == nil {
|
||||
return nil, nil
|
||||
}
|
||||
rows, err := s.conversationJournal.QueryxContext(ctx, `SELECT session_id FROM conversation_session_streams ORDER BY session_id`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query conversation stream partitions: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
var sessionIDs []string
|
||||
for rows.Next() {
|
||||
var sessionID string
|
||||
if err := rows.Scan(&sessionID); err != nil {
|
||||
return nil, fmt.Errorf("scan conversation stream partition: %w", err)
|
||||
}
|
||||
sessionIDs = append(sessionIDs, sessionID)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate conversation stream partitions: %w", err)
|
||||
}
|
||||
var mirrored []SessionEvent
|
||||
var failures []string
|
||||
for _, sessionID := range sessionIDs {
|
||||
events, err := s.SyncCommittedSessionEvents(ctx, sessionID)
|
||||
if err != nil {
|
||||
// Isolate one unhealthy partition: it must not freeze retention
|
||||
// for every other session. The combined error is still returned
|
||||
// so the failure stays loud (it is only reachable through state
|
||||
// tampering, since the maintenance tick prunes both sides).
|
||||
failures = append(failures, sessionID+": "+err.Error())
|
||||
continue
|
||||
}
|
||||
mirrored = append(mirrored, events...)
|
||||
}
|
||||
if len(failures) > 0 {
|
||||
return mirrored, fmt.Errorf("mirror %d session partition(s): %s", len(failures), strings.Join(failures, "; "))
|
||||
}
|
||||
return mirrored, nil
|
||||
}
|
||||
|
||||
func (s *Service) SetSessionEventSink(sink func(SessionEvent)) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
s.sessionEventSink = sink
|
||||
}
|
||||
|
||||
//nolint:cyclop // Retention orchestration: schema gates plus four prune passes over three tables.
|
||||
func (s *Service) pruneConversationJournal(
|
||||
ctx context.Context,
|
||||
cutoff time.Time,
|
||||
retained map[string]struct{},
|
||||
) error {
|
||||
if s.conversationJournal == nil {
|
||||
return nil
|
||||
}
|
||||
exists, err := s.conversationJournalTablesExist(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
purgeable, err := s.deadSessionPurgeEnabled(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ids := make([]string, 0, len(retained))
|
||||
for sessionID := range retained {
|
||||
ids = append(ids, sessionID)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
exclusion, extraArgs := retainedSessionExclusion(ids)
|
||||
args := append([]any{cutoff.UTC()}, extraArgs...)
|
||||
tx, err := s.conversationJournal.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin conversation journal retention: %w", err)
|
||||
}
|
||||
rollback := func(cause error) error {
|
||||
_ = tx.Rollback()
|
||||
return cause
|
||||
}
|
||||
eventQuery := s.conversationJournal.Rebind(
|
||||
`DELETE FROM conversation_session_events
|
||||
WHERE created_at < ? AND event_type <> 'session.removed' AND ` + exclusion,
|
||||
)
|
||||
if _, err := tx.ExecContext(ctx, eventQuery, args...); err != nil {
|
||||
return rollback(fmt.Errorf("prune conversation events: %w", err))
|
||||
}
|
||||
messageQuery := s.conversationJournal.Rebind(
|
||||
`DELETE FROM conversation_message_versions AS current_version
|
||||
WHERE current_version.created_at < ? AND ` + exclusion + `
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM conversation_message_versions AS newer
|
||||
WHERE newer.session_id = current_version.session_id
|
||||
AND newer.message_id = current_version.message_id
|
||||
AND newer.row_sequence > current_version.row_sequence
|
||||
)`,
|
||||
)
|
||||
if _, err := tx.ExecContext(ctx, messageQuery, args...); err != nil {
|
||||
return rollback(fmt.Errorf("prune conversation message versions: %w", err))
|
||||
}
|
||||
turnQuery := s.conversationJournal.Rebind(
|
||||
`DELETE FROM conversation_turn_versions AS current_version
|
||||
WHERE current_version.started_at < ? AND ` + exclusion + `
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM conversation_turn_versions AS newer
|
||||
WHERE newer.session_id = current_version.session_id
|
||||
AND newer.turn_id = current_version.turn_id
|
||||
AND newer.row_sequence > current_version.row_sequence
|
||||
)`,
|
||||
)
|
||||
if _, err := tx.ExecContext(ctx, turnQuery, args...); err != nil {
|
||||
return rollback(fmt.Errorf("prune conversation turn versions: %w", err))
|
||||
}
|
||||
if purgeable {
|
||||
if err := s.purgeDeadSessionPartitions(ctx, tx, exclusion, args); err != nil {
|
||||
return rollback(err)
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("commit conversation journal retention: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// deadSessionPurgeEnabled reports whether the retention pass can purge whole
|
||||
// dead-session partitions: both the journal stream table and the source
|
||||
// session table must be present. Checked before the retention tx opens so a
|
||||
// single-connection pool (in-memory test DBs) cannot deadlock on the catalog
|
||||
// lookups.
|
||||
func (s *Service) deadSessionPurgeEnabled(ctx context.Context) (bool, error) {
|
||||
streams, err := s.conversationTableExists(ctx, "conversation_session_streams")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !streams {
|
||||
return false, nil
|
||||
}
|
||||
return s.conversationTableExists(ctx, "task_sessions")
|
||||
}
|
||||
|
||||
// retainedSessionExclusion builds the SQL exclusion clause and its bound
|
||||
// arguments for the retained session ids; with none retained it returns the
|
||||
// no-op "1 = 1" clause so every row is eligible for age-based pruning.
|
||||
func retainedSessionExclusion(ids []string) (string, []any) {
|
||||
if len(ids) == 0 {
|
||||
return "1 = 1", nil
|
||||
}
|
||||
placeholders := make([]string, len(ids))
|
||||
extra := make([]any, len(ids))
|
||||
for index, sessionID := range ids {
|
||||
placeholders[index] = "?"
|
||||
extra[index] = sessionID
|
||||
}
|
||||
return "session_id NOT IN (" + strings.Join(placeholders, ",") + ")", extra
|
||||
}
|
||||
|
||||
// purgeDeadSessionPartitions deletes every journal row (message and turn
|
||||
// versions, events, and the stream row) for sessions whose source task_sessions
|
||||
// row no longer exists and whose partition is older than the retention cutoff
|
||||
// (args[0]) and not protected by a live cursor/poison (remaining args). The
|
||||
// dead ids are materialized first because a CTE whose DELETE targets the same
|
||||
// table the CTE reads can stall SQLite.
|
||||
//
|
||||
// The session.removed terminal marker intentionally lives exactly as long as
|
||||
// the rest of the partition: this pass fires only after updated_at has aged
|
||||
// past SessionEventRetention, so a subscriber arriving within that window
|
||||
// still receives session_removed (the durable-log mirror keeps the terminal
|
||||
// tombstone through the same window and only then drops it). After both
|
||||
// stores forget the partition, the session is gone for good and later
|
||||
// snapshot reads correctly 404.
|
||||
func (s *Service) purgeDeadSessionPartitions(ctx context.Context, tx *sqlx.Tx, exclusion string, args []any) error {
|
||||
deadQuery := s.conversationJournal.Rebind(`
|
||||
SELECT st.session_id
|
||||
FROM conversation_session_streams st
|
||||
WHERE st.updated_at < ? AND ` + exclusion + `
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM task_sessions ts WHERE ts.id = st.session_id
|
||||
)`)
|
||||
var deadIDs []string
|
||||
if err := tx.SelectContext(ctx, &deadIDs, deadQuery, args...); err != nil {
|
||||
return fmt.Errorf("select dead session journal partitions: %w", err)
|
||||
}
|
||||
if len(deadIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
placeholders := make([]string, len(deadIDs))
|
||||
for index := range deadIDs {
|
||||
placeholders[index] = "?"
|
||||
}
|
||||
inClause := strings.Join(placeholders, ",")
|
||||
for _, child := range []string{"conversation_message_versions", "conversation_turn_versions", "conversation_session_events"} {
|
||||
query := s.conversationJournal.Rebind(
|
||||
`DELETE FROM ` + child + ` WHERE session_id IN (` + inClause + `)`)
|
||||
if _, err := tx.ExecContext(ctx, query, toAnySlice(deadIDs)...); err != nil {
|
||||
return fmt.Errorf("purge dead session journal rows (%s): %w", child, err)
|
||||
}
|
||||
}
|
||||
streamQuery := s.conversationJournal.Rebind(
|
||||
`DELETE FROM conversation_session_streams WHERE session_id IN (` + inClause + `)`)
|
||||
if _, err := tx.ExecContext(ctx, streamQuery, toAnySlice(deadIDs)...); err != nil {
|
||||
return fmt.Errorf("purge dead session stream rows: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type journalMessagePayload struct {
|
||||
MessageID string `json:"message_id"`
|
||||
TurnID string `json:"turn_id"`
|
||||
TaskID string `json:"task_id"`
|
||||
AuthorType string `json:"author_type"`
|
||||
Content string `json:"content"`
|
||||
MessageType string `json:"message_type"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
PromptIndex int `json:"prompt_index"`
|
||||
RequestsInput any `json:"requests_input"`
|
||||
SenderTaskID string `json:"sender_task_id"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
}
|
||||
|
||||
//nolint:goconst // SQL projection values are protocol literals.
|
||||
func (s *Service) conversationMessagesAt(
|
||||
ctx context.Context,
|
||||
sessionID string,
|
||||
cutoff uint64,
|
||||
taskID *string,
|
||||
authors []string,
|
||||
sortOrder string,
|
||||
cursorID string,
|
||||
limit int,
|
||||
) ([]*taskmodels.Message, bool, error) {
|
||||
if s.conversationJournal == nil {
|
||||
return nil, false, nil
|
||||
}
|
||||
args := []any{sessionID, cutoff}
|
||||
where := []string{"version_rank = 1", "tombstone = FALSE"}
|
||||
if taskID != nil {
|
||||
where = append(where, "task_id = ?")
|
||||
args = append(args, *taskID)
|
||||
}
|
||||
if len(authors) > 0 {
|
||||
placeholders := make([]string, len(authors))
|
||||
for index, author := range authors {
|
||||
placeholders[index] = "?"
|
||||
args = append(args, author)
|
||||
}
|
||||
where = append(where, "author_type IN ("+strings.Join(placeholders, ",")+")")
|
||||
}
|
||||
orderDirection := "ASC"
|
||||
comparison := ">"
|
||||
if sortOrder == "desc" {
|
||||
orderDirection = "DESC"
|
||||
comparison = "<"
|
||||
}
|
||||
if cursorID != "" {
|
||||
// A cursor whose message was deleted/tombstoned mid-walk is gone from
|
||||
// the live projection; the cursor predicate would otherwise match
|
||||
// nothing and the client would read a silent empty terminal page.
|
||||
// Surface an error (parity with the source-table fallback) instead.
|
||||
live, err := s.liveConversationMessageAtCutoff(ctx, sessionID, cutoff, cursorID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if !live {
|
||||
return nil, false, fmt.Errorf("%w: %s", errConversationCursorGone, cursorID)
|
||||
}
|
||||
where = append(where, fmt.Sprintf("(created_at %s (SELECT created_at FROM live WHERE message_id = ?) OR (created_at = (SELECT created_at FROM live WHERE message_id = ?) AND message_id %s ?))", comparison, comparison))
|
||||
args = append(args, cursorID, cursorID, cursorID)
|
||||
}
|
||||
args = append(args, limit+1)
|
||||
query := fmt.Sprintf(`
|
||||
WITH ranked AS (
|
||||
SELECT message_id, task_id, author_type, created_at, tombstone, payload,
|
||||
ROW_NUMBER() OVER (PARTITION BY message_id ORDER BY row_sequence DESC) AS version_rank
|
||||
FROM conversation_message_versions
|
||||
WHERE session_id = ? AND row_sequence <= ?
|
||||
), live AS (
|
||||
SELECT * FROM ranked WHERE version_rank = 1 AND tombstone = FALSE
|
||||
)
|
||||
SELECT payload FROM live
|
||||
WHERE %s
|
||||
ORDER BY created_at %s, message_id %s
|
||||
LIMIT ?`, strings.Join(where, " AND "), orderDirection, orderDirection)
|
||||
query = s.conversationJournal.Rebind(query)
|
||||
rows, err := s.conversationJournal.QueryxContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("query conversation message snapshot: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
return decodeConversationSnapshotRows(rows, sessionID, limit)
|
||||
}
|
||||
|
||||
type conversationRowScanner interface {
|
||||
Next() bool
|
||||
Scan(dest ...any) error
|
||||
Err() error
|
||||
}
|
||||
|
||||
// decodeConversationSnapshotRows scans message-version payload rows into
|
||||
// messages, applying the limit+1 page bound, and reports whether another page
|
||||
// remains.
|
||||
func decodeConversationSnapshotRows(rows conversationRowScanner, sessionID string, limit int) ([]*taskmodels.Message, bool, error) {
|
||||
messages := make([]*taskmodels.Message, 0, limit+1)
|
||||
for rows.Next() {
|
||||
var raw []byte
|
||||
if err := rows.Scan(&raw); err != nil {
|
||||
return nil, false, fmt.Errorf("scan conversation message snapshot: %w", err)
|
||||
}
|
||||
message, err := decodeJournalMessage(raw, sessionID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
messages = append(messages, message)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, false, fmt.Errorf("iterate conversation message snapshot: %w", err)
|
||||
}
|
||||
hasMore := len(messages) > limit
|
||||
if hasMore {
|
||||
messages = messages[:limit]
|
||||
}
|
||||
return messages, hasMore, nil
|
||||
}
|
||||
|
||||
// liveConversationMessageAtCutoff reports whether the message cursor still has
|
||||
// a live (non-tombstoned) version at or before the snapshot cutoff.
|
||||
func (s *Service) liveConversationMessageAtCutoff(ctx context.Context, sessionID string, cutoff uint64, cursorID string) (bool, error) {
|
||||
query := s.conversationJournal.Rebind(`
|
||||
WITH ranked AS (
|
||||
SELECT message_id, tombstone,
|
||||
ROW_NUMBER() OVER (PARTITION BY message_id ORDER BY row_sequence DESC) AS version_rank
|
||||
FROM conversation_message_versions
|
||||
WHERE session_id = ? AND row_sequence <= ?
|
||||
)
|
||||
SELECT COUNT(*) FROM ranked WHERE message_id = ? AND version_rank = 1 AND tombstone = FALSE`)
|
||||
var live int
|
||||
if err := s.conversationJournal.GetContext(ctx, &live, query, sessionID, cutoff, cursorID); err != nil {
|
||||
return false, fmt.Errorf("resolve conversation page cursor: %w", err)
|
||||
}
|
||||
return live > 0, nil
|
||||
}
|
||||
|
||||
func conversationRequestsInput(value any) bool {
|
||||
switch typed := value.(type) {
|
||||
case bool:
|
||||
return typed
|
||||
case float64:
|
||||
return typed != 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func decodeJournalMessage(raw []byte, sessionID string) (*taskmodels.Message, error) {
|
||||
var payload journalMessagePayload
|
||||
if err := json.Unmarshal(raw, &payload); err != nil {
|
||||
return nil, fmt.Errorf("decode conversation message version: %w", err)
|
||||
}
|
||||
createdAt, err := parseJournalTime(payload.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode conversation message created_at: %w", err)
|
||||
}
|
||||
updatedAt, err := parseJournalTime(payload.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode conversation message updated_at: %w", err)
|
||||
}
|
||||
metadata := payload.Metadata
|
||||
if metadata == nil {
|
||||
metadata = map[string]any{}
|
||||
}
|
||||
if payload.SenderTaskID != "" {
|
||||
metadata["sender_task_id"] = payload.SenderTaskID
|
||||
}
|
||||
return &taskmodels.Message{
|
||||
ID: payload.MessageID, TaskSessionID: sessionID, TurnID: payload.TurnID, TaskID: payload.TaskID,
|
||||
AuthorType: taskmodels.MessageAuthorType(payload.AuthorType),
|
||||
Content: payload.Content, Type: taskmodels.MessageType(payload.MessageType),
|
||||
RequestsInput: conversationRequestsInput(payload.RequestsInput),
|
||||
CreatedAt: createdAt, UpdatedAt: updatedAt, PromptIndex: payload.PromptIndex, Metadata: metadata,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type journalTurnPayload struct {
|
||||
TurnID string `json:"id"`
|
||||
TaskID string `json:"task_id"`
|
||||
StartedAt string `json:"started_at"`
|
||||
CompletedAt *string `json:"completed_at"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
}
|
||||
|
||||
func (s *Service) conversationTurnsAt(ctx context.Context, sessionID string, cutoff uint64, taskID *string) ([]*taskmodels.Turn, error) {
|
||||
if s.conversationJournal == nil {
|
||||
return nil, nil
|
||||
}
|
||||
args := []any{sessionID, cutoff}
|
||||
where := "version_rank = 1 AND tombstone = FALSE"
|
||||
if taskID != nil {
|
||||
where += " AND task_id = ?"
|
||||
args = append(args, *taskID)
|
||||
}
|
||||
query := fmt.Sprintf(`
|
||||
WITH ranked AS (
|
||||
SELECT turn_id, task_id, started_at, tombstone, payload,
|
||||
ROW_NUMBER() OVER (PARTITION BY turn_id ORDER BY row_sequence DESC) AS version_rank
|
||||
FROM conversation_turn_versions
|
||||
WHERE session_id = ? AND row_sequence <= ?
|
||||
)
|
||||
SELECT payload FROM ranked WHERE %s ORDER BY started_at ASC, turn_id ASC`, where)
|
||||
query = s.conversationJournal.Rebind(query)
|
||||
rows, err := s.conversationJournal.QueryxContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query conversation turn snapshot: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
var turns []*taskmodels.Turn
|
||||
for rows.Next() {
|
||||
var raw []byte
|
||||
if err := rows.Scan(&raw); err != nil {
|
||||
return nil, fmt.Errorf("scan conversation turn snapshot: %w", err)
|
||||
}
|
||||
var payload journalTurnPayload
|
||||
if err := json.Unmarshal(raw, &payload); err != nil {
|
||||
return nil, fmt.Errorf("decode conversation turn version: %w", err)
|
||||
}
|
||||
startedAt, err := parseJournalTime(payload.StartedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode conversation turn started_at: %w", err)
|
||||
}
|
||||
createdAt, err := parseJournalTime(payload.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode conversation turn created_at: %w", err)
|
||||
}
|
||||
updatedAt, err := parseJournalTime(payload.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode conversation turn updated_at: %w", err)
|
||||
}
|
||||
metadata := payload.Metadata
|
||||
if metadata == nil {
|
||||
metadata = map[string]any{}
|
||||
}
|
||||
turn := &taskmodels.Turn{
|
||||
ID: payload.TurnID, TaskSessionID: sessionID, TaskID: payload.TaskID,
|
||||
StartedAt: startedAt, Metadata: metadata, CreatedAt: createdAt, UpdatedAt: updatedAt,
|
||||
}
|
||||
if payload.CompletedAt != nil {
|
||||
completedAt, err := parseJournalTime(*payload.CompletedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode conversation turn completed_at: %w", err)
|
||||
}
|
||||
turn.CompletedAt = &completedAt
|
||||
}
|
||||
turns = append(turns, turn)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate conversation turn snapshot: %w", err)
|
||||
}
|
||||
return turns, nil
|
||||
}
|
||||
|
||||
func parseJournalTime(value string) (time.Time, error) {
|
||||
for _, layout := range []string{time.RFC3339Nano, "2006-01-02 15:04:05.999999999Z07:00", "2006-01-02 15:04:05Z07:00"} {
|
||||
if parsed, err := time.Parse(layout, value); err == nil {
|
||||
return parsed, nil
|
||||
}
|
||||
}
|
||||
return time.Time{}, fmt.Errorf("unsupported timestamp %q", value)
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
tasksqlite "github.com/kandev/kandev/internal/task/repository/sqlite"
|
||||
"github.com/kandev/kandev/internal/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestPostgresConversationJournalProjectsRFC3339Times covers the real
|
||||
// PostgreSQL trigger output at both consumers: immutable snapshot decoding and
|
||||
// ordered ProjectSessionEvent validation. It skips when KANDEV_TEST_POSTGRES_DSN
|
||||
// is unavailable.
|
||||
func TestPostgresConversationJournalProjectsRFC3339Times(t *testing.T) {
|
||||
database := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repository, err := tasksqlite.NewWithDB(database, database, nil)
|
||||
require.NoError(t, err)
|
||||
_, err = database.Exec(`SET TIME ZONE 'America/New_York'`)
|
||||
require.NoError(t, err)
|
||||
|
||||
const (
|
||||
taskID = "task-pg-journal-projection"
|
||||
sessionID = "session-pg-journal-projection"
|
||||
turnID = "turn-pg-journal-projection"
|
||||
)
|
||||
startedAt := time.Date(2026, 9, 7, 12, 0, 0, 123456000, time.UTC)
|
||||
insertPostgresConversationTurn(t, database, taskID, sessionID, turnID, startedAt)
|
||||
messageAt := time.Date(2026, 9, 7, 12, 1, 0, 654321000, time.UTC)
|
||||
require.NoError(t, repository.CreateMessage(context.Background(), &models.Message{
|
||||
ID: "message-pg-journal-projection",
|
||||
TaskSessionID: sessionID,
|
||||
TaskID: taskID,
|
||||
TurnID: turnID,
|
||||
AuthorType: models.MessageAuthorAgent,
|
||||
Type: models.MessageTypeMessage,
|
||||
Content: "visible",
|
||||
CreatedAt: messageAt,
|
||||
UpdatedAt: messageAt,
|
||||
}))
|
||||
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
service.SetConversationJournalDB(database)
|
||||
var cutoff uint64
|
||||
require.NoError(t, database.Get(&cutoff,
|
||||
`SELECT watermark FROM conversation_session_streams WHERE session_id = $1`, sessionID))
|
||||
|
||||
turns, err := service.conversationTurnsAt(context.Background(), sessionID, cutoff, nil)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, turns, 1)
|
||||
require.True(t, turns[0].StartedAt.Equal(startedAt), "started_at = %s", turns[0].StartedAt)
|
||||
require.True(t, turns[0].UpdatedAt.Equal(startedAt), "updated_at = %s", turns[0].UpdatedAt)
|
||||
messages, more, err := service.conversationMessagesAt(
|
||||
context.Background(), sessionID, cutoff, nil, nil, "asc", "", 20,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.False(t, more)
|
||||
require.Len(t, messages, 1)
|
||||
require.True(t, messages[0].CreatedAt.Equal(messageAt), "created_at = %s", messages[0].CreatedAt)
|
||||
require.True(t, messages[0].UpdatedAt.Equal(messageAt), "updated_at = %s", messages[0].UpdatedAt)
|
||||
|
||||
events, err := service.SyncCommittedSessionEvents(context.Background(), sessionID)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, events, 2)
|
||||
projectedTimes := make(map[string]map[string]any, len(events))
|
||||
for _, event := range events {
|
||||
projected, projectionErr := ProjectSessionEvent(event)
|
||||
require.NoError(t, projectionErr)
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal(projected, &payload))
|
||||
projectedTimes[event.EventType] = payload
|
||||
}
|
||||
require.Equal(
|
||||
t,
|
||||
startedAt.Format(time.RFC3339Nano),
|
||||
projectedTimes["session.turn.started"]["started_at"],
|
||||
)
|
||||
require.Equal(
|
||||
t,
|
||||
startedAt.Format(time.RFC3339Nano),
|
||||
projectedTimes["session.turn.started"]["updated_at"],
|
||||
)
|
||||
require.Equal(
|
||||
t,
|
||||
messageAt.Format(time.RFC3339Nano),
|
||||
projectedTimes["message.added"]["created_at"],
|
||||
)
|
||||
require.Equal(
|
||||
t,
|
||||
messageAt.Format(time.RFC3339Nano),
|
||||
projectedTimes["message.added"]["updated_at"],
|
||||
)
|
||||
}
|
||||
func insertPostgresConversationTurn(
|
||||
t *testing.T,
|
||||
database *sqlx.DB,
|
||||
taskID string,
|
||||
sessionID string,
|
||||
turnID string,
|
||||
startedAt time.Time,
|
||||
) {
|
||||
t.Helper()
|
||||
_, err := database.Exec(
|
||||
`INSERT INTO tasks (id, workspace_id, title, created_at, updated_at)
|
||||
VALUES ($1, '', 'test task', $2, $2)`,
|
||||
taskID,
|
||||
startedAt,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
_, err = database.Exec(
|
||||
`INSERT INTO task_sessions (id, task_id, started_at, updated_at)
|
||||
VALUES ($1, $2, $3, $3)`,
|
||||
sessionID,
|
||||
taskID,
|
||||
startedAt,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
_, err = database.Exec(
|
||||
`INSERT INTO task_session_turns (
|
||||
id, task_session_id, task_id, started_at, created_at, updated_at
|
||||
) VALUES ($1, $2, $3, $4, $4, $4)`,
|
||||
turnID,
|
||||
sessionID,
|
||||
taskID,
|
||||
startedAt,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
@@ -0,0 +1,484 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConversationMessagesAtUsesImmutableCutoff(t *testing.T) {
|
||||
database, err := sqlx.Open("sqlite3", ":memory:")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, database.Close()) })
|
||||
_, err = database.Exec(`
|
||||
CREATE TABLE conversation_message_versions (
|
||||
session_id TEXT NOT NULL, message_id TEXT NOT NULL, row_sequence INTEGER NOT NULL,
|
||||
task_id TEXT, author_type TEXT, created_at TIMESTAMP NOT NULL,
|
||||
tombstone BOOLEAN NOT NULL, payload TEXT NOT NULL
|
||||
);`)
|
||||
require.NoError(t, err)
|
||||
|
||||
createdAt := "2026-09-07T12:00:00Z"
|
||||
insertJournalMessageVersion(t, database, 1, false, map[string]any{
|
||||
"message_id": "message-1", "turn_id": "turn-1", "task_id": "task-1",
|
||||
"author_type": "agent", "author_id": "agent-1", "content": "before",
|
||||
"requests_input": 0, "message_type": "message", "created_at": createdAt,
|
||||
"updated_at": createdAt, "prompt_index": 0, "metadata_json": `{}`,
|
||||
})
|
||||
insertJournalMessageVersion(t, database, 2, false, map[string]any{
|
||||
"message_id": "message-1", "turn_id": "turn-1", "task_id": "task-1",
|
||||
"author_type": "agent", "author_id": "agent-1", "content": "after",
|
||||
"requests_input": 0, "message_type": "message", "created_at": createdAt,
|
||||
"updated_at": "2026-09-07T12:01:00Z", "prompt_index": 0, "metadata_json": `{}`,
|
||||
})
|
||||
insertJournalMessageVersion(t, database, 3, true, map[string]any{"message_id": "message-1"})
|
||||
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
service.SetConversationJournalDB(database)
|
||||
|
||||
atInsert, more, err := service.conversationMessagesAt(context.Background(), "session-1", 1, nil, nil, "asc", "", 20)
|
||||
require.NoError(t, err)
|
||||
require.False(t, more)
|
||||
require.Len(t, atInsert, 1)
|
||||
require.Equal(t, "before", atInsert[0].Content)
|
||||
require.Equal(t, "session-1", atInsert[0].TaskSessionID)
|
||||
|
||||
atUpdate, _, err := service.conversationMessagesAt(context.Background(), "session-1", 2, nil, nil, "asc", "", 20)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, atUpdate, 1)
|
||||
require.Equal(t, "after", atUpdate[0].Content)
|
||||
|
||||
afterDelete, _, err := service.conversationMessagesAt(context.Background(), "session-1", 3, nil, nil, "asc", "", 20)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, afterDelete)
|
||||
}
|
||||
|
||||
func TestSessionEventMaintenanceDispatchesUnsignaledCommittedRowsOnce(t *testing.T) {
|
||||
database, err := sqlx.Open("sqlite3", ":memory:")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, database.Close()) })
|
||||
_, err = database.Exec(`
|
||||
CREATE TABLE conversation_session_streams (
|
||||
session_id TEXT PRIMARY KEY, watermark INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE conversation_session_events (
|
||||
session_id TEXT NOT NULL, sequence INTEGER NOT NULL, event_id TEXT NOT NULL,
|
||||
protocol_version INTEGER NOT NULL, event_type TEXT NOT NULL, task_id TEXT,
|
||||
payload TEXT NOT NULL, created_at TIMESTAMP NOT NULL
|
||||
);
|
||||
INSERT INTO conversation_session_streams(session_id, watermark) VALUES ('session-1', 1);
|
||||
INSERT INTO conversation_session_events(
|
||||
session_id, sequence, event_id, protocol_version, event_type, task_id, payload, created_at
|
||||
) VALUES (
|
||||
'session-1', 1, 'session-1:1', 1, 'message.deleted', 'task-1',
|
||||
'{"type":"message.deleted","session_id":"session-1","task_id":"task-1","message_id":"message-1"}',
|
||||
'2026-09-07T12:00:00Z'
|
||||
);`)
|
||||
require.NoError(t, err)
|
||||
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
service.SetConversationJournalDB(database)
|
||||
var dispatched []SessionEvent
|
||||
service.SetSessionEventSink(func(event SessionEvent) {
|
||||
dispatched = append(dispatched, event)
|
||||
})
|
||||
|
||||
require.NoError(t, service.maintainSessionEvents(context.Background(), time.Now().UTC()))
|
||||
require.Len(t, dispatched, 1)
|
||||
require.Equal(t, uint64(1), dispatched[0].Sequence)
|
||||
require.NoError(t, service.maintainSessionEvents(context.Background(), time.Now().UTC()))
|
||||
require.Len(t, dispatched, 1)
|
||||
}
|
||||
|
||||
func TestPruneConversationJournalKeepsLatestRowsAndRecentEvents(t *testing.T) {
|
||||
database, err := sqlx.Open("sqlite3", ":memory:")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, database.Close()) })
|
||||
_, err = database.Exec(`
|
||||
CREATE TABLE conversation_message_versions (
|
||||
session_id TEXT NOT NULL, message_id TEXT NOT NULL, row_sequence INTEGER NOT NULL,
|
||||
task_id TEXT, author_type TEXT, created_at TIMESTAMP NOT NULL,
|
||||
tombstone BOOLEAN NOT NULL, payload TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE conversation_turn_versions (
|
||||
session_id TEXT NOT NULL, turn_id TEXT NOT NULL, row_sequence INTEGER NOT NULL,
|
||||
task_id TEXT, started_at TIMESTAMP NOT NULL, tombstone BOOLEAN NOT NULL, payload TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE conversation_session_events (
|
||||
session_id TEXT NOT NULL, sequence INTEGER NOT NULL, event_id TEXT NOT NULL,
|
||||
protocol_version INTEGER NOT NULL, event_type TEXT NOT NULL, task_id TEXT,
|
||||
payload TEXT NOT NULL, created_at TIMESTAMP NOT NULL
|
||||
);`)
|
||||
require.NoError(t, err)
|
||||
old := time.Now().UTC().Add(-2 * SessionEventRetention)
|
||||
recent := time.Now().UTC()
|
||||
_, err = database.Exec(`
|
||||
INSERT INTO conversation_message_versions VALUES
|
||||
('session-1', 'message-1', 1, 'task-1', 'agent', ?, FALSE, '{}'),
|
||||
('session-1', 'message-1', 2, 'task-1', 'agent', ?, FALSE, '{}');
|
||||
INSERT INTO conversation_turn_versions VALUES
|
||||
('session-1', 'turn-1', 1, 'task-1', ?, FALSE, '{}'),
|
||||
('session-1', 'turn-1', 2, 'task-1', ?, FALSE, '{}');
|
||||
INSERT INTO conversation_session_events VALUES
|
||||
('session-1', 1, 'session-1:1', 1, 'message.added', 'task-1', '{}', ?),
|
||||
('session-1', 2, 'session-1:2', 1, 'message.updated', 'task-1', '{}', ?);`,
|
||||
old, recent, old, recent, old, recent)
|
||||
require.NoError(t, err)
|
||||
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
service.SetConversationJournalDB(database)
|
||||
require.NoError(t, service.pruneConversationJournal(context.Background(), time.Now().UTC().Add(-SessionEventRetention), nil))
|
||||
|
||||
var count int
|
||||
require.NoError(t, database.Get(&count, "SELECT COUNT(*) FROM conversation_message_versions"))
|
||||
require.Equal(t, 1, count)
|
||||
require.NoError(t, database.Get(&count, "SELECT COUNT(*) FROM conversation_turn_versions"))
|
||||
require.Equal(t, 1, count)
|
||||
require.NoError(t, database.Get(&count, "SELECT COUNT(*) FROM conversation_session_events"))
|
||||
require.Equal(t, 1, count)
|
||||
}
|
||||
func TestPruneConversationJournalPurgestDeadSessionPartitions(t *testing.T) {
|
||||
database, err := sqlx.Open("sqlite3", ":memory:")
|
||||
require.NoError(t, err)
|
||||
database.SetMaxOpenConns(1)
|
||||
t.Cleanup(func() { require.NoError(t, database.Close()) })
|
||||
_, err = database.Exec(`
|
||||
CREATE TABLE conversation_session_streams (
|
||||
session_id TEXT PRIMARY KEY, watermark INTEGER NOT NULL DEFAULT 0,
|
||||
terminal BOOLEAN NOT NULL DEFAULT FALSE, updated_at TIMESTAMP NOT NULL
|
||||
);
|
||||
CREATE TABLE conversation_message_versions (
|
||||
session_id TEXT NOT NULL, message_id TEXT NOT NULL, row_sequence INTEGER NOT NULL,
|
||||
task_id TEXT, author_type TEXT, created_at TIMESTAMP NOT NULL,
|
||||
tombstone BOOLEAN NOT NULL, payload TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE conversation_turn_versions (
|
||||
session_id TEXT NOT NULL, turn_id TEXT NOT NULL, row_sequence INTEGER NOT NULL,
|
||||
task_id TEXT, started_at TIMESTAMP NOT NULL, tombstone BOOLEAN NOT NULL, payload TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE conversation_session_events (
|
||||
session_id TEXT NOT NULL, sequence INTEGER NOT NULL, event_id TEXT NOT NULL,
|
||||
protocol_version INTEGER NOT NULL, event_type TEXT NOT NULL, task_id TEXT,
|
||||
payload TEXT NOT NULL, created_at TIMESTAMP NOT NULL
|
||||
);
|
||||
CREATE TABLE task_sessions (
|
||||
id TEXT PRIMARY KEY, task_id TEXT NOT NULL, started_at TIMESTAMP NOT NULL
|
||||
);`)
|
||||
require.NoError(t, err)
|
||||
old := time.Now().UTC().Add(-2 * SessionEventRetention)
|
||||
recent := time.Now().UTC()
|
||||
for _, seed := range []struct {
|
||||
sql string
|
||||
args []any
|
||||
}{
|
||||
{`INSERT INTO conversation_session_streams VALUES ('session-dead', 5, TRUE, ?)`, []any{old}},
|
||||
{`INSERT INTO conversation_session_streams VALUES ('session-live', 5, TRUE, ?)`, []any{recent}},
|
||||
{`INSERT INTO conversation_session_streams VALUES ('session-retained', 5, TRUE, ?)`, []any{recent}},
|
||||
{`INSERT INTO conversation_message_versions VALUES ('session-dead', 'm-1', 1, 'task-1', 'agent', ?, FALSE, '{}')`, []any{old}},
|
||||
{`INSERT INTO conversation_message_versions VALUES ('session-dead', 'm-1', 2, 'task-1', 'agent', ?, FALSE, '{}')`, []any{recent}},
|
||||
{`INSERT INTO conversation_turn_versions VALUES ('session-dead', 'turn-1', 3, 'task-1', ?, FALSE, '{}')`, []any{old}},
|
||||
{`INSERT INTO conversation_session_events VALUES ('session-dead', 1, 'session-dead:1', 1, 'message.added', 'task-1', '{}', ?)`, []any{old}},
|
||||
{`INSERT INTO conversation_session_events VALUES ('session-dead', 5, 'session-dead:5', 1, 'session.removed', 'task-1', '{}', ?)`, []any{recent}},
|
||||
{`INSERT INTO task_sessions VALUES ('session-live', 'task-1', ?)`, []any{recent}},
|
||||
} {
|
||||
if _, err := database.Exec(seed.sql, seed.args...); err != nil {
|
||||
t.Fatalf("seed: %v\n%s", err, seed.sql)
|
||||
}
|
||||
}
|
||||
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
service.SetConversationJournalDB(database)
|
||||
retained := map[string]struct{}{"session-retained": {}}
|
||||
require.NoError(t, service.pruneConversationJournal(context.Background(), time.Now().UTC(), retained))
|
||||
|
||||
var streams int
|
||||
require.NoError(t, database.Get(&streams, "SELECT COUNT(*) FROM conversation_session_streams"))
|
||||
require.Equal(t, 2, streams) // live + retained survive; dead purged
|
||||
var versions int
|
||||
require.NoError(t, database.Get(&versions, "SELECT COUNT(*) FROM conversation_message_versions"))
|
||||
require.Equal(t, 0, versions)
|
||||
require.NoError(t, database.Get(&versions, "SELECT COUNT(*) FROM conversation_turn_versions"))
|
||||
require.Equal(t, 0, versions)
|
||||
require.NoError(t, database.Get(&versions, "SELECT COUNT(*) FROM conversation_session_events"))
|
||||
require.Equal(t, 0, versions)
|
||||
require.NoError(t, database.Get(&streams, "SELECT COUNT(*) FROM conversation_session_streams WHERE session_id = 'session-dead'"))
|
||||
require.Equal(t, 0, streams)
|
||||
}
|
||||
|
||||
func insertJournalMessageVersion(t *testing.T, database *sqlx.DB, sequence int, tombstone bool, payload map[string]any) {
|
||||
|
||||
t.Helper()
|
||||
raw, err := json.Marshal(payload)
|
||||
require.NoError(t, err)
|
||||
_, err = database.Exec(`
|
||||
INSERT INTO conversation_message_versions(
|
||||
session_id, message_id, row_sequence, task_id, author_type, created_at, tombstone, payload
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
"session-1", "message-1", sequence, "task-1", "agent", time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC), tombstone, string(raw),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
func TestSanitizeConversationTurnEventPreservesPublicMetadata(t *testing.T) {
|
||||
raw := json.RawMessage(`{
|
||||
"type": "session.turn.completed",
|
||||
"session_id": "session-1",
|
||||
"task_id": "task-1",
|
||||
"id": "turn-1",
|
||||
"started_at": "2026-09-07T12:00:00Z",
|
||||
"completed_at": "2026-09-07T12:01:00Z",
|
||||
"metadata": {
|
||||
"runtime_config_snapshot": {
|
||||
"model": "mock-fast",
|
||||
"config_baseline": {"effort": "medium"}
|
||||
}
|
||||
},
|
||||
"private": "must-not-be-copied"
|
||||
}`)
|
||||
|
||||
sanitized := sanitizeConversationEventPayload(conversationTurnCompleted, raw)
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal(sanitized, &payload))
|
||||
require.Equal(t, map[string]any{
|
||||
"runtime_config_snapshot": map[string]any{
|
||||
"model": "mock-fast",
|
||||
"config_baseline": map[string]any{"effort": "medium"},
|
||||
},
|
||||
}, payload["metadata"])
|
||||
require.NotContains(t, payload, "private")
|
||||
}
|
||||
func TestSanitizeConversationMessageEventPreservesPresentationMetadata(t *testing.T) {
|
||||
raw := json.RawMessage(`{
|
||||
"type": "message.added",
|
||||
"session_id": "session-1",
|
||||
"task_id": "task-1",
|
||||
"message_id": "message-1",
|
||||
"author_type": "agent",
|
||||
"message_type": "clarification_request",
|
||||
"requests_input": true,
|
||||
"metadata": {
|
||||
"request_id": "request-1",
|
||||
"tool_call_id": "tool-1",
|
||||
"action_type": "execute",
|
||||
"action_details": {"command": "ls"},
|
||||
"options": [
|
||||
{"option_id": "allow", "name": "Allow", "kind": "allow_once"},
|
||||
{"option_id": "reject", "name": "Reject", "kind": "reject_once"}
|
||||
],
|
||||
"pending_id": "pending-1",
|
||||
"question": {"id": "question-1", "prompt": "Choose <kandev-system>hidden</kandev-system>"},
|
||||
"secret": "must-not-be-copied",
|
||||
"raw_content": "must-not-be-copied"
|
||||
},
|
||||
"content": "Question"
|
||||
}`)
|
||||
|
||||
sanitized := sanitizeConversationEventPayload(events.MessageAdded, raw)
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal(sanitized, &payload))
|
||||
require.Equal(t, "clarification_request", payload["message_type"])
|
||||
require.Equal(t, true, payload["requests_input"])
|
||||
require.Equal(t, map[string]any{
|
||||
"action_details": map[string]any{"command": "ls"},
|
||||
"action_type": "execute",
|
||||
"options": []any{
|
||||
map[string]any{"option_id": "allow", "name": "Allow", "kind": "allow_once"},
|
||||
map[string]any{"option_id": "reject", "name": "Reject", "kind": "reject_once"},
|
||||
},
|
||||
"pending_id": "pending-1",
|
||||
"question": map[string]any{"id": "question-1", "prompt": "Choose"},
|
||||
"request_id": "request-1",
|
||||
"tool_call_id": "tool-1",
|
||||
}, payload["metadata"])
|
||||
}
|
||||
|
||||
func TestSanitizeConversationMessageEventPreservesAgentBootMetadata(t *testing.T) {
|
||||
raw := json.RawMessage(`{
|
||||
"type": "message.updated",
|
||||
"session_id": "session-1",
|
||||
"task_id": "task-1",
|
||||
"message_id": "message-1",
|
||||
"author_type": "agent",
|
||||
"message_type": "script_execution",
|
||||
"metadata": {
|
||||
"script_type": "agent_boot",
|
||||
"agent_name": "Mock",
|
||||
"command": "/usr/local/bin/mock-agent",
|
||||
"status": "exited",
|
||||
"exit_code": 0,
|
||||
"is_resuming": true,
|
||||
"started_at": "2026-09-15T09:45:58.245720115Z",
|
||||
"completed_at": "2026-09-15T09:45:59.774503743Z",
|
||||
"error": "must-not-be-dropped",
|
||||
"private": "must-not-be-copied"
|
||||
},
|
||||
"content": "boot output"
|
||||
}`)
|
||||
|
||||
sanitized := sanitizeConversationEventPayload(events.MessageUpdated, raw)
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal(sanitized, &payload))
|
||||
require.Equal(t, map[string]any{
|
||||
"agent_name": "Mock",
|
||||
"command": "/usr/local/bin/mock-agent",
|
||||
"completed_at": "2026-09-15T09:45:59.774503743Z",
|
||||
"error": "must-not-be-dropped",
|
||||
"exit_code": float64(0),
|
||||
"is_resuming": true,
|
||||
"script_type": "agent_boot",
|
||||
"started_at": "2026-09-15T09:45:58.245720115Z",
|
||||
"status": "exited",
|
||||
}, payload["metadata"])
|
||||
require.NotContains(t, payload["metadata"], "private")
|
||||
}
|
||||
|
||||
func TestSyncCommittedSessionEventsStripsSystemContent(t *testing.T) {
|
||||
database, err := sqlx.Open("sqlite3", ":memory:")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, database.Close()) })
|
||||
_, err = database.Exec(`
|
||||
CREATE TABLE conversation_session_streams (
|
||||
session_id TEXT PRIMARY KEY, watermark INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE conversation_session_events (
|
||||
session_id TEXT NOT NULL, sequence INTEGER NOT NULL, event_id TEXT NOT NULL,
|
||||
protocol_version INTEGER NOT NULL, event_type TEXT NOT NULL, task_id TEXT,
|
||||
payload TEXT NOT NULL, created_at TIMESTAMP NOT NULL
|
||||
);
|
||||
INSERT INTO conversation_session_streams(session_id, watermark) VALUES ('session-1', 1);
|
||||
INSERT INTO conversation_session_events VALUES (
|
||||
'session-1', 1, 'session-1:1', 1, 'message.added', 'task-1',
|
||||
'{"type":"message.added","session_id":"session-1","task_id":"task-1","message_id":"message-1","author_type":"user","author_id":"agent-1","metadata":{"sender_task_id":"sender-1","secret":"no"},"content":"visible <kandev-system>secret</kandev-system>"}',
|
||||
'2026-09-07T12:00:00Z'
|
||||
);`)
|
||||
require.NoError(t, err)
|
||||
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
service.SetConversationJournalDB(database)
|
||||
var payload map[string]any
|
||||
events, err := service.SyncCommittedSessionEvents(context.Background(), "session-1")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, json.Unmarshal(events[0].Payload, &payload))
|
||||
require.Equal(t, "visible", payload["content"])
|
||||
require.Equal(t, "sender-1", payload["sender_task_id"])
|
||||
require.NotContains(t, payload, "author_id")
|
||||
require.Equal(t, map[string]any{"sender_task_id": "sender-1"}, payload["metadata"])
|
||||
}
|
||||
|
||||
func TestSyncCommittedSessionEventsPoisonsMalformedPayloadWithoutRetainingRawBytes(t *testing.T) {
|
||||
const sessionID = "malformed-journal-session"
|
||||
database, err := sqlx.Open("sqlite3", ":memory:")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, database.Close()) })
|
||||
_, err = database.Exec(`
|
||||
CREATE TABLE conversation_session_streams (
|
||||
session_id TEXT PRIMARY KEY, watermark INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE conversation_session_events (
|
||||
session_id TEXT NOT NULL, sequence INTEGER NOT NULL, event_id TEXT NOT NULL,
|
||||
protocol_version INTEGER NOT NULL, event_type TEXT NOT NULL, task_id TEXT,
|
||||
payload TEXT NOT NULL, created_at TIMESTAMP NOT NULL
|
||||
);
|
||||
INSERT INTO conversation_session_streams(session_id, watermark) VALUES ('` + sessionID + `', 1);
|
||||
INSERT INTO conversation_session_events VALUES (
|
||||
'` + sessionID + `', 1, '` + sessionID + `:1', 1, 'message.added', 'task-1',
|
||||
'{"content":"private <kandev-system>secret</kandev-system>"',
|
||||
'2026-09-07T12:00:00Z'
|
||||
);`)
|
||||
require.NoError(t, err)
|
||||
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
service.SetConversationJournalDB(database)
|
||||
events, err := service.SyncCommittedSessionEvents(context.Background(), sessionID)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, events, 1)
|
||||
require.Equal(t, "{}", string(events[0].Payload))
|
||||
require.NotContains(t, string(events[0].Payload), "private")
|
||||
require.NotContains(t, string(events[0].Payload), "kandev-system")
|
||||
_, projectionErr := ProjectSessionEvent(events[0])
|
||||
require.Error(t, projectionErr)
|
||||
}
|
||||
|
||||
func TestConversationMessagesAtErrorsWhenPageCursorWasTombstoned(t *testing.T) {
|
||||
database, err := sqlx.Open("sqlite3", ":memory:")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, database.Close()) })
|
||||
_, err = database.Exec(`
|
||||
CREATE TABLE conversation_message_versions (
|
||||
session_id TEXT NOT NULL, message_id TEXT NOT NULL, row_sequence INTEGER NOT NULL,
|
||||
task_id TEXT, author_type TEXT, created_at TIMESTAMP NOT NULL,
|
||||
tombstone BOOLEAN NOT NULL, payload TEXT NOT NULL
|
||||
);`)
|
||||
require.NoError(t, err)
|
||||
createdAt := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
insertJournalMessageVersion(t, database, 1, false, map[string]any{
|
||||
"message_id": "message-1", "turn_id": "turn-1", "task_id": "task-1",
|
||||
"author_type": "agent", "content": "first", "message_type": "message",
|
||||
"created_at": createdAt.Format(time.RFC3339Nano),
|
||||
"updated_at": createdAt.Format(time.RFC3339Nano),
|
||||
})
|
||||
insertJournalMessageVersion(t, database, 2, true, map[string]any{"message_id": "message-1"})
|
||||
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
service.SetConversationJournalDB(database)
|
||||
|
||||
_, _, err = service.conversationMessagesAt(
|
||||
context.Background(), "session-1", 2, nil, nil, "asc", "message-1", 20,
|
||||
)
|
||||
require.ErrorIs(t, err, errConversationCursorGone)
|
||||
}
|
||||
|
||||
// TestSyncCommittedSessionEventsPoisonsMalformedPayloadOnDurableLog pins that
|
||||
// a malformed primary-journal row is mirrored into the FILE-backed durable log
|
||||
// as a poison record with a non-nil, non-raw payload (the session_events
|
||||
// payload column is BLOB NOT NULL, so nil would fail the append).
|
||||
func TestSyncCommittedSessionEventsPoisonsMalformedPayloadOnDurableLog(t *testing.T) {
|
||||
database, err := sqlx.Open("sqlite3", ":memory:")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, database.Close()) })
|
||||
_, err = database.Exec(`
|
||||
CREATE TABLE conversation_session_streams (
|
||||
session_id TEXT PRIMARY KEY, watermark INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE conversation_session_events (
|
||||
session_id TEXT NOT NULL, sequence INTEGER NOT NULL, event_id TEXT NOT NULL,
|
||||
protocol_version INTEGER NOT NULL, event_type TEXT NOT NULL, task_id TEXT,
|
||||
payload TEXT NOT NULL, created_at TIMESTAMP NOT NULL
|
||||
);
|
||||
INSERT INTO conversation_session_streams(session_id, watermark) VALUES ('sess-durable-poison', 1);
|
||||
INSERT INTO conversation_session_events VALUES (
|
||||
'sess-durable-poison', 1, 'sess-durable-poison:1', 1, 'message.added', 'task-1',
|
||||
'{"content":"private <kandev-system>secret</kandev-system>"',
|
||||
'2026-09-07T12:00:00Z'
|
||||
);`)
|
||||
require.NoError(t, err)
|
||||
|
||||
dir := t.TempDir()
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
require.NoError(t, service.SetPluginsDir(dir))
|
||||
t.Cleanup(func() { _ = service.Close() })
|
||||
service.SetConversationJournalDB(database)
|
||||
|
||||
events, err := service.SyncCommittedSessionEvents(context.Background(), "sess-durable-poison")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, events, 1)
|
||||
require.Equal(t, "{}", string(events[0].Payload))
|
||||
log := service.SessionEvents()
|
||||
record, ok := log.Poison("sess-durable-poison", events[0].ID)
|
||||
require.True(t, ok)
|
||||
require.NotEmpty(t, record.LastError)
|
||||
|
||||
// A restart of the durable log keeps the poison and the non-raw payload.
|
||||
reopened, err := NewSessionEventLog(filepath.Join(dir, ".host", "session-events.sqlite"))
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = reopened.Close() })
|
||||
restored, ok := reopened.Poison("sess-durable-poison", events[0].ID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, record.Attempts, restored.Attempts)
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,125 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrConversationBindingInvalid = errors.New("invalid conversation binding")
|
||||
ErrConversationGenerationSuperseded = errors.New("conversation generation superseded")
|
||||
)
|
||||
|
||||
// SessionEvents returns the service-owned durable stream store used by the
|
||||
// production WebSocket gateway.
|
||||
func (s *Service) SessionEvents() *SessionEventLog {
|
||||
return s.sessionEvents
|
||||
}
|
||||
|
||||
// isSessionRemoved reports whether the durable stream has a terminal removal
|
||||
// marker for sessionID. It is the only journal fallback authorization after
|
||||
// the task-service session lookup no longer succeeds.
|
||||
func (s *Service) isSessionRemoved(sessionID string) bool {
|
||||
if s.sessionEvents == nil {
|
||||
return false
|
||||
}
|
||||
_, _, terminal := s.sessionEvents.ReplayState(sessionID, 0)
|
||||
return terminal
|
||||
}
|
||||
|
||||
// SessionDelivery returns the service-owned poison delivery dispatcher.
|
||||
func (s *Service) SessionDelivery() *SessionDeliveryDispatcher {
|
||||
return s.sessionDelivery
|
||||
}
|
||||
|
||||
// AuthorizeConversationConsumer validates an active capability declaration and
|
||||
// the loader-issued user/plugin/generation binding without exposing token data.
|
||||
func (s *Service) AuthorizeConversationConsumer(
|
||||
pluginID string,
|
||||
userID string,
|
||||
generation int64,
|
||||
bindingToken string,
|
||||
) error {
|
||||
record, err := s.Get(pluginID)
|
||||
if err != nil || record.Status != StatusActive || !record.Capabilities.CanRead("messages") {
|
||||
return ErrConversationBindingInvalid
|
||||
}
|
||||
if conversationGeneration(record.InstalledAt) != generation {
|
||||
return ErrConversationGenerationSuperseded
|
||||
}
|
||||
claims, err := s.conversationTokens.parse(bindingToken)
|
||||
if err != nil ||
|
||||
claims.Kind != tokenKindBinding ||
|
||||
claims.PluginID != pluginID ||
|
||||
claims.UserID != userID ||
|
||||
claims.Generation != generation {
|
||||
return ErrConversationBindingInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) MintSessionStreamGrant(
|
||||
pluginID string,
|
||||
userID string,
|
||||
generation int64,
|
||||
sessionID string,
|
||||
consumerID string,
|
||||
wireID string,
|
||||
snapshotCutoff uint64,
|
||||
resumeCursor uint64,
|
||||
) (snapshotToken string, resumeToken string, expiresAt time.Time, err error) {
|
||||
fingerprint := consumerID
|
||||
if fingerprint == "" {
|
||||
fingerprint = wireID
|
||||
}
|
||||
snapshotToken, err = s.conversationTokens.mintSnapshot(
|
||||
pluginID,
|
||||
userID,
|
||||
generation,
|
||||
sessionID,
|
||||
nil,
|
||||
"",
|
||||
nil,
|
||||
int64(snapshotCutoff),
|
||||
fingerprint,
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", time.Time{}, err
|
||||
}
|
||||
resumeToken, err = s.conversationTokens.mintResume(
|
||||
pluginID,
|
||||
userID,
|
||||
generation,
|
||||
sessionID,
|
||||
consumerID,
|
||||
wireID,
|
||||
resumeCursor,
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", time.Time{}, err
|
||||
}
|
||||
return snapshotToken, resumeToken, time.Now().UTC().Add(conversationTokenTTL), nil
|
||||
}
|
||||
|
||||
func (s *Service) ValidateSessionResume(
|
||||
token string,
|
||||
key SessionDeliveryCursorKey,
|
||||
sequence uint64,
|
||||
) error {
|
||||
if token == "" {
|
||||
return ErrConversationBindingInvalid
|
||||
}
|
||||
claims, err := s.conversationTokens.parse(token)
|
||||
if err != nil ||
|
||||
claims.Kind != tokenKindResume ||
|
||||
claims.PluginID != key.PluginID ||
|
||||
claims.UserID != key.UserID ||
|
||||
claims.Generation != key.Generation ||
|
||||
claims.SessionID != key.SessionID ||
|
||||
claims.ConsumerID != key.ConsumerID ||
|
||||
claims.WireID != key.WireID ||
|
||||
claims.Sequence > sequence {
|
||||
return ErrConversationBindingInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,439 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSessionEventLogPersistsSequenceAndTerminalRemoval(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "session-events.db")
|
||||
log, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, log.Close()) })
|
||||
first, err := log.Append("session-1", stringPtr("task-1"), "message.added", validMessageAddedPayload("m1"))
|
||||
require.NoError(t, err)
|
||||
removed, err := log.Append("session-1", stringPtr("task-1"), "session.removed", validSessionRemovedPayload())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, uint64(1), first.Sequence)
|
||||
require.Equal(t, uint64(2), removed.Sequence)
|
||||
require.NoError(t, log.Close())
|
||||
|
||||
reopened, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, reopened.Close()) })
|
||||
events, terminal := reopened.EventsAfter("session-1", 0)
|
||||
require.True(t, terminal)
|
||||
require.Equal(t, []uint64{1, 2}, []uint64{events[0].Sequence, events[1].Sequence})
|
||||
_, err = reopened.Append("session-1", stringPtr("task-1"), "message.updated", json.RawMessage(`{"type":"message.updated","id":"m1"}`))
|
||||
require.ErrorIs(t, err, ErrSessionRemoved)
|
||||
}
|
||||
|
||||
func TestSessionEventLogPoisonBlocksAckUntilAuditedRequeue(t *testing.T) {
|
||||
log, err := NewSessionEventLog("")
|
||||
require.NoError(t, err)
|
||||
dispatcher := NewSessionDeliveryDispatcher(log)
|
||||
_, err = log.Append("session-1", stringPtr("task-1"), "message.added", validMessageAddedPayload("m1"))
|
||||
require.NoError(t, err)
|
||||
poison, err := log.Append("session-1", stringPtr("task-1"), "unknown.event", json.RawMessage(`{"type":"unknown.event","session_id":"session-1","task_id":"task-1"}`))
|
||||
require.NoError(t, err)
|
||||
cursor := testCursor()
|
||||
require.NoError(t, log.RegisterCursor(cursor, 0))
|
||||
require.NoError(t, log.Acknowledge(cursor, 1))
|
||||
require.ErrorIs(t, log.Acknowledge(cursor, 2), ErrPoisonEvent)
|
||||
base := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
for attempt := range SessionPoisonMaxAttempts {
|
||||
claimAndCompletePoison(t, dispatcher, "session-1", poison.ID, base.Add(time.Duration(attempt)*time.Minute), true)
|
||||
}
|
||||
record, ok := log.Poison("session-1", poison.ID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, SessionPoisonExhausted, record.State)
|
||||
require.Equal(t, SessionPoisonMaxAttempts, record.Attempts)
|
||||
require.ErrorIs(t, dispatcher.Requeue("session-1", poison.ID, record.OwnerEpoch+1, "admin-1", base), ErrStaleOwnerEpoch)
|
||||
require.NoError(t, dispatcher.Requeue("session-1", poison.ID, record.OwnerEpoch, "admin-1", base))
|
||||
}
|
||||
|
||||
func TestSessionEventPoisonDeliveryClaimControlsLeaseBackoffExhaustionAndRequeue(t *testing.T) {
|
||||
log, err := NewSessionEventLog("")
|
||||
require.NoError(t, err)
|
||||
dispatcher := NewSessionDeliveryDispatcher(log)
|
||||
plain, err := log.Append("session-1", stringPtr("task-1"), "message.added", validMessageAddedPayload("plain"))
|
||||
require.NoError(t, err)
|
||||
plainClaim, err := dispatcher.Claim("session-1", plain.ID, time.Now())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, SessionDeliveryUntracked, plainClaim.Disposition)
|
||||
|
||||
poison, err := log.Append("session-1", stringPtr("task-1"), "unknown.event", json.RawMessage(`{"type":"unknown.event","session_id":"session-1","task_id":"task-1"}`))
|
||||
require.NoError(t, err)
|
||||
base := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
first := claimAndCompletePoison(t, dispatcher, "session-1", poison.ID, base, true)
|
||||
require.Equal(t, SessionDeliveryClaimed, first.Disposition)
|
||||
leased, err := dispatcher.Claim("session-1", poison.ID, base.Add(time.Second))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, SessionDeliveryLeased, leased.Disposition)
|
||||
backoff, err := dispatcher.Claim("session-1", poison.ID, base.Add(SessionPoisonLease))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, SessionDeliveryBackoff, backoff.Disposition)
|
||||
|
||||
for attempt := 1; attempt < SessionPoisonMaxAttempts; attempt++ {
|
||||
claimAndCompletePoison(t, dispatcher, "session-1", poison.ID, base.Add(time.Duration(attempt)*time.Minute), true)
|
||||
}
|
||||
exhausted, err := dispatcher.Claim("session-1", poison.ID, base.Add(10*time.Minute))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, SessionDeliveryExhausted, exhausted.Disposition)
|
||||
record, ok := log.Poison("session-1", poison.ID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, SessionPoisonMaxAttempts, record.Attempts)
|
||||
|
||||
require.NoError(t, dispatcher.Requeue("session-1", poison.ID, record.OwnerEpoch, "admin-1", base.Add(11*time.Minute)))
|
||||
requeued := claimAndCompletePoison(t, dispatcher, "session-1", poison.ID, base.Add(11*time.Minute), true)
|
||||
require.Equal(t, SessionDeliveryClaimed, requeued.Disposition)
|
||||
record, ok = log.Poison("session-1", poison.ID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 1, record.Attempts)
|
||||
}
|
||||
|
||||
func claimAndCompletePoison(
|
||||
t *testing.T,
|
||||
dispatcher *SessionDeliveryDispatcher,
|
||||
sessionID string,
|
||||
eventID string,
|
||||
at time.Time,
|
||||
queued bool,
|
||||
) SessionDeliveryClaim {
|
||||
t.Helper()
|
||||
claim, err := dispatcher.Claim(sessionID, eventID, at)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, SessionDeliveryClaimed, claim.Disposition)
|
||||
require.NoError(t, dispatcher.Complete(claim, queued, at))
|
||||
return claim
|
||||
}
|
||||
|
||||
func TestSessionEventRetentionKeepsTerminalTombstoneThroughBothBounds(t *testing.T) {
|
||||
log, err := NewSessionEventLog("")
|
||||
require.NoError(t, err)
|
||||
start := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
log.now = func() time.Time { return start }
|
||||
_, err = log.Append("session-1", stringPtr("task-1"), "session.removed", validSessionRemovedPayload())
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, log.CollectExpired(start.Add(SessionEventRetention-time.Second)))
|
||||
_, terminal := log.EventsAfter("session-1", 0)
|
||||
require.True(t, terminal)
|
||||
require.NoError(t, log.CollectExpired(start.Add(SessionEventRetention)))
|
||||
events, terminal := log.EventsAfter("session-1", 0)
|
||||
require.False(t, terminal)
|
||||
require.Empty(t, events)
|
||||
}
|
||||
|
||||
func TestSessionEventResyncAfterTerminalCollectionHealsForwardGap(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "session-events.db")
|
||||
log, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, log.Close()) })
|
||||
start := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
log.now = func() time.Time { return start }
|
||||
removed, err := log.AppendCommitted(SessionEvent{
|
||||
SessionID: "session-1", TaskID: stringPtr("task-1"), Sequence: 3,
|
||||
ID: "session-1:3", ProtocolVersion: SessionEventProtocolVersion, EventType: "session.removed",
|
||||
Payload: validSessionRemovedPayload(), CreatedAt: start,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, removed)
|
||||
|
||||
// After retention both bounds lapse the local partition is dropped: the
|
||||
// terminal tombstone is gone from the durable log.
|
||||
require.NoError(t, log.CollectExpired(start.Add(SessionEventRetention+time.Hour)))
|
||||
_, terminal := log.EventsAfter("session-1", 0)
|
||||
require.False(t, terminal)
|
||||
|
||||
// The primary journal keeps only its session.removed row (all other rows
|
||||
// are pruned), which is not at sequence 1. Re-mirroring it must heal the
|
||||
// gap instead of failing forever.
|
||||
removed, err = log.AppendCommitted(SessionEvent{
|
||||
SessionID: "session-1", TaskID: stringPtr("task-1"), Sequence: 3,
|
||||
ID: "session-1:3", ProtocolVersion: SessionEventProtocolVersion, EventType: "session.removed",
|
||||
Payload: validSessionRemovedPayload(), CreatedAt: start.Add(time.Hour),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, removed)
|
||||
events, terminal := log.EventsAfter("session-1", 0)
|
||||
require.True(t, terminal)
|
||||
require.Len(t, events, 1)
|
||||
require.Equal(t, uint64(3), events[0].Sequence)
|
||||
}
|
||||
|
||||
func TestSessionEventResyncAfterTruncationHealsForwardGap(t *testing.T) {
|
||||
log, err := NewSessionEventLog("")
|
||||
require.NoError(t, err)
|
||||
start := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
log.now = func() time.Time { return start }
|
||||
// seq 1 and 2 mirror fine; then the idle partition's rows age out and are
|
||||
// truncated while the primary keeps a newer row at seq 4 (seq 3 pruned).
|
||||
for _, sequence := range []uint64{1, 2} {
|
||||
_, err := log.AppendCommitted(SessionEvent{
|
||||
SessionID: "session-1", TaskID: stringPtr("task-1"), Sequence: sequence,
|
||||
ID: "session-1:" + strconv.FormatUint(sequence, 10), ProtocolVersion: SessionEventProtocolVersion, EventType: "message.added",
|
||||
Payload: validMessageAddedPayload("m" + strconv.FormatUint(sequence, 10)), CreatedAt: start,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
require.NoError(t, log.CollectExpired(start.Add(SessionEventRetention+time.Hour)))
|
||||
_, terminal := log.EventsAfter("session-1", 0)
|
||||
require.False(t, terminal)
|
||||
require.Equal(t, uint64(2), log.Watermark("session-1"))
|
||||
|
||||
appended, err := log.AppendCommitted(SessionEvent{
|
||||
SessionID: "session-1", TaskID: stringPtr("task-1"), Sequence: 4,
|
||||
ID: "session-1:4", ProtocolVersion: SessionEventProtocolVersion, EventType: "message.added",
|
||||
Payload: validMessageAddedPayload("m4"), CreatedAt: start.Add(time.Hour),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, appended)
|
||||
require.Equal(t, uint64(4), log.Watermark("session-1"))
|
||||
}
|
||||
|
||||
func TestSessionEventRetentionExpiresExhaustedPoisonState(t *testing.T) {
|
||||
log, err := NewSessionEventLog("")
|
||||
require.NoError(t, err)
|
||||
start := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
log.now = func() time.Time { return start }
|
||||
event, err := log.Append("session-1", stringPtr("task-1"), "unknown.event", json.RawMessage(`{"type":"unknown.event","session_id":"session-1","task_id":"task-1"}`))
|
||||
require.NoError(t, err)
|
||||
dispatcher := NewSessionDeliveryDispatcher(log)
|
||||
for attempt := range SessionPoisonMaxAttempts {
|
||||
claimAndCompletePoison(t, dispatcher, "session-1", event.ID, start.Add(time.Duration(attempt)*time.Minute), true)
|
||||
}
|
||||
require.NoError(t, log.CollectExpired(start.Add(SessionEventRetention+5*time.Minute)))
|
||||
_, ok := log.Poison("session-1", event.ID)
|
||||
require.False(t, ok)
|
||||
events, terminal := log.EventsAfter("session-1", 0)
|
||||
require.False(t, terminal)
|
||||
require.Empty(t, events)
|
||||
}
|
||||
|
||||
func TestSessionEventCursorRegistrationAndExplicitRelease(t *testing.T) {
|
||||
log, err := NewSessionEventLog("")
|
||||
require.NoError(t, err)
|
||||
event, err := log.Append("session-1", stringPtr("task-1"), "message.added", validMessageAddedPayload("m1"))
|
||||
require.NoError(t, err)
|
||||
key := testCursor()
|
||||
require.NoError(t, log.RegisterCursor(key, event.Sequence))
|
||||
require.Contains(t, log.state.Cursors, deliveryCursorKey(key))
|
||||
require.NoError(t, log.ReleaseCursor(key))
|
||||
require.NotContains(t, log.state.Cursors, deliveryCursorKey(key))
|
||||
}
|
||||
|
||||
func TestSessionEventCursorReplacementAdvancesExistingCursor(t *testing.T) {
|
||||
log, err := NewSessionEventLog("")
|
||||
require.NoError(t, err)
|
||||
event, err := log.Append("session-1", stringPtr("task-1"), "unknown.event", json.RawMessage(`{"type":"unknown.event","session_id":"session-1","task_id":"task-1"}`))
|
||||
require.NoError(t, err)
|
||||
key := testCursor()
|
||||
require.NoError(t, log.RegisterCursor(key, 0))
|
||||
require.NoError(t, log.ReplaceCursor(key, event.Sequence))
|
||||
require.NoError(t, log.Acknowledge(key, event.Sequence))
|
||||
}
|
||||
|
||||
func TestSessionEventCursorLifecycleFieldsSurviveRestart(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "session-events.db")
|
||||
log, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, log.Close()) })
|
||||
key := testCursor()
|
||||
key.Generation = 4
|
||||
require.NoError(t, log.RegisterCursor(key, 2))
|
||||
leaseUntil := time.Date(2026, 9, 7, 12, 5, 0, 0, time.UTC)
|
||||
retryAt := time.Date(2026, 9, 7, 12, 6, 0, 0, time.UTC)
|
||||
log.mu.Lock()
|
||||
cursor := log.state.Cursors[deliveryCursorKey(key)]
|
||||
cursor.LeaseUntil = leaseUntil
|
||||
cursor.RetryAt = retryAt
|
||||
cursor.OwnerEpoch = 9
|
||||
err = log.persistLocked()
|
||||
log.mu.Unlock()
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, log.Close())
|
||||
|
||||
reopened, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, reopened.Close()) })
|
||||
restored := reopened.state.Cursors[deliveryCursorKey(key)]
|
||||
require.Equal(t, uint64(9), restored.OwnerEpoch)
|
||||
require.Equal(t, leaseUntil, restored.LeaseUntil)
|
||||
require.Equal(t, retryAt, restored.RetryAt)
|
||||
}
|
||||
|
||||
func TestSessionEventMaintenanceDoesNotCountUndeliveredPoisonAttempts(t *testing.T) {
|
||||
service := NewService(nil, NewRegistry(), nil, testLogger(t))
|
||||
start := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
poison, err := service.SessionEvents().Append("session-1", stringPtr("task-1"), "unknown.event", json.RawMessage(`{"type":"unknown.event","session_id":"session-1","task_id":"task-1"}`))
|
||||
require.NoError(t, err)
|
||||
// Maintenance runs many times; without any actual delivery attempt the
|
||||
// poison must stay pending at zero attempts instead of being exhausted.
|
||||
for _, at := range []time.Time{start, start.Add(SessionPoisonLease), start.Add(2 * SessionPoisonLease)} {
|
||||
require.NoError(t, service.maintainSessionEvents(context.Background(), at))
|
||||
record, ok := service.SessionEvents().Poison("session-1", poison.ID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 0, record.Attempts)
|
||||
require.Equal(t, SessionPoisonPending, record.State)
|
||||
}
|
||||
// A real queued delivery advances the attempt bookkeeping.
|
||||
claimAndCompletePoison(t, service.SessionDelivery(), "session-1", poison.ID, start.Add(3*SessionPoisonLease), true)
|
||||
record, ok := service.SessionEvents().Poison("session-1", poison.ID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 1, record.Attempts)
|
||||
require.Equal(t, SessionPoisonLeased, record.State)
|
||||
}
|
||||
|
||||
func TestSessionEventCursorReplacementRestoresMemoryOnPersistFailure(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "session-events.db")
|
||||
log, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, log.Close()) })
|
||||
key := testCursor()
|
||||
require.NoError(t, log.RegisterCursor(key, 0))
|
||||
event, err := log.Append("session-1", stringPtr("task-1"), "message.added", validMessageAddedPayload("m1"))
|
||||
require.NoError(t, err)
|
||||
// Close the held database before redirecting the path so persistence
|
||||
// failures exercise the closed-log guard.
|
||||
require.NoError(t, log.Close())
|
||||
log.path = filepath.Join(t.TempDir(), "missing", "session-events.db")
|
||||
|
||||
require.Error(t, log.ReplaceCursor(key, event.Sequence))
|
||||
|
||||
// The in-memory mutation rolled back, so a restart sees the committed
|
||||
// acknowledgement, not the failed replacement.
|
||||
restored := log.state.Cursors[deliveryCursorKey(key)]
|
||||
require.Equal(t, uint64(0), restored.AcknowledgedSequence)
|
||||
require.Equal(t, uint64(1), restored.OwnerEpoch)
|
||||
require.NoError(t, log.Close())
|
||||
|
||||
reopened, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, reopened.Close()) })
|
||||
require.Equal(t, uint64(0), reopened.state.Cursors[deliveryCursorKey(key)].AcknowledgedSequence)
|
||||
}
|
||||
|
||||
func TestSessionEventPoisonAttemptRestoresMemoryOnPersistFailure(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "session-events.db")
|
||||
log, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, log.Close()) })
|
||||
dispatcher := NewSessionDeliveryDispatcher(log)
|
||||
poison, err := log.Append("session-1", stringPtr("task-1"), "unknown.event", json.RawMessage(`{"type":"unknown.event","session_id":"session-1","task_id":"task-1"}`))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, log.Close())
|
||||
log.path = filepath.Join(t.TempDir(), "missing", "session-events.db")
|
||||
|
||||
base := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
_, err = dispatcher.Claim("session-1", poison.ID, base)
|
||||
require.Error(t, err)
|
||||
|
||||
record, ok := log.Poison("session-1", poison.ID)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 0, record.Attempts)
|
||||
require.Equal(t, SessionPoisonPending, record.State)
|
||||
}
|
||||
|
||||
func TestSessionEventCollectionRestoresMemoryOnPersistFailure(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "session-events.db")
|
||||
log, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, log.Close()) })
|
||||
start := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
log.now = func() time.Time { return start }
|
||||
_, err = log.Append("session-1", stringPtr("task-1"), "message.added", validMessageAddedPayload("m1"))
|
||||
require.NoError(t, err)
|
||||
_, err = log.Append("session-1", stringPtr("task-1"), "session.removed", validSessionRemovedPayload())
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, log.RegisterCursor(testCursor(), 0))
|
||||
require.NoError(t, log.Close())
|
||||
log.path = filepath.Join(t.TempDir(), "missing", "session-events.db")
|
||||
|
||||
require.Error(t, log.CollectExpired(start.Add(SessionEventRetention+time.Hour)))
|
||||
|
||||
// Nothing was collected in memory: the cursor, poison, and partition
|
||||
// survive until persistence actually commits.
|
||||
require.True(t, log.HasCursor(testCursor()))
|
||||
events, terminal := log.EventsAfter("session-1", 0)
|
||||
require.True(t, terminal)
|
||||
require.Len(t, events, 2)
|
||||
}
|
||||
|
||||
func testCursor() SessionDeliveryCursorKey {
|
||||
return SessionDeliveryCursorKey{SessionID: "session-1", ConsumerKind: "plugin", ConsumerID: "consumer-1", PluginID: "plugin-1", Generation: 1, UserID: "user-1"}
|
||||
}
|
||||
|
||||
func stringPtr(value string) *string { return &value }
|
||||
|
||||
func validMessageAddedPayload(messageID string) json.RawMessage {
|
||||
return json.RawMessage(`{"type":"message.added","session_id":"session-1","task_id":"task-1","message_id":"` + messageID + `","author_type":"user","content":"hello","message_type":"message","created_at":"2026-09-07T12:00:00Z"}`)
|
||||
}
|
||||
|
||||
func validSessionRemovedPayload() json.RawMessage {
|
||||
return json.RawMessage(`{"type":"session.removed","session_id":"session-1","task_id":"task-1"}`)
|
||||
}
|
||||
|
||||
func TestSessionEventAcknowledgePersistsOnlyTheCursorRow(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "session-events.db")
|
||||
log, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, log.Close()) })
|
||||
event, err := log.Append("session-1", stringPtr("task-1"), "message.added", validMessageAddedPayload("m1"))
|
||||
require.NoError(t, err)
|
||||
_, err = log.AppendCommitted(SessionEvent{
|
||||
SessionID: "session-1", TaskID: stringPtr("task-1"), Sequence: event.Sequence + 1,
|
||||
ID: "session-1:2", EventType: "message.added", ProtocolVersion: 1,
|
||||
Payload: validMessageAddedPayload("m2"), CreatedAt: event.CreatedAt,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
cursorA := testCursor()
|
||||
cursorA.ConsumerID = "consumer-a"
|
||||
cursorB := testCursor()
|
||||
cursorB.ConsumerID = "consumer-b"
|
||||
require.NoError(t, log.RegisterCursor(cursorA, 0))
|
||||
require.NoError(t, log.RegisterCursor(cursorB, 0))
|
||||
require.NoError(t, log.Acknowledge(cursorA, 2))
|
||||
|
||||
require.NoError(t, log.Close())
|
||||
reopened, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, reopened.Close()) })
|
||||
restoredA := reopened.state.Cursors[deliveryCursorKey(cursorA)]
|
||||
require.Equal(t, uint64(2), restoredA.AcknowledgedSequence)
|
||||
restoredB := reopened.state.Cursors[deliveryCursorKey(cursorB)]
|
||||
require.Equal(t, uint64(0), restoredB.AcknowledgedSequence)
|
||||
// The partition and both event rows survive the delta write untouched.
|
||||
events, terminal := reopened.EventsAfter("session-1", 0)
|
||||
require.False(t, terminal)
|
||||
require.Len(t, events, 2)
|
||||
require.Equal(t, uint64(2), reopened.Watermark("session-1"))
|
||||
}
|
||||
|
||||
func TestSessionEventAcknowledgeRestoresTimestampOnPersistFailure(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "session-events.db")
|
||||
log, err := NewSessionEventLog(path)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, log.Close()) })
|
||||
base := time.Date(2026, 9, 7, 12, 0, 0, 0, time.UTC)
|
||||
log.now = func() time.Time { return base }
|
||||
event, err := log.Append("session-1", stringPtr("task-1"), "message.added", validMessageAddedPayload("m1"))
|
||||
require.NoError(t, err)
|
||||
key := testCursor()
|
||||
require.NoError(t, log.RegisterCursor(key, 0))
|
||||
previous := log.state.Cursors[deliveryCursorKey(key)].UpdatedAt
|
||||
log.now = func() time.Time { return base.Add(time.Minute) }
|
||||
|
||||
require.NoError(t, log.Close())
|
||||
require.Error(t, log.Acknowledge(key, event.Sequence))
|
||||
|
||||
restored := log.state.Cursors[deliveryCursorKey(key)]
|
||||
require.Equal(t, uint64(0), restored.AcknowledgedSequence)
|
||||
require.Equal(t, previous, restored.UpdatedAt)
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
conversationTokenTTL = 10 * time.Minute
|
||||
tokenKindBinding = "binding"
|
||||
tokenKindCursor = "cursor"
|
||||
tokenKindSnapshot = "snapshot"
|
||||
tokenKindResume = "resume"
|
||||
)
|
||||
|
||||
// conversationGeneration remains exactly representable by JavaScript while
|
||||
// retaining microsecond separation between plugin installations.
|
||||
func conversationGeneration(installedAt time.Time) int64 {
|
||||
return installedAt.UnixMicro()
|
||||
}
|
||||
|
||||
type conversationTokenClaims struct {
|
||||
Version int `json:"v"`
|
||||
Kind string `json:"kind"`
|
||||
PluginID string `json:"plugin_id"`
|
||||
UserID string `json:"user_id"`
|
||||
Generation int64 `json:"generation"`
|
||||
ExpiresAt int64 `json:"expires_at"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
TaskID *string `json:"task_id,omitempty"`
|
||||
Sort string `json:"sort,omitempty"`
|
||||
Authors []string `json:"authors,omitempty"`
|
||||
LastID string `json:"last_id,omitempty"`
|
||||
Cutoff int64 `json:"cutoff,omitempty"`
|
||||
Fingerprint string `json:"fingerprint,omitempty"`
|
||||
ConsumerID string `json:"consumer_id,omitempty"`
|
||||
WireID string `json:"wire_id,omitempty"`
|
||||
Sequence uint64 `json:"sequence,omitempty"`
|
||||
Nonce string `json:"nonce"`
|
||||
}
|
||||
|
||||
type conversationTokenManager struct {
|
||||
key []byte
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func newConversationTokenManager() *conversationTokenManager {
|
||||
key := make([]byte, 32)
|
||||
if _, err := rand.Read(key); err != nil {
|
||||
panic("plugins: cannot initialize conversation token signer: " + err.Error())
|
||||
}
|
||||
return &conversationTokenManager{key: key, now: time.Now}
|
||||
}
|
||||
|
||||
func loadOrCreateConversationTokenManager(path string) (*conversationTokenManager, error) {
|
||||
key, err := os.ReadFile(path)
|
||||
if err == nil {
|
||||
return conversationTokenManagerFromKey(key)
|
||||
}
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, fmt.Errorf("read conversation token signing key: %w", err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
return nil, fmt.Errorf("create conversation token key directory: %w", err)
|
||||
}
|
||||
key = make([]byte, 32)
|
||||
if _, err := rand.Read(key); err != nil {
|
||||
return nil, fmt.Errorf("generate conversation token signing key: %w", err)
|
||||
}
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
key, err = os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read concurrently created conversation token signing key: %w", err)
|
||||
}
|
||||
return conversationTokenManagerFromKey(key)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create conversation token signing key: %w", err)
|
||||
}
|
||||
if _, err := file.Write(key); err != nil {
|
||||
_ = file.Close()
|
||||
return nil, fmt.Errorf("write conversation token signing key: %w", err)
|
||||
}
|
||||
if err := file.Sync(); err != nil {
|
||||
_ = file.Close()
|
||||
return nil, fmt.Errorf("sync conversation token signing key: %w", err)
|
||||
}
|
||||
if err := file.Close(); err != nil {
|
||||
return nil, fmt.Errorf("close conversation token signing key: %w", err)
|
||||
}
|
||||
return conversationTokenManagerFromKey(key)
|
||||
}
|
||||
|
||||
func conversationTokenManagerFromKey(key []byte) (*conversationTokenManager, error) {
|
||||
if len(key) != 32 {
|
||||
return nil, fmt.Errorf("conversation token signing key must be 32 bytes, got %d", len(key))
|
||||
}
|
||||
return &conversationTokenManager{key: append([]byte(nil), key...), now: time.Now}, nil
|
||||
}
|
||||
|
||||
func (m *conversationTokenManager) mintBinding(
|
||||
pluginID string,
|
||||
userID string,
|
||||
generation int64,
|
||||
) (string, time.Time, error) {
|
||||
expiresAt := m.now().UTC().Add(conversationTokenTTL)
|
||||
claims := conversationTokenClaims{
|
||||
Version: 1, Kind: tokenKindBinding, PluginID: pluginID, UserID: userID,
|
||||
Generation: generation, ExpiresAt: expiresAt.Unix(),
|
||||
}
|
||||
token, err := m.seal(claims)
|
||||
return token, expiresAt, err
|
||||
}
|
||||
|
||||
func (m *conversationTokenManager) mintCursor(
|
||||
pluginID string,
|
||||
userID string,
|
||||
generation int64,
|
||||
sessionID string,
|
||||
taskID *string,
|
||||
sortOrder string,
|
||||
authors []string,
|
||||
lastID string,
|
||||
cutoff int64,
|
||||
fingerprint string,
|
||||
) (string, error) {
|
||||
return m.seal(conversationTokenClaims{
|
||||
Version: 1, Kind: tokenKindCursor, PluginID: pluginID, UserID: userID,
|
||||
Generation: generation, ExpiresAt: m.now().UTC().Add(conversationTokenTTL).Unix(),
|
||||
SessionID: sessionID, TaskID: taskID, Sort: sortOrder, Authors: authors, LastID: lastID,
|
||||
Cutoff: cutoff, Fingerprint: fingerprint,
|
||||
})
|
||||
}
|
||||
|
||||
func (m *conversationTokenManager) mintSnapshot(
|
||||
pluginID string,
|
||||
userID string,
|
||||
generation int64,
|
||||
sessionID string,
|
||||
taskID *string,
|
||||
sortOrder string,
|
||||
authors []string,
|
||||
cutoff int64,
|
||||
fingerprint string,
|
||||
) (string, error) {
|
||||
return m.seal(conversationTokenClaims{
|
||||
Version: 1, Kind: tokenKindSnapshot, PluginID: pluginID, UserID: userID,
|
||||
Generation: generation, ExpiresAt: m.now().UTC().Add(conversationTokenTTL).Unix(),
|
||||
SessionID: sessionID, TaskID: taskID, Sort: sortOrder, Authors: authors,
|
||||
Cutoff: cutoff, Fingerprint: fingerprint,
|
||||
})
|
||||
}
|
||||
|
||||
func (m *conversationTokenManager) renew(claims conversationTokenClaims) (string, time.Time, error) {
|
||||
expiresAt := m.now().UTC().Add(conversationTokenTTL)
|
||||
claims.ExpiresAt = expiresAt.Unix()
|
||||
claims.Nonce = ""
|
||||
token, err := m.seal(claims)
|
||||
return token, expiresAt, err
|
||||
}
|
||||
|
||||
func (m *conversationTokenManager) mintResume(
|
||||
pluginID string,
|
||||
userID string,
|
||||
generation int64,
|
||||
sessionID string,
|
||||
consumerID string,
|
||||
wireID string,
|
||||
sequence uint64,
|
||||
) (string, error) {
|
||||
return m.seal(conversationTokenClaims{
|
||||
Version: 1, Kind: tokenKindResume, PluginID: pluginID, UserID: userID,
|
||||
Generation: generation, ExpiresAt: m.now().UTC().Add(conversationTokenTTL).Unix(),
|
||||
SessionID: sessionID, ConsumerID: consumerID, WireID: wireID, Sequence: sequence,
|
||||
})
|
||||
}
|
||||
|
||||
func (m *conversationTokenManager) seal(claims conversationTokenClaims) (string, error) {
|
||||
if claims.Nonce == "" {
|
||||
nonce := make([]byte, 16)
|
||||
if _, err := rand.Read(nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
claims.Nonce = base64.RawURLEncoding.EncodeToString(nonce)
|
||||
}
|
||||
payload, err := json.Marshal(claims)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
encoded := base64.RawURLEncoding.EncodeToString(payload)
|
||||
mac := hmac.New(sha256.New, m.key)
|
||||
_, _ = mac.Write([]byte(encoded))
|
||||
signature := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
||||
return encoded + "." + signature, nil
|
||||
}
|
||||
|
||||
func (m *conversationTokenManager) parse(token string) (conversationTokenClaims, error) {
|
||||
var claims conversationTokenClaims
|
||||
payloadPart, signaturePart, ok := strings.Cut(token, ".")
|
||||
if !ok || payloadPart == "" || signaturePart == "" {
|
||||
return claims, errors.New("invalid token")
|
||||
}
|
||||
signature, err := base64.RawURLEncoding.DecodeString(signaturePart)
|
||||
if err != nil {
|
||||
return claims, errors.New("invalid token")
|
||||
}
|
||||
mac := hmac.New(sha256.New, m.key)
|
||||
_, _ = mac.Write([]byte(payloadPart))
|
||||
if !hmac.Equal(signature, mac.Sum(nil)) {
|
||||
return claims, errors.New("invalid token")
|
||||
}
|
||||
payload, err := base64.RawURLEncoding.DecodeString(payloadPart)
|
||||
if err != nil || json.Unmarshal(payload, &claims) != nil || claims.Version != 1 {
|
||||
return conversationTokenClaims{}, errors.New("invalid token")
|
||||
}
|
||||
if !m.now().UTC().Before(time.Unix(claims.ExpiresAt, 0)) {
|
||||
return conversationTokenClaims{}, errors.New("expired token")
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
@@ -62,18 +62,37 @@ type webhookInvoker interface {
|
||||
// static-file serving (from the extracted package on disk), and the
|
||||
// external webhook relay (HTTP -> Host RPC over the live subprocess).
|
||||
type Controller struct {
|
||||
svc *Service
|
||||
log *logger.Logger
|
||||
actionInvoker actionInvoker
|
||||
webhookInvoker webhookInvoker
|
||||
svc *Service
|
||||
log *logger.Logger
|
||||
actionInvoker actionInvoker
|
||||
webhookInvoker webhookInvoker
|
||||
conversationTokens *conversationTokenManager
|
||||
conversationReader ConversationReader
|
||||
}
|
||||
|
||||
// RegisterRoutes wires the plugin HTTP surface. deliverer is accepted for
|
||||
// parity with the backendapp wiring (svc.SetDeliverer(deliverer) happens
|
||||
// alongside this call) — no handler in this file calls it directly, since
|
||||
// Service already notifies it on every install/status change.
|
||||
func RegisterRoutes(router *gin.Engine, svc *Service, _ Deliverer, log *logger.Logger) {
|
||||
ctrl := &Controller{svc: svc, log: log, actionInvoker: svc, webhookInvoker: svc}
|
||||
func RegisterRoutes(
|
||||
router *gin.Engine,
|
||||
svc *Service,
|
||||
_ Deliverer,
|
||||
log *logger.Logger,
|
||||
conversationReaders ...ConversationReader,
|
||||
) {
|
||||
var conversationReader ConversationReader
|
||||
if len(conversationReaders) > 0 {
|
||||
conversationReader = conversationReaders[0]
|
||||
}
|
||||
ctrl := &Controller{
|
||||
svc: svc,
|
||||
log: log,
|
||||
actionInvoker: svc,
|
||||
webhookInvoker: svc,
|
||||
conversationTokens: svc.conversationTokens,
|
||||
conversationReader: conversationReader,
|
||||
}
|
||||
|
||||
api := router.Group("/api/plugins")
|
||||
// Instance admin, not an org scope: plugins load into the shared host
|
||||
@@ -106,6 +125,7 @@ func RegisterRoutes(router *gin.Engine, svc *Service, _ Deliverer, log *logger.L
|
||||
// /settings is registered before /:id above: some gin/httprouter tree
|
||||
// versions reject a static-ish sibling added after an existing wildcard.
|
||||
registerUserStateRoutes(api, ctrl)
|
||||
registerConversationRoutes(api, ctrl)
|
||||
api.POST("/:id/webhooks/:key", ctrl.webhook)
|
||||
api.GET("/:id/webhooks/:key", ctrl.webhook)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,12 @@ import (
|
||||
"github.com/kandev/kandev/internal/plugins/store"
|
||||
)
|
||||
|
||||
func registerPluginRoutesWithIdentity(t *testing.T, svc *Service, identity authn.Identity) *gin.Engine {
|
||||
func registerPluginRoutesWithIdentity(
|
||||
t *testing.T,
|
||||
svc *Service,
|
||||
identity authn.Identity,
|
||||
conversationReaders ...ConversationReader,
|
||||
) *gin.Engine {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
@@ -20,7 +25,7 @@ func registerPluginRoutesWithIdentity(t *testing.T, svc *Service, identity authn
|
||||
authn.SetOnGin(ctx, identity)
|
||||
ctx.Next()
|
||||
})
|
||||
RegisterRoutes(router, svc, nil, testLogger(t))
|
||||
RegisterRoutes(router, svc, nil, testLogger(t), conversationReaders...)
|
||||
return router
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
package manifest
|
||||
|
||||
// Clone returns an independent copy of the manifest and all nested mutable
|
||||
// values. Registry callers can mutate the result without changing stored state.
|
||||
func (m Manifest) Clone() Manifest {
|
||||
clone := m
|
||||
clone.Categories = cloneStrings(m.Categories)
|
||||
clone.Webhooks = cloneWebhooks(m.Webhooks)
|
||||
clone.Actions = cloneActions(m.Actions)
|
||||
clone.RepositoryProviders = cloneStrings(m.RepositoryProviders)
|
||||
clone.ReferenceSources = cloneReferenceSources(m.ReferenceSources)
|
||||
clone.AuthProviders = cloneAuthProviders(m.AuthProviders)
|
||||
clone.ConfigSchema = cloneMap(m.ConfigSchema)
|
||||
clone.AgentTools = cloneAgentTools(m.AgentTools)
|
||||
clone.Capabilities.Events = cloneStrings(m.Capabilities.Events)
|
||||
clone.Capabilities.APIRead = cloneStrings(m.Capabilities.APIRead)
|
||||
clone.Capabilities.APIWrite = cloneStrings(m.Capabilities.APIWrite)
|
||||
clone.UI.Pages = append([]UIPage(nil), m.UI.Pages...)
|
||||
clone.UI.Styles = cloneStrings(m.UI.Styles)
|
||||
clone.UI.Keybindings = append([]UIKeybinding(nil), m.UI.Keybindings...)
|
||||
clone.UI.WebApps = cloneWebApps(m.UI.WebApps)
|
||||
clone.Runtime.Executables = cloneStringMap(m.Runtime.Executables)
|
||||
return clone
|
||||
}
|
||||
|
||||
func cloneStrings(values []string) []string {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
clone := make([]string, len(values))
|
||||
copy(clone, values)
|
||||
return clone
|
||||
}
|
||||
|
||||
func cloneStringMap(values map[string]string) map[string]string {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
clone := make(map[string]string, len(values))
|
||||
for key, value := range values {
|
||||
clone[key] = value
|
||||
}
|
||||
return clone
|
||||
}
|
||||
|
||||
func cloneWebhooks(values []Webhook) []Webhook {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
return append([]Webhook(nil), values...)
|
||||
}
|
||||
|
||||
func cloneActions(values []Action) []Action {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
return append([]Action(nil), values...)
|
||||
}
|
||||
|
||||
func cloneReferenceSources(values []ReferenceSource) []ReferenceSource {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
return append([]ReferenceSource(nil), values...)
|
||||
}
|
||||
|
||||
func cloneAuthProviders(values []AuthProvider) []AuthProvider {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
return append([]AuthProvider(nil), values...)
|
||||
}
|
||||
|
||||
func cloneWebApps(values []WebApp) []WebApp {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
clone := make([]WebApp, len(values))
|
||||
for index, value := range values {
|
||||
clone[index] = value
|
||||
clone[index].Placements = cloneStrings(value.Placements)
|
||||
clone[index].NetworkOrigins = cloneStrings(value.NetworkOrigins)
|
||||
}
|
||||
return clone
|
||||
}
|
||||
|
||||
func cloneAgentTools(values []AgentTool) []AgentTool {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
clone := make([]AgentTool, len(values))
|
||||
for index, value := range values {
|
||||
clone[index] = value
|
||||
clone[index].Surfaces = cloneStrings(value.Surfaces)
|
||||
clone[index].InputSchema = cloneMap(value.InputSchema)
|
||||
clone[index].OutputSchema = cloneMap(value.OutputSchema)
|
||||
clone[index].Annotations.ReadOnlyHint = cloneBool(value.Annotations.ReadOnlyHint)
|
||||
clone[index].Annotations.DestructiveHint = cloneBool(value.Annotations.DestructiveHint)
|
||||
clone[index].Annotations.IdempotentHint = cloneBool(value.Annotations.IdempotentHint)
|
||||
clone[index].Annotations.OpenWorldHint = cloneBool(value.Annotations.OpenWorldHint)
|
||||
}
|
||||
return clone
|
||||
}
|
||||
|
||||
func cloneBool(value *bool) *bool {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
clone := *value
|
||||
return &clone
|
||||
}
|
||||
|
||||
func cloneMap(values map[string]any) map[string]any {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
clone := make(map[string]any, len(values))
|
||||
for key, value := range values {
|
||||
clone[key] = cloneValue(value)
|
||||
}
|
||||
return clone
|
||||
}
|
||||
|
||||
func cloneValue(value any) any {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
return cloneMap(typed)
|
||||
case map[any]any:
|
||||
clone := make(map[any]any, len(typed))
|
||||
for key, nested := range typed {
|
||||
clone[key] = cloneValue(nested)
|
||||
}
|
||||
return clone
|
||||
case []any:
|
||||
clone := make([]any, len(typed))
|
||||
for index, nested := range typed {
|
||||
clone[index] = cloneValue(nested)
|
||||
}
|
||||
return clone
|
||||
case []string:
|
||||
return cloneStrings(typed)
|
||||
default:
|
||||
return value
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package manifest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestManifestCloneDoesNotAliasNestedValues(t *testing.T) {
|
||||
readOnly := true
|
||||
manifest := Manifest{
|
||||
Categories: []string{"tools"},
|
||||
RepositoryProviders: []string{"github"},
|
||||
ConfigSchema: map[string]any{
|
||||
"nested": map[string]any{"values": []any{"one", map[string]any{"enabled": true}}},
|
||||
},
|
||||
AgentTools: []AgentTool{{
|
||||
Surfaces: []string{"kanban-task"},
|
||||
InputSchema: map[string]any{"type": "object"},
|
||||
Annotations: AgentToolAnnotations{ReadOnlyHint: &readOnly},
|
||||
}},
|
||||
UI: UISection{WebApps: []WebApp{{Placements: []string{"task-canvas"}}}},
|
||||
Runtime: Runtime{Executables: map[string]string{"linux-amd64": "server/plugin"}},
|
||||
}
|
||||
|
||||
clone := manifest.Clone()
|
||||
clone.Categories[0] = "changed"
|
||||
clone.ConfigSchema["nested"].(map[string]any)["values"].([]any)[1].(map[string]any)["enabled"] = false
|
||||
clone.AgentTools[0].Surfaces[0] = "office-task"
|
||||
*clone.AgentTools[0].Annotations.ReadOnlyHint = false
|
||||
clone.UI.WebApps[0].Placements[0] = "workspace-canvas"
|
||||
clone.Runtime.Executables["linux-amd64"] = "changed"
|
||||
|
||||
require.Equal(t, "tools", manifest.Categories[0])
|
||||
require.Equal(t, true, manifest.ConfigSchema["nested"].(map[string]any)["values"].([]any)[1].(map[string]any)["enabled"])
|
||||
require.Equal(t, "kanban-task", manifest.AgentTools[0].Surfaces[0])
|
||||
require.True(t, *manifest.AgentTools[0].Annotations.ReadOnlyHint)
|
||||
require.Equal(t, "task-canvas", manifest.UI.WebApps[0].Placements[0])
|
||||
require.Equal(t, "server/plugin", manifest.Runtime.Executables["linux-amd64"])
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package manifest
|
||||
|
||||
import "fmt"
|
||||
|
||||
// MinimumMessagesCapabilityVersion is the first Host release that supports
|
||||
// both Go Messages().List and the browser conversation facade safely.
|
||||
const MinimumMessagesCapabilityVersion = "0.91.1"
|
||||
|
||||
// RequiresMessagesCapabilityMinimum reports whether a manifest opts into the
|
||||
// messages read resource whose Host contract requires a minimum release.
|
||||
func RequiresMessagesCapabilityMinimum(manifest *Manifest) bool {
|
||||
return manifest != nil && manifest.Capabilities.CanRead("messages")
|
||||
}
|
||||
|
||||
func (m *Manifest) validateCapabilityMinimumVersions() []error {
|
||||
if !RequiresMessagesCapabilityMinimum(m) {
|
||||
return nil
|
||||
}
|
||||
minimum, valid := NormalizeReleaseVersion(m.MinKandevVersion)
|
||||
if !valid || CompareVersions(minimum, MinimumMessagesCapabilityVersion) < 0 {
|
||||
return []error{fmt.Errorf(
|
||||
"api_read resource %q requires min_kandev_version >= %s",
|
||||
"messages",
|
||||
MinimumMessagesCapabilityVersion,
|
||||
)}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package manifest
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMessagesReadCapabilityRequiresSupportedMinimumVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
minimum string
|
||||
wantReject bool
|
||||
}{
|
||||
{name: "missing", wantReject: true},
|
||||
{name: "malformed", minimum: "next", wantReject: true},
|
||||
{name: "lower", minimum: "0.91.0", wantReject: true},
|
||||
{name: "equal", minimum: MinimumMessagesCapabilityVersion},
|
||||
{name: "higher", minimum: "0.92.0"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
manifest := validManifest(t)
|
||||
manifest.Capabilities.APIRead = append(manifest.Capabilities.APIRead, "messages")
|
||||
manifest.MinKandevVersion = test.minimum
|
||||
|
||||
err := manifest.Validate()
|
||||
if test.wantReject {
|
||||
if err == nil || !strings.Contains(err.Error(), "api_read resource \"messages\" requires min_kandev_version >= 0.91.1") {
|
||||
t.Fatalf("Validate() error = %v, want messages capability minimum-version rejection", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Validate() unexpected error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessagesMinimumVersionPolicyDoesNotAffectOtherResources(t *testing.T) {
|
||||
manifest := validManifest(t)
|
||||
manifest.Capabilities.APIRead = []string{"tasks"}
|
||||
manifest.MinKandevVersion = ""
|
||||
|
||||
if err := manifest.Validate(); err != nil {
|
||||
t.Fatalf("Validate() unexpected error: %v", err)
|
||||
}
|
||||
if RequiresMessagesCapabilityMinimum(manifest) {
|
||||
t.Fatal("tasks-only manifest unexpectedly requires the messages capability minimum")
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,7 @@ func (m *Manifest) Validate() error {
|
||||
errs = append(errs, m.validateUIKeybindings()...)
|
||||
errs = append(errs, m.validateWebhooks()...)
|
||||
errs = append(errs, m.validateActions()...)
|
||||
errs = append(errs, m.validateCapabilityMinimumVersions()...)
|
||||
errs = append(errs, m.validateRepositoryProviders()...)
|
||||
errs = append(errs, m.validateReferenceSources()...)
|
||||
errs = append(errs, m.validateAgentTools()...)
|
||||
|
||||
@@ -118,7 +118,14 @@ func ProvideWithStoreErrors(cfg *config.Config, dbPool *db.Pool, secrets SecretV
|
||||
|
||||
configureWebAppStorage(cfg, svc, instanceStore, instanceState, log)
|
||||
svc.SetSecrets(secrets)
|
||||
svc.SetPluginsDir(dir)
|
||||
if dbPool != nil && dbPool.Writer() != nil {
|
||||
svc.SetConversationJournalDB(dbPool.Writer())
|
||||
}
|
||||
if err := svc.SetPluginsDir(dir); err != nil {
|
||||
warnProvider(log, "Plugins durable conversation state initialization failed; continuing with degraded conversation capabilities", err)
|
||||
} else if _, err := svc.syncAllCommittedSessionEvents(context.Background()); err != nil {
|
||||
warnProvider(log, "Plugins committed conversation journal synchronization failed; continuing with degraded conversation capabilities", err)
|
||||
}
|
||||
|
||||
seedMarketplace(svc, sourceStore, storeErrors, log)
|
||||
if settingsStore != nil {
|
||||
@@ -131,12 +138,13 @@ func ProvideWithStoreErrors(cfg *config.Config, dbPool *db.Pool, secrets SecretV
|
||||
if cfg.Features.Canvases {
|
||||
stopArtifactCleanup = svc.StartWebAppArtifactCleanupWorker(context.Background())
|
||||
}
|
||||
stopSessionEventMaintenance := svc.StartSessionEventMaintenanceWorker(context.Background())
|
||||
|
||||
cleanup := func() error {
|
||||
stopArtifactCleanup()
|
||||
stopSessionEventMaintenance()
|
||||
svc.closeWebAppEvents()
|
||||
rt.StopAll()
|
||||
return nil
|
||||
return svc.Close()
|
||||
}
|
||||
return svc, cleanup, storeErrors
|
||||
}
|
||||
|
||||
@@ -21,6 +21,13 @@ func newTestPool(t *testing.T) *db.Pool {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
conn.SetMaxOpenConns(1)
|
||||
if _, err := conn.Exec(`
|
||||
CREATE TABLE conversation_session_streams (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
watermark INTEGER NOT NULL
|
||||
)`); err != nil {
|
||||
t.Fatalf("create conversation journal schema: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = conn.Close() })
|
||||
return db.NewPool(conn, conn)
|
||||
}
|
||||
@@ -54,6 +61,27 @@ func TestProvideConstructsServiceUsingHomeDirPluginsSubdir(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetPluginsDirKeepsInstallRootWhenConversationStateFails(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
hostDir := filepath.Join(dir, ".host")
|
||||
if err := os.MkdirAll(hostDir, 0o700); err != nil {
|
||||
t.Fatalf("create host directory: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(hostDir, "conversation-token.key"), []byte("corrupt"), 0o600); err != nil {
|
||||
t.Fatalf("write corrupt signing key: %v", err)
|
||||
}
|
||||
|
||||
svc := NewService(store.NewFSStore(filepath.Join(dir, "store")), NewRegistry(), nil, testLogger(t))
|
||||
t.Cleanup(func() { _ = svc.Close() })
|
||||
|
||||
if err := svc.SetPluginsDir(dir); err == nil {
|
||||
t.Fatal("SetPluginsDir() expected corrupt signing key error")
|
||||
}
|
||||
if svc.pluginsDir != dir {
|
||||
t.Fatalf("pluginsDir = %q, want %q after initialization failure", svc.pluginsDir, dir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvideLoadsExistingInstallationsFromDisk(t *testing.T) {
|
||||
homeDir := t.TempDir()
|
||||
cfg := &config.Config{HomeDir: homeDir}
|
||||
|
||||
@@ -10,12 +10,6 @@ import (
|
||||
"github.com/kandev/kandev/internal/plugins/store"
|
||||
)
|
||||
|
||||
// Registry is an in-memory, mutex-guarded index of installed plugins, keyed
|
||||
// by plugin id. It is loaded from the filesystem store at startup (Load)
|
||||
// and kept in sync as Service mutates installations, so read paths (List,
|
||||
// Get) never hit disk.
|
||||
//
|
||||
// Get and List return copies of the stored *store.Record so callers cannot
|
||||
// mutate registry state by holding onto a returned pointer; all writes go
|
||||
// through Add / Remove / SetStatus / SetRuntimeState / SetAutoUpdate /
|
||||
// SetRestartCount.
|
||||
@@ -156,6 +150,7 @@ func (r *Registry) SetRuntimeState(id string, status Status, lastError string, l
|
||||
|
||||
func cloneRecord(rec *store.Record) *store.Record {
|
||||
clone := *rec
|
||||
clone.Manifest = rec.Clone()
|
||||
if rec.AutoUpdate != nil {
|
||||
autoUpdate := *rec.AutoUpdate
|
||||
clone.AutoUpdate = &autoUpdate
|
||||
|
||||
@@ -106,8 +106,13 @@ func TestRegistrySetStatusMissingReturnsNotOK(t *testing.T) {
|
||||
func TestRegistryGetReturnsIndependentCopy(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
autoUpdate := true
|
||||
mf := testManifest("kandev-plugin-slack")
|
||||
mf.Categories = []string{"chat"}
|
||||
mf.RepositoryProviders = []string{"github"}
|
||||
mf.ConfigSchema = map[string]any{"nested": map[string]any{"deep": "value"}}
|
||||
mf.AgentTools = []manifest.AgentTool{{Name: "tool", Surfaces: []string{"kanban-task"}}}
|
||||
reg.Add(&store.Record{
|
||||
Manifest: *testManifest("kandev-plugin-slack"),
|
||||
Manifest: *mf,
|
||||
Status: store.StatusRegistered,
|
||||
AutoUpdate: &autoUpdate,
|
||||
})
|
||||
@@ -118,6 +123,11 @@ func TestRegistryGetReturnsIndependentCopy(t *testing.T) {
|
||||
}
|
||||
rec.Status = store.StatusActive // mutate the returned copy
|
||||
*rec.AutoUpdate = false // mutate the returned pointer copy
|
||||
rec.Categories[0] = "mutated" // mutate a returned nested slice element
|
||||
rec.RepositoryProviders = append(rec.RepositoryProviders, "gitlab")
|
||||
rec.ConfigSchema["nested"].(map[string]any)["deep"] = "mutated"
|
||||
rec.AgentTools[0].Surfaces[0] = "office-task"
|
||||
rec.Runtime.Executables["linux-amd64"] = "server/mutated"
|
||||
|
||||
fresh, ok := reg.Get("kandev-plugin-slack")
|
||||
if !ok {
|
||||
@@ -129,6 +139,18 @@ func TestRegistryGetReturnsIndependentCopy(t *testing.T) {
|
||||
if fresh.AutoUpdate == nil || !*fresh.AutoUpdate {
|
||||
t.Fatalf("mutating a Get() result leaked into the registry: AutoUpdate = %v, want true", fresh.AutoUpdate)
|
||||
}
|
||||
if fresh.Categories[0] != "chat" || len(fresh.RepositoryProviders) != 1 || fresh.RepositoryProviders[0] != "github" {
|
||||
t.Fatalf("mutating a Get() result leaked into the registry: Categories=%v RepositoryProviders=%v", fresh.Categories, fresh.RepositoryProviders)
|
||||
}
|
||||
if fresh.ConfigSchema["nested"].(map[string]any)["deep"] != "value" {
|
||||
t.Fatalf("mutating a Get() result leaked into the registry: ConfigSchema=%v", fresh.ConfigSchema)
|
||||
}
|
||||
if fresh.AgentTools[0].Surfaces[0] != "kanban-task" {
|
||||
t.Fatalf("mutating a Get() result leaked into the registry: AgentTools=%v", fresh.AgentTools)
|
||||
}
|
||||
if fresh.Runtime.Executables["linux-amd64"] != "server/plugin-linux-amd64" {
|
||||
t.Fatalf("mutating a Get() result leaked into the registry: Executables=%v", fresh.Runtime.Executables)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistrySetRestartCountUpdatesRecord(t *testing.T) {
|
||||
|
||||
@@ -3,10 +3,14 @@ package plugins
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -89,20 +93,25 @@ type Service struct {
|
||||
// directory belongs to an install still waiting for it.
|
||||
extractingPaths map[string]int
|
||||
|
||||
pluginsDir string
|
||||
store store.Store
|
||||
registry *Registry
|
||||
state *state.Store
|
||||
userState *state.UserStore
|
||||
instances *instances.Store
|
||||
instanceState *state.InstanceStore
|
||||
webArtifacts *webapp.ArtifactStore
|
||||
webRuntime *webapp.Runtime
|
||||
eventHub *webapp.EventHub
|
||||
eventSubscription bus.Subscription
|
||||
userStateCleanup userStateCleanupStore
|
||||
eventBus bus.EventBus
|
||||
log *logger.Logger
|
||||
pluginsDir string
|
||||
store store.Store
|
||||
registry *Registry
|
||||
state *state.Store
|
||||
userState *state.UserStore
|
||||
instances *instances.Store
|
||||
instanceState *state.InstanceStore
|
||||
webArtifacts *webapp.ArtifactStore
|
||||
webRuntime *webapp.Runtime
|
||||
eventHub *webapp.EventHub
|
||||
eventSubscription bus.Subscription
|
||||
userStateCleanup userStateCleanupStore
|
||||
eventBus bus.EventBus
|
||||
conversationTokens *conversationTokenManager
|
||||
sessionEvents *SessionEventLog
|
||||
sessionDelivery *SessionDeliveryDispatcher
|
||||
conversationJournal *sqlx.DB
|
||||
sessionEventSink func(SessionEvent)
|
||||
log *logger.Logger
|
||||
|
||||
deliverer Deliverer
|
||||
agentToolCatalogListener AgentToolCatalogListener
|
||||
@@ -183,6 +192,8 @@ type Service struct {
|
||||
reservedReferenceProviderKinds map[string]struct{}
|
||||
}
|
||||
|
||||
const sessionEventMaintenanceInterval = time.Minute
|
||||
|
||||
// ReferenceIdentity reserves a host-owned composer source and its canonical
|
||||
// provider/kind pair so a plugin cannot shadow a built-in integration.
|
||||
type ReferenceIdentity struct {
|
||||
@@ -195,7 +206,8 @@ type ReferenceIdentity struct {
|
||||
// Provide is the usual entry point in production; NewService is exposed
|
||||
// directly for tests that want a fake store.Store/PluginRuntime.
|
||||
func NewService(pluginStore store.Store, registry *Registry, eventBus bus.EventBus, log *logger.Logger) *Service {
|
||||
return &Service{
|
||||
sessionEvents := mustSessionEventLog()
|
||||
service := &Service{
|
||||
store: pluginStore,
|
||||
registry: registry,
|
||||
eventBus: eventBus,
|
||||
@@ -205,7 +217,11 @@ func NewService(pluginStore store.Store, registry *Registry, eventBus bus.EventB
|
||||
dispatchLocks: newKeyedRWMutex(),
|
||||
agentToolGeneration: uuid.NewString(),
|
||||
eventHub: webapp.NewEventHub(),
|
||||
conversationTokens: newConversationTokenManager(),
|
||||
sessionEvents: sessionEvents,
|
||||
}
|
||||
service.sessionDelivery = NewSessionDeliveryDispatcher(sessionEvents)
|
||||
return service
|
||||
}
|
||||
|
||||
// SetGitCredentialLeaseRevoker wires immediate provider-lease revocation for
|
||||
@@ -667,10 +683,113 @@ func (s *Service) Shutdown() {
|
||||
}
|
||||
}
|
||||
|
||||
// Close releases durable plugin conversation state after workers stop.
|
||||
func (s *Service) Close() error {
|
||||
s.Shutdown()
|
||||
if s.sessionEvents == nil {
|
||||
return nil
|
||||
}
|
||||
return s.sessionEvents.Close()
|
||||
}
|
||||
|
||||
// SetPluginsDir wires the root directory pkgtar.Install/pkgtar.Remove
|
||||
// operate under (the same directory store.FSStore persists records in).
|
||||
func (s *Service) SetPluginsDir(dir string) {
|
||||
// operate under and initializes mandatory durable conversation state.
|
||||
func (s *Service) SetPluginsDir(dir string) error {
|
||||
// Keep package installation rooted correctly even when durable conversation
|
||||
// state initialization fails and the caller continues in degraded mode.
|
||||
s.pluginsDir = dir
|
||||
hostDir := filepath.Join(dir, ".host")
|
||||
conversationTokens, err := loadOrCreateConversationTokenManager(
|
||||
filepath.Join(hostDir, "conversation-token.key"),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sessionEvents, err := NewSessionEventLog(filepath.Join(hostDir, "session-events.sqlite"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sessionDelivery := NewSessionDeliveryDispatcher(sessionEvents)
|
||||
now := time.Now().UTC()
|
||||
if err := sessionDelivery.ReclaimExpiredLeases(now); err != nil {
|
||||
_ = sessionEvents.Close()
|
||||
return fmt.Errorf("reclaim plugin session event leases: %w", err)
|
||||
}
|
||||
if err := sessionEvents.CollectExpired(now); err != nil {
|
||||
_ = sessionEvents.Close()
|
||||
return fmt.Errorf("collect expired plugin session events: %w", err)
|
||||
}
|
||||
previousSessionEvents := s.sessionEvents
|
||||
s.pluginsDir = dir
|
||||
s.conversationTokens = conversationTokens
|
||||
s.sessionEvents = sessionEvents
|
||||
s.sessionDelivery = sessionDelivery
|
||||
if previousSessionEvents != nil {
|
||||
_ = previousSessionEvents.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) maintainSessionEvents(ctx context.Context, now time.Time) error {
|
||||
// A failing partition must not freeze the rest of maintenance: mirror
|
||||
// errors are isolated per session (syncAll collects them), and the
|
||||
// remaining passes still run so healthy sessions are collected and the
|
||||
// primary journal is pruned every tick.
|
||||
events, syncErr := s.syncAllCommittedSessionEvents(ctx)
|
||||
s.mu.Lock()
|
||||
sink := s.sessionEventSink
|
||||
s.mu.Unlock()
|
||||
if sink != nil {
|
||||
for _, event := range events {
|
||||
sink(event)
|
||||
}
|
||||
}
|
||||
if err := s.sessionDelivery.ReclaimExpiredLeases(now); err != nil {
|
||||
return fmt.Errorf("reclaim plugin session event leases: %w", err)
|
||||
}
|
||||
// Poison attempts are counted only when delivery is actually observed
|
||||
// (a hub fanout or replay attempt), never by the maintenance ticker.
|
||||
// A poison no subscriber ever attempted to consume stays pending until
|
||||
// retention reaps it instead of being exhaustively cycled blind.
|
||||
if err := s.sessionEvents.CollectExpired(now); err != nil {
|
||||
return fmt.Errorf("collect expired plugin session events: %w", err)
|
||||
}
|
||||
retained := s.sessionEvents.RetainedSessionIDs(now)
|
||||
if err := s.pruneConversationJournal(
|
||||
ctx, now.UTC().Add(-SessionEventRetention), retained,
|
||||
); err != nil {
|
||||
return fmt.Errorf("prune primary conversation journal: %w", err)
|
||||
}
|
||||
if syncErr != nil {
|
||||
return fmt.Errorf("synchronize committed conversation journal: %w", syncErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartSessionEventMaintenanceWorker keeps cursor retention and expired poison
|
||||
// leases bounded for the lifetime of the backend, not only during startup.
|
||||
func (s *Service) StartSessionEventMaintenanceWorker(ctx context.Context) func() {
|
||||
workerContext, cancel := context.WithCancel(ctx)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
ticker := time.NewTicker(sessionEventMaintenanceInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-workerContext.Done():
|
||||
return
|
||||
case now := <-ticker.C:
|
||||
if err := s.maintainSessionEvents(workerContext, now.UTC()); err != nil && s.log != nil {
|
||||
s.log.Error("plugins: maintain session event stream", zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
return func() {
|
||||
cancel()
|
||||
<-done
|
||||
}
|
||||
}
|
||||
|
||||
// RevealSecret resolves the cleartext value of the secret reference ref via
|
||||
|
||||
@@ -206,7 +206,10 @@ func previousVersion(oldRec *store.Record, hadOldRec bool) string {
|
||||
// (cmd/kandev's `Version` default, mirrored by internal/system/updates'
|
||||
// devVersion). It sorts meaninglessly against real semver, and a developer
|
||||
// running from source must still be able to install a package that declares
|
||||
// a min_kandev_version, so it disables the check entirely.
|
||||
// a min_kandev_version, so it disables the release-boundary comparison only.
|
||||
// Manifest-level capability floors (e.g. api_read:messages >= 0.91.1) are
|
||||
// validated independently of the running version and remain enforced on dev
|
||||
// and unwired builds.
|
||||
const DevKandevVersion = "dev"
|
||||
|
||||
// checkMinKandevVersion rejects a package whose manifest declares a
|
||||
@@ -214,6 +217,8 @@ const DevKandevVersion = "dev"
|
||||
// tags may carry a leading `v`; development and git-describe build strings do
|
||||
// not provide a trustworthy release boundary, so they deliberately skip this
|
||||
// release-only compatibility gate. An invalid manifest minimum is rejected.
|
||||
// The capability floor above is enforced earlier, in Manifest validation,
|
||||
// regardless of what running version (if any) is wired here.
|
||||
func (s *Service) checkMinKandevVersion(minVersion string) error {
|
||||
return manifest.CheckMinimumKandevVersion(minVersion, s.kandevVersion)
|
||||
}
|
||||
|
||||
@@ -150,6 +150,7 @@ func TestServiceInstallUpgradeSaveFailurePreservesOldVersionAndData(t *testing.T
|
||||
svc.SetPluginsDir(dir)
|
||||
rt := newFakeRuntime()
|
||||
svc.SetRuntime(rt)
|
||||
t.Cleanup(func() { _ = svc.Close() })
|
||||
|
||||
rec1 := installTestPlugin(t, svc, "kandev-plugin-slack") // v1.0.0, active + running
|
||||
if !rt.Running("kandev-plugin-slack") {
|
||||
@@ -371,3 +372,76 @@ func TestServiceInstallFromURL_RejectsNonHTTPSchemeBeforeAnyRequest(t *testing.T
|
||||
t.Fatal("InstallFromURL() expected error for file:// scheme, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// messagesMinKandevVersionPackage is like minKandevVersionPackage but
|
||||
// declares capabilities.api_read: messages, which subjects the manifest to
|
||||
// the capability minimum floor (MinimumMessagesCapabilityVersion).
|
||||
func messagesMinKandevVersionPackage(t *testing.T, id, version, minKandevVersion string) *bytes.Buffer {
|
||||
t.Helper()
|
||||
platformKey := goruntime.GOOS + "-" + goruntime.GOARCH
|
||||
manifestYAML := fmt.Sprintf(`
|
||||
id: %s
|
||||
api_version: 1
|
||||
version: %s
|
||||
display_name: Test Plugin
|
||||
min_kandev_version: %q
|
||||
capabilities:
|
||||
api_read:
|
||||
- messages
|
||||
runtime:
|
||||
type: binary
|
||||
executables:
|
||||
%s: server/plugin
|
||||
`, id, version, minKandevVersion, platformKey)
|
||||
|
||||
var buf bytes.Buffer
|
||||
files := map[string][]byte{
|
||||
"manifest.yaml": []byte(manifestYAML),
|
||||
"server/plugin": []byte("#!/bin/sh\necho fake\n"),
|
||||
}
|
||||
if err := pkgtartest.WritePackage(&buf, files); err != nil {
|
||||
t.Fatalf("WritePackage: %v", err)
|
||||
}
|
||||
return &buf
|
||||
}
|
||||
|
||||
// TestServiceInstall_EnforcesMessagesCapabilityFloorOnDevAndUnwiredBuilds
|
||||
// pins the design contract that the api_read:messages minimum (0.91.1) is a
|
||||
// manifest-level floor enforced in every build - including unstamped dev
|
||||
// builds where the newer-than-running release gate is intentionally a no-op.
|
||||
// The dev escape hatch applies only to the release-boundary comparison, never
|
||||
// to the capability floor.
|
||||
func TestServiceInstall_EnforcesMessagesCapabilityFloorOnDevAndUnwiredBuilds(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
want string
|
||||
}{
|
||||
{name: "below floor is rejected", want: "0.9.0"},
|
||||
{name: "malformed minimum is rejected", want: "not-a-release"},
|
||||
} {
|
||||
t.Run("dev/"+tc.name, func(t *testing.T) {
|
||||
svc, _, _ := newTestService(t)
|
||||
svc.SetKandevVersion(DevKandevVersion)
|
||||
if _, err := svc.Install(context.Background(), messagesMinKandevVersionPackage(t, "kandev-plugin-messages", "1.0.0", tc.want)); err == nil {
|
||||
t.Fatalf("Install() on a dev build accepted a messages manifest with min %q", tc.want)
|
||||
}
|
||||
})
|
||||
t.Run("unwired/"+tc.name, func(t *testing.T) {
|
||||
svc, _, _ := newTestService(t)
|
||||
if _, err := svc.Install(context.Background(), messagesMinKandevVersionPackage(t, "kandev-plugin-messages", "1.0.0", tc.want)); err == nil {
|
||||
t.Fatalf("Install() with no running version accepted a messages manifest with min %q", tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run("dev/floor allowed", func(t *testing.T) {
|
||||
svc, _, _ := newTestService(t)
|
||||
svc.SetKandevVersion(DevKandevVersion)
|
||||
rec, err := svc.Install(context.Background(), messagesMinKandevVersionPackage(t, "kandev-plugin-messages", "1.0.0", "0.91.1"))
|
||||
if err != nil {
|
||||
t.Fatalf("Install() on a dev build rejected the floor minimum: %v", err)
|
||||
}
|
||||
if rec.Status != StatusActive {
|
||||
t.Fatalf("Install() Status = %q, want %q", rec.Status, StatusActive)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -248,6 +248,7 @@ func TestServiceInstallSaveFailurePrunesNothingAndRestartsPrevious(t *testing.T)
|
||||
svc.SetPluginsDir(dir)
|
||||
rt := newFakeRuntime()
|
||||
svc.SetRuntime(rt)
|
||||
t.Cleanup(func() { _ = svc.Close() })
|
||||
|
||||
installTestPlugin(t, svc, prunePluginID) // 1.0.0, active + running
|
||||
seedVersionDir(t, dir, prunePluginID, "0.9.0")
|
||||
@@ -320,6 +321,7 @@ func TestServiceInstallWithoutRuntimePrunesNothing(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
svc := NewService(store.NewFSStore(dir), NewRegistry(), nil, testLogger(t))
|
||||
svc.SetPluginsDir(dir)
|
||||
t.Cleanup(func() { _ = svc.Close() })
|
||||
|
||||
for _, v := range []string{"1.0.0", "1.1.0", "1.2.0"} {
|
||||
if _, err := svc.Install(context.Background(), testPackage(t, prunePluginID, v, false)); err != nil {
|
||||
@@ -367,6 +369,7 @@ func TestConcurrentInstallsKeepBothExtractions(t *testing.T) {
|
||||
svc := NewService(barrier, NewRegistry(), nil, testLogger(t))
|
||||
svc.SetPluginsDir(dir)
|
||||
svc.SetRuntime(newFakeRuntime())
|
||||
t.Cleanup(func() { _ = svc.Close() })
|
||||
|
||||
// 1.0.0 must be a real installed record, so the first racing install
|
||||
// treats it as its rollback target and 1.2.0 becomes a prune candidate.
|
||||
|
||||
@@ -360,6 +360,7 @@ func TestServiceInstallSerializesAgentToolCollisionValidation(t *testing.T) {
|
||||
svc := NewService(barrier, NewRegistry(), nil, testLogger(t))
|
||||
svc.SetPluginsDir(dir)
|
||||
svc.SetRuntime(newFakeRuntime())
|
||||
t.Cleanup(func() { _ = svc.Close() })
|
||||
firstPackage := testAgentToolPackage(t, "plugin-a", "echo")
|
||||
secondPackage := testAgentToolPackage(t, "plugin_a", "echo")
|
||||
|
||||
@@ -409,6 +410,7 @@ func newTestServiceWithDir(t *testing.T) (*Service, string, *store.FSStore, *fak
|
||||
svc.SetPluginsDir(dir)
|
||||
rt := newFakeRuntime()
|
||||
svc.SetRuntime(rt)
|
||||
t.Cleanup(func() { _ = svc.Close() })
|
||||
return svc, dir, fsStore, rt
|
||||
}
|
||||
|
||||
@@ -1338,6 +1340,7 @@ func TestServiceStartActivePluginsSpawnsOnlyActiveManagedNotAlreadyRunning(t *te
|
||||
svc2.SetPluginsDir(dir)
|
||||
rt2 := newFakeRuntime()
|
||||
svc2.SetRuntime(rt2)
|
||||
t.Cleanup(func() { _ = svc2.Close() })
|
||||
|
||||
svc2.StartActivePlugins(context.Background())
|
||||
|
||||
@@ -1359,6 +1362,7 @@ func TestServiceStartActivePluginsFailurePersistsDiagnosticAndRefreshesDeliverer
|
||||
rt2 := newFakeRuntime()
|
||||
rt2.setStartErr("kandev-plugin-slack", errors.New("boot handshake failed"))
|
||||
svc2.SetRuntime(rt2)
|
||||
t.Cleanup(func() { _ = svc2.Close() })
|
||||
deliverer := &fakeDeliverer{}
|
||||
svc2.SetDeliverer(deliverer)
|
||||
|
||||
|
||||
@@ -64,7 +64,12 @@ type ListMessagesOptions struct {
|
||||
After string
|
||||
Sort string
|
||||
AuthorType string
|
||||
Around string
|
||||
// AuthorTypes narrows by any of the listed authors (IN); when non-empty
|
||||
// it takes precedence over AuthorType. TaskID, when non-empty, narrows
|
||||
// to rows for that task.
|
||||
AuthorTypes []string
|
||||
TaskID string
|
||||
Around string
|
||||
}
|
||||
|
||||
// SearchMessagesOptions defines options for searching a session's messages.
|
||||
|
||||
@@ -60,6 +60,7 @@ func (r *Repository) initSchemaContext(ctx context.Context) error {
|
||||
r.ensureWorkspaceIndexes,
|
||||
r.ensureMessageMetadataIndexes,
|
||||
r.ensurePromptOrderIndex,
|
||||
r.initConversationJournalSchema,
|
||||
}
|
||||
// Every boundary is checked before and after its step. The task repository
|
||||
// passes the same context to startup SQL through migrationContext, so a
|
||||
|
||||
@@ -0,0 +1,945 @@
|
||||
//nolint:revive // This file keeps the dialect-specific journal trigger lifecycle atomic.
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/kandev/kandev/internal/db/dialect"
|
||||
"github.com/kandev/kandev/internal/sysprompt"
|
||||
)
|
||||
|
||||
const conversationJournalTables = `
|
||||
CREATE TABLE IF NOT EXISTS conversation_session_streams (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
watermark BIGINT NOT NULL DEFAULT 0,
|
||||
terminal BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
updated_at TIMESTAMP NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS conversation_session_events (
|
||||
session_id TEXT NOT NULL,
|
||||
sequence BIGINT NOT NULL,
|
||||
event_id TEXT NOT NULL UNIQUE,
|
||||
protocol_version INTEGER NOT NULL DEFAULT 1,
|
||||
event_type TEXT NOT NULL,
|
||||
task_id TEXT,
|
||||
payload TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY (session_id, sequence)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_conversation_events_created
|
||||
ON conversation_session_events(created_at);
|
||||
CREATE TABLE IF NOT EXISTS conversation_message_versions (
|
||||
session_id TEXT NOT NULL,
|
||||
message_id TEXT NOT NULL,
|
||||
row_sequence BIGINT NOT NULL,
|
||||
task_id TEXT,
|
||||
author_type TEXT,
|
||||
created_at TIMESTAMP NOT NULL,
|
||||
tombstone BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
payload TEXT NOT NULL,
|
||||
PRIMARY KEY (session_id, message_id, row_sequence)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_conversation_message_snapshot
|
||||
ON conversation_message_versions(session_id, row_sequence, created_at, message_id);
|
||||
CREATE TABLE IF NOT EXISTS conversation_turn_versions (
|
||||
session_id TEXT NOT NULL,
|
||||
turn_id TEXT NOT NULL,
|
||||
row_sequence BIGINT NOT NULL,
|
||||
task_id TEXT,
|
||||
started_at TIMESTAMP NOT NULL,
|
||||
tombstone BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
payload TEXT NOT NULL,
|
||||
PRIMARY KEY (session_id, turn_id, row_sequence)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_conversation_turn_snapshot
|
||||
ON conversation_turn_versions(session_id, row_sequence, started_at, turn_id);
|
||||
CREATE TABLE IF NOT EXISTS conversation_journal_meta (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
// journalSchemaVersion is the current conversation journal schema version.
|
||||
// Boot runs the trigger (re)creation plus its one-time sanitize migration only
|
||||
// while the stored version is below this constant, then records it, so the
|
||||
// per-boot full-corpus payload rewrite never repeats.
|
||||
const journalSchemaVersion = 4
|
||||
|
||||
// journalBackfillKey records that the one-time backfill of pre-trigger source
|
||||
// rows completed, so boot does not re-scan the whole message/turn corpus.
|
||||
const (
|
||||
// Repeated payload/column keys kept as constants for goconst.
|
||||
jKeySessionID = "session_id"
|
||||
jKeyTaskID = "task_id"
|
||||
)
|
||||
|
||||
const journalBackfillKey = "backfill.complete"
|
||||
|
||||
func (r *Repository) initConversationJournalSchema() error {
|
||||
if _, err := r.db.Exec(conversationJournalTables); err != nil {
|
||||
return fmt.Errorf("create conversation journal tables: %w", err)
|
||||
}
|
||||
version, err := r.readConversationJournalMetaInt("schema.version")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if version < journalSchemaVersion {
|
||||
var triggerErr error
|
||||
if dialect.IsPostgres(r.db.DriverName()) {
|
||||
triggerErr = r.initPostgresConversationJournalTriggers()
|
||||
} else {
|
||||
triggerErr = r.initSQLiteConversationJournalTriggers()
|
||||
}
|
||||
if triggerErr != nil {
|
||||
return triggerErr
|
||||
}
|
||||
if err := r.writeConversationJournalMeta("schema.version", journalSchemaVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
backfilled, err := r.readConversationJournalMetaFlag(journalBackfillKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if backfilled {
|
||||
return nil
|
||||
}
|
||||
if err := r.backfillConversationJournal(); err != nil {
|
||||
return err
|
||||
}
|
||||
return r.writeConversationJournalMeta(journalBackfillKey, 1)
|
||||
}
|
||||
|
||||
func (r *Repository) readConversationJournalMetaInt(key string) (int, error) {
|
||||
var value string
|
||||
err := r.db.Get(&value, r.db.Rebind(`SELECT value FROM conversation_journal_meta WHERE key = ?`), key)
|
||||
if err == sql.ErrNoRows {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("read conversation journal meta %s: %w", key, err)
|
||||
}
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("parse conversation journal meta %s: %w", key, err)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func (r *Repository) readConversationJournalMetaFlag(key string) (bool, error) {
|
||||
value, err := r.readConversationJournalMetaInt(key)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return value > 0, nil
|
||||
}
|
||||
|
||||
func (r *Repository) writeConversationJournalMeta(key string, value int) error {
|
||||
_, err := r.db.Exec(r.db.Rebind(`
|
||||
INSERT INTO conversation_journal_meta(key, value) VALUES (?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value`), key, strconv.Itoa(value))
|
||||
if err != nil {
|
||||
return fmt.Errorf("write conversation journal meta %s: %w", key, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type conversationJournalMessageSeed struct {
|
||||
ID string `db:"id"`
|
||||
TaskSessionID string `db:"task_session_id"`
|
||||
TaskID string `db:"task_id"`
|
||||
TurnID string `db:"turn_id"`
|
||||
AuthorType string `db:"author_type"`
|
||||
AuthorID string `db:"author_id"`
|
||||
Content string `db:"content"`
|
||||
RequestsInput bool `db:"requests_input"`
|
||||
MessageType string `db:"message_type"`
|
||||
Metadata string `db:"metadata"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
PromptIndex int `db:"prompt_index"`
|
||||
}
|
||||
|
||||
type conversationJournalTurnSeed struct {
|
||||
ID string `db:"id"`
|
||||
TaskSessionID string `db:"task_session_id"`
|
||||
TaskID string `db:"task_id"`
|
||||
ExecutionProfileID string `db:"execution_profile_id"`
|
||||
RouteGeneration int64 `db:"route_generation"`
|
||||
Metadata string `db:"metadata"`
|
||||
StartedAt time.Time `db:"started_at"`
|
||||
CompletedAt *time.Time `db:"completed_at"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
const conversationJournalBackfillBatchSize = 100
|
||||
|
||||
//nolint:cyclop,dupl,gocognit,funlen // Turn and message backfills intentionally share bounded keyset control flow.
|
||||
func (r *Repository) backfillConversationJournal() error {
|
||||
var lastSessionID string
|
||||
var lastStartedAt time.Time
|
||||
var lastTurnID string
|
||||
//nolint:dupl // Turn and message batches use the same transaction boundaries.
|
||||
for {
|
||||
tx, err := r.db.Beginx()
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin conversation turn journal backfill: %w", err)
|
||||
}
|
||||
var turns []conversationJournalTurnSeed
|
||||
query := `
|
||||
SELECT t.id, t.task_session_id, t.task_id, t.execution_profile_id, t.route_generation,
|
||||
t.metadata, t.started_at, t.completed_at, t.created_at, t.updated_at
|
||||
FROM task_session_turns t
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM conversation_turn_versions v
|
||||
WHERE v.session_id = t.task_session_id AND v.turn_id = t.id
|
||||
)`
|
||||
args := []any{}
|
||||
if lastTurnID != "" {
|
||||
query += `
|
||||
AND (t.task_session_id > ? OR
|
||||
(t.task_session_id = ? AND
|
||||
(t.started_at > ? OR (t.started_at = ? AND t.id > ?))))`
|
||||
args = append(args, lastSessionID, lastSessionID, lastStartedAt, lastStartedAt, lastTurnID)
|
||||
}
|
||||
query += ` ORDER BY t.task_session_id, t.started_at, t.id LIMIT ?`
|
||||
args = append(args, conversationJournalBackfillBatchSize)
|
||||
if err := tx.Select(&turns, r.db.Rebind(query), args...); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return fmt.Errorf("list conversation turn backfill: %w", err)
|
||||
}
|
||||
if len(turns) == 0 {
|
||||
_ = tx.Rollback()
|
||||
break
|
||||
}
|
||||
for _, turn := range turns {
|
||||
if err := r.backfillConversationTurn(tx, turn); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("commit conversation turn journal backfill: %w", err)
|
||||
}
|
||||
lastTurn := turns[len(turns)-1]
|
||||
lastSessionID, lastStartedAt, lastTurnID = lastTurn.TaskSessionID, lastTurn.StartedAt, lastTurn.ID
|
||||
}
|
||||
|
||||
lastSessionID = ""
|
||||
var lastCreatedAt time.Time
|
||||
var lastMessageID string
|
||||
for {
|
||||
tx, err := r.db.Beginx()
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin conversation message journal backfill: %w", err)
|
||||
}
|
||||
var messages []conversationJournalMessageSeed
|
||||
query := `
|
||||
SELECT m.id, m.task_session_id, m.task_id, m.turn_id, m.author_type, m.author_id,
|
||||
m.content, m.requests_input, m.type AS message_type, m.metadata,
|
||||
m.created_at, m.updated_at, m.prompt_seq AS prompt_index
|
||||
FROM task_session_messages m
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM conversation_message_versions v
|
||||
WHERE v.session_id = m.task_session_id AND v.message_id = m.id
|
||||
)`
|
||||
args := []any{}
|
||||
if lastMessageID != "" {
|
||||
query += `
|
||||
AND (m.task_session_id > ? OR
|
||||
(m.task_session_id = ? AND
|
||||
(m.created_at > ? OR (m.created_at = ? AND m.id > ?))))`
|
||||
args = append(args, lastSessionID, lastSessionID, lastCreatedAt, lastCreatedAt, lastMessageID)
|
||||
}
|
||||
query += ` ORDER BY m.task_session_id, m.created_at, m.id LIMIT ?`
|
||||
args = append(args, conversationJournalBackfillBatchSize)
|
||||
if err := tx.Select(&messages, r.db.Rebind(query), args...); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return fmt.Errorf("list conversation message backfill: %w", err)
|
||||
}
|
||||
if len(messages) == 0 {
|
||||
_ = tx.Rollback()
|
||||
break
|
||||
}
|
||||
for _, message := range messages {
|
||||
if err := r.backfillConversationMessage(tx, message); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("commit conversation message journal backfill: %w", err)
|
||||
}
|
||||
lastMessage := messages[len(messages)-1]
|
||||
lastSessionID, lastCreatedAt, lastMessageID = lastMessage.TaskSessionID, lastMessage.CreatedAt, lastMessage.ID
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) nextConversationJournalSequence(tx *sqlx.Tx, sessionID string) (uint64, error) {
|
||||
var sequence uint64
|
||||
query := r.db.Rebind(`UPDATE conversation_session_streams
|
||||
SET watermark = watermark + 1, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE session_id = ? RETURNING watermark`)
|
||||
err := tx.Get(&sequence, query, sessionID)
|
||||
if err == nil {
|
||||
return sequence, nil
|
||||
}
|
||||
if err != sql.ErrNoRows {
|
||||
return 0, fmt.Errorf("advance conversation journal sequence: %w", err)
|
||||
}
|
||||
query = r.db.Rebind(`INSERT INTO conversation_session_streams(session_id, watermark, terminal, updated_at)
|
||||
VALUES (?, 1, FALSE, CURRENT_TIMESTAMP)`)
|
||||
if _, err := tx.Exec(query, sessionID); err != nil {
|
||||
return 0, fmt.Errorf("initialize conversation journal sequence: %w", err)
|
||||
}
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
// sameTurnSourceImage reports whether the freshly read source row still
|
||||
// matches the backfill seed for every payload-bound field.
|
||||
//
|
||||
//nolint:goconst // Backfill payload values mirror the public event contract.
|
||||
func sameTurnSourceImage(current struct {
|
||||
TaskID string `db:"task_id"`
|
||||
ExecutionProfileID string `db:"execution_profile_id"`
|
||||
RouteGeneration int64 `db:"route_generation"`
|
||||
Metadata string `db:"metadata"`
|
||||
StartedAt time.Time `db:"started_at"`
|
||||
CompletedAt *time.Time `db:"completed_at"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}, turn conversationJournalTurnSeed) bool {
|
||||
completedAtEqual := (current.CompletedAt == nil) == (turn.CompletedAt == nil)
|
||||
if current.CompletedAt != nil && turn.CompletedAt != nil && !current.CompletedAt.Equal(*turn.CompletedAt) {
|
||||
return false
|
||||
}
|
||||
return completedAtEqual &&
|
||||
current.TaskID == turn.TaskID &&
|
||||
current.ExecutionProfileID == turn.ExecutionProfileID &&
|
||||
current.RouteGeneration == turn.RouteGeneration &&
|
||||
current.Metadata == turn.Metadata &&
|
||||
current.StartedAt.Equal(turn.StartedAt) &&
|
||||
current.CreatedAt.Equal(turn.CreatedAt) &&
|
||||
current.UpdatedAt.Equal(turn.UpdatedAt)
|
||||
}
|
||||
|
||||
func (r *Repository) backfillConversationTurn(tx *sqlx.Tx, turn conversationJournalTurnSeed) error {
|
||||
// Optimistic re-check of the complete source image: if any payload-bound
|
||||
// field changed since the backfill SELECT, its trigger already journaled
|
||||
// the current image, and a stale pre-update image must not win the
|
||||
// snapshot with a later sequence. Comparing completion state alone would
|
||||
// let a same-state update (metadata, profile, timestamps) pass.
|
||||
var current struct {
|
||||
TaskID string `db:"task_id"`
|
||||
ExecutionProfileID string `db:"execution_profile_id"`
|
||||
RouteGeneration int64 `db:"route_generation"`
|
||||
Metadata string `db:"metadata"`
|
||||
StartedAt time.Time `db:"started_at"`
|
||||
CompletedAt *time.Time `db:"completed_at"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
err := tx.Get(¤t, r.db.Rebind(`
|
||||
SELECT task_id, execution_profile_id, route_generation, metadata,
|
||||
started_at, completed_at, created_at, updated_at
|
||||
FROM task_session_turns WHERE id = ?`), turn.ID)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil // deleted concurrently; its history is gone
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("re-check conversation turn backfill %s: %w", turn.ID, err)
|
||||
}
|
||||
if !sameTurnSourceImage(current, turn) {
|
||||
return nil // source image changed concurrently; trigger journaled it
|
||||
}
|
||||
sequence, err := r.nextConversationJournalSequence(tx, turn.TaskSessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
eventType := "session.turn.started"
|
||||
if turn.CompletedAt != nil {
|
||||
eventType = "session.turn.completed"
|
||||
}
|
||||
payload, err := json.Marshal(map[string]any{
|
||||
"type": eventType, jKeySessionID: turn.TaskSessionID, jKeyTaskID: journalTaskID(turn.TaskID),
|
||||
"id": turn.ID, "execution_profile_id": turn.ExecutionProfileID, "route_generation": turn.RouteGeneration,
|
||||
"started_at": turn.StartedAt.UTC().Format(time.RFC3339Nano), "completed_at": journalTime(turn.CompletedAt),
|
||||
"metadata_json": turn.Metadata, columnCreatedAt: turn.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
columnUpdatedAt: turn.UpdatedAt.UTC().Format(time.RFC3339Nano),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode conversation turn backfill: %w", err)
|
||||
}
|
||||
query := r.db.Rebind(`INSERT INTO conversation_turn_versions(
|
||||
session_id, turn_id, row_sequence, task_id, started_at, tombstone, payload
|
||||
) VALUES (?, ?, ?, ?, ?, FALSE, ?)`)
|
||||
if _, err := tx.Exec(query, turn.TaskSessionID, turn.ID, sequence, journalTaskID(turn.TaskID), turn.StartedAt, payload); err != nil {
|
||||
return fmt.Errorf("insert conversation turn backfill version: %w", err)
|
||||
}
|
||||
return r.insertConversationBackfillEvent(tx, turn.TaskSessionID, turn.TaskID, sequence, eventType, payload)
|
||||
}
|
||||
|
||||
//nolint:goconst // Backfill payload values mirror the public event contract.
|
||||
func (r *Repository) backfillConversationMessage(tx *sqlx.Tx, message conversationJournalMessageSeed) error {
|
||||
// Optimistic re-check of the complete source image: if any payload-bound
|
||||
// field changed since the backfill SELECT, its trigger already journaled
|
||||
// the current image, and a stale pre-update image must not win the
|
||||
// snapshot with a later sequence. Comparing content alone would let a
|
||||
// same-content update (metadata, task/turn/type/author, timestamps) pass.
|
||||
var current struct {
|
||||
TaskID string `db:"task_id"`
|
||||
TurnID string `db:"turn_id"`
|
||||
AuthorType string `db:"author_type"`
|
||||
Content string `db:"content"`
|
||||
Type string `db:"type"`
|
||||
Metadata string `db:"metadata"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
err := tx.Get(¤t, r.db.Rebind(`
|
||||
SELECT task_id, turn_id, author_type, content, type, metadata, created_at, updated_at
|
||||
FROM task_session_messages WHERE id = ?`), message.ID)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil // deleted concurrently; its tombstone was journaled
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("re-check conversation message backfill %s: %w", message.ID, err)
|
||||
}
|
||||
if current.TaskID != message.TaskID ||
|
||||
current.TurnID != message.TurnID ||
|
||||
current.AuthorType != message.AuthorType ||
|
||||
current.Content != message.Content ||
|
||||
current.Type != message.MessageType ||
|
||||
current.Metadata != message.Metadata ||
|
||||
!current.CreatedAt.Equal(message.CreatedAt) ||
|
||||
!current.UpdatedAt.Equal(message.UpdatedAt) {
|
||||
return nil // source image changed concurrently; trigger journaled it
|
||||
}
|
||||
sequence, err := r.nextConversationJournalSequence(tx, message.TaskSessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var metadata map[string]any
|
||||
_ = json.Unmarshal([]byte(message.Metadata), &metadata)
|
||||
senderTaskID, _ := metadata["sender_task_id"].(string)
|
||||
payload, err := json.Marshal(map[string]any{
|
||||
"type": "message.added", jKeySessionID: message.TaskSessionID, jKeyTaskID: journalTaskID(message.TaskID),
|
||||
"message_id": message.ID, "turn_id": message.TurnID, "author_type": message.AuthorType,
|
||||
"content": sysprompt.StripSystemContent(message.Content),
|
||||
"message_type": message.MessageType, columnCreatedAt: message.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
columnUpdatedAt: message.UpdatedAt.UTC().Format(time.RFC3339Nano), "prompt_index": message.PromptIndex,
|
||||
"sender_task_id": senderTaskID,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode conversation message backfill: %w", err)
|
||||
}
|
||||
query := r.db.Rebind(`INSERT INTO conversation_message_versions(
|
||||
session_id, message_id, row_sequence, task_id, author_type, created_at, tombstone, payload
|
||||
) VALUES (?, ?, ?, ?, ?, ?, FALSE, ?)`)
|
||||
if _, err := tx.Exec(query, message.TaskSessionID, message.ID, sequence, journalTaskID(message.TaskID), message.AuthorType, message.CreatedAt, payload); err != nil {
|
||||
return fmt.Errorf("insert conversation message backfill version: %w", err)
|
||||
}
|
||||
return r.insertConversationBackfillEvent(tx, message.TaskSessionID, message.TaskID, sequence, "message.added", payload)
|
||||
}
|
||||
|
||||
func (r *Repository) insertConversationBackfillEvent(
|
||||
tx *sqlx.Tx,
|
||||
sessionID string,
|
||||
taskID string,
|
||||
sequence uint64,
|
||||
eventType string,
|
||||
payload []byte,
|
||||
) error {
|
||||
query := r.db.Rebind(`INSERT INTO conversation_session_events(
|
||||
session_id, sequence, event_id, protocol_version, event_type, task_id, payload, created_at
|
||||
) VALUES (?, ?, ?, 1, ?, ?, ?, CURRENT_TIMESTAMP)`)
|
||||
if _, err := tx.Exec(query, sessionID, sequence, sessionID+":"+strconv.FormatUint(sequence, 10), eventType, journalTaskID(taskID), payload); err != nil {
|
||||
return fmt.Errorf("insert conversation journal backfill event: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sqliteStripSystemContentExpr returns a scalar subquery that removes every
|
||||
// well-formed <kandev-system>...</kandev-system> block from expr - including
|
||||
// multi-line blocks, multiple blocks, and nested evasion - and trims the
|
||||
// result, mirroring sysprompt.StripSystemContent. SQLite has no scalar regex
|
||||
// replace, so a bounded recursive CTE reapplies first-block removal until no
|
||||
// well-formed block remains. Depth 64 caps adversarial input. If another full
|
||||
// block remains at that bound, the expression keeps only the prefix before it;
|
||||
// dropping the unexamined suffix fails closed rather than persisting system
|
||||
// content. The final row is always non-NULL for text input.
|
||||
func sqliteStripSystemContentExpr(expr string) string {
|
||||
// Go's helper also consumes the whitespace run after each closing tag
|
||||
// (`\s*` in the pattern); ltrim the tail so junction spacing matches.
|
||||
whitespace := "char(32) || char(9) || char(10) || char(13)"
|
||||
return `(SELECT trim(x) FROM (
|
||||
WITH RECURSIVE strip(x, depth) AS (
|
||||
SELECT ` + expr + `, 0
|
||||
UNION ALL
|
||||
SELECT substr(x, 1, instr(x, '<kandev-system>') - 1)
|
||||
|| ltrim(substr(x,
|
||||
instr(x, '<kandev-system>') + length('<kandev-system>')
|
||||
+ instr(substr(x, instr(x, '<kandev-system>') + length('<kandev-system>')), '</kandev-system>') - 1
|
||||
+ length('</kandev-system>')),
|
||||
` + whitespace + `), depth + 1
|
||||
FROM strip
|
||||
WHERE depth < 64
|
||||
AND instr(x, '<kandev-system>') > 0
|
||||
AND instr(substr(x, instr(x, '<kandev-system>') + length('<kandev-system>')), '</kandev-system>') > 0
|
||||
)
|
||||
SELECT CASE WHEN depth = 64 AND instr(x, '<kandev-system>') > 0 AND instr(substr(x, instr(x, '<kandev-system>') + length('<kandev-system>')), '</kandev-system>') > 0 THEN substr(x, 1, instr(x, '<kandev-system>') - 1) ELSE x END AS x FROM strip ORDER BY depth DESC LIMIT 1
|
||||
))`
|
||||
}
|
||||
|
||||
// sqliteStripSystemToken marks where a trigger or migration must insert the
|
||||
// strip expression; the raw SQL literal keeps strftime %-formats untouched.
|
||||
const sqliteStripSystemToken = "__SQLITE_STRIP_SYSTEM__"
|
||||
const sqliteConversationMetadataToken = "__SQLITE_CONVERSATION_METADATA__"
|
||||
const postgresConversationMetadataToken = "__POSTGRES_CONVERSATION_METADATA__"
|
||||
|
||||
//nolint:goconst // These keys are an explicit privacy allowlist.
|
||||
var conversationMessageMetadataKeys = []string{
|
||||
"action_details", "action_type", "action_visibility", "actions", "agent_disconnected", "attempt", "attachments",
|
||||
"auth_methods", "auto_start", "base_branch", "context", "context_files",
|
||||
"decision_id", "effective_model", "entity_references", "error_output",
|
||||
"failure_code", "failure_details", "failure_kind", "fallback_model",
|
||||
"has_hidden_prompts", "has_resume_token", "has_review_comments", "is_auth_error",
|
||||
"kind", "max_attempts", "message", "missing_branch", "model_id", "new_branch",
|
||||
"original_branch", "options", "pending_id", "plan_mode", "progress", "provider_name",
|
||||
"question", "question_id", "question_index", "question_total", "recovery_actions",
|
||||
"remediation", "remediation_url", "requested_model", "request_id", "response", "reset_at",
|
||||
"retry_at", "retry_in_seconds", "retrying", "sender_session_id",
|
||||
"sender_session_name", "sender_task_id", "sender_task_title", "stage", "status",
|
||||
"script_type", "agent_name", "command", "exit_code", "is_resuming", "started_at",
|
||||
"completed_at", "error",
|
||||
"task_id", "text", "tool_call_id", "variant", "workflow_message", "workflow_step_color",
|
||||
"workflow_step_id", "workflow_step_name",
|
||||
}
|
||||
|
||||
func sqliteConversationMetadataExpr(metadataExpr string) string {
|
||||
arguments := make([]string, 0, len(conversationMessageMetadataKeys)*2)
|
||||
for _, key := range conversationMessageMetadataKeys {
|
||||
arguments = append(arguments, "'"+key+"'", sqliteConversationMetadataValueExpr(metadataExpr, key))
|
||||
}
|
||||
return "CASE WHEN json_valid(" + metadataExpr + ") THEN json_object(" + strings.Join(arguments, ",") + ") ELSE json_object() END"
|
||||
}
|
||||
|
||||
func sqliteConversationMetadataValueExpr(metadataExpr, key string) string {
|
||||
path := "'$." + key + "'"
|
||||
value := "json_extract(" + metadataExpr + ", " + path + ")"
|
||||
return "CASE json_type(" + metadataExpr + ", " + path + ") " +
|
||||
"WHEN 'true' THEN json('true') " +
|
||||
"WHEN 'false' THEN json('false') " +
|
||||
"ELSE " + value + " END"
|
||||
}
|
||||
|
||||
func postgresConversationMetadataExpr(metadataExpr string) string {
|
||||
source := "conversation_safe_jsonb(" + metadataExpr + ")"
|
||||
const maxKeysPerJSONBObject = 40 // PostgreSQL limits function calls to 100 arguments.
|
||||
objects := make([]string, 0, (len(conversationMessageMetadataKeys)+maxKeysPerJSONBObject-1)/maxKeysPerJSONBObject)
|
||||
for start := 0; start < len(conversationMessageMetadataKeys); start += maxKeysPerJSONBObject {
|
||||
end := min(start+maxKeysPerJSONBObject, len(conversationMessageMetadataKeys))
|
||||
arguments := make([]string, 0, (end-start)*2)
|
||||
for _, key := range conversationMessageMetadataKeys[start:end] {
|
||||
arguments = append(arguments, "'"+key+"'", source+" -> '"+key+"'")
|
||||
}
|
||||
objects = append(objects, "jsonb_build_object("+strings.Join(arguments, ",")+")")
|
||||
}
|
||||
return "jsonb_strip_nulls(" + strings.Join(objects, " || ") + ")"
|
||||
}
|
||||
|
||||
func journalTaskID(taskID string) any {
|
||||
if taskID == "" {
|
||||
return nil
|
||||
}
|
||||
return taskID
|
||||
}
|
||||
func journalTime(value *time.Time) any {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
return value.UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
|
||||
//nolint:funlen // Trigger definitions are kept together so schema initialization is atomic.
|
||||
func (r *Repository) initSQLiteConversationJournalTriggers() error {
|
||||
triggerSQL := strings.ReplaceAll(`
|
||||
DROP TRIGGER IF EXISTS conversation_message_insert;
|
||||
CREATE TRIGGER IF NOT EXISTS conversation_message_insert
|
||||
AFTER INSERT ON task_session_messages
|
||||
BEGIN
|
||||
INSERT INTO conversation_session_streams(session_id, watermark, terminal, updated_at)
|
||||
VALUES (NEW.task_session_id, 1, FALSE, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(session_id) DO UPDATE SET watermark = watermark + 1, updated_at = CURRENT_TIMESTAMP;
|
||||
INSERT INTO conversation_message_versions(session_id, message_id, row_sequence, task_id, author_type, created_at, tombstone, payload)
|
||||
SELECT NEW.task_session_id, NEW.id, watermark, NULLIF(NEW.task_id, ''), NEW.author_type, NEW.created_at, FALSE,
|
||||
json_object('type','message.added','session_id',NEW.task_session_id,'task_id',NULLIF(NEW.task_id,''),
|
||||
'message_id',NEW.id,'turn_id',NULLIF(NEW.turn_id,''),'author_type',NEW.author_type,
|
||||
'content',__SQLITE_STRIP_SYSTEM__,'message_type',NEW.type,'requests_input',NEW.requests_input,
|
||||
'created_at',__SQLITE_RFC3339_MILLIS__(NEW.created_at),
|
||||
'updated_at',__SQLITE_RFC3339_MILLIS__(COALESCE(NEW.updated_at,NEW.created_at)),'prompt_index',NEW.prompt_seq,
|
||||
'metadata',__SQLITE_CONVERSATION_METADATA__(NEW.metadata),
|
||||
'sender_task_id',CASE WHEN json_valid(NEW.metadata) AND typeof(json_extract(NEW.metadata,'$.sender_task_id')) = 'text' THEN json_extract(NEW.metadata,'$.sender_task_id') END)
|
||||
FROM conversation_session_streams WHERE session_id = NEW.task_session_id;
|
||||
INSERT INTO conversation_session_events(session_id, sequence, event_id, event_type, task_id, payload, created_at)
|
||||
SELECT NEW.task_session_id, watermark, NEW.task_session_id || ':' || watermark, 'message.added', NULLIF(NEW.task_id,''), payload, CURRENT_TIMESTAMP
|
||||
FROM conversation_session_streams JOIN conversation_message_versions
|
||||
ON conversation_message_versions.session_id = conversation_session_streams.session_id
|
||||
AND conversation_message_versions.message_id = NEW.id
|
||||
AND conversation_message_versions.row_sequence = conversation_session_streams.watermark
|
||||
WHERE conversation_session_streams.session_id = NEW.task_session_id;
|
||||
END;
|
||||
|
||||
DROP TRIGGER IF EXISTS conversation_message_update;
|
||||
CREATE TRIGGER IF NOT EXISTS conversation_message_update
|
||||
AFTER UPDATE ON task_session_messages
|
||||
WHEN OLD.id IS NOT NEW.id
|
||||
OR OLD.task_session_id IS NOT NEW.task_session_id
|
||||
OR OLD.task_id IS NOT NEW.task_id
|
||||
OR OLD.turn_id IS NOT NEW.turn_id
|
||||
OR OLD.author_type IS NOT NEW.author_type
|
||||
OR OLD.author_id IS NOT NEW.author_id
|
||||
OR OLD.content IS NOT NEW.content
|
||||
OR OLD.requests_input IS NOT NEW.requests_input
|
||||
OR OLD.type IS NOT NEW.type
|
||||
OR OLD.metadata IS NOT NEW.metadata
|
||||
OR OLD.created_at IS NOT NEW.created_at
|
||||
OR OLD.updated_at IS NOT NEW.updated_at
|
||||
OR OLD.prompt_seq IS NOT NEW.prompt_seq
|
||||
BEGIN
|
||||
INSERT INTO conversation_session_streams(session_id, watermark, terminal, updated_at)
|
||||
VALUES (NEW.task_session_id, 1, FALSE, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(session_id) DO UPDATE SET watermark = watermark + 1, updated_at = CURRENT_TIMESTAMP;
|
||||
INSERT INTO conversation_message_versions(session_id, message_id, row_sequence, task_id, author_type, created_at, tombstone, payload)
|
||||
SELECT NEW.task_session_id, NEW.id, watermark, NULLIF(NEW.task_id, ''), NEW.author_type, NEW.created_at, FALSE,
|
||||
json_object('type','message.updated','session_id',NEW.task_session_id,'task_id',NULLIF(NEW.task_id,''),
|
||||
'message_id',NEW.id,'turn_id',NULLIF(NEW.turn_id,''),'author_type',NEW.author_type,
|
||||
'content',__SQLITE_STRIP_SYSTEM__,'message_type',NEW.type,'requests_input',NEW.requests_input,
|
||||
'created_at',__SQLITE_RFC3339_MILLIS__(NEW.created_at),
|
||||
'updated_at',__SQLITE_RFC3339_MILLIS__(COALESCE(NEW.updated_at,NEW.created_at)),'prompt_index',NEW.prompt_seq,
|
||||
'metadata',__SQLITE_CONVERSATION_METADATA__(NEW.metadata),
|
||||
'sender_task_id',CASE WHEN json_valid(NEW.metadata) AND typeof(json_extract(NEW.metadata,'$.sender_task_id')) = 'text' THEN json_extract(NEW.metadata,'$.sender_task_id') END)
|
||||
FROM conversation_session_streams WHERE session_id = NEW.task_session_id;
|
||||
INSERT INTO conversation_session_events(session_id, sequence, event_id, event_type, task_id, payload, created_at)
|
||||
SELECT NEW.task_session_id, watermark, NEW.task_session_id || ':' || watermark, 'message.updated', NULLIF(NEW.task_id,''), payload, CURRENT_TIMESTAMP
|
||||
FROM conversation_session_streams JOIN conversation_message_versions
|
||||
ON conversation_message_versions.session_id = conversation_session_streams.session_id
|
||||
AND conversation_message_versions.message_id = NEW.id
|
||||
AND conversation_message_versions.row_sequence = conversation_session_streams.watermark
|
||||
WHERE conversation_session_streams.session_id = NEW.task_session_id;
|
||||
END;
|
||||
DROP TRIGGER IF EXISTS conversation_message_delete;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS conversation_message_delete
|
||||
BEFORE DELETE ON task_session_messages
|
||||
BEGIN
|
||||
INSERT INTO conversation_session_streams(session_id, watermark, terminal, updated_at)
|
||||
VALUES (OLD.task_session_id, 1, FALSE, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(session_id) DO UPDATE SET watermark = watermark + 1, updated_at = CURRENT_TIMESTAMP;
|
||||
INSERT INTO conversation_message_versions(session_id, message_id, row_sequence, task_id, author_type, created_at, tombstone, payload)
|
||||
SELECT OLD.task_session_id, OLD.id, watermark, NULLIF(OLD.task_id, ''), OLD.author_type, OLD.created_at, TRUE,
|
||||
json_object('type','message.deleted','session_id',OLD.task_session_id,'task_id',NULLIF(OLD.task_id,''),'message_id',OLD.id)
|
||||
FROM conversation_session_streams WHERE session_id = OLD.task_session_id;
|
||||
INSERT INTO conversation_session_events(session_id, sequence, event_id, event_type, task_id, payload, created_at)
|
||||
SELECT OLD.task_session_id, watermark, OLD.task_session_id || ':' || watermark, 'message.deleted', NULLIF(OLD.task_id,''), payload, CURRENT_TIMESTAMP
|
||||
FROM conversation_session_streams JOIN conversation_message_versions
|
||||
ON conversation_message_versions.session_id = conversation_session_streams.session_id
|
||||
AND conversation_message_versions.message_id = OLD.id
|
||||
AND conversation_message_versions.row_sequence = conversation_session_streams.watermark
|
||||
WHERE conversation_session_streams.session_id = OLD.task_session_id;
|
||||
END;
|
||||
DROP TRIGGER IF EXISTS conversation_turn_insert;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS conversation_turn_insert
|
||||
AFTER INSERT ON task_session_turns
|
||||
BEGIN
|
||||
INSERT INTO conversation_session_streams(session_id, watermark, terminal, updated_at)
|
||||
VALUES (NEW.task_session_id, 1, FALSE, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(session_id) DO UPDATE SET watermark = watermark + 1, updated_at = CURRENT_TIMESTAMP;
|
||||
INSERT INTO conversation_turn_versions(session_id, turn_id, row_sequence, task_id, started_at, tombstone, payload)
|
||||
SELECT NEW.task_session_id, NEW.id, watermark, NULLIF(NEW.task_id, ''), NEW.started_at, FALSE,
|
||||
json_object('type','session.turn.started','session_id',NEW.task_session_id,'task_id',NULLIF(NEW.task_id,''),
|
||||
'id',NEW.id,'started_at',__SQLITE_RFC3339_MILLIS__(NEW.started_at),
|
||||
'completed_at',__SQLITE_RFC3339_MILLIS__(NEW.completed_at),
|
||||
'created_at',__SQLITE_RFC3339_MILLIS__(NEW.created_at),
|
||||
'updated_at',__SQLITE_RFC3339_MILLIS__(COALESCE(NEW.updated_at,NEW.started_at)))
|
||||
FROM conversation_session_streams WHERE session_id = NEW.task_session_id;
|
||||
INSERT INTO conversation_session_events(session_id, sequence, event_id, event_type, task_id, payload, created_at)
|
||||
SELECT NEW.task_session_id, watermark, NEW.task_session_id || ':' || watermark, 'session.turn.started', NULLIF(NEW.task_id,''), payload, CURRENT_TIMESTAMP
|
||||
FROM conversation_session_streams JOIN conversation_turn_versions
|
||||
ON conversation_turn_versions.session_id = conversation_session_streams.session_id
|
||||
AND conversation_turn_versions.turn_id = NEW.id
|
||||
AND conversation_turn_versions.row_sequence = conversation_session_streams.watermark
|
||||
WHERE conversation_session_streams.session_id = NEW.task_session_id;
|
||||
END;
|
||||
DROP TRIGGER IF EXISTS conversation_turn_complete;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS conversation_turn_complete
|
||||
AFTER UPDATE OF completed_at ON task_session_turns
|
||||
WHEN OLD.completed_at IS NULL AND NEW.completed_at IS NOT NULL
|
||||
BEGIN
|
||||
INSERT INTO conversation_session_streams(session_id, watermark, terminal, updated_at)
|
||||
VALUES (NEW.task_session_id, 1, FALSE, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(session_id) DO UPDATE SET watermark = watermark + 1, updated_at = CURRENT_TIMESTAMP;
|
||||
INSERT INTO conversation_turn_versions(session_id, turn_id, row_sequence, task_id, started_at, tombstone, payload)
|
||||
SELECT NEW.task_session_id, NEW.id, watermark, NULLIF(NEW.task_id, ''), NEW.started_at, FALSE,
|
||||
json_object('type','session.turn.completed','session_id',NEW.task_session_id,'task_id',NULLIF(NEW.task_id,''),
|
||||
'id',NEW.id,'started_at',__SQLITE_RFC3339_MILLIS__(NEW.started_at),
|
||||
'completed_at',__SQLITE_RFC3339_MILLIS__(NEW.completed_at),
|
||||
'created_at',__SQLITE_RFC3339_MILLIS__(NEW.created_at),
|
||||
'updated_at',__SQLITE_RFC3339_MILLIS__(COALESCE(NEW.updated_at,NEW.completed_at,NEW.started_at)),
|
||||
'metadata',CASE WHEN json_valid(NEW.metadata) THEN json(NEW.metadata) ELSE json('{}') END,
|
||||
'had_output',json(CASE WHEN EXISTS (
|
||||
SELECT 1 FROM task_session_messages output
|
||||
WHERE output.turn_id = NEW.id AND output.author_type = 'agent'
|
||||
AND (
|
||||
output.type IN ('tool_call','tool_edit','tool_read','tool_search','tool_execute',
|
||||
'agent_plan','todo','permission_request','clarification_request')
|
||||
OR (output.type IN ('message','content','') AND trim(output.content) <> '')
|
||||
)) THEN 'true' ELSE 'false' END))
|
||||
FROM conversation_session_streams WHERE session_id = NEW.task_session_id;
|
||||
INSERT INTO conversation_session_events(session_id, sequence, event_id, event_type, task_id, payload, created_at)
|
||||
SELECT NEW.task_session_id, watermark, NEW.task_session_id || ':' || watermark, 'session.turn.completed', NULLIF(NEW.task_id,''), payload, CURRENT_TIMESTAMP
|
||||
FROM conversation_session_streams JOIN conversation_turn_versions
|
||||
ON conversation_turn_versions.session_id = conversation_session_streams.session_id
|
||||
AND conversation_turn_versions.turn_id = NEW.id
|
||||
AND conversation_turn_versions.row_sequence = conversation_session_streams.watermark
|
||||
WHERE conversation_session_streams.session_id = NEW.task_session_id;
|
||||
END;
|
||||
|
||||
DROP TRIGGER IF EXISTS conversation_turn_delete;
|
||||
CREATE TRIGGER IF NOT EXISTS conversation_turn_delete
|
||||
AFTER DELETE ON task_session_turns
|
||||
BEGIN
|
||||
INSERT INTO conversation_session_streams(session_id, watermark, terminal, updated_at)
|
||||
VALUES (OLD.task_session_id, 1, FALSE, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(session_id) DO UPDATE SET watermark = watermark + 1, updated_at = CURRENT_TIMESTAMP;
|
||||
INSERT INTO conversation_turn_versions(session_id, turn_id, row_sequence, task_id, started_at, tombstone, payload)
|
||||
SELECT OLD.task_session_id, OLD.id, watermark, NULLIF(OLD.task_id, ''), OLD.started_at, TRUE,
|
||||
json_object('type','session.turn.removed','session_id',OLD.task_session_id,'task_id',NULLIF(OLD.task_id,''),'id',OLD.id)
|
||||
FROM conversation_session_streams WHERE session_id = OLD.task_session_id;
|
||||
INSERT INTO conversation_session_events(session_id, sequence, event_id, event_type, task_id, payload, created_at)
|
||||
SELECT OLD.task_session_id, watermark, OLD.task_session_id || ':' || watermark, 'session.turn.removed', NULLIF(OLD.task_id,''),
|
||||
payload, CURRENT_TIMESTAMP
|
||||
FROM conversation_session_streams JOIN conversation_turn_versions
|
||||
ON conversation_turn_versions.session_id = conversation_session_streams.session_id
|
||||
AND conversation_turn_versions.turn_id = OLD.id
|
||||
AND conversation_turn_versions.row_sequence = conversation_session_streams.watermark
|
||||
WHERE conversation_session_streams.session_id = OLD.task_session_id;
|
||||
END;
|
||||
|
||||
DROP TRIGGER IF EXISTS conversation_session_delete;
|
||||
CREATE TRIGGER IF NOT EXISTS conversation_session_delete
|
||||
AFTER DELETE ON task_sessions
|
||||
BEGIN
|
||||
INSERT INTO conversation_session_streams(session_id, watermark, terminal, updated_at)
|
||||
VALUES (OLD.id, 1, TRUE, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(session_id) DO UPDATE SET watermark = watermark + 1, terminal = TRUE, updated_at = CURRENT_TIMESTAMP;
|
||||
INSERT INTO conversation_session_events(session_id, sequence, event_id, event_type, task_id, payload, created_at)
|
||||
SELECT OLD.id, watermark, OLD.id || ':' || watermark, 'session.removed', NULLIF(OLD.task_id,''),
|
||||
json_object('type','session.removed','session_id',OLD.id,'task_id',NULLIF(OLD.task_id,'')), CURRENT_TIMESTAMP
|
||||
FROM conversation_session_streams WHERE session_id = OLD.id;
|
||||
END;
|
||||
`, sqliteStripSystemToken, sqliteStripSystemContentExpr("NEW.content"))
|
||||
triggerSQL = strings.ReplaceAll(triggerSQL,
|
||||
sqliteConversationMetadataToken+"(NEW.metadata)", sqliteConversationMetadataExpr("NEW.metadata"))
|
||||
triggerSQL = strings.NewReplacer(
|
||||
"__SQLITE_RFC3339_MILLIS__(NEW.created_at)", dialect.RFC3339Millis(dialect.SQLite3, "NEW.created_at"),
|
||||
"__SQLITE_RFC3339_MILLIS__(COALESCE(NEW.updated_at,NEW.created_at))", dialect.RFC3339Millis(dialect.SQLite3, "COALESCE(NEW.updated_at,NEW.created_at)"),
|
||||
"__SQLITE_RFC3339_MILLIS__(NEW.started_at)", dialect.RFC3339Millis(dialect.SQLite3, "NEW.started_at"),
|
||||
"__SQLITE_RFC3339_MILLIS__(NEW.completed_at)", dialect.RFC3339Millis(dialect.SQLite3, "NEW.completed_at"),
|
||||
"__SQLITE_RFC3339_MILLIS__(COALESCE(NEW.updated_at,NEW.started_at))", dialect.RFC3339Millis(dialect.SQLite3, "COALESCE(NEW.updated_at,NEW.started_at)"),
|
||||
"__SQLITE_RFC3339_MILLIS__(COALESCE(NEW.updated_at,NEW.completed_at,NEW.started_at))", dialect.RFC3339Millis(dialect.SQLite3, "COALESCE(NEW.updated_at,NEW.completed_at,NEW.started_at)"),
|
||||
).Replace(triggerSQL)
|
||||
if _, err := r.db.Exec(triggerSQL); err != nil {
|
||||
return fmt.Errorf("create SQLite conversation journal triggers: %w", err)
|
||||
}
|
||||
messageMigrate := `
|
||||
UPDATE conversation_message_versions
|
||||
SET payload = json_set(payload, '$.content', __SQLITE_STRIP_SYSTEM__)
|
||||
WHERE json_valid(payload)
|
||||
AND json_extract(payload, '$.content') LIKE '%<kandev-system>%'
|
||||
`
|
||||
contentExpr := sqliteStripSystemContentExpr("json_extract(payload, '$.content')")
|
||||
if _, err := r.db.Exec(strings.ReplaceAll(messageMigrate, sqliteStripSystemToken, contentExpr)); err != nil {
|
||||
return fmt.Errorf("sanitize SQLite conversation message journal: %w", err)
|
||||
}
|
||||
eventMigrate := `
|
||||
UPDATE conversation_session_events
|
||||
SET payload = json_set(payload, '$.content', __SQLITE_STRIP_SYSTEM__)
|
||||
WHERE json_valid(payload)
|
||||
AND json_extract(payload, '$.content') LIKE '%<kandev-system>%'
|
||||
`
|
||||
if _, err := r.db.Exec(strings.ReplaceAll(eventMigrate, sqliteStripSystemToken, contentExpr)); err != nil {
|
||||
return fmt.Errorf("sanitize SQLite conversation event journal: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//nolint:funlen // Trigger definitions are kept together so schema initialization is atomic.
|
||||
func (r *Repository) initPostgresConversationJournalTriggers() error {
|
||||
triggerSQL := strings.ReplaceAll(`
|
||||
CREATE OR REPLACE FUNCTION conversation_next_sequence(p_session_id TEXT, p_terminal BOOLEAN DEFAULT FALSE)
|
||||
RETURNS BIGINT AS $$
|
||||
DECLARE next_value BIGINT;
|
||||
BEGIN
|
||||
INSERT INTO conversation_session_streams(session_id, watermark, terminal, updated_at)
|
||||
VALUES (p_session_id, 1, p_terminal, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(session_id) DO UPDATE SET
|
||||
watermark = conversation_session_streams.watermark + 1,
|
||||
terminal = conversation_session_streams.terminal OR EXCLUDED.terminal,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
RETURNING watermark INTO next_value;
|
||||
RETURN next_value;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE OR REPLACE FUNCTION conversation_safe_jsonb(value TEXT) RETURNS JSONB AS $$
|
||||
BEGIN
|
||||
RETURN COALESCE(NULLIF(value, ''), '{}')::jsonb;
|
||||
EXCEPTION WHEN others THEN
|
||||
RETURN '{}'::jsonb;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE OR REPLACE FUNCTION conversation_visible_content(value TEXT) RETURNS TEXT AS $$
|
||||
DECLARE
|
||||
result TEXT := value;
|
||||
open_pos INTEGER;
|
||||
close_offset INTEGER;
|
||||
close_pos INTEGER;
|
||||
BEGIN
|
||||
LOOP
|
||||
open_pos := strpos(result, '<kandev-system>');
|
||||
IF open_pos = 0 THEN
|
||||
EXIT;
|
||||
END IF;
|
||||
close_offset := strpos(
|
||||
substr(result, open_pos + length('<kandev-system>')),
|
||||
'</kandev-system>'
|
||||
);
|
||||
IF close_offset = 0 THEN
|
||||
EXIT;
|
||||
END IF;
|
||||
close_pos := open_pos + length('<kandev-system>') + close_offset - 1;
|
||||
result := substr(result, 1, open_pos - 1)
|
||||
|| ltrim(substr(result, close_pos + length('</kandev-system>')), E' \t\n\r');
|
||||
END LOOP;
|
||||
RETURN btrim(result);
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
CREATE OR REPLACE FUNCTION conversation_message_journal() RETURNS TRIGGER AS $$
|
||||
DECLARE seq BIGINT; event_name TEXT; source_row task_session_messages%ROWTYPE; deleted BOOLEAN;
|
||||
BEGIN
|
||||
IF TG_OP = 'UPDATE' AND OLD IS NOT DISTINCT FROM NEW THEN RETURN NEW; END IF;
|
||||
deleted := TG_OP = 'DELETE';
|
||||
IF deleted THEN source_row := OLD; event_name := 'message.deleted';
|
||||
ELSIF TG_OP = 'INSERT' THEN source_row := NEW; event_name := 'message.added';
|
||||
ELSE source_row := NEW; event_name := 'message.updated'; END IF;
|
||||
seq := conversation_next_sequence(source_row.task_session_id);
|
||||
INSERT INTO conversation_message_versions(session_id,message_id,row_sequence,task_id,author_type,created_at,tombstone,payload)
|
||||
VALUES (source_row.task_session_id,source_row.id,seq,NULLIF(source_row.task_id,''),source_row.author_type,source_row.created_at,deleted,
|
||||
CASE WHEN deleted THEN json_build_object('type',event_name,'session_id',source_row.task_session_id,'task_id',NULLIF(source_row.task_id,''),'message_id',source_row.id)::text
|
||||
ELSE json_build_object('type',event_name,'session_id',source_row.task_session_id,'task_id',NULLIF(source_row.task_id,''),
|
||||
'message_id',source_row.id,'turn_id',NULLIF(source_row.turn_id,''),'author_type',source_row.author_type,
|
||||
'content',conversation_visible_content(source_row.content),'message_type',source_row.type,
|
||||
'requests_input',source_row.requests_input,
|
||||
'created_at',to_char(source_row.created_at,'YYYY-MM-DD"T"HH24:MI:SS.US"Z"'),
|
||||
'updated_at',to_char(COALESCE(source_row.updated_at,source_row.created_at),'YYYY-MM-DD"T"HH24:MI:SS.US"Z"'),'prompt_index',source_row.prompt_seq,
|
||||
'metadata',__POSTGRES_CONVERSATION_METADATA__(source_row.metadata),
|
||||
'sender_task_id',CASE WHEN jsonb_typeof(conversation_safe_jsonb(source_row.metadata) -> 'sender_task_id') = 'string' THEN conversation_safe_jsonb(source_row.metadata) ->> 'sender_task_id' END)::text END);
|
||||
INSERT INTO conversation_session_events(session_id,sequence,event_id,event_type,task_id,payload,created_at)
|
||||
SELECT source_row.task_session_id,seq,source_row.task_session_id || ':' || seq,event_name,NULLIF(source_row.task_id,''),payload,CURRENT_TIMESTAMP
|
||||
FROM conversation_message_versions WHERE session_id=source_row.task_session_id AND message_id=source_row.id AND row_sequence=seq;
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
DROP TRIGGER IF EXISTS conversation_message_journal_trigger ON task_session_messages;
|
||||
CREATE TRIGGER conversation_message_journal_trigger AFTER INSERT OR UPDATE OR DELETE ON task_session_messages
|
||||
FOR EACH ROW EXECUTE FUNCTION conversation_message_journal();
|
||||
|
||||
CREATE OR REPLACE FUNCTION conversation_turn_journal() RETURNS TRIGGER AS $$
|
||||
DECLARE seq BIGINT; event_name TEXT; source_row task_session_turns%ROWTYPE; deleted BOOLEAN;
|
||||
BEGIN
|
||||
deleted := TG_OP = 'DELETE';
|
||||
IF deleted THEN source_row := OLD; event_name := 'session.turn.removed';
|
||||
ELSIF TG_OP = 'UPDATE' AND NOT (OLD.completed_at IS NULL AND NEW.completed_at IS NOT NULL) THEN RETURN NEW;
|
||||
ELSIF TG_OP = 'INSERT' THEN source_row := NEW; event_name := 'session.turn.started';
|
||||
ELSE source_row := NEW; event_name := 'session.turn.completed'; END IF;
|
||||
seq := conversation_next_sequence(source_row.task_session_id);
|
||||
INSERT INTO conversation_turn_versions(session_id,turn_id,row_sequence,task_id,started_at,tombstone,payload)
|
||||
VALUES (source_row.task_session_id,source_row.id,seq,NULLIF(source_row.task_id,''),source_row.started_at,deleted,
|
||||
CASE WHEN deleted THEN
|
||||
json_build_object('type',event_name,'session_id',source_row.task_session_id,'task_id',NULLIF(source_row.task_id,''),'id',source_row.id)::text
|
||||
ELSE
|
||||
json_build_object('type',event_name,'session_id',source_row.task_session_id,'task_id',NULLIF(source_row.task_id,''),
|
||||
'id',source_row.id,'started_at',to_char(source_row.started_at,'YYYY-MM-DD"T"HH24:MI:SS.US"Z"'),
|
||||
'completed_at',CASE WHEN source_row.completed_at IS NULL THEN NULL ELSE to_char(source_row.completed_at,'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') END,
|
||||
'created_at',to_char(source_row.created_at,'YYYY-MM-DD"T"HH24:MI:SS.US"Z"'),
|
||||
'updated_at',to_char(COALESCE(source_row.updated_at,source_row.completed_at,source_row.started_at),'YYYY-MM-DD"T"HH24:MI:SS.US"Z"'),
|
||||
'metadata',CASE WHEN source_row.completed_at IS NULL THEN NULL ELSE conversation_safe_jsonb(source_row.metadata) END,
|
||||
'had_output',CASE WHEN source_row.completed_at IS NULL THEN NULL ELSE EXISTS (
|
||||
SELECT 1 FROM task_session_messages output
|
||||
WHERE output.turn_id = source_row.id AND output.author_type = 'agent'
|
||||
AND (
|
||||
output.type IN ('tool_call','tool_edit','tool_read','tool_search','tool_execute',
|
||||
'agent_plan','todo','permission_request','clarification_request')
|
||||
OR (output.type IN ('message','content','') AND btrim(output.content) <> '')
|
||||
)) END)::text
|
||||
END);
|
||||
INSERT INTO conversation_session_events(session_id,sequence,event_id,event_type,task_id,payload,created_at)
|
||||
SELECT source_row.task_session_id,seq,source_row.task_session_id || ':' || seq,event_name,NULLIF(source_row.task_id,''),payload,CURRENT_TIMESTAMP
|
||||
FROM conversation_turn_versions
|
||||
WHERE session_id=source_row.task_session_id AND turn_id=source_row.id AND row_sequence=seq;
|
||||
IF deleted THEN RETURN OLD; END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
DROP TRIGGER IF EXISTS conversation_turn_journal_trigger ON task_session_turns;
|
||||
CREATE TRIGGER conversation_turn_journal_trigger AFTER INSERT OR UPDATE OR DELETE ON task_session_turns
|
||||
FOR EACH ROW EXECUTE FUNCTION conversation_turn_journal();
|
||||
|
||||
CREATE OR REPLACE FUNCTION conversation_session_delete_journal() RETURNS TRIGGER AS $$
|
||||
DECLARE seq BIGINT;
|
||||
BEGIN
|
||||
seq := conversation_next_sequence(OLD.id, TRUE);
|
||||
INSERT INTO conversation_session_events(session_id,sequence,event_id,event_type,task_id,payload,created_at)
|
||||
VALUES (OLD.id,seq,OLD.id || ':' || seq,'session.removed',NULLIF(OLD.task_id,''),
|
||||
json_build_object('type','session.removed','session_id',OLD.id,'task_id',NULLIF(OLD.task_id,''))::text,CURRENT_TIMESTAMP);
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
DROP TRIGGER IF EXISTS conversation_session_delete_trigger ON task_sessions;
|
||||
CREATE TRIGGER conversation_session_delete_trigger AFTER DELETE ON task_sessions
|
||||
FOR EACH ROW EXECUTE FUNCTION conversation_session_delete_journal();
|
||||
`, postgresConversationMetadataToken+"(source_row.metadata)", postgresConversationMetadataExpr("source_row.metadata"))
|
||||
_, err := r.db.Exec(r.db.Rebind(triggerSQL))
|
||||
if err != nil {
|
||||
return fmt.Errorf("create PostgreSQL conversation journal triggers: %w", err)
|
||||
}
|
||||
if _, err := r.db.Exec(`
|
||||
UPDATE conversation_message_versions
|
||||
SET payload = jsonb_set(payload::jsonb, '{content}',
|
||||
to_jsonb(conversation_visible_content(payload::jsonb ->> 'content')))::text
|
||||
WHERE conversation_safe_jsonb(payload) ->> 'content' LIKE '%<kandev-system>%'
|
||||
`); err != nil {
|
||||
return fmt.Errorf("sanitize PostgreSQL conversation message journal: %w", err)
|
||||
}
|
||||
if _, err := r.db.Exec(`
|
||||
UPDATE conversation_session_events
|
||||
SET payload = jsonb_set(payload::jsonb, '{content}',
|
||||
to_jsonb(conversation_visible_content(payload::jsonb ->> 'content')))::text
|
||||
WHERE conversation_safe_jsonb(payload) ->> 'content' LIKE '%<kandev-system>%'
|
||||
`); err != nil {
|
||||
return fmt.Errorf("sanitize PostgreSQL conversation event journal: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/sysprompt"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
"github.com/kandev/kandev/internal/testutil"
|
||||
)
|
||||
|
||||
// TestPostgresConversationJournalRoundTrip exercises the PostgreSQL journal
|
||||
// triggers end to end: message insert/update sanitization through
|
||||
// conversation_visible_content, the meta schema-version marker, and the
|
||||
// version-guarded one-time migration. Skips unless KANDEV_TEST_POSTGRES_DSN is
|
||||
// set (CI runs it in postgres-boot).
|
||||
func TestPostgresConversationJournalRoundTrip(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("new repo: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
seedPostgresConversationJournal(t, repo, "task-pg-journal", "session-pg-journal", "turn-pg-journal")
|
||||
|
||||
content := "pre <kandev-system>hidden one\nline two</kandev-system> mid <kandev-system>two</kandev-system> post"
|
||||
message := &models.Message{
|
||||
ID: "message-pg-journal", TaskSessionID: "session-pg-journal", TaskID: "task-pg-journal",
|
||||
TurnID: "turn-pg-journal", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: content,
|
||||
Metadata: map[string]interface{}{
|
||||
"pending_id": "pending-pg", "status": "pending", "raw_content": "secret",
|
||||
},
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
want := sysprompt.StripSystemContent(content)
|
||||
|
||||
var eventPayload string
|
||||
if err := repo.db.Get(&eventPayload, `SELECT payload FROM conversation_session_events WHERE session_id = 'session-pg-journal' AND event_type = 'message.added'`); err != nil {
|
||||
t.Fatalf("read event payload: %v", err)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal([]byte(eventPayload), &payload); err != nil {
|
||||
t.Fatalf("decode event payload: %v", err)
|
||||
}
|
||||
metadata, ok := payload["metadata"].(map[string]interface{})
|
||||
if !ok || metadata["pending_id"] != "pending-pg" || metadata["status"] != "pending" {
|
||||
t.Fatalf("postgres journal metadata = %#v, want allowlisted presentation metadata", payload["metadata"])
|
||||
}
|
||||
if _, ok := metadata["raw_content"]; ok {
|
||||
t.Fatalf("postgres journal retained internal raw_content metadata: %#v", metadata)
|
||||
}
|
||||
got, ok := payload["content"].(string)
|
||||
if !ok || got != want || strings.Contains(got, "hidden") {
|
||||
t.Fatalf("postgres journal content = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
// The schema marker must be recorded so the one-time migration is not
|
||||
// re-run (and the full-corpus rewrite never repeats on later boots).
|
||||
version, err := repo.readConversationJournalMetaInt("schema.version")
|
||||
if err != nil {
|
||||
t.Fatalf("read schema version: %v", err)
|
||||
}
|
||||
if version < journalSchemaVersion {
|
||||
t.Fatalf("schema version = %d, want >= %d", version, journalSchemaVersion)
|
||||
}
|
||||
backfilled, err := repo.readConversationJournalMetaFlag(journalBackfillKey)
|
||||
if err != nil {
|
||||
t.Fatalf("read backfill flag: %v", err)
|
||||
}
|
||||
if !backfilled {
|
||||
t.Fatal("backfill flag not recorded after init")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresConversationJournalStripsPastSQLiteDepthLimit(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("new repo: %v", err)
|
||||
}
|
||||
seedPostgresConversationJournal(t, repo, "task-pg-strip-deep", "session-pg-strip-deep", "turn-pg-strip-deep")
|
||||
content := strings.Repeat("<kandev-system>secret</kandev-system>", 65) + "visible"
|
||||
message := &models.Message{
|
||||
ID: "message-pg-strip-deep", TaskSessionID: "session-pg-strip-deep", TaskID: "task-pg-strip-deep",
|
||||
TurnID: "turn-pg-strip-deep", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: content,
|
||||
}
|
||||
if err := repo.CreateMessage(context.Background(), message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
|
||||
for _, table := range []string{"conversation_message_versions", "conversation_session_events"} {
|
||||
var payload string
|
||||
orderColumn := "row_sequence"
|
||||
if table == "conversation_session_events" {
|
||||
orderColumn = "sequence"
|
||||
}
|
||||
query := "SELECT payload FROM " + table + " WHERE session_id = $1 ORDER BY " + orderColumn + " DESC LIMIT 1"
|
||||
if err := repo.db.Get(&payload, query, "session-pg-strip-deep"); err != nil {
|
||||
t.Fatalf("read %s payload: %v", table, err)
|
||||
}
|
||||
if strings.Contains(payload, "secret") || strings.Contains(payload, "<kandev-system>") {
|
||||
t.Fatalf("%s persisted system content: %s", table, payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresConversationJournalTurnDeletePreservesCutoffHistory(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("new repo: %v", err)
|
||||
}
|
||||
const (
|
||||
taskID = "task-pg-turn-delete"
|
||||
sessionID = "session-pg-turn-delete"
|
||||
turnID = "turn-pg-turn-delete"
|
||||
)
|
||||
seedPostgresConversationJournal(t, repo, taskID, sessionID, turnID)
|
||||
now := time.Now().UTC()
|
||||
if _, err := repo.db.Exec(repo.db.Rebind(`UPDATE task_session_turns SET completed_at = ?, updated_at = ? WHERE id = ?`), now, now, turnID); err != nil {
|
||||
t.Fatalf("complete turn: %v", err)
|
||||
}
|
||||
var cutoffBefore int64
|
||||
if err := repo.db.Get(&cutoffBefore, repo.db.Rebind(`SELECT watermark FROM conversation_session_streams WHERE session_id = ?`), sessionID); err != nil {
|
||||
t.Fatalf("read pre-delete cutoff: %v", err)
|
||||
}
|
||||
if _, err := repo.db.Exec(repo.db.Rebind(`DELETE FROM task_session_turns WHERE id = ?`), turnID); err != nil {
|
||||
t.Fatalf("delete turn: %v", err)
|
||||
}
|
||||
var cutoffAfter int64
|
||||
if err := repo.db.Get(&cutoffAfter, repo.db.Rebind(`SELECT watermark FROM conversation_session_streams WHERE session_id = ?`), sessionID); err != nil {
|
||||
t.Fatalf("read deletion cutoff: %v", err)
|
||||
}
|
||||
assertTurnLiveAtCutoff(t, repo, sessionID, turnID, cutoffBefore, true)
|
||||
assertTurnLiveAtCutoff(t, repo, sessionID, turnID, cutoffAfter, false)
|
||||
}
|
||||
|
||||
func seedPostgresConversationJournal(t *testing.T, repo *Repository, taskID, sessionID, turnID string) {
|
||||
t.Helper()
|
||||
now := time.Now().UTC()
|
||||
if _, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO tasks (id, workspace_id, title, created_at, updated_at)
|
||||
VALUES (?, '', 'test task', ?, ?)
|
||||
`), taskID, now, now); err != nil {
|
||||
t.Fatalf("seed task: %v", err)
|
||||
}
|
||||
if _, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_sessions (id, task_id, started_at, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`), sessionID, taskID, now, now); err != nil {
|
||||
t.Fatalf("seed session: %v", err)
|
||||
}
|
||||
if _, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_session_turns (id, task_session_id, task_id, started_at, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`), turnID, sessionID, taskID, now, now, now); err != nil {
|
||||
t.Fatalf("seed turn: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,652 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/sysprompt"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
func TestConversationJournalVersionsMutationsAndTerminalDeletion(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal", "session-journal", "turn-journal")
|
||||
message := &models.Message{
|
||||
ID: "message-journal", TaskSessionID: "session-journal", TaskID: "task-journal",
|
||||
TurnID: "turn-journal", AuthorType: models.MessageAuthorAgent,
|
||||
Type: models.MessageTypeMessage, Content: "<kandev-system>hidden</kandev-system>first",
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
message.Content = "<kandev-system>hidden-update</kandev-system>updated"
|
||||
if err := repo.UpdateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("update message: %v", err)
|
||||
}
|
||||
if err := repo.DeleteMessage(ctx, message.ID); err != nil {
|
||||
t.Fatalf("delete message: %v", err)
|
||||
}
|
||||
session, err := repo.GetTaskSession(ctx, "session-journal")
|
||||
if err != nil {
|
||||
t.Fatalf("load session: %v", err)
|
||||
}
|
||||
if err := repo.DeleteTaskSession(ctx, session); err != nil {
|
||||
t.Fatalf("delete session: %v", err)
|
||||
}
|
||||
|
||||
var terminal bool
|
||||
var watermark int64
|
||||
if err := repo.db.QueryRow(`SELECT watermark, terminal FROM conversation_session_streams WHERE session_id = 'session-journal'`).Scan(&watermark, &terminal); err != nil {
|
||||
t.Fatalf("read stream: %v", err)
|
||||
}
|
||||
if !terminal || watermark != 6 {
|
||||
t.Fatalf("stream = watermark %d terminal %v, want 6 true", watermark, terminal)
|
||||
}
|
||||
rows, err := repo.db.Query(`SELECT event_type FROM conversation_session_events WHERE session_id = 'session-journal' ORDER BY sequence`)
|
||||
if err != nil {
|
||||
t.Fatalf("list events: %v", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
var eventTypes []string
|
||||
for rows.Next() {
|
||||
var eventType string
|
||||
if err := rows.Scan(&eventType); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eventTypes = append(eventTypes, eventType)
|
||||
}
|
||||
want := []string{"session.turn.started", "message.added", "message.updated", "message.deleted", "session.turn.removed", "session.removed"}
|
||||
if len(eventTypes) != len(want) {
|
||||
t.Fatalf("event types = %v, want %v", eventTypes, want)
|
||||
}
|
||||
for index := range want {
|
||||
if eventTypes[index] != want[index] {
|
||||
t.Fatalf("event types = %v, want %v", eventTypes, want)
|
||||
}
|
||||
}
|
||||
var payloadBytes []byte
|
||||
if err := repo.db.Get(&payloadBytes, `SELECT payload FROM conversation_session_events WHERE session_id = 'session-journal' AND event_type = 'message.added'`); err != nil {
|
||||
t.Fatalf("read message event payload: %v", err)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(payloadBytes, &payload); err != nil {
|
||||
t.Fatalf("decode message event payload: %v", err)
|
||||
}
|
||||
if payload["type"] != "message.added" || payload["session_id"] != "session-journal" {
|
||||
t.Fatalf("message event identity = %#v", payload)
|
||||
}
|
||||
if content, ok := payload["content"].(string); !ok || content != "first" || strings.Contains(content, "hidden") {
|
||||
t.Fatalf("journal leaked system content: %#v", payload["content"])
|
||||
}
|
||||
createdAt, ok := payload["created_at"].(string)
|
||||
if !ok {
|
||||
t.Fatalf("message created_at = %#v", payload["created_at"])
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339Nano, createdAt); err != nil {
|
||||
t.Fatalf("message created_at is not RFC3339: %q: %v", createdAt, err)
|
||||
}
|
||||
var tombstone bool
|
||||
if err := repo.db.QueryRow(`SELECT tombstone FROM conversation_message_versions WHERE session_id = 'session-journal' AND message_id = 'message-journal' ORDER BY row_sequence DESC LIMIT 1`).Scan(&tombstone); err != nil {
|
||||
t.Fatalf("read message tombstone: %v", err)
|
||||
}
|
||||
if !tombstone {
|
||||
t.Fatal("latest message version is not a tombstone")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalRollsBackWithSourceMutation(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal-rollback", "session-journal-rollback", "turn-journal-rollback")
|
||||
tx, err := repo.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
_, err = tx.ExecContext(ctx, repo.db.Rebind(`
|
||||
INSERT INTO task_session_messages
|
||||
(id, task_session_id, task_id, turn_id, author_type, content, type, metadata, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, 'agent', 'rolled back', 'message', '{}', ?, ?)
|
||||
`), "message-rollback", "session-journal-rollback", "task-journal-rollback", "turn-journal-rollback", now, now)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
t.Fatalf("insert source row: %v", err)
|
||||
}
|
||||
if err := tx.Rollback(); err != nil {
|
||||
t.Fatalf("rollback: %v", err)
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := repo.db.Get(&count, `SELECT COUNT(*) FROM conversation_session_events WHERE session_id = 'session-journal-rollback' AND event_type = 'message.added'`); err != nil {
|
||||
t.Fatalf("count rolled-back event: %v", err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Fatalf("rolled-back mutation left %d event rows", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalStripsMultiLineAndMultiBlockSystemContent(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal-strip", "session-journal-strip", "turn-journal-strip")
|
||||
content := "pre <kandev-system>hidden one\nline two</kandev-system> mid\n<kandev-system>hidden2</kandev-system> post"
|
||||
message := &models.Message{
|
||||
ID: "message-strip", TaskSessionID: "session-journal-strip", TaskID: "task-journal-strip",
|
||||
TurnID: "turn-journal-strip", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: content,
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
want := sysprompt.StripSystemContent(content)
|
||||
|
||||
var versionContent string
|
||||
if err := repo.db.Get(&versionContent, `SELECT json_extract(payload, '$.content') FROM conversation_message_versions WHERE message_id = 'message-strip'`); err != nil {
|
||||
t.Fatalf("read message version content: %v", err)
|
||||
}
|
||||
if versionContent != want {
|
||||
t.Fatalf("version content = %q, want %q", versionContent, want)
|
||||
}
|
||||
var eventContent string
|
||||
if err := repo.db.Get(&eventContent, `SELECT json_extract(payload, '$.content') FROM conversation_session_events WHERE session_id = 'session-journal-strip' AND event_type = 'message.added'`); err != nil {
|
||||
t.Fatalf("read message event content: %v", err)
|
||||
}
|
||||
if eventContent != want {
|
||||
t.Fatalf("event content = %q, want %q", eventContent, want)
|
||||
}
|
||||
|
||||
// The update trigger strips the same way.
|
||||
message.Content = "<kandev-system>a</kandev-system>\nupdated <kandev-system>b</kandev-system><kandev-system>c</kandev-system> end"
|
||||
if err := repo.UpdateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("update message: %v", err)
|
||||
}
|
||||
wantUpdated := sysprompt.StripSystemContent(message.Content)
|
||||
if err := repo.db.Get(&versionContent, `SELECT json_extract(payload, '$.content') FROM conversation_message_versions WHERE message_id = 'message-strip' ORDER BY row_sequence DESC LIMIT 1`); err != nil {
|
||||
t.Fatalf("read updated message version content: %v", err)
|
||||
}
|
||||
if versionContent != wantUpdated {
|
||||
t.Fatalf("updated version content = %q, want %q", versionContent, wantUpdated)
|
||||
}
|
||||
if err := repo.db.Get(&eventContent, `SELECT json_extract(payload, '$.content') FROM conversation_session_events WHERE session_id = 'session-journal-strip' AND event_type = 'message.updated'`); err != nil {
|
||||
t.Fatalf("read updated message event content: %v", err)
|
||||
}
|
||||
if eventContent != wantUpdated {
|
||||
t.Fatalf("updated event content = %q, want %q", eventContent, wantUpdated)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalStripIgnoresClosingTagWithoutOpeningTag(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal-closing-tag", "session-journal-closing-tag", "turn-journal-closing-tag")
|
||||
content := "visible</kandev-system>tail"
|
||||
message := &models.Message{
|
||||
ID: "message-closing-tag", TaskSessionID: "session-journal-closing-tag",
|
||||
TaskID: "task-journal-closing-tag", TurnID: "turn-journal-closing-tag",
|
||||
AuthorType: models.MessageAuthorUser, Type: models.MessageTypeMessage, Content: content,
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
var eventContent string
|
||||
if err := repo.db.Get(&eventContent, `SELECT json_extract(payload, '$.content') FROM conversation_session_events WHERE session_id = 'session-journal-closing-tag' AND event_type = 'message.added'`); err != nil {
|
||||
t.Fatalf("read message event content: %v", err)
|
||||
}
|
||||
if eventContent != content {
|
||||
t.Fatalf("event content = %q, want %q", eventContent, content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalStripFailsClosedPastDepthLimit(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal-strip-deep", "session-journal-strip-deep", "turn-journal-strip-deep")
|
||||
content := strings.Repeat("<kandev-system>secret</kandev-system>", 65) + "visible"
|
||||
message := &models.Message{
|
||||
ID: "message-strip-deep", TaskSessionID: "session-journal-strip-deep", TaskID: "task-journal-strip-deep",
|
||||
TurnID: "turn-journal-strip-deep", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: content,
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
|
||||
for _, table := range []string{"conversation_message_versions", "conversation_session_events"} {
|
||||
var payload string
|
||||
query := "SELECT payload FROM " + table + " WHERE session_id = ? ORDER BY row_sequence DESC LIMIT 1"
|
||||
if table == "conversation_session_events" {
|
||||
query = "SELECT payload FROM " + table + " WHERE session_id = ? AND event_type = 'message.added' ORDER BY sequence DESC LIMIT 1"
|
||||
}
|
||||
if err := repo.db.Get(&payload, repo.db.Rebind(query), "session-journal-strip-deep"); err != nil {
|
||||
t.Fatalf("read %s payload: %v", table, err)
|
||||
}
|
||||
if strings.Contains(payload, "secret") || strings.Contains(payload, "<kandev-system>") {
|
||||
t.Fatalf("%s persisted system content past sanitizer depth: %s", table, payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestConversationJournalPreservesPresentationMetadata(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
seedForMsgTest(t, repo, "task-presentation-metadata", "session-presentation-metadata", "turn-presentation-metadata")
|
||||
message := &models.Message{
|
||||
ID: "message-presentation-metadata", TaskSessionID: "session-presentation-metadata",
|
||||
TaskID: "task-presentation-metadata", TurnID: "turn-presentation-metadata",
|
||||
AuthorType: models.MessageAuthorAgent, Type: models.MessageTypeClarificationRequest,
|
||||
Content: "Question", RequestsInput: true,
|
||||
Metadata: map[string]any{
|
||||
"agent_disconnected": true,
|
||||
"agent_name": "Mock",
|
||||
"command": "/usr/local/bin/mock-agent",
|
||||
"completed_at": "2026-09-15T09:45:59.774503743Z",
|
||||
"error": "must-not-be-dropped",
|
||||
"exit_code": 0,
|
||||
"has_hidden_prompts": false,
|
||||
"is_resuming": true,
|
||||
"pending_id": "pending-1",
|
||||
"question": map[string]any{"id": "question-1", "prompt": "Choose"},
|
||||
"raw_content": "must-not-be-copied",
|
||||
"script_type": "agent_boot",
|
||||
"started_at": "2026-09-15T09:45:58.245720115Z",
|
||||
},
|
||||
}
|
||||
if err := repo.CreateMessage(context.Background(), message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
|
||||
var payload string
|
||||
if err := repo.db.Get(&payload, `SELECT payload FROM conversation_session_events WHERE session_id = ? AND event_type = 'message.added'`, message.TaskSessionID); err != nil {
|
||||
t.Fatalf("read event payload: %v", err)
|
||||
}
|
||||
var event map[string]any
|
||||
if err := json.Unmarshal([]byte(payload), &event); err != nil {
|
||||
t.Fatalf("decode event payload: %v", err)
|
||||
}
|
||||
if event["message_type"] != string(models.MessageTypeClarificationRequest) || event["requests_input"] != float64(1) {
|
||||
t.Fatalf("message presentation fields = %#v", event)
|
||||
}
|
||||
metadata, ok := event["metadata"].(map[string]any)
|
||||
if !ok || metadata["pending_id"] != "pending-1" || metadata["agent_disconnected"] != true || metadata["has_hidden_prompts"] != false {
|
||||
t.Fatalf("message metadata = %#v", event["metadata"])
|
||||
}
|
||||
for key, want := range map[string]any{
|
||||
"agent_name": "Mock", "command": "/usr/local/bin/mock-agent", "completed_at": "2026-09-15T09:45:59.774503743Z",
|
||||
"error": "must-not-be-dropped", "exit_code": float64(0), "is_resuming": true, "script_type": "agent_boot",
|
||||
"started_at": "2026-09-15T09:45:58.245720115Z",
|
||||
} {
|
||||
if metadata[key] != want {
|
||||
t.Fatalf("message metadata[%q] = %#v, want %#v", key, metadata[key], want)
|
||||
}
|
||||
}
|
||||
if _, exists := metadata["raw_content"]; exists {
|
||||
t.Fatalf("private message metadata leaked: %#v", metadata)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalStripKeepsRawTextWhenNoWellFormedBlock(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal-strip-raw", "session-journal-strip-raw", "turn-journal-strip-raw")
|
||||
// Shapes where the Go helper has no full match: an unterminated opening
|
||||
// tag and a stray closing tag before the first opening tag. The SQLite
|
||||
// strip must keep the raw text (never NULL), byte-identical to Go.
|
||||
cases := map[string]string{
|
||||
"unterminated-open": "visible <kandev-system>never closed",
|
||||
"stray-close": "</kandev-system>before <kandev-system>closed</kandev-system> after",
|
||||
"deep-nesting": "<kandev-system>" + strings.Repeat("<kandev-system>", 70) + "x" + strings.Repeat("</kandev-system>", 70) + "</kandev-system>tail",
|
||||
}
|
||||
for name, content := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
id := "message-strip-raw-" + name
|
||||
message := &models.Message{
|
||||
ID: id, TaskSessionID: "session-journal-strip-raw", TaskID: "task-journal-strip-raw",
|
||||
TurnID: "turn-journal-strip-raw", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: content,
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
var versionContent *string
|
||||
if err := repo.db.Get(&versionContent, `SELECT json_extract(payload, '$.content') FROM conversation_message_versions WHERE message_id = ?`, id); err != nil {
|
||||
t.Fatalf("read message version content: %v", err)
|
||||
}
|
||||
if versionContent == nil {
|
||||
t.Fatalf("strip returned NULL for %q", content)
|
||||
}
|
||||
want := sysprompt.StripSystemContent(content)
|
||||
if name != "deep-nesting" && *versionContent != want {
|
||||
t.Fatalf("version content = %q, want %q", *versionContent, want)
|
||||
}
|
||||
if strings.Contains(*versionContent, "<kandev-system>") && name == "unterminated-open" {
|
||||
// Unterminated opener text is preserved verbatim.
|
||||
if *versionContent != content {
|
||||
t.Fatalf("unterminated content = %q, want raw %q", *versionContent, content)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalSanitizeMigrationRewritesLegacyRows(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal-migrate", "session-journal-migrate", "turn-journal-migrate")
|
||||
legacy := "<kandev-system>old\nsecret</kandev-system>kept <kandev-system>second</kandev-system> text"
|
||||
message := &models.Message{
|
||||
ID: "message-legacy", TaskSessionID: "session-journal-migrate", TaskID: "task-journal-migrate",
|
||||
TurnID: "turn-journal-migrate", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: "placeholder",
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
// Simulate rows written before the sanitize migration existed.
|
||||
if _, err := repo.db.Exec(`
|
||||
UPDATE conversation_message_versions SET payload = json_set(payload, '$.content', ?) WHERE message_id = 'message-legacy';
|
||||
`, legacy); err != nil {
|
||||
t.Fatalf("seed legacy version payload: %v", err)
|
||||
}
|
||||
if _, err := repo.db.Exec(`
|
||||
UPDATE conversation_session_events SET payload = json_set(payload, '$.content', ?) WHERE session_id = 'session-journal-migrate' AND event_type = 'message.added';
|
||||
`, legacy); err != nil {
|
||||
t.Fatalf("seed legacy event payload: %v", err)
|
||||
}
|
||||
|
||||
if err := repo.initSQLiteConversationJournalTriggers(); err != nil {
|
||||
t.Fatalf("re-init triggers (runs sanitize migration): %v", err)
|
||||
}
|
||||
want := sysprompt.StripSystemContent(legacy)
|
||||
var versionContent string
|
||||
if err := repo.db.Get(&versionContent, `SELECT json_extract(payload, '$.content') FROM conversation_message_versions WHERE message_id = 'message-legacy'`); err != nil {
|
||||
t.Fatalf("read migrated version content: %v", err)
|
||||
}
|
||||
if versionContent != want {
|
||||
t.Fatalf("migrated version content = %q, want %q", versionContent, want)
|
||||
}
|
||||
var eventContent string
|
||||
if err := repo.db.Get(&eventContent, `SELECT json_extract(payload, '$.content') FROM conversation_session_events WHERE session_id = 'session-journal-migrate' AND event_type = 'message.added'`); err != nil {
|
||||
t.Fatalf("read migrated event content: %v", err)
|
||||
}
|
||||
if eventContent != want {
|
||||
t.Fatalf("migrated event content = %q, want %q", eventContent, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalTurnDeletePreservesHistoryWithTombstone(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
seedForMsgTest(t, repo, "task-journal-turn-del", "session-journal-turn-del", "turn-journal-turn-del")
|
||||
|
||||
now := time.Now().UTC()
|
||||
if _, err := repo.db.Exec(repo.db.Rebind(`UPDATE task_session_turns SET completed_at = ?, updated_at = ? WHERE id = ?`), now, now, "turn-journal-turn-del"); err != nil {
|
||||
t.Fatalf("complete turn: %v", err)
|
||||
}
|
||||
var cutoffBefore int64
|
||||
if err := repo.db.Get(&cutoffBefore, `SELECT watermark FROM conversation_session_streams WHERE session_id = 'session-journal-turn-del'`); err != nil {
|
||||
t.Fatalf("read pre-delete cutoff: %v", err)
|
||||
}
|
||||
|
||||
if _, err := repo.db.Exec(repo.db.Rebind(`DELETE FROM task_session_turns WHERE id = ?`), "turn-journal-turn-del"); err != nil {
|
||||
t.Fatalf("delete turn: %v", err)
|
||||
}
|
||||
var cutoffAfter int64
|
||||
if err := repo.db.Get(&cutoffAfter, `SELECT watermark FROM conversation_session_streams WHERE session_id = 'session-journal-turn-del'`); err != nil {
|
||||
t.Fatalf("read deletion cutoff: %v", err)
|
||||
}
|
||||
if cutoffAfter != cutoffBefore+1 {
|
||||
t.Fatalf("turn delete watermark = %d, want %d", cutoffAfter, cutoffBefore+1)
|
||||
}
|
||||
|
||||
var versions int
|
||||
if err := repo.db.Get(&versions, `SELECT COUNT(*) FROM conversation_turn_versions WHERE turn_id = 'turn-journal-turn-del'`); err != nil {
|
||||
t.Fatalf("count turn versions: %v", err)
|
||||
}
|
||||
if versions != 3 {
|
||||
t.Fatalf("turn version count = %d, want two live versions plus tombstone", versions)
|
||||
}
|
||||
var tombstoneSequence int64
|
||||
if err := repo.db.Get(&tombstoneSequence, `
|
||||
SELECT row_sequence FROM conversation_turn_versions
|
||||
WHERE turn_id = 'turn-journal-turn-del' AND tombstone = TRUE
|
||||
`); err != nil {
|
||||
t.Fatalf("read deletion tombstone: %v", err)
|
||||
}
|
||||
if tombstoneSequence != cutoffAfter {
|
||||
t.Fatalf("tombstone sequence = %d, want deletion cutoff %d", tombstoneSequence, cutoffAfter)
|
||||
}
|
||||
|
||||
assertTurnLiveAtCutoff(t, repo, "session-journal-turn-del", "turn-journal-turn-del", cutoffBefore, true)
|
||||
assertTurnLiveAtCutoff(t, repo, "session-journal-turn-del", "turn-journal-turn-del", cutoffAfter, false)
|
||||
|
||||
var removalEvent string
|
||||
if err := repo.db.Get(&removalEvent, `SELECT event_type FROM conversation_session_events WHERE session_id = 'session-journal-turn-del' ORDER BY sequence DESC LIMIT 1`); err != nil {
|
||||
t.Fatalf("read last event type: %v", err)
|
||||
}
|
||||
if removalEvent != "session.turn.removed" {
|
||||
t.Fatalf("last event type = %q, want session.turn.removed", removalEvent)
|
||||
}
|
||||
}
|
||||
|
||||
func assertTurnLiveAtCutoff(t *testing.T, repo *Repository, sessionID, turnID string, cutoff int64, want bool) {
|
||||
t.Helper()
|
||||
var count int
|
||||
if err := repo.db.Get(&count, repo.db.Rebind(`
|
||||
WITH ranked AS (
|
||||
SELECT tombstone,
|
||||
ROW_NUMBER() OVER (PARTITION BY turn_id ORDER BY row_sequence DESC) AS version_rank
|
||||
FROM conversation_turn_versions
|
||||
WHERE session_id = ? AND turn_id = ? AND row_sequence <= ?
|
||||
)
|
||||
SELECT COUNT(*) FROM ranked WHERE version_rank = 1 AND tombstone = FALSE
|
||||
`), sessionID, turnID, cutoff); err != nil {
|
||||
t.Fatalf("query turn at cutoff %d: %v", cutoff, err)
|
||||
}
|
||||
if got := count == 1; got != want {
|
||||
t.Fatalf("turn live at cutoff %d = %v, want %v", cutoff, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalBackfillsExistingRowsIdempotently(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
seedForMsgTest(t, repo, "task-backfill", "session-backfill", "turn-backfill")
|
||||
message := &models.Message{
|
||||
ID: "message-backfill", TaskSessionID: "session-backfill", TaskID: "task-backfill",
|
||||
TurnID: "turn-backfill", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: "<kandev-system>hidden</kandev-system>existing",
|
||||
}
|
||||
if err := repo.CreateMessage(context.Background(), message); err != nil {
|
||||
t.Fatalf("create source message: %v", err)
|
||||
}
|
||||
if _, err := repo.db.Exec(`
|
||||
DELETE FROM conversation_session_events;
|
||||
DELETE FROM conversation_message_versions;
|
||||
DELETE FROM conversation_turn_versions;
|
||||
DELETE FROM conversation_session_streams;
|
||||
`); err != nil {
|
||||
t.Fatalf("clear journal: %v", err)
|
||||
}
|
||||
|
||||
if err := repo.backfillConversationJournal(); err != nil {
|
||||
t.Fatalf("backfill conversation journal: %v", err)
|
||||
}
|
||||
if err := repo.backfillConversationJournal(); err != nil {
|
||||
t.Fatalf("repeat conversation journal backfill: %v", err)
|
||||
}
|
||||
var eventCount int
|
||||
if err := repo.db.Get(&eventCount, `SELECT COUNT(*) FROM conversation_session_events WHERE session_id = 'session-backfill'`); err != nil {
|
||||
t.Fatalf("count backfill events: %v", err)
|
||||
}
|
||||
if eventCount != 2 {
|
||||
t.Fatalf("backfill event count = %d, want 2", eventCount)
|
||||
}
|
||||
var content string
|
||||
if err := repo.db.Get(&content, `SELECT json_extract(payload, '$.content') FROM conversation_message_versions WHERE message_id = 'message-backfill'`); err != nil {
|
||||
t.Fatalf("read backfill message content: %v", err)
|
||||
}
|
||||
if content != "existing" {
|
||||
t.Fatalf("backfill content = %q, want existing", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalSenderTaskIDRequiresString(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal-sender", "session-journal-sender", "turn-journal-sender")
|
||||
if err := repo.CreateMessage(ctx, &models.Message{
|
||||
ID: "message-sender-numeric", TaskSessionID: "session-journal-sender", TaskID: "task-journal-sender",
|
||||
TurnID: "turn-journal-sender", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: "numeric",
|
||||
Metadata: map[string]any{"sender_task_id": 42},
|
||||
}); err != nil {
|
||||
t.Fatalf("create numeric-sender message: %v", err)
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, &models.Message{
|
||||
ID: "message-sender-string", TaskSessionID: "session-journal-sender", TaskID: "task-journal-sender",
|
||||
TurnID: "turn-journal-sender", AuthorType: models.MessageAuthorAgent,
|
||||
Type: models.MessageTypeMessage, Content: "string",
|
||||
Metadata: map[string]any{"sender_task_id": "task-sender-1"},
|
||||
}); err != nil {
|
||||
t.Fatalf("create string-sender message: %v", err)
|
||||
}
|
||||
|
||||
var numericSender any
|
||||
if err := repo.db.Get(&numericSender, `SELECT json_extract(payload, '$.sender_task_id') FROM conversation_message_versions WHERE message_id = 'message-sender-numeric'`); err != nil {
|
||||
t.Fatalf("read numeric-sender payload: %v", err)
|
||||
}
|
||||
if numericSender != nil {
|
||||
t.Fatalf("numeric sender_task_id stored as %v, want NULL", numericSender)
|
||||
}
|
||||
var stringSender string
|
||||
if err := repo.db.Get(&stringSender, `SELECT json_extract(payload, '$.sender_task_id') FROM conversation_message_versions WHERE message_id = 'message-sender-string'`); err != nil {
|
||||
t.Fatalf("read string-sender payload: %v", err)
|
||||
}
|
||||
if stringSender != "task-sender-1" {
|
||||
t.Fatalf("string sender_task_id = %q, want task-sender-1", stringSender)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBackfillSkipsStaleMessageImageWithSameContent pins the round-9 fix: a
|
||||
// concurrent update that only touches metadata (same content) must not let the
|
||||
// stale pre-update backfill image win a later version.
|
||||
func TestBackfillSkipsStaleMessageImageWithSameContent(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-backfill-stale", "session-backfill-stale", "turn-backfill-stale")
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
message := &models.Message{
|
||||
ID: "message-stale", TaskSessionID: "session-backfill-stale", TaskID: "task-backfill-stale",
|
||||
TurnID: "turn-backfill-stale", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: "same-content",
|
||||
Metadata: map[string]any{"sender_task_id": "sender-1"}, CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
// A concurrent metadata-only update journals its own version.
|
||||
message.Metadata = map[string]any{"sender_task_id": "sender-2"}
|
||||
message.UpdatedAt = now.Add(time.Minute)
|
||||
if err := repo.UpdateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("update message: %v", err)
|
||||
}
|
||||
var versionsAfterUpdate int
|
||||
if err := repo.db.Get(&versionsAfterUpdate, `SELECT COUNT(*) FROM conversation_message_versions WHERE message_id = 'message-stale'`); err != nil {
|
||||
t.Fatalf("count versions: %v", err)
|
||||
}
|
||||
|
||||
tx, err := repo.db.Beginx()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The backfill seed reflects the pre-update image (same content, older
|
||||
// timestamp): it must be skipped entirely.
|
||||
stale := conversationJournalMessageSeed{
|
||||
ID: "message-stale", TaskSessionID: "session-backfill-stale", TaskID: "task-backfill-stale",
|
||||
TurnID: "turn-backfill-stale", AuthorType: string(models.MessageAuthorUser),
|
||||
Content: "same-content", MessageType: string(models.MessageTypeMessage),
|
||||
Metadata: `{"sender_task_id":"sender-1"}`, CreatedAt: now, UpdatedAt: now, PromptIndex: message.PromptIndex,
|
||||
}
|
||||
if err := repo.backfillConversationMessage(tx, stale); err != nil {
|
||||
_ = tx.Rollback()
|
||||
t.Fatalf("backfill stale image: %v", err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var versions int
|
||||
if err := repo.db.Get(&versions, `SELECT COUNT(*) FROM conversation_message_versions WHERE message_id = 'message-stale'`); err != nil {
|
||||
t.Fatalf("count versions: %v", err)
|
||||
}
|
||||
if versions != versionsAfterUpdate {
|
||||
t.Fatalf("stale backfill added a version: %d -> %d", versionsAfterUpdate, versions)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConversationJournalSanitizeMigrationSkipsMalformedRows pins the
|
||||
// round-9 fix: a pre-existing malformed payload must not abort the boot-time
|
||||
// sanitize migration; the row is preserved verbatim so the mirror path can
|
||||
// turn it into a durable poison record.
|
||||
func TestConversationJournalSanitizeMigrationSkipsMalformedRows(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal-malformed", "session-journal-malformed", "turn-journal-malformed")
|
||||
message := &models.Message{
|
||||
ID: "message-malformed", TaskSessionID: "session-journal-malformed", TaskID: "task-journal-malformed",
|
||||
TurnID: "turn-journal-malformed", AuthorType: models.MessageAuthorUser,
|
||||
Type: models.MessageTypeMessage, Content: "placeholder",
|
||||
}
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
malformed := `{"content":"not valid json`
|
||||
if _, err := repo.db.Exec(`
|
||||
UPDATE conversation_message_versions SET payload = ? WHERE message_id = 'message-malformed';
|
||||
UPDATE conversation_session_events SET payload = ? WHERE session_id = 'session-journal-malformed' AND event_type = 'message.added';
|
||||
`, malformed, malformed); err != nil {
|
||||
t.Fatalf("seed malformed rows: %v", err)
|
||||
}
|
||||
|
||||
if err := repo.initSQLiteConversationJournalTriggers(); err != nil {
|
||||
t.Fatalf("re-init triggers (runs sanitize migration): %v", err)
|
||||
}
|
||||
var versionPayload string
|
||||
if err := repo.db.Get(&versionPayload, `SELECT payload FROM conversation_message_versions WHERE message_id = 'message-malformed'`); err != nil {
|
||||
t.Fatalf("read version payload: %v", err)
|
||||
}
|
||||
if versionPayload != malformed {
|
||||
t.Fatalf("malformed version payload was rewritten to %q", versionPayload)
|
||||
}
|
||||
var eventPayload string
|
||||
if err := repo.db.Get(&eventPayload, `SELECT payload FROM conversation_session_events WHERE session_id = 'session-journal-malformed' AND event_type = 'message.added'`); err != nil {
|
||||
t.Fatalf("read event payload: %v", err)
|
||||
}
|
||||
if eventPayload != malformed {
|
||||
t.Fatalf("malformed event payload was rewritten to %q", eventPayload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationJournalTurnCompletionIncludesHadOutput(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-journal-empty", "session-journal-empty", "turn-journal-empty")
|
||||
|
||||
if err := repo.CompleteTurn(ctx, "turn-journal-empty"); err != nil {
|
||||
t.Fatalf("complete turn: %v", err)
|
||||
}
|
||||
|
||||
var raw string
|
||||
if err := repo.db.Get(&raw, `
|
||||
SELECT payload FROM conversation_session_events
|
||||
WHERE session_id = 'session-journal-empty' AND event_type = 'session.turn.completed'
|
||||
`); err != nil {
|
||||
t.Fatalf("read completed turn event: %v", err)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &payload); err != nil {
|
||||
t.Fatalf("decode completed turn event: %v", err)
|
||||
}
|
||||
if hadOutput, ok := payload["had_output"].(bool); !ok || hadOutput {
|
||||
t.Fatalf("had_output = %#v, want false", payload["had_output"])
|
||||
}
|
||||
}
|
||||
@@ -455,10 +455,21 @@ func buildListMessagesQuery(driverName, sessionID string, opts models.ListMessag
|
||||
FROM task_session_messages
|
||||
WHERE task_session_id = ?`
|
||||
args := []interface{}{sessionID}
|
||||
if opts.AuthorType != "" {
|
||||
if len(opts.AuthorTypes) > 0 {
|
||||
placeholders := make([]string, len(opts.AuthorTypes))
|
||||
for index, author := range opts.AuthorTypes {
|
||||
placeholders[index] = "?"
|
||||
args = append(args, author)
|
||||
}
|
||||
query += " AND author_type IN (" + strings.Join(placeholders, ",") + ")"
|
||||
} else if opts.AuthorType != "" {
|
||||
query += " AND author_type = ?"
|
||||
args = append(args, opts.AuthorType)
|
||||
}
|
||||
if opts.TaskID != "" {
|
||||
query += " AND task_id = ?"
|
||||
args = append(args, opts.TaskID)
|
||||
}
|
||||
if cursor != nil {
|
||||
if opts.Before != "" {
|
||||
query += fmt.Sprintf(" AND (%s < %s OR (%s = %s AND id < ?))", nm, bound, nm, bound)
|
||||
|
||||
@@ -665,3 +665,38 @@ func ids(msgs []*models.Message) []string {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestListMessagesPaginatedAuthorTypesAndTaskFilter(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-page-filters", "session-page-filters", "turn-page-filters")
|
||||
now := time.Now().UTC()
|
||||
seeds := []*models.Message{
|
||||
{ID: "page-user-1", TaskID: "task-page-filters", TaskSessionID: "session-page-filters", TurnID: "turn-page-filters", AuthorType: models.MessageAuthorUser, Type: models.MessageTypeMessage, Content: "u1", CreatedAt: now},
|
||||
{ID: "page-agent-1", TaskID: "task-page-filters", TaskSessionID: "session-page-filters", TurnID: "turn-page-filters", AuthorType: models.MessageAuthorAgent, Type: models.MessageTypeMessage, Content: "a1", CreatedAt: now.Add(time.Second)},
|
||||
{ID: "page-user-2", TaskID: "task-page-filters", TaskSessionID: "session-page-filters", TurnID: "turn-page-filters", AuthorType: models.MessageAuthorUser, Type: models.MessageTypeMessage, Content: "u2", CreatedAt: now.Add(2 * time.Second)},
|
||||
}
|
||||
for _, message := range seeds {
|
||||
if err := repo.CreateMessage(ctx, message); err != nil {
|
||||
t.Fatalf("create %s: %v", message.ID, err)
|
||||
}
|
||||
}
|
||||
page := func(opts models.ListMessagesOptions) []string {
|
||||
t.Helper()
|
||||
opts.Sort = "asc"
|
||||
messages, _, err := repo.ListMessagesPaginated(ctx, "session-page-filters", opts)
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
ids := make([]string, 0, len(messages))
|
||||
for _, message := range messages {
|
||||
ids = append(ids, message.ID)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
require.Equal(t, []string{"page-user-1", "page-user-2"}, page(models.ListMessagesOptions{Limit: 10, AuthorTypes: []string{string(models.MessageAuthorUser)}}))
|
||||
require.Equal(t, []string{"page-user-1", "page-agent-1", "page-user-2"}, page(models.ListMessagesOptions{Limit: 10, AuthorTypes: []string{string(models.MessageAuthorUser), string(models.MessageAuthorAgent)}}))
|
||||
require.Equal(t, []string{"page-user-1", "page-user-2"}, page(models.ListMessagesOptions{Limit: 10, AuthorType: string(models.MessageAuthorUser)}))
|
||||
require.Empty(t, page(models.ListMessagesOptions{Limit: 10, TaskID: "task-other"}))
|
||||
require.Len(t, page(models.ListMessagesOptions{Limit: 10, TaskID: "task-page-filters"}), 3)
|
||||
}
|
||||
|
||||
@@ -516,9 +516,16 @@ func seedForMsgTest(t *testing.T, repo *Repository, taskID, sessionID, turnID st
|
||||
}
|
||||
_, err = repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT OR IGNORE INTO task_sessions
|
||||
(id, task_id, started_at, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`), sessionID, taskID, now, now)
|
||||
(id, task_id, queue_incarnation_id, started_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`), sessionID, taskID, "incarnation-"+sessionID, now, now)
|
||||
if err != nil && strings.Contains(err.Error(), "no column named queue_incarnation_id") {
|
||||
_, err = repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT OR IGNORE INTO task_sessions
|
||||
(id, task_id, started_at, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`), sessionID, taskID, now, now)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("seed session %s: %v", sessionID, err)
|
||||
}
|
||||
|
||||
@@ -825,19 +825,22 @@ func (s *Service) ListMessagesPaginated(ctx context.Context, req ListMessagesReq
|
||||
return nil, false, err
|
||||
}
|
||||
limit := req.Limit
|
||||
if limit <= 0 && (req.Before != "" || req.After != "" || req.Around != "" || req.AuthorType != "") {
|
||||
if limit <= 0 && (req.Before != "" || req.After != "" || req.Around != "" ||
|
||||
req.AuthorType != "" || len(req.AuthorTypes) > 0 || req.TaskID != "") {
|
||||
limit = DefaultMessagesPageSize
|
||||
}
|
||||
if limit > MaxMessagesPageSize {
|
||||
limit = MaxMessagesPageSize
|
||||
}
|
||||
return s.messages.ListMessagesPaginated(ctx, req.TaskSessionID, models.ListMessagesOptions{
|
||||
Limit: limit,
|
||||
Before: req.Before,
|
||||
After: req.After,
|
||||
Sort: req.Sort,
|
||||
AuthorType: req.AuthorType,
|
||||
Around: req.Around,
|
||||
Limit: limit,
|
||||
Before: req.Before,
|
||||
After: req.After,
|
||||
Sort: req.Sort,
|
||||
AuthorType: req.AuthorType,
|
||||
AuthorTypes: req.AuthorTypes,
|
||||
TaskID: req.TaskID,
|
||||
Around: req.Around,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -398,6 +399,49 @@ func TestListMessagesPaginatedClampsLimit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListMessagesPaginatedDefaultsLimitForAdditionalFilters(t *testing.T) {
|
||||
svc, _, repo := newMessageTestService(t)
|
||||
ctx := context.Background()
|
||||
for index := 0; index <= DefaultMessagesPageSize; index++ {
|
||||
seedMessage(t, repo, &models.Message{
|
||||
ID: fmt.Sprintf("filtered-%d", index),
|
||||
AuthorType: models.MessageAuthorUser,
|
||||
Content: fmt.Sprintf("message-%d", index),
|
||||
})
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
request ListMessagesRequest
|
||||
}{
|
||||
{
|
||||
name: "author types",
|
||||
request: ListMessagesRequest{
|
||||
TaskSessionID: "sess-msg",
|
||||
AuthorTypes: []string{string(models.MessageAuthorUser)},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "task id",
|
||||
request: ListMessagesRequest{
|
||||
TaskSessionID: "sess-msg",
|
||||
TaskID: "task-msg",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
page, hasMore, err := svc.ListMessagesPaginated(ctx, test.request)
|
||||
if err != nil {
|
||||
t.Fatalf("ListMessagesPaginated: %v", err)
|
||||
}
|
||||
if len(page) != DefaultMessagesPageSize || !hasMore {
|
||||
t.Fatalf("page = %d, hasMore = %v; want %d and true", len(page), hasMore, DefaultMessagesPageSize)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestListMessagesForPluginFiltersBySession(t *testing.T) {
|
||||
svc, _, repo := newMessageTestService(t)
|
||||
seedMessage(t, repo, &models.Message{ID: "m-plugin", Content: "visible"})
|
||||
|
||||
@@ -338,7 +338,11 @@ type ListMessagesRequest struct {
|
||||
After string
|
||||
Sort string
|
||||
AuthorType string
|
||||
Around string
|
||||
// AuthorTypes narrows by any listed author; when non-empty it takes
|
||||
// precedence over AuthorType. TaskID narrows the page to one task.
|
||||
AuthorTypes []string
|
||||
TaskID string
|
||||
Around string
|
||||
}
|
||||
|
||||
// CreateRepositoryScriptRequest contains the data for creating a repository script
|
||||
|
||||
@@ -90,6 +90,27 @@ func (s *Service) GetTaskSession(ctx context.Context, sessionID string) (*models
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// DeleteSessionAndPublishRemoval removes a session inside the repository's
|
||||
// transaction and publishes the terminal event only after that commit.
|
||||
// Internal rollback callers use this to keep the ordered stream authoritative.
|
||||
func (s *Service) DeleteSessionAndPublishRemoval(ctx context.Context, sessionID string) error {
|
||||
session, err := s.sessions.GetTaskSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.sessions.DeleteTaskSession(ctx, session); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.eventBus == nil {
|
||||
return nil
|
||||
}
|
||||
return s.eventBus.Publish(ctx, events.SessionRemoved, bus.NewEvent(
|
||||
events.SessionRemoved,
|
||||
"task-service",
|
||||
map[string]interface{}{sessionEventFieldSessionID: session.ID, sessionEventFieldTaskID: session.TaskID},
|
||||
))
|
||||
}
|
||||
|
||||
// GetExecutorRunningBySessionID returns the live executor row for sessionID,
|
||||
// or models.ErrExecutorRunningNotFound if the session has none (e.g. it
|
||||
// never started, or has since completed and been cleaned up). Exposed at
|
||||
|
||||
@@ -267,7 +267,21 @@ func (s *Service) RollbackReservedTurn(
|
||||
ctx context.Context,
|
||||
sessionID, turnID string,
|
||||
) (bool, error) {
|
||||
return s.turns.DeleteTurnIfUnreferenced(ctx, sessionID, turnID)
|
||||
turn, err := s.turns.GetTurn(ctx, turnID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if turn.TaskSessionID != sessionID {
|
||||
return false, nil
|
||||
}
|
||||
removed, err := s.turns.DeleteTurnIfUnreferenced(ctx, sessionID, turnID)
|
||||
if err != nil || !removed {
|
||||
return removed, err
|
||||
}
|
||||
if err := s.publishTurnEvent(events.TurnRemoved, turn, nil); err != nil {
|
||||
return true, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// createCompletedTurn persists a synthetic turn that is never observable as
|
||||
|
||||
@@ -253,6 +253,10 @@ func TestReservedTurnPublishesOnlyAfterAcceptanceAndRollsBackWhenEmpty(t *testin
|
||||
if _, err := repo.GetTurn(ctx, rejected.ID); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatalf("GetTurn(rolled back) error = %v, want sql.ErrNoRows", err)
|
||||
}
|
||||
removedEvents := eventBus.GetPublishedEvents()
|
||||
if len(removedEvents) != 1 || removedEvents[0].Type != events.TurnRemoved {
|
||||
t.Fatalf("rollback events = %#v, want one turn.removed event", removedEvents)
|
||||
}
|
||||
|
||||
accepted, err := svc.ReserveTurn(ctx, sessionID, recovery)
|
||||
if err != nil {
|
||||
|
||||
@@ -169,10 +169,12 @@ const (
|
||||
ActionWorkflowHistoryList = "workflow.history.list"
|
||||
|
||||
// Subscription actions
|
||||
ActionTaskSubscribe = "task.subscribe"
|
||||
ActionTaskUnsubscribe = "task.unsubscribe"
|
||||
ActionSessionSubscribe = "session.subscribe"
|
||||
ActionSessionUnsubscribe = "session.unsubscribe"
|
||||
ActionTaskSubscribe = "task.subscribe"
|
||||
ActionTaskUnsubscribe = "task.unsubscribe"
|
||||
ActionSessionSubscribe = "session.subscribe"
|
||||
ActionSessionUnsubscribe = "session.unsubscribe"
|
||||
ActionSessionAck = "session.ack"
|
||||
ActionSessionPoisonRequeue = "session.event.poison.requeue"
|
||||
// Focus signals are layered on top of subscriptions to indicate which
|
||||
// session the user is actively viewing (task details page or task panel),
|
||||
// vs merely subscribed (sidebar diff badges). Drives backend polling tier.
|
||||
@@ -248,6 +250,7 @@ const (
|
||||
ActionSessionMessageAdded = "session.message.added"
|
||||
ActionSessionMessageUpdated = "session.message.updated"
|
||||
ActionSessionMessageDeleted = "session.message.deleted"
|
||||
ActionSessionRemoved = "session.removed"
|
||||
ActionSessionStateChanged = "session.state_changed"
|
||||
ActionSessionPendingActionChanged = "session.pending_action_changed"
|
||||
ActionSessionActivityChanged = "session.activity_changed"
|
||||
@@ -258,6 +261,7 @@ const (
|
||||
ActionSessionAgentctlError = "session.agentctl_error"
|
||||
ActionSessionTurnStarted = "session.turn.started"
|
||||
ActionSessionTurnCompleted = "session.turn.completed"
|
||||
ActionSessionTurnRemoved = "session.turn.removed"
|
||||
ActionSessionAvailableCommands = "session.available_commands"
|
||||
ActionSessionModeChanged = "session.mode_changed"
|
||||
ActionSessionAgentCapabilities = "session.agent_capabilities"
|
||||
|
||||
@@ -85,6 +85,7 @@ export interface HostReact {
|
||||
createElement: ElementFactory;
|
||||
useState<Value>(initialValue: Value | (() => Value)): [Value, StateSetter<Value>];
|
||||
useEffect(effect: () => void | (() => void), dependencies?: readonly unknown[]): void;
|
||||
useLayoutEffect(effect: () => void | (() => void), dependencies?: readonly unknown[]): void;
|
||||
useMemo<Value>(factory: () => Value, dependencies: readonly unknown[]): Value;
|
||||
useCallback<Callback extends (...args: never[]) => unknown>(
|
||||
callback: Callback,
|
||||
@@ -291,19 +292,107 @@ export interface PluginRouteOptions {
|
||||
topbar?: boolean | PluginPageChrome;
|
||||
}
|
||||
|
||||
export interface PluginTaskPanelProps {
|
||||
panelId: string;
|
||||
export type PluginSessionKind = "managed" | "passthrough" | null;
|
||||
export type PluginConversationAuthor = "user" | "agent";
|
||||
export type PluginConversationSort = "asc" | "desc";
|
||||
|
||||
export interface PluginConversationMessage {
|
||||
id: string;
|
||||
taskId: string | null;
|
||||
sessionId: string;
|
||||
turnId?: string;
|
||||
authorType: PluginConversationAuthor;
|
||||
type: string;
|
||||
content: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
promptIndex?: number;
|
||||
senderTaskId?: string;
|
||||
}
|
||||
|
||||
export interface PluginConversationTurn {
|
||||
id: string;
|
||||
taskId: string | null;
|
||||
sessionId: string;
|
||||
startedAt: string;
|
||||
completedAt?: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface PluginSessionMessagesQuery {
|
||||
sessionId: string | null;
|
||||
taskId?: string | null;
|
||||
authorTypes?: readonly PluginConversationAuthor[];
|
||||
sort?: PluginConversationSort;
|
||||
pageSize?: number;
|
||||
}
|
||||
|
||||
export type PluginConversationErrorCode =
|
||||
| "unauthenticated"
|
||||
| "not_found"
|
||||
| "invalid_query"
|
||||
| "upstream_failure";
|
||||
|
||||
export interface PluginConversationError {
|
||||
code: PluginConversationErrorCode;
|
||||
message: string;
|
||||
retryable: boolean;
|
||||
}
|
||||
|
||||
export interface PluginSessionMessagesState {
|
||||
messages: readonly PluginConversationMessage[];
|
||||
loading: boolean;
|
||||
hydrated: boolean;
|
||||
loadingMore: boolean;
|
||||
error: PluginConversationError | null;
|
||||
hasMore: boolean;
|
||||
removed: boolean;
|
||||
loadMore(): Promise<number>;
|
||||
retry(): void;
|
||||
}
|
||||
|
||||
export interface PluginSessionTurnsState {
|
||||
turns: readonly PluginConversationTurn[];
|
||||
loading: boolean;
|
||||
hydrated: boolean;
|
||||
error: PluginConversationError | null;
|
||||
removed: boolean;
|
||||
retry(): void;
|
||||
}
|
||||
|
||||
export interface PluginConversationApi {
|
||||
useSessionMessages(query: PluginSessionMessagesQuery): PluginSessionMessagesState;
|
||||
useSessionTurns(sessionId: string | null, taskId?: string | null): PluginSessionTurnsState;
|
||||
useMessageFavorite(sessionId: string | null, messageId: string): boolean;
|
||||
}
|
||||
|
||||
export type PluginOpenMessageResult = { status: "accepted" | "unavailable" };
|
||||
|
||||
export interface PluginTaskPanelConversationCapability {
|
||||
openMessage(messageId: string): PluginOpenMessageResult;
|
||||
history: PluginConversationApi;
|
||||
}
|
||||
|
||||
export interface PluginTaskPanelContext {
|
||||
taskId: string;
|
||||
sessionId: string | null;
|
||||
sessionKind: PluginSessionKind;
|
||||
presentation: "desktop" | "mobile";
|
||||
}
|
||||
|
||||
export interface PluginTaskPanelProps extends PluginTaskPanelContext {
|
||||
panelId: string;
|
||||
conversation: PluginTaskPanelConversationCapability;
|
||||
}
|
||||
|
||||
export interface TaskPanelRegistration {
|
||||
id: string;
|
||||
title: string;
|
||||
titleKey?: string;
|
||||
icon?: PluginIcon;
|
||||
Component: Component<PluginTaskPanelProps>;
|
||||
mobileEnabled?: boolean;
|
||||
visible?(context: PluginTaskPanelContext): boolean;
|
||||
}
|
||||
|
||||
export interface PluginTaskMenuContext {
|
||||
@@ -485,6 +574,7 @@ interface PluginUIShape {
|
||||
PopoverTitle: unknown;
|
||||
PopoverTrigger: unknown;
|
||||
Progress: unknown;
|
||||
PromptMentionText: Component<{ text: string; interactive?: boolean }>;
|
||||
RichTextEditor: unknown;
|
||||
RichTextReadOnly: unknown;
|
||||
ScrollArea: unknown;
|
||||
@@ -544,7 +634,9 @@ export type SettingsSaveContributor = {
|
||||
};
|
||||
|
||||
export type PluginUIApi = {
|
||||
readonly [Name in keyof PluginUIShape]: HostComponent;
|
||||
readonly [Name in keyof PluginUIShape]: PluginUIShape[Name] extends Component<infer Props>
|
||||
? Component<Props>
|
||||
: HostComponent;
|
||||
};
|
||||
|
||||
export interface PluginToastApi {
|
||||
@@ -580,6 +672,7 @@ export interface PluginHostApi {
|
||||
React: HostReact;
|
||||
jsx: ElementFactory;
|
||||
ui: PluginUIApi;
|
||||
conversation: PluginConversationApi;
|
||||
i18n: PluginI18nApi;
|
||||
context: PluginContextApi;
|
||||
api: {
|
||||
|
||||
@@ -114,6 +114,7 @@ export const QuickChatContent = memo(function QuickChatContent({
|
||||
key={sessionId}
|
||||
pending={Boolean(pendingClarification)}
|
||||
messages={pendingClarificationGroup}
|
||||
agentDisconnected={panelState.session?.pending_action === null}
|
||||
onResolved={handleClarificationResolved}
|
||||
shortcutScopeRef={shortcutScopeRef}
|
||||
maxHeightVh={35}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { cleanup, render } from "@testing-library/react";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mockUnsubscribeSession = vi.hoisted(() => vi.fn());
|
||||
const mockSubscribeSession = vi.hoisted(() => vi.fn(() => mockUnsubscribeSession));
|
||||
|
||||
const providerState = vi.hoisted(() => ({
|
||||
quickChat: {
|
||||
isOpen: true,
|
||||
@@ -11,6 +14,7 @@ const providerState = vi.hoisted(() => ({
|
||||
activeTerminalTabId: null,
|
||||
},
|
||||
workspaces: { activeId: "ws-1" },
|
||||
connection: { status: "disconnected" as "connected" | "disconnected" },
|
||||
}));
|
||||
|
||||
vi.mock("@/components/state-provider", () => ({
|
||||
@@ -18,6 +22,9 @@ vi.mock("@/components/state-provider", () => ({
|
||||
}));
|
||||
vi.mock("@/hooks/domains/settings/use-settings-data", () => ({ useSettingsData: vi.fn() }));
|
||||
vi.mock("@/hooks/use-quick-chat-resync", () => ({ useQuickChatResync: vi.fn() }));
|
||||
vi.mock("@/lib/ws/connection", () => ({
|
||||
getWebSocketClient: () => ({ subscribeSession: mockSubscribeSession }),
|
||||
}));
|
||||
vi.mock("./quick-chat-modal", () => ({ QuickChatModal: () => null }));
|
||||
|
||||
import { captureQuickChatLauncherFocus } from "./quick-chat-focus";
|
||||
@@ -25,6 +32,10 @@ import { QuickChatProvider } from "./quick-chat-provider";
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
providerState.quickChat.isOpen = true;
|
||||
providerState.connection.status = "disconnected";
|
||||
mockSubscribeSession.mockClear();
|
||||
mockUnsubscribeSession.mockClear();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
@@ -52,4 +63,23 @@ describe("QuickChatProvider focus contract", () => {
|
||||
|
||||
expect(document.activeElement).toBe(launcher);
|
||||
});
|
||||
it("keeps Quick Chat sessions subscribed after the modal closes", () => {
|
||||
providerState.connection.status = "connected";
|
||||
const { rerender } = render(
|
||||
<QuickChatProvider>
|
||||
<button type="button">launch</button>
|
||||
</QuickChatProvider>,
|
||||
);
|
||||
|
||||
expect(mockSubscribeSession).toHaveBeenCalledWith("chat-1");
|
||||
|
||||
providerState.quickChat.isOpen = false;
|
||||
rerender(
|
||||
<QuickChatProvider>
|
||||
<button type="button">launch</button>
|
||||
</QuickChatProvider>,
|
||||
);
|
||||
|
||||
expect(mockUnsubscribeSession).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ import { useEffect, useRef, useSyncExternalStore } from "react";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
import { useSettingsData } from "@/hooks/domains/settings/use-settings-data";
|
||||
import { useQuickChatResync } from "@/hooks/use-quick-chat-resync";
|
||||
import { getWebSocketClient } from "@/lib/ws/connection";
|
||||
import { restoreQuickChatLauncherFocus } from "./quick-chat-focus";
|
||||
import { QuickChatModal } from "./quick-chat-modal";
|
||||
|
||||
@@ -68,6 +69,22 @@ export function QuickChatProvider({ children }: { children: React.ReactNode }) {
|
||||
// Quick chats are shared across devices: re-read the server's list whenever
|
||||
// the socket connects so tabs opened or closed elsewhere show up here too.
|
||||
useQuickChatResync(activeWorkspace);
|
||||
const connectionStatus = useAppStore((s) => s.connection.status);
|
||||
|
||||
// Keep persisted Quick Chat sessions subscribed while their modal is closed.
|
||||
// The sidebar activity indicator must receive the completion event that arms
|
||||
// the unseen marker after the conversation view unmounts.
|
||||
useEffect(() => {
|
||||
if (connectionStatus !== "connected") return;
|
||||
const client = getWebSocketClient();
|
||||
if (!client) return;
|
||||
const unsubscribes = quickChatSessions.map((session) =>
|
||||
client.subscribeSession(session.sessionId),
|
||||
);
|
||||
return () => {
|
||||
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
||||
};
|
||||
}, [connectionStatus, quickChatSessions]);
|
||||
|
||||
const workspaceId = getWorkspaceId({
|
||||
sessions: quickChatSessions,
|
||||
|
||||
@@ -30,6 +30,8 @@ type ClarificationInputOverlayProps = {
|
||||
onResolved: () => void;
|
||||
shortcutScopeRef: RefObject<HTMLElement | null>;
|
||||
keyboardShortcutsEnabled?: boolean;
|
||||
/** True when the session no longer has a live clarification waiter. */
|
||||
agentDisconnected?: boolean;
|
||||
// Called when the user presses Escape. Unlike Skip, this must not answer or
|
||||
// reject the bundle — it only dismisses the UI (e.g. collapses the panel).
|
||||
// The question stays pending and the agent stays blocked.
|
||||
@@ -393,6 +395,7 @@ type CarouselBodyProps = {
|
||||
setCustomDrafts: React.Dispatch<React.SetStateAction<Record<string, string>>>;
|
||||
allAnswered: boolean;
|
||||
isSubmitting: boolean;
|
||||
agentDisconnected: boolean;
|
||||
shortcutScopeRef: RefObject<HTMLElement | null>;
|
||||
armedEventRef: RefObject<KeyboardEvent | null>;
|
||||
keyboardShortcutsEnabled: boolean;
|
||||
@@ -495,7 +498,6 @@ function buildQuestionHandlers(ctx: QuestionHandlerCtx): QuestionHandlers {
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function ClarificationCarouselBody({
|
||||
sortedMessages,
|
||||
meta,
|
||||
@@ -506,6 +508,7 @@ function ClarificationCarouselBody({
|
||||
setCustomDrafts,
|
||||
allAnswered,
|
||||
isSubmitting,
|
||||
agentDisconnected,
|
||||
shortcutScopeRef,
|
||||
armedEventRef,
|
||||
keyboardShortcutsEnabled,
|
||||
@@ -513,9 +516,11 @@ function ClarificationCarouselBody({
|
||||
onDismiss,
|
||||
}: CarouselBodyProps) {
|
||||
const total = sortedMessages.length;
|
||||
const showAgentDisconnectedAtTop = sortedMessages.some(
|
||||
(m) => (m.metadata as ClarificationRequestMetadata | undefined)?.agent_disconnected === true,
|
||||
);
|
||||
const showAgentDisconnectedAtTop =
|
||||
agentDisconnected ||
|
||||
sortedMessages.some(
|
||||
(m) => (m.metadata as ClarificationRequestMetadata | undefined)?.agent_disconnected === true,
|
||||
);
|
||||
const isSingleQuestion = total === 1;
|
||||
|
||||
if (!meta) return null;
|
||||
@@ -580,12 +585,14 @@ function ClarificationCarouselBody({
|
||||
);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function -- coordinates the complete clarification overlay lifecycle.
|
||||
export function ClarificationInputOverlay({
|
||||
messages,
|
||||
onResolved,
|
||||
onOutcome,
|
||||
shortcutScopeRef,
|
||||
keyboardShortcutsEnabled = true,
|
||||
agentDisconnected = false,
|
||||
onDismiss,
|
||||
onCollapse,
|
||||
collapseContentId,
|
||||
@@ -667,6 +674,7 @@ export function ClarificationInputOverlay({
|
||||
setCustomDrafts={setCustomDrafts}
|
||||
allAnswered={allAnswered}
|
||||
isSubmitting={isSubmitting}
|
||||
agentDisconnected={agentDisconnected}
|
||||
shortcutScopeRef={shortcutScopeRef}
|
||||
armedEventRef={armedEventRef}
|
||||
keyboardShortcutsEnabled={keyboardShortcutsEnabled}
|
||||
|
||||
@@ -16,18 +16,22 @@ type ClarificationPanelSectionProps = {
|
||||
messages: readonly Message[] | null | undefined;
|
||||
onResolved: () => void;
|
||||
shortcutScopeRef: RefObject<HTMLElement | null>;
|
||||
/**
|
||||
* True when the session no longer has a live clarification waiter.
|
||||
* Detached requests remain answerable even if their row refresh lags.
|
||||
*/
|
||||
agentDisconnected?: boolean;
|
||||
/**
|
||||
* Caps the expanded overlay's height as a percentage of the viewport.
|
||||
* Drives both the rendered CSS max-height and the resize hook's drag
|
||||
* clamp — the two MUST agree, or the drag handle silently stops
|
||||
* responding before its own visible ceiling.
|
||||
* Drives both the rendered CSS max-height and the resize hook's drag clamp
|
||||
* - the two MUST agree, or the drag handle silently stops responding before
|
||||
* its own visible ceiling.
|
||||
*/
|
||||
maxHeightVh: number;
|
||||
// Additive: forwarded straight through to ClarificationInputOverlay.
|
||||
// Existing hosts (task chat, Quick Chat) leave this unset.
|
||||
onOutcome?: (outcome: ClarificationOutcome) => void;
|
||||
};
|
||||
|
||||
function pendingIdFromMessages(messages: readonly Message[] | null | undefined): string | null {
|
||||
const first = messages?.[0];
|
||||
if (!first) return null;
|
||||
@@ -61,6 +65,7 @@ export function ClarificationPanelSection({
|
||||
messages,
|
||||
onResolved,
|
||||
shortcutScopeRef,
|
||||
agentDisconnected = false,
|
||||
maxHeightVh,
|
||||
onOutcome,
|
||||
}: ClarificationPanelSectionProps) {
|
||||
@@ -89,6 +94,67 @@ export function ClarificationPanelSection({
|
||||
|
||||
const questionCount = messages?.length ?? 0;
|
||||
const actionLabel = collapsed ? t("chat:expandClarification") : t("chat:collapseClarification");
|
||||
|
||||
return (
|
||||
<ClarificationPanelContent
|
||||
actionLabel={actionLabel}
|
||||
agentDisconnected={agentDisconnected}
|
||||
collapsed={collapsed}
|
||||
containerRef={containerRef}
|
||||
contentId={contentId}
|
||||
disclosure={disclosure}
|
||||
height={height}
|
||||
maxHeightVh={maxHeightVh}
|
||||
messages={messages}
|
||||
onCollapse={() => setCollapsed(true)}
|
||||
onOutcome={onOutcome}
|
||||
onResolved={onResolved}
|
||||
onToggleCollapse={() => setCollapsed((current) => !current)}
|
||||
questionCount={questionCount}
|
||||
resizeHandleProps={resizeHandleProps}
|
||||
shortcutScopeRef={shortcutScopeRef}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
type ClarificationPanelContentProps = {
|
||||
actionLabel: string;
|
||||
agentDisconnected: boolean;
|
||||
collapsed: boolean;
|
||||
containerRef: RefObject<HTMLDivElement | null>;
|
||||
contentId: string;
|
||||
disclosure: ReturnType<typeof useComposerDisclosureContext>;
|
||||
height: number | null;
|
||||
maxHeightVh: number;
|
||||
messages: readonly Message[] | null | undefined;
|
||||
onCollapse: () => void;
|
||||
onOutcome?: (outcome: ClarificationOutcome) => void;
|
||||
onResolved: () => void;
|
||||
onToggleCollapse: () => void;
|
||||
questionCount: number;
|
||||
resizeHandleProps: Parameters<typeof ResizeHandle>[0];
|
||||
shortcutScopeRef: RefObject<HTMLElement | null>;
|
||||
};
|
||||
|
||||
function ClarificationPanelContent({
|
||||
actionLabel,
|
||||
agentDisconnected,
|
||||
collapsed,
|
||||
containerRef,
|
||||
contentId,
|
||||
disclosure,
|
||||
height,
|
||||
maxHeightVh,
|
||||
messages,
|
||||
onCollapse,
|
||||
onOutcome,
|
||||
onResolved,
|
||||
onToggleCollapse,
|
||||
questionCount,
|
||||
resizeHandleProps,
|
||||
shortcutScopeRef,
|
||||
}: ClarificationPanelContentProps) {
|
||||
const { t } = useTranslation();
|
||||
const compact = collapsed || questionCount === 0;
|
||||
|
||||
return (
|
||||
@@ -135,7 +201,7 @@ export function ClarificationPanelSection({
|
||||
aria-controls={contentId}
|
||||
title={actionLabel}
|
||||
data-testid="clarification-collapse-toggle"
|
||||
onClick={() => setCollapsed((current) => !current)}
|
||||
onClick={onToggleCollapse}
|
||||
>
|
||||
{collapsed ? (
|
||||
<IconChevronUp className="h-4 w-4" />
|
||||
@@ -156,8 +222,9 @@ export function ClarificationPanelSection({
|
||||
onOutcome={onOutcome}
|
||||
shortcutScopeRef={shortcutScopeRef}
|
||||
keyboardShortcutsEnabled={!collapsed}
|
||||
onDismiss={() => setCollapsed(true)}
|
||||
onCollapse={() => setCollapsed(true)}
|
||||
agentDisconnected={agentDisconnected}
|
||||
onDismiss={onCollapse}
|
||||
onCollapse={onCollapse}
|
||||
collapseContentId={contentId}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -465,6 +465,11 @@ function useSessionData(
|
||||
() => clarificationTurnIdForSession(session?.state, turns),
|
||||
[session?.state, turns],
|
||||
);
|
||||
const currentTurnCompleted = useMemo(() => {
|
||||
if (currentTurnId === null) return true;
|
||||
if (currentTurnId === undefined || !turns) return undefined;
|
||||
return turns.find((turn) => turn.id === currentTurnId)?.completed_at != null;
|
||||
}, [currentTurnId, turns]);
|
||||
const lastAgentError = useMemo(() => readLastAgentError(session?.metadata), [session?.metadata]);
|
||||
const processed = useProcessedMessages(messages, taskId, resolvedSessionId, taskDescription, {
|
||||
initialPromptPreview: session?.metadata?.initial_prompt_preview,
|
||||
@@ -472,6 +477,7 @@ function useSessionData(
|
||||
hasOlderMessages,
|
||||
lastAgentError,
|
||||
currentTurnId,
|
||||
currentTurnCompleted,
|
||||
pendingAction: session?.pending_action,
|
||||
});
|
||||
const { sessionModel, activeModel } = useSessionModel(
|
||||
|
||||
@@ -456,7 +456,7 @@ describe("AddPanelMenuItems — plugin task panels (AC1)", () => {
|
||||
.forPlugin("kandev-plugin-notes")
|
||||
.registerTaskPanel({ id: "notes", title: "Notes", Component: Notes });
|
||||
|
||||
renderMenu();
|
||||
renderMenu({ taskId: "task-1" });
|
||||
const row = screen.getByTestId("add-panel-plugin-item-kandev-plugin-notes-notes");
|
||||
expect(row.textContent).toContain("Notes");
|
||||
|
||||
|
||||
@@ -26,7 +26,9 @@ import { useDockviewStore } from "@/lib/state/dockview-store";
|
||||
import { reviewPanelId } from "@/lib/state/dockview-review-panel-id";
|
||||
import { pluginRegistry, usePluginRegistry } from "@/lib/plugins/registry";
|
||||
import { resolvePluginIcon } from "@/lib/plugins/icons";
|
||||
import type { ReviewItemSummary } from "@/lib/plugins/types";
|
||||
import { registrationIsVisible } from "./plugin-task-panel";
|
||||
import { resolveTaskPanelTitle } from "@/lib/state/layout-manager/plugin-panels";
|
||||
import type { PluginSessionKind, ReviewItemSummary } from "@/lib/plugins/types";
|
||||
import type { TaskPR } from "@/lib/types/github";
|
||||
import type { TaskMR } from "@/lib/types/gitlab";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
@@ -200,20 +202,41 @@ function PRPanelMenuItems({ prs, onOpenPR }: { prs: TaskPR[]; onOpenPR: (pr: Tas
|
||||
}
|
||||
|
||||
/** One "+" menu row per plugin-registered task panel (AC1), rendered after Plan. */
|
||||
function PluginTaskPanelMenuItems({ groupId }: { groupId: string }) {
|
||||
function PluginTaskPanelMenuItems({
|
||||
groupId,
|
||||
state,
|
||||
}: {
|
||||
groupId: string;
|
||||
state: AddPanelMenuState;
|
||||
}) {
|
||||
usePluginRegistry();
|
||||
const addPluginPanel = useDockviewStore((s) => s.addPluginPanel);
|
||||
const panels = pluginRegistry.getTaskPanels();
|
||||
const sessionId = useAppStore((store) => store.tasks.activeSessionId);
|
||||
let sessionKind: PluginSessionKind = null;
|
||||
if (sessionId) {
|
||||
sessionKind = state.isPassthrough ? "passthrough" : "managed";
|
||||
}
|
||||
const panels = state.taskId
|
||||
? pluginRegistry.getTaskPanels().filter((registration) =>
|
||||
registrationIsVisible(registration, {
|
||||
taskId: state.taskId!,
|
||||
sessionId,
|
||||
sessionKind,
|
||||
presentation: "desktop",
|
||||
}),
|
||||
)
|
||||
: [];
|
||||
|
||||
return (
|
||||
<>
|
||||
{panels.map((registration) => {
|
||||
const Icon = resolvePluginIcon(registration.icon);
|
||||
const title = resolveTaskPanelTitle(registration);
|
||||
return (
|
||||
<DropdownMenuItem
|
||||
key={`${registration.pluginId}:${registration.id}`}
|
||||
onClick={() =>
|
||||
addPluginPanel(registration.pluginId, registration.id, registration.title, {
|
||||
addPluginPanel(registration.pluginId, registration.id, title, {
|
||||
groupId,
|
||||
})
|
||||
}
|
||||
@@ -221,7 +244,7 @@ function PluginTaskPanelMenuItems({ groupId }: { groupId: string }) {
|
||||
data-testid={`add-panel-plugin-item-${registration.pluginId}-${registration.id}`}
|
||||
>
|
||||
<Icon className={MENU_ICON_CLASS} />
|
||||
{registration.title}
|
||||
{title}
|
||||
</DropdownMenuItem>
|
||||
);
|
||||
})}
|
||||
@@ -354,7 +377,7 @@ export function AddPanelMenuItems({
|
||||
{t("task:portForwarding")}
|
||||
</DropdownMenuCheckboxItem>
|
||||
)}
|
||||
<PluginTaskPanelMenuItems groupId={groupId} />
|
||||
<PluginTaskPanelMenuItems groupId={groupId} state={state} />
|
||||
<TaskCanvasMenuItems groupId={groupId} taskId={state.taskId} />
|
||||
{!state.isPassthrough && (
|
||||
<DropdownMenuItem onClick={() => addTodosPanel({ groupId })} className={MENU_ITEM_CLASS}>
|
||||
|
||||
@@ -28,6 +28,7 @@ vi.mock("react-i18next", () => ({
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/i18n", () => ({
|
||||
i18n: { language: "en", on: vi.fn() },
|
||||
t: (key: string) => key,
|
||||
}));
|
||||
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("@/components/state-provider", () => ({
|
||||
useAppStore: (selector: (state: Record<string, unknown>) => unknown) =>
|
||||
selector({
|
||||
tasks: { activeTaskId: "task-1", activeSessionId: "session-1" },
|
||||
taskSessions: {
|
||||
items: {
|
||||
"session-1": { id: "session-1", is_passthrough: false },
|
||||
},
|
||||
},
|
||||
}),
|
||||
}));
|
||||
import { pluginRegistry } from "@/lib/plugins/registry";
|
||||
import { PluginPanelPicker } from "./plugin-panel-picker";
|
||||
|
||||
@@ -24,7 +36,7 @@ describe("PluginPanelPicker", () => {
|
||||
.forPlugin(PLUGIN_B)
|
||||
.registerTaskPanel({ id: "notes", title: "Notes", Component: Notes, mobileEnabled: true });
|
||||
|
||||
render(<PluginPanelPicker open onOpenChange={vi.fn()} onSelect={vi.fn()} />);
|
||||
render(<PluginPanelPicker taskId="task-1" open onOpenChange={vi.fn()} onSelect={vi.fn()} />);
|
||||
|
||||
const options = screen.getAllByTestId(/^mobile-plugin-panel-option-/);
|
||||
expect(options).toHaveLength(2);
|
||||
@@ -43,11 +55,49 @@ describe("PluginPanelPicker", () => {
|
||||
const onOpenChange = vi.fn();
|
||||
const onSelect = vi.fn();
|
||||
|
||||
render(<PluginPanelPicker open onOpenChange={onOpenChange} onSelect={onSelect} />);
|
||||
render(
|
||||
<PluginPanelPicker taskId="task-1" open onOpenChange={onOpenChange} onSelect={onSelect} />,
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByTestId("mobile-plugin-panel-option-picker-plugin-b-notes"));
|
||||
|
||||
expect(onSelect).toHaveBeenCalledWith("plugin:picker-plugin-b:notes");
|
||||
expect(onOpenChange).toHaveBeenCalledWith(false);
|
||||
});
|
||||
|
||||
it("omits registrations whose visibility predicate rejects the active mobile context", () => {
|
||||
function Notes() {
|
||||
return null;
|
||||
}
|
||||
pluginRegistry.forPlugin(PLUGIN_A).registerTaskPanel({
|
||||
id: "notes",
|
||||
title: "Notes",
|
||||
Component: Notes,
|
||||
mobileEnabled: true,
|
||||
visible: (context) => context.presentation === "desktop",
|
||||
});
|
||||
|
||||
render(<PluginPanelPicker taskId="task-1" open onOpenChange={vi.fn()} onSelect={vi.fn()} />);
|
||||
|
||||
expect(screen.queryByTestId(`mobile-plugin-panel-option-${PLUGIN_A}-notes`)).toBeNull();
|
||||
});
|
||||
|
||||
it("does not invoke plugin visibility without a task context", () => {
|
||||
function Notes() {
|
||||
return null;
|
||||
}
|
||||
const visible = vi.fn(() => true);
|
||||
pluginRegistry.forPlugin(PLUGIN_A).registerTaskPanel({
|
||||
id: "notes",
|
||||
title: "Notes",
|
||||
Component: Notes,
|
||||
mobileEnabled: true,
|
||||
visible,
|
||||
});
|
||||
|
||||
render(<PluginPanelPicker open onOpenChange={vi.fn()} onSelect={vi.fn()} />);
|
||||
|
||||
expect(visible).not.toHaveBeenCalled();
|
||||
expect(screen.queryByTestId(`mobile-plugin-panel-option-${PLUGIN_A}-notes`)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,6 +7,8 @@ import { pluginPanelId } from "@/lib/state/layout-manager/plugin-panels";
|
||||
import type { MobileSessionPanel } from "@/lib/state/slices/ui/types";
|
||||
import { resolvePluginIcon } from "@/lib/plugins/icons";
|
||||
import { pluginRegistry, usePluginRegistry } from "@/lib/plugins/registry";
|
||||
import { registrationIsVisible } from "../plugin-task-panel";
|
||||
import { resolveTaskPanelTitle } from "@/lib/state/layout-manager/plugin-panels";
|
||||
import { MobilePickerSheet } from "./mobile-picker-sheet";
|
||||
|
||||
type PluginPanelPickerProps = {
|
||||
@@ -16,6 +18,9 @@ type PluginPanelPickerProps = {
|
||||
showPromptHistory?: boolean;
|
||||
taskCanvases?: Canvas[];
|
||||
onOpenCanvas?: (canvasId: string) => void;
|
||||
taskId?: string | null;
|
||||
sessionId?: string | null;
|
||||
sessionKind?: "managed" | "passthrough" | null;
|
||||
};
|
||||
|
||||
/** One grouped, scrollable phone picker for all mobile-enabled plugin panels. */
|
||||
@@ -26,12 +31,25 @@ export function PluginPanelPicker({
|
||||
showPromptHistory = false,
|
||||
taskCanvases = [],
|
||||
onOpenCanvas,
|
||||
taskId = null,
|
||||
sessionId = null,
|
||||
sessionKind = null,
|
||||
}: PluginPanelPickerProps) {
|
||||
const { t } = useTranslation();
|
||||
usePluginRegistry();
|
||||
const registrations = pluginRegistry
|
||||
.getTaskPanels()
|
||||
.filter((registration) => registration.mobileEnabled);
|
||||
const registrations = taskId
|
||||
? pluginRegistry
|
||||
.getTaskPanels()
|
||||
.filter((registration) => registration.mobileEnabled)
|
||||
.filter((registration) =>
|
||||
registrationIsVisible(registration, {
|
||||
taskId,
|
||||
sessionId,
|
||||
sessionKind,
|
||||
presentation: "mobile",
|
||||
}),
|
||||
)
|
||||
: [];
|
||||
|
||||
if (!open) return null;
|
||||
|
||||
@@ -83,7 +101,7 @@ export function PluginPanelPicker({
|
||||
}}
|
||||
>
|
||||
<Icon className="h-5 w-5 shrink-0 text-muted-foreground" />
|
||||
<span className="min-w-0 truncate">{registration.title}</span>
|
||||
<span className="min-w-0 truncate">{resolveTaskPanelTitle(registration)}</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
|
||||
@@ -110,6 +110,7 @@ describe("SessionMobileBottomNav plugin panels", () => {
|
||||
render(
|
||||
<SessionMobileBottomNav
|
||||
activePanel="chat"
|
||||
taskId="task-1"
|
||||
onPanelChange={onPanelChange}
|
||||
showStatus={false}
|
||||
onOpenStatus={vi.fn()}
|
||||
@@ -142,6 +143,7 @@ describe("SessionMobileBottomNav plugin panels", () => {
|
||||
render(
|
||||
<SessionMobileBottomNav
|
||||
activePanel="plugin:plugin-a:notes"
|
||||
taskId="task-1"
|
||||
onPanelChange={vi.fn()}
|
||||
showStatus={false}
|
||||
onOpenStatus={vi.fn()}
|
||||
@@ -151,6 +153,32 @@ describe("SessionMobileBottomNav plugin panels", () => {
|
||||
expect(screen.getByRole("button", { name: "Panels" }).className).toContain("text-primary");
|
||||
});
|
||||
|
||||
it("does not evaluate task-panel visibility without a task", () => {
|
||||
function Notes() {
|
||||
return null;
|
||||
}
|
||||
const visible = vi.fn(() => true);
|
||||
pluginRegistry.forPlugin(PLUGIN_A).registerTaskPanel({
|
||||
id: "notes",
|
||||
title: "Notes",
|
||||
Component: Notes,
|
||||
mobileEnabled: true,
|
||||
visible,
|
||||
});
|
||||
|
||||
render(
|
||||
<SessionMobileBottomNav
|
||||
activePanel="chat"
|
||||
onPanelChange={vi.fn()}
|
||||
showStatus={false}
|
||||
onOpenStatus={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(visible).not.toHaveBeenCalled();
|
||||
expect(screen.queryByRole("button", { name: "Panels" })).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps the grouped Panels action active for Prompt history", () => {
|
||||
render(
|
||||
<SessionMobileBottomNav
|
||||
|
||||
@@ -21,6 +21,7 @@ import { useConnectionIssueCopy } from "@/components/app-status-bar/connection-s
|
||||
import { pluginRegistry, usePluginRegistry } from "@/lib/plugins/registry";
|
||||
import { parsePluginPanelId } from "@/lib/state/layout-manager/plugin-panels";
|
||||
import { PluginPanelPicker } from "./plugin-panel-picker";
|
||||
import { registrationIsVisible } from "../plugin-task-panel";
|
||||
|
||||
type SessionMobileBottomNavProps = {
|
||||
activePanel: MobileSessionPanel;
|
||||
@@ -34,6 +35,9 @@ type SessionMobileBottomNavProps = {
|
||||
connectionIssueSeverity?: ConnectionIssueSeverity;
|
||||
taskCanvases?: Canvas[];
|
||||
onOpenCanvas?: (canvasId: string) => void;
|
||||
taskId?: string | null;
|
||||
sessionId?: string | null;
|
||||
sessionKind?: "managed" | "passthrough" | null;
|
||||
};
|
||||
|
||||
type NavItem = {
|
||||
@@ -44,8 +48,22 @@ type NavItem = {
|
||||
connectionIssueSeverity?: Exclude<ConnectionIssueSeverity, "none">;
|
||||
} & ({ panel: MobileSessionPanel; onClick?: never } | { panel?: never; onClick: () => void });
|
||||
|
||||
function hasMobilePluginPanels(): boolean {
|
||||
return pluginRegistry.getTaskPanels().some((registration) => registration.mobileEnabled);
|
||||
function hasMobilePluginPanels(
|
||||
taskId: string | null,
|
||||
sessionId: string | null,
|
||||
sessionKind: "managed" | "passthrough" | null,
|
||||
): boolean {
|
||||
if (!taskId) return false;
|
||||
return pluginRegistry.getTaskPanels().some(
|
||||
(registration) =>
|
||||
registration.mobileEnabled &&
|
||||
registrationIsVisible(registration, {
|
||||
taskId,
|
||||
sessionId,
|
||||
sessionKind,
|
||||
presentation: "mobile",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function buildMobileNavItems({
|
||||
@@ -58,6 +76,7 @@ function buildMobileNavItems({
|
||||
onOpenPluginPicker,
|
||||
showPromptHistory,
|
||||
hasTaskCanvases,
|
||||
mobilePluginPanelsAvailable,
|
||||
connectionIssueSeverity,
|
||||
t,
|
||||
}: {
|
||||
@@ -72,6 +91,7 @@ function buildMobileNavItems({
|
||||
hasTaskCanvases: boolean;
|
||||
connectionIssueSeverity: ConnectionIssueSeverity;
|
||||
t: (key: string) => string;
|
||||
mobilePluginPanelsAvailable: boolean;
|
||||
}): NavItem[] {
|
||||
return [
|
||||
{
|
||||
@@ -120,7 +140,7 @@ function buildMobileNavItems({
|
||||
label: t("task:terminal"),
|
||||
icon: <IconTerminal2 className="h-5 w-5" />,
|
||||
},
|
||||
...(showPromptHistory || hasTaskCanvases || hasMobilePluginPanels()
|
||||
...(showPromptHistory || hasTaskCanvases || mobilePluginPanelsAvailable
|
||||
? [
|
||||
{
|
||||
label: t("common:panels"),
|
||||
@@ -156,11 +176,15 @@ export function SessionMobileBottomNav({
|
||||
connectionIssueSeverity = "none",
|
||||
taskCanvases = [],
|
||||
onOpenCanvas,
|
||||
taskId = null,
|
||||
sessionId = null,
|
||||
sessionKind = null,
|
||||
}: SessionMobileBottomNavProps) {
|
||||
const { t } = useTranslation();
|
||||
usePluginRegistry();
|
||||
const registryVersion = pluginRegistry.getVersion();
|
||||
const [pluginPickerOpen, setPluginPickerOpen] = useState(false);
|
||||
const mobilePluginPanelsAvailable = hasMobilePluginPanels(taskId, sessionId, sessionKind);
|
||||
const items: NavItem[] = useMemo(
|
||||
() =>
|
||||
buildMobileNavItems({
|
||||
@@ -173,6 +197,7 @@ export function SessionMobileBottomNav({
|
||||
onOpenPluginPicker: () => setPluginPickerOpen(true),
|
||||
showPromptHistory,
|
||||
hasTaskCanvases: taskCanvases.length > 0,
|
||||
mobilePluginPanelsAvailable,
|
||||
connectionIssueSeverity,
|
||||
t,
|
||||
}),
|
||||
@@ -187,6 +212,7 @@ export function SessionMobileBottomNav({
|
||||
activePanel,
|
||||
showPromptHistory,
|
||||
taskCanvases.length,
|
||||
mobilePluginPanelsAvailable,
|
||||
t,
|
||||
],
|
||||
);
|
||||
@@ -211,6 +237,9 @@ export function SessionMobileBottomNav({
|
||||
showPromptHistory={showPromptHistory}
|
||||
taskCanvases={taskCanvases}
|
||||
onOpenCanvas={onOpenCanvas}
|
||||
taskId={taskId}
|
||||
sessionId={sessionId}
|
||||
sessionKind={sessionKind}
|
||||
/>
|
||||
</nav>
|
||||
);
|
||||
|
||||
@@ -27,7 +27,11 @@ import type { MobileSessionPanel } from "@/lib/state/slices/ui/types";
|
||||
import type { OpenFileTab } from "@/lib/types/backend";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
import { useNormalizedTaskReviewsState } from "../review-panel-provider";
|
||||
import type { ReviewItemSummary } from "@/lib/plugins/types";
|
||||
import type {
|
||||
PluginOpenMessageResult,
|
||||
PluginSessionKind,
|
||||
ReviewItemSummary,
|
||||
} from "@/lib/plugins/types";
|
||||
import type { Canvas } from "@/lib/api/domains/canvas-api";
|
||||
import { reviewItemId, useReviewItemSelection } from "../review-selection";
|
||||
import { PluginTaskPanel } from "../plugin-task-panel";
|
||||
@@ -165,7 +169,7 @@ type MobilePanelAreaProps = {
|
||||
handleClearSelectedDiff: () => void;
|
||||
handleOpenFile: (file: OpenFileTab) => void;
|
||||
handlePanelChangeAndClearSheet: (panel: MobileSessionPanel) => void;
|
||||
onNavigateToPrompt: (messageId: string) => void;
|
||||
onNavigateToPrompt: (messageId: string) => PluginOpenMessageResult;
|
||||
onScrollTargetConsumed?: (messageId: string) => void;
|
||||
mobileScrollTarget: PendingMessageScrollTarget | null;
|
||||
topNavHeight: string;
|
||||
@@ -189,6 +193,26 @@ export function terminalPaddingBottom(
|
||||
: `${KEYBAR_HEIGHT_PX}px`;
|
||||
}
|
||||
|
||||
function MobileTerminalPanel({
|
||||
sessionId,
|
||||
paddingBottom,
|
||||
}: {
|
||||
sessionId: string | null;
|
||||
paddingBottom: string;
|
||||
}) {
|
||||
return (
|
||||
<div
|
||||
data-testid="terminal-panel"
|
||||
className="flex-1 min-h-0 flex flex-col px-2"
|
||||
style={{ paddingBottom }}
|
||||
>
|
||||
<SessionPanelContent className="p-0 flex-1 min-h-0 flex flex-col">
|
||||
<MobileTerminalPane key={sessionId} sessionId={sessionId} />
|
||||
</SessionPanelContent>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function MobilePanelArea({
|
||||
currentMobilePanel,
|
||||
activeTaskId,
|
||||
@@ -211,7 +235,6 @@ export function MobilePanelArea({
|
||||
onSelectReview,
|
||||
}: MobilePanelAreaProps) {
|
||||
const { keyboardOpen, bottomOffset } = useVisualViewportOffset();
|
||||
const terminalPadding = terminalPaddingBottom(keyboardOpen, bottomOffset, bottomNavHeight);
|
||||
return (
|
||||
<div
|
||||
className="flex flex-col"
|
||||
@@ -267,15 +290,10 @@ export function MobilePanelArea({
|
||||
</div>
|
||||
)}
|
||||
{currentMobilePanel === "terminal" && (
|
||||
<div
|
||||
data-testid="terminal-panel"
|
||||
className="flex-1 min-h-0 flex flex-col px-2"
|
||||
style={{ paddingBottom: terminalPadding }}
|
||||
>
|
||||
<SessionPanelContent className="p-0 flex-1 min-h-0 flex flex-col">
|
||||
<MobileTerminalPane key={effectiveSessionId} sessionId={effectiveSessionId} />
|
||||
</SessionPanelContent>
|
||||
</div>
|
||||
<MobileTerminalPanel
|
||||
sessionId={effectiveSessionId}
|
||||
paddingBottom={terminalPaddingBottom(keyboardOpen, bottomOffset, bottomNavHeight)}
|
||||
/>
|
||||
)}
|
||||
<MobileReviewPanel
|
||||
currentMobilePanel={currentMobilePanel}
|
||||
@@ -283,7 +301,10 @@ export function MobilePanelArea({
|
||||
selectedReview={selectedReview}
|
||||
onSelectReview={onSelectReview}
|
||||
/>
|
||||
<MobilePluginPanel currentMobilePanel={currentMobilePanel} />
|
||||
<MobilePluginPanel
|
||||
currentMobilePanel={currentMobilePanel}
|
||||
onOpenMessage={onNavigateToPrompt}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -308,7 +329,13 @@ function MobilePlanPanel({
|
||||
* plugin-contributed nav entries. A non-plugin id (the common case) is a
|
||||
* no-op, so this stays a single trailing branch instead of one per plugin.
|
||||
*/
|
||||
function MobilePluginPanel({ currentMobilePanel }: { currentMobilePanel: MobileSessionPanel }) {
|
||||
function MobilePluginPanel({
|
||||
currentMobilePanel,
|
||||
onOpenMessage,
|
||||
}: {
|
||||
currentMobilePanel: MobileSessionPanel;
|
||||
onOpenMessage: (messageId: string) => PluginOpenMessageResult;
|
||||
}) {
|
||||
const parsed = parsePluginPanelId(currentMobilePanel);
|
||||
if (!parsed) return null;
|
||||
return (
|
||||
@@ -318,6 +345,7 @@ function MobilePluginPanel({ currentMobilePanel }: { currentMobilePanel: MobileS
|
||||
panelKey={parsed.panelKey}
|
||||
panelId={currentMobilePanel}
|
||||
presentation="mobile"
|
||||
onOpenMessage={onOpenMessage}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
@@ -529,6 +557,8 @@ export function useMobilePanelHandlers({
|
||||
|
||||
type SessionMobileFooterProps = {
|
||||
sessionId: string | null;
|
||||
taskId: string | null;
|
||||
sessionKind: "managed" | "passthrough" | null;
|
||||
activePanel: MobileSessionPanel;
|
||||
onPanelChange: (panel: MobileSessionPanel) => void;
|
||||
showPromptHistory: boolean;
|
||||
@@ -544,6 +574,8 @@ type SessionMobileFooterProps = {
|
||||
|
||||
function SessionMobileFooter({
|
||||
sessionId,
|
||||
taskId,
|
||||
sessionKind,
|
||||
activePanel,
|
||||
onPanelChange,
|
||||
showPromptHistory,
|
||||
@@ -575,6 +607,9 @@ function SessionMobileFooter({
|
||||
connectionIssueSeverity={connectionIssueSeverity}
|
||||
taskCanvases={taskCanvases}
|
||||
onOpenCanvas={onOpenCanvas}
|
||||
taskId={taskId}
|
||||
sessionId={sessionId}
|
||||
sessionKind={sessionKind}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
@@ -637,18 +672,22 @@ export const SessionMobileLayout = memo(function SessionMobileLayout(
|
||||
[handlePanelChangeAndClearSheet],
|
||||
);
|
||||
const handleNavigateToPrompt = useCallback(
|
||||
(messageId: string) => {
|
||||
if (!effectiveSessionId) return;
|
||||
(messageId: string): PluginOpenMessageResult => {
|
||||
const normalizedMessageId = messageId.trim();
|
||||
if (isPassthroughMode || !effectiveSessionId || normalizedMessageId === "") {
|
||||
return { status: "unavailable" };
|
||||
}
|
||||
const token = ++mobileScrollTokenRef.current;
|
||||
setMobileScrollTarget({
|
||||
sessionId: effectiveSessionId,
|
||||
messageId,
|
||||
messageId: normalizedMessageId,
|
||||
token,
|
||||
hostPanelId: "mobile-chat",
|
||||
});
|
||||
handlePanelChangeAndClearSheet("chat");
|
||||
return { status: "accepted" };
|
||||
},
|
||||
[effectiveSessionId, handlePanelChangeAndClearSheet],
|
||||
[effectiveSessionId, handlePanelChangeAndClearSheet, isPassthroughMode],
|
||||
);
|
||||
const handleMobileScrollTargetConsumed = useCallback(() => {
|
||||
setMobileScrollTarget(null);
|
||||
@@ -666,6 +705,10 @@ export const SessionMobileLayout = memo(function SessionMobileLayout(
|
||||
setMobileScrollTarget(null);
|
||||
}
|
||||
}, [currentMobilePanel, effectiveMobilePanel]);
|
||||
let sessionKind: PluginSessionKind = null;
|
||||
if (effectiveSessionId) {
|
||||
sessionKind = isPassthroughMode ? "passthrough" : "managed";
|
||||
}
|
||||
return (
|
||||
<div className="relative h-full min-h-0 bg-background" data-testid="mobile-task-layout">
|
||||
<MobileTopBarSticky
|
||||
@@ -699,6 +742,8 @@ export const SessionMobileLayout = memo(function SessionMobileLayout(
|
||||
/>
|
||||
<StatusAwareSessionMobileFooter
|
||||
sessionId={effectiveSessionId ?? null}
|
||||
taskId={activeTaskId ?? null}
|
||||
sessionKind={sessionKind}
|
||||
activePanel={effectiveMobilePanel}
|
||||
onPanelChange={handleMobilePanelChange}
|
||||
planBadge={hasUnseenPlanUpdate}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { DockviewDefaultTab, type IDockviewPanelHeaderProps } from "dockview-react";
|
||||
import { pluginRegistry, usePluginRegistry } from "@/lib/plugins/registry";
|
||||
import { resolvePluginIcon } from "@/lib/plugins/icons";
|
||||
import { resolveTaskPanelTitle } from "@/lib/state/layout-manager/plugin-panels";
|
||||
|
||||
/**
|
||||
* Tab chrome for a plugin task panel: the plugin's curated icon (if any)
|
||||
@@ -13,11 +16,16 @@ import { resolvePluginIcon } from "@/lib/plugins/icons";
|
||||
*/
|
||||
export function PluginPanelTab(props: IDockviewPanelHeaderProps) {
|
||||
usePluginRegistry();
|
||||
const { i18n } = useTranslation();
|
||||
const pluginId = props.params?.pluginId as string | undefined;
|
||||
const panelKey = props.params?.panelKey as string | undefined;
|
||||
const registration =
|
||||
pluginId && panelKey ? pluginRegistry.getTaskPanel(pluginId, panelKey) : undefined;
|
||||
const Icon = resolvePluginIcon(registration?.icon);
|
||||
const title = registration ? resolveTaskPanelTitle(registration, i18n) : (props.api.title ?? "");
|
||||
useEffect(() => {
|
||||
if (title && props.api.title !== title) props.api.setTitle(title);
|
||||
}, [props.api, title]);
|
||||
|
||||
return (
|
||||
<div data-testid="plugin-panel-tab" className="relative flex items-center gap-1.5">
|
||||
|
||||
@@ -8,6 +8,11 @@ vi.mock("@/components/state-provider", () => ({
|
||||
useAppStore: (selector: (state: Record<string, unknown>) => unknown) =>
|
||||
selector({
|
||||
tasks: { activeTaskId: mockActiveTaskId, activeSessionId: mockActiveSessionId },
|
||||
taskSessions: {
|
||||
items: {
|
||||
session_1: { id: "session_1", is_passthrough: false, name: "Session" },
|
||||
},
|
||||
},
|
||||
}),
|
||||
}));
|
||||
|
||||
@@ -53,6 +58,148 @@ describe("PluginTaskPanel", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("passes session kind and a presentation-scoped navigation facade", () => {
|
||||
const onOpenMessage = vi.fn(() => ({ status: "accepted" as const }));
|
||||
function Notes(props: {
|
||||
sessionKind: string | null;
|
||||
conversation: { openMessage: (messageId: string) => { status: string } };
|
||||
}) {
|
||||
return (
|
||||
<button type="button" onClick={() => props.conversation.openMessage("message-1")}>
|
||||
{props.sessionKind}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
pluginRegistry
|
||||
.forPlugin("plugin-a")
|
||||
.registerTaskPanel({ id: "notes", title: "Notes", Component: Notes, mobileEnabled: true });
|
||||
|
||||
render(
|
||||
<PluginTaskPanel
|
||||
pluginId="plugin-a"
|
||||
panelKey="notes"
|
||||
panelId="plugin:plugin-a:notes"
|
||||
presentation="mobile"
|
||||
onOpenMessage={onOpenMessage}
|
||||
/>,
|
||||
);
|
||||
|
||||
screen.getByRole("button", { name: "managed" }).click();
|
||||
expect(onOpenMessage).toHaveBeenCalledWith("message-1");
|
||||
});
|
||||
|
||||
it("reports unavailable for invalid or rejected mobile navigation", () => {
|
||||
let emptyResult = "";
|
||||
let rejectedResult = "";
|
||||
const onOpenMessage = vi.fn(() => ({ status: "unavailable" as const }));
|
||||
function Notes(props: {
|
||||
conversation: { openMessage: (messageId: string) => { status: string } };
|
||||
}) {
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => (emptyResult = props.conversation.openMessage(" ").status)}
|
||||
>
|
||||
empty
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => (rejectedResult = props.conversation.openMessage("message-1").status)}
|
||||
>
|
||||
rejected
|
||||
</button>
|
||||
</>
|
||||
);
|
||||
}
|
||||
pluginRegistry
|
||||
.forPlugin("plugin-a")
|
||||
.registerTaskPanel({ id: "notes", title: "Notes", Component: Notes, mobileEnabled: true });
|
||||
|
||||
render(
|
||||
<PluginTaskPanel
|
||||
pluginId="plugin-a"
|
||||
panelKey="notes"
|
||||
panelId="plugin:plugin-a:notes"
|
||||
presentation="mobile"
|
||||
onOpenMessage={onOpenMessage}
|
||||
/>,
|
||||
);
|
||||
|
||||
screen.getByRole("button", { name: "empty" }).click();
|
||||
expect(emptyResult).toBe("unavailable");
|
||||
expect(onOpenMessage).not.toHaveBeenCalled();
|
||||
|
||||
screen.getByRole("button", { name: "rejected" }).click();
|
||||
expect(rejectedResult).toBe("unavailable");
|
||||
expect(onOpenMessage).toHaveBeenCalledWith("message-1");
|
||||
});
|
||||
});
|
||||
describe("PluginTaskPanel session lifecycle", () => {
|
||||
it("keeps the panel bound to a deleted active session", () => {
|
||||
function Notes(props: { sessionId: string | null }) {
|
||||
return <div data-testid="notes-session">{props.sessionId}</div>;
|
||||
}
|
||||
pluginRegistry
|
||||
.forPlugin("plugin-a")
|
||||
.registerTaskPanel({ id: "notes", title: "Notes", Component: Notes });
|
||||
|
||||
const view = render(
|
||||
<PluginTaskPanel
|
||||
pluginId="plugin-a"
|
||||
panelKey="notes"
|
||||
panelId="plugin:plugin-a:notes"
|
||||
presentation="desktop"
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByTestId("notes-session").textContent).toBe("session_1");
|
||||
|
||||
mockActiveSessionId = null;
|
||||
view.rerender(
|
||||
<PluginTaskPanel
|
||||
pluginId="plugin-a"
|
||||
panelKey="notes"
|
||||
panelId="plugin:plugin-a:notes"
|
||||
presentation="desktop"
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByTestId("notes-session").textContent).toBe("session_1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("PluginTaskPanel failure containment", () => {
|
||||
it("fails closed when a managed visibility predicate throws", () => {
|
||||
function Notes() {
|
||||
return <div>secret panel</div>;
|
||||
}
|
||||
const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);
|
||||
pluginRegistry.forPlugin("plugin-a").registerTaskPanel({
|
||||
id: "notes",
|
||||
title: "Notes",
|
||||
Component: Notes,
|
||||
visible: () => {
|
||||
throw new Error("predicate failed");
|
||||
},
|
||||
});
|
||||
|
||||
render(
|
||||
<PluginTaskPanel
|
||||
pluginId="plugin-a"
|
||||
panelKey="notes"
|
||||
panelId="plugin:plugin-a:notes"
|
||||
presentation="desktop"
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.queryByText("secret panel")).toBeNull();
|
||||
expect(screen.getByText("This panel is no longer available.")).not.toBeNull();
|
||||
expect(consoleError).toHaveBeenCalledWith(
|
||||
expect.stringContaining("plugin-a:notes"),
|
||||
expect.any(Error),
|
||||
);
|
||||
consoleError.mockRestore();
|
||||
});
|
||||
|
||||
it("renders a not-available fallback when the plugin is no longer registered (AC5)", () => {
|
||||
render(
|
||||
<PluginTaskPanel
|
||||
@@ -89,3 +236,51 @@ describe("PluginTaskPanel", () => {
|
||||
console.error = originalConsoleError;
|
||||
});
|
||||
});
|
||||
describe("PluginTaskPanel lease", () => {
|
||||
it("revokes a panel navigation lease when visibility changes", () => {
|
||||
let visible = true;
|
||||
let openMessage: ((messageId: string) => { status: string }) | undefined;
|
||||
const onOpenMessage = vi.fn(() => ({ status: "accepted" as const }));
|
||||
function Notes(props: {
|
||||
conversation: { openMessage: (messageId: string) => { status: string } };
|
||||
}) {
|
||||
openMessage = props.conversation.openMessage;
|
||||
return <div>visible panel</div>;
|
||||
}
|
||||
pluginRegistry.forPlugin("plugin-a").registerTaskPanel({
|
||||
id: "notes",
|
||||
title: "Notes",
|
||||
Component: Notes,
|
||||
mobileEnabled: true,
|
||||
visible: () => visible,
|
||||
});
|
||||
|
||||
const view = render(
|
||||
<PluginTaskPanel
|
||||
pluginId="plugin-a"
|
||||
panelKey="notes"
|
||||
panelId="plugin:plugin-a:notes"
|
||||
presentation="mobile"
|
||||
onOpenMessage={onOpenMessage}
|
||||
/>,
|
||||
);
|
||||
const initialOpenMessage = openMessage;
|
||||
expect(initialOpenMessage?.("message-1").status).toBe("accepted");
|
||||
expect(onOpenMessage).toHaveBeenCalledWith("message-1");
|
||||
|
||||
visible = false;
|
||||
view.rerender(
|
||||
<PluginTaskPanel
|
||||
pluginId="plugin-a"
|
||||
panelKey="notes"
|
||||
panelId="plugin:plugin-a:notes"
|
||||
presentation="mobile"
|
||||
onOpenMessage={onOpenMessage}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByText("This panel is no longer available.")).not.toBeNull();
|
||||
expect(initialOpenMessage?.("message-2").status).toBe("unavailable");
|
||||
expect(onOpenMessage).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,22 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useRef } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
import { PluginErrorBoundary } from "@/components/plugins/plugin-error-boundary";
|
||||
import {
|
||||
PluginConversationScopeProvider,
|
||||
pluginConversationApi,
|
||||
} from "@/lib/plugins/conversation-host";
|
||||
import type { PluginTaskPanelRegistration } from "@/lib/plugins/registry-registration-types";
|
||||
import { pluginRegistry, usePluginRegistry } from "@/lib/plugins/registry";
|
||||
import type { PluginPresentation } from "@/lib/plugins/types";
|
||||
import type {
|
||||
PluginOpenMessageResult,
|
||||
PluginPresentation,
|
||||
PluginTaskPanelContext,
|
||||
PluginSessionKind,
|
||||
} from "@/lib/plugins/types";
|
||||
import { useDockviewStore } from "@/lib/state/dockview-store";
|
||||
|
||||
export interface PluginTaskPanelContainerProps {
|
||||
pluginId: string;
|
||||
@@ -12,6 +24,7 @@ export interface PluginTaskPanelContainerProps {
|
||||
/** Full dockview/mobile panel id, e.g. `plugin:<pluginId>:<panelKey>`. */
|
||||
panelId: string;
|
||||
presentation: PluginPresentation;
|
||||
onOpenMessage?: (messageId: string) => PluginOpenMessageResult;
|
||||
}
|
||||
|
||||
function PluginTaskPanelUnavailable() {
|
||||
@@ -32,42 +45,120 @@ function PluginTaskPanelFailed() {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves `{ pluginId, panelKey }` to its current `TaskPanelRegistration`
|
||||
* and renders the plugin's `Component` with `PluginTaskPanelProps`, wrapped
|
||||
* in a `PluginErrorBoundary` (AC6) so a throw inside the plugin's render
|
||||
* can't take down the surrounding dockview/mobile layout. Renders a
|
||||
* "no longer available" fallback — not a throw — when the plugin was
|
||||
* disabled/uninstalled after the panel was opened, or the layout it was
|
||||
* restored from references a panel of a plugin that is no longer installed
|
||||
* (AC5).
|
||||
*/
|
||||
export function registrationIsVisible(
|
||||
registration: PluginTaskPanelRegistration,
|
||||
context: PluginTaskPanelContext,
|
||||
): boolean {
|
||||
if (!registration.visible) return true;
|
||||
try {
|
||||
return registration.visible(context);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[plugins] visibility predicate for "${registration.pluginId}:${registration.id}" threw`,
|
||||
error,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
type RememberedSession = { taskId: string | null; sessionId: string };
|
||||
|
||||
function useRememberedPanelSessionId(
|
||||
taskId: string | null,
|
||||
activeSessionId: string | null,
|
||||
): string | null {
|
||||
const rememberedSession = useRef<RememberedSession | null>(null);
|
||||
if (activeSessionId) {
|
||||
rememberedSession.current = { taskId, sessionId: activeSessionId };
|
||||
}
|
||||
return (
|
||||
activeSessionId ??
|
||||
(rememberedSession.current?.taskId === taskId ? rememberedSession.current.sessionId : null)
|
||||
);
|
||||
}
|
||||
|
||||
/** Resolves and contains one plugin-contributed task panel. */
|
||||
export function PluginTaskPanel({
|
||||
pluginId,
|
||||
panelKey,
|
||||
panelId,
|
||||
presentation,
|
||||
onOpenMessage,
|
||||
}: PluginTaskPanelContainerProps) {
|
||||
// Re-render when the registry changes (plugin disable/uninstall/reload) so
|
||||
// this panel picks up the fallback the instant its registration disappears.
|
||||
usePluginRegistry();
|
||||
const { t } = useTranslation();
|
||||
const taskId = useAppStore((state) => state.tasks.activeTaskId);
|
||||
const sessionId = useAppStore((state) => state.tasks.activeSessionId);
|
||||
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
|
||||
// Keep the deleted session identity until the host unmounts this panel.
|
||||
const sessionId = useRememberedPanelSessionId(taskId, activeSessionId);
|
||||
const session = useAppStore((state) =>
|
||||
sessionId ? state.taskSessions?.items?.[sessionId] : undefined,
|
||||
);
|
||||
const registration = pluginRegistry.getTaskPanel(pluginId, panelKey);
|
||||
const generation = pluginRegistry.getPluginLifecycle(pluginId)?.generation ?? 0;
|
||||
let sessionKind: PluginSessionKind = null;
|
||||
if (sessionId) {
|
||||
sessionKind = session?.is_passthrough ? "passthrough" : "managed";
|
||||
}
|
||||
const context: PluginTaskPanelContext = {
|
||||
taskId: taskId ?? "",
|
||||
sessionId,
|
||||
sessionKind,
|
||||
presentation,
|
||||
};
|
||||
const registrationVisible = registration ? registrationIsVisible(registration, context) : false;
|
||||
|
||||
if (!registration || !taskId) {
|
||||
const lease = useMemo(
|
||||
() => ({ active: true }),
|
||||
[generation, panelId, panelKey, pluginId, presentation, registrationVisible, sessionId, taskId],
|
||||
);
|
||||
useEffect(
|
||||
() => () => {
|
||||
lease.active = false;
|
||||
},
|
||||
[lease],
|
||||
);
|
||||
const conversation = useMemo(
|
||||
() => ({
|
||||
history: pluginConversationApi,
|
||||
openMessage(messageId: string): PluginOpenMessageResult {
|
||||
if (!lease.active || !sessionId || messageId.trim() === "") {
|
||||
return { status: "unavailable" };
|
||||
}
|
||||
if (presentation === "mobile") {
|
||||
return onOpenMessage?.(messageId) ?? { status: "unavailable" };
|
||||
}
|
||||
const queued = useDockviewStore
|
||||
.getState()
|
||||
.scrollTranscriptToMessage(sessionId, messageId, session?.name || t("task:chat"));
|
||||
return { status: queued ? "accepted" : "unavailable" };
|
||||
},
|
||||
}),
|
||||
[lease, onOpenMessage, presentation, session?.name, sessionId, t],
|
||||
);
|
||||
|
||||
if (!registration || !taskId || !registrationVisible) {
|
||||
return <PluginTaskPanelUnavailable />;
|
||||
}
|
||||
|
||||
const { Component } = registration;
|
||||
return (
|
||||
<PluginErrorBoundary context={`task panel "${panelId}"`} fallback={<PluginTaskPanelFailed />}>
|
||||
<Component
|
||||
panelId={panelId}
|
||||
<PluginConversationScopeProvider
|
||||
pluginId={pluginId}
|
||||
taskId={taskId}
|
||||
sessionId={sessionId}
|
||||
generation={generation}
|
||||
presentation={presentation}
|
||||
/>
|
||||
>
|
||||
<registration.Component
|
||||
panelId={panelId}
|
||||
taskId={taskId}
|
||||
sessionId={sessionId}
|
||||
sessionKind={sessionKind}
|
||||
presentation={presentation}
|
||||
conversation={conversation}
|
||||
/>
|
||||
</PluginConversationScopeProvider>
|
||||
</PluginErrorBoundary>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -57,12 +57,31 @@ vi.mock("@/hooks/domains/session/load-message-window", () => ({
|
||||
}));
|
||||
|
||||
import {
|
||||
isMessageRowRendered,
|
||||
usePendingMessageScroll,
|
||||
useScrollTargetConsumption,
|
||||
type PendingMessageScrollTarget,
|
||||
} from "./task-chat-panel";
|
||||
import type { RenderItem } from "@/hooks/use-processed-messages";
|
||||
import { loadMessageWindowAround } from "@/hooks/domains/session/load-message-window";
|
||||
|
||||
describe("isMessageRowRendered", () => {
|
||||
it("only treats direct message items as scrollable rows", () => {
|
||||
const items = [
|
||||
{
|
||||
type: "turn_group",
|
||||
id: "group-1",
|
||||
turnId: "turn-1",
|
||||
messages: [{ id: "grouped-prompt" }],
|
||||
},
|
||||
{ type: "message", message: { id: "direct-prompt" } },
|
||||
] as unknown as RenderItem[];
|
||||
|
||||
expect(isMessageRowRendered(items, "grouped-prompt")).toBe(false);
|
||||
expect(isMessageRowRendered(items, "direct-prompt")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
let pendingFrames: Array<(() => void) | undefined> = [];
|
||||
|
||||
/** Runs two rounds of pending requestAnimationFrame callbacks inside act. */
|
||||
|
||||
@@ -44,6 +44,8 @@ import { useLazyLoadMessages } from "@/hooks/use-lazy-load-messages";
|
||||
import { findUnreadDividerItemId, lastRenderedMessageId } from "@/lib/session-unread-divider";
|
||||
import { useSessionReadTracking } from "./chat/use-session-read-tracking";
|
||||
import { useDrainOlderMessages } from "@/components/task/chat/use-drain-older-messages";
|
||||
import type { RenderItem } from "@/hooks/use-processed-messages";
|
||||
|
||||
import { useAppStore, useAppStoreApi } from "@/components/state-provider";
|
||||
import { getSessionWorkspacePath } from "@/lib/session-workspace-path";
|
||||
import type { AppState } from "@/lib/state/store";
|
||||
@@ -77,6 +79,10 @@ export type PendingMessageScrollTarget = {
|
||||
token: number;
|
||||
hostPanelId: string;
|
||||
};
|
||||
/** Reports whether a target has a dedicated DOM row in the transcript. */
|
||||
export function isMessageRowRendered(items: readonly RenderItem[], messageId: string): boolean {
|
||||
return items.some((item) => item.type === "message" && item.message.id === messageId);
|
||||
}
|
||||
|
||||
/** Scrolls a non-Dockview host target after the message row becomes rendered. */
|
||||
type PendingMessageScrollOptions = {
|
||||
@@ -1063,8 +1069,7 @@ export const TaskChatPanel = memo(function TaskChatPanel({
|
||||
messageListRef,
|
||||
isInitialMessagesLoading,
|
||||
targetRendered: Boolean(
|
||||
dockviewTargetMessageId &&
|
||||
allMessages.some((message) => message.id === dockviewTargetMessageId),
|
||||
dockviewTargetMessageId && isMessageRowRendered(groupedItems, dockviewTargetMessageId),
|
||||
),
|
||||
renderedMessageCount: allMessages.length,
|
||||
});
|
||||
@@ -1229,6 +1234,7 @@ export const TaskChatPanel = memo(function TaskChatPanel({
|
||||
<ClarificationPanelSection
|
||||
pending={Boolean(pendingClarification)}
|
||||
messages={pendingClarificationGroup}
|
||||
agentDisconnected={session?.pending_action === null}
|
||||
onResolved={handleClarificationResolved}
|
||||
shortcutScopeRef={panelRef}
|
||||
maxHeightVh={50}
|
||||
|
||||
+13
-7
@@ -17,18 +17,24 @@ Playwright-based end-to-end tests. Each Playwright worker spawns its own real Go
|
||||
|
||||
E2E runs the **prebuilt** backend, not a live rebuild — `fixtures/backend.ts` spawns
|
||||
`apps/backend/bin/kandev`, and `pnpm run build:e2e` only rebuilds the Vite bundle.
|
||||
Before running specs (or after touching anything under `apps/backend`), rebuild both:
|
||||
Before running specs (or after touching either fixture source or backend code),
|
||||
build in this order:
|
||||
|
||||
```sh
|
||||
make -C apps/backend build # bin/kandev, bin/mock-agent
|
||||
make -C apps/backend e2e-plugin-package # .build/kandev-plugin-e2e-1.0.0.tar.gz, for tests/plugins/plugins.spec.ts
|
||||
make -C apps/backend build
|
||||
cd apps/web && pnpm run build:e2e
|
||||
make -C apps/backend e2e-plugin-ui
|
||||
make -C apps/backend e2e-plugin-package
|
||||
```
|
||||
|
||||
These are prerequisites, not optional steps — `global-setup.ts` fails fast with the
|
||||
exact remedy command if a required backend artifact is older than any file under
|
||||
`apps/backend` or is missing. This includes the fixture plugin package. The
|
||||
`containers` project also checks the Linux mock-agent and agentctl binaries.
|
||||
`e2e-plugin-ui` deletes generated UI and rebuilds it from
|
||||
`apps/web/e2e/fixtures/plugins/prompt-history-plugin/`. Packaging depends on
|
||||
that phony target, then writes the archive and
|
||||
`apps/backend/.build/e2e-plugin-identity.json`. The schema-version-2 identity
|
||||
binds the source, generated output, manifest, archive digest, capability,
|
||||
minimum Kandev version, and panel key. Global setup verifies every field and
|
||||
digest before any spec installs the package, so direct package use cannot hide
|
||||
missing or stale generated UI.
|
||||
|
||||
"Any file", not just `*.go`: the binary `//go:embed`s a large asset surface, so
|
||||
editing `internal/profiles/profiles.yaml` (runtime feature-flag defaults), a
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,7 +3,7 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import globalSetup, { assertBackendArtifactsFresh, isContainerRun } from "./global-setup";
|
||||
import { assertBackendArtifactsFresh, isContainerRun, runGlobalSetup } from "./global-setup";
|
||||
|
||||
let backendDir: string;
|
||||
let binPath: string;
|
||||
@@ -182,6 +182,20 @@ describe("assertBackendBinaryFresh", () => {
|
||||
expect(() => assertBackendBinaryFresh(backendDir, [binPath], {})).not.toThrow();
|
||||
});
|
||||
|
||||
it("ignores generated plugin fixture UI rebuilt after the backend binary", () => {
|
||||
touch(
|
||||
path.join(backendDir, "internal", "task", "service.go"),
|
||||
new Date("2025-01-01T00:00:00Z"),
|
||||
);
|
||||
touch(binPath, new Date("2026-01-01T00:00:00Z"));
|
||||
const generated = path.join(backendDir, "cmd", "plugin-fixture", "fixture-package", "ui");
|
||||
fs.mkdirSync(generated, { recursive: true });
|
||||
fs.writeFileSync(path.join(generated, "bundle.js"), "generated");
|
||||
touch(path.join(generated, "bundle.js"), new Date("2026-06-01T00:00:00Z"));
|
||||
|
||||
expect(() => assertBackendBinaryFresh(backendDir, [binPath], {})).not.toThrow();
|
||||
});
|
||||
|
||||
it("ignores Go coverage and compiled-test artifacts left in the source tree", () => {
|
||||
// `make -C apps/backend test-coverage` writes these next to go.mod. They are
|
||||
// gitignored build outputs, so a test run must not demand a backend rebuild.
|
||||
@@ -345,7 +359,7 @@ describe("globalSetup", () => {
|
||||
vi.stubEnv("KANDEV_E2E_SKIP_FRESHNESS", "1");
|
||||
const existsSync = vi.spyOn(fs, "existsSync").mockReturnValue(true);
|
||||
|
||||
(globalSetup as () => void)();
|
||||
runGlobalSetup({ verifyPluginFixtureIdentity: false });
|
||||
|
||||
expect(existsSync).toHaveBeenCalledWith(customBackend);
|
||||
expect(existsSync).not.toHaveBeenCalledWith(path.join(backendDirForTest(), "bin", "kandev"));
|
||||
@@ -358,7 +372,9 @@ describe("globalSetup", () => {
|
||||
.spyOn(fs, "existsSync")
|
||||
.mockImplementation((artifactPath) => artifactPath !== customBackend);
|
||||
|
||||
expect(() => (globalSetup as () => void)()).toThrow(/KANDEV_E2E_BIN file does not exist/);
|
||||
expect(() => runGlobalSetup({ verifyPluginFixtureIdentity: false })).toThrow(
|
||||
/KANDEV_E2E_BIN file does not exist/,
|
||||
);
|
||||
expect(existsSync).toHaveBeenCalledWith(customBackend);
|
||||
});
|
||||
|
||||
@@ -376,7 +392,7 @@ describe("globalSetup", () => {
|
||||
vi.stubEnv(marker, value);
|
||||
const existsSync = vi.spyOn(fs, "existsSync").mockReturnValue(true);
|
||||
|
||||
(globalSetup as () => void)();
|
||||
runGlobalSetup({ verifyPluginFixtureIdentity: false });
|
||||
|
||||
expect(existsSync).toHaveBeenCalledWith(
|
||||
path.join(backendDirForTest(), "bin", "mock-agent-linux-amd64"),
|
||||
@@ -393,7 +409,7 @@ describe("globalSetup", () => {
|
||||
process.argv = [...originalArgv, "--project=containers"];
|
||||
const existsSync = vi.spyOn(fs, "existsSync").mockReturnValue(true);
|
||||
|
||||
(globalSetup as () => void)();
|
||||
runGlobalSetup({ verifyPluginFixtureIdentity: false });
|
||||
|
||||
expect(existsSync).toHaveBeenCalledWith(
|
||||
path.join(backendDirForTest(), "bin", "mock-agent-linux-amd64"),
|
||||
@@ -410,7 +426,7 @@ describe("globalSetup", () => {
|
||||
process.argv = [...originalArgv, "--project=kubernetes-compat"];
|
||||
const existsSync = vi.spyOn(fs, "existsSync").mockReturnValue(true);
|
||||
|
||||
(globalSetup as () => void)();
|
||||
runGlobalSetup({ verifyPluginFixtureIdentity: false });
|
||||
|
||||
expect(existsSync).toHaveBeenCalledWith(
|
||||
path.join(backendDirForTest(), "bin", "mock-agent-linux-amd64"),
|
||||
@@ -426,7 +442,7 @@ describe("globalSetup", () => {
|
||||
vi.stubEnv("KANDEV_E2E_DOCKER", "");
|
||||
const existsSync = vi.spyOn(fs, "existsSync").mockReturnValue(true);
|
||||
|
||||
(globalSetup as () => void)();
|
||||
runGlobalSetup({ verifyPluginFixtureIdentity: false });
|
||||
|
||||
expect(existsSync).not.toHaveBeenCalledWith(
|
||||
path.join(backendDirForTest(), "bin", "mock-agent-linux-amd64"),
|
||||
|
||||
@@ -10,7 +10,15 @@ export type BackendArtifact = {
|
||||
rebuildTarget: string;
|
||||
};
|
||||
|
||||
export type GlobalSetupOptions = {
|
||||
verifyPluginFixtureIdentity?: boolean;
|
||||
};
|
||||
|
||||
export default function globalSetup() {
|
||||
runGlobalSetup();
|
||||
}
|
||||
|
||||
export function runGlobalSetup(options: GlobalSetupOptions = {}): void {
|
||||
const customKandevBin = process.env.KANDEV_E2E_BIN;
|
||||
const kandevBin = customKandevBin ?? path.join(BACKEND_DIR, "bin", "kandev");
|
||||
const artifacts: BackendArtifact[] = [
|
||||
@@ -44,6 +52,11 @@ export default function globalSetup() {
|
||||
assertArtifactExists(artifact.path, artifact.rebuildTarget);
|
||||
}
|
||||
assertBackendArtifactsFresh(BACKEND_DIR, artifacts);
|
||||
const pluginIdentityPath = path.join(BACKEND_DIR, ".build", "e2e-plugin-identity.json");
|
||||
assertArtifactExists(pluginIdentityPath, "e2e-plugin-package");
|
||||
if (options.verifyPluginFixtureIdentity !== false) {
|
||||
assertPluginFixtureIdentity(pluginIdentityPath);
|
||||
}
|
||||
|
||||
const spaIndex = path.join(WEB_DIR, "dist", "index.html");
|
||||
if (!fs.existsSync(spaIndex)) {
|
||||
@@ -136,6 +149,7 @@ const BACKEND_SOURCE_SKIP_FILE_PATTERNS = [
|
||||
* pointless backend rebuild over a bundle the tests ignore.
|
||||
*/
|
||||
const EMBEDDED_WEB_DIR = path.join("internal", "webapp", "embedded", "generated");
|
||||
const GENERATED_PLUGIN_FIXTURE_UI_DIR = path.join("cmd", "plugin-fixture", "fixture-package", "ui");
|
||||
|
||||
/**
|
||||
* Every file counts, not just `*.go`: the binary `//go:embed`s a large asset
|
||||
@@ -160,6 +174,7 @@ function findNewestBackendSource(
|
||||
if (entry.isDirectory()) {
|
||||
if (BACKEND_SOURCE_SKIP_DIRS.has(entry.name)) continue;
|
||||
if (path.relative(backendDir, entryPath) === EMBEDDED_WEB_DIR) continue;
|
||||
if (path.relative(backendDir, entryPath) === GENERATED_PLUGIN_FIXTURE_UI_DIR) continue;
|
||||
walk(entryPath);
|
||||
continue;
|
||||
}
|
||||
@@ -276,6 +291,73 @@ function validateE2EBuildIdentity(
|
||||
return true;
|
||||
}
|
||||
|
||||
type PluginFixtureIdentity = {
|
||||
schemaVersion: number;
|
||||
pluginId: string;
|
||||
pluginVersion: string;
|
||||
bundlePath: string;
|
||||
sourceHash: string;
|
||||
generatedOutputHash: string;
|
||||
manifestHash: string;
|
||||
archiveSha256: string;
|
||||
manifestCapability: string;
|
||||
minKandevVersion: string;
|
||||
panelKey: string;
|
||||
};
|
||||
|
||||
function assertPluginFixtureIdentity(identityPath: string): void {
|
||||
const identity = JSON.parse(fs.readFileSync(identityPath, "utf8")) as PluginFixtureIdentity;
|
||||
if (
|
||||
identity.schemaVersion !== 2 ||
|
||||
identity.pluginId !== "kandev-plugin-e2e" ||
|
||||
identity.pluginVersion !== "1.0.0" ||
|
||||
identity.bundlePath !== "ui/bundle.js" ||
|
||||
identity.manifestCapability !== "messages" ||
|
||||
identity.minKandevVersion !== "0.91.1" ||
|
||||
identity.panelKey !== "prompt-history-plugin"
|
||||
) {
|
||||
throw new Error(`Unsupported E2E plugin identity schema in ${identityPath}`);
|
||||
}
|
||||
const source = path.join(
|
||||
WEB_DIR,
|
||||
"e2e",
|
||||
"fixtures",
|
||||
"plugins",
|
||||
"prompt-history-plugin",
|
||||
"bundle.js",
|
||||
);
|
||||
const output = path.join(
|
||||
BACKEND_DIR,
|
||||
"cmd",
|
||||
"plugin-fixture",
|
||||
"fixture-package",
|
||||
"ui",
|
||||
"bundle.js",
|
||||
);
|
||||
const manifest = path.join(
|
||||
BACKEND_DIR,
|
||||
"cmd",
|
||||
"plugin-fixture",
|
||||
"fixture-package",
|
||||
"manifest.yaml",
|
||||
);
|
||||
const archive = path.join(BACKEND_DIR, ".build", "kandev-plugin-e2e-1.0.0.tar.gz");
|
||||
for (const [file, expected] of [
|
||||
[source, identity.sourceHash],
|
||||
[output, identity.generatedOutputHash],
|
||||
[manifest, identity.manifestHash],
|
||||
[archive, identity.archiveSha256],
|
||||
]) {
|
||||
assertArtifactExists(file);
|
||||
const actual = sha256File(file);
|
||||
if (actual !== expected) {
|
||||
throw new Error(
|
||||
`E2E plugin identity checksum mismatch for ${file}: got ${actual}, expected ${expected}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fail fast when the coverage oracle is asked to run against a bundle that has
|
||||
* no pseudo catalog.
|
||||
|
||||
@@ -1519,7 +1519,7 @@ export class ApiClient {
|
||||
turnStartedAt?: string;
|
||||
turnCompletedAt?: string;
|
||||
},
|
||||
): Promise<void> {
|
||||
): Promise<{ messageId: string; turnId: string | null }> {
|
||||
const body: Record<string, unknown> = { session_id: sessionId, type: opts.type };
|
||||
if (opts.content !== undefined) body.content = opts.content;
|
||||
if (opts.metadata !== undefined) body.metadata = opts.metadata;
|
||||
@@ -1529,7 +1529,24 @@ export class ApiClient {
|
||||
if (opts.newTurn !== undefined) body.new_turn = opts.newTurn;
|
||||
if (opts.turnStartedAt !== undefined) body.turn_started_at = opts.turnStartedAt;
|
||||
if (opts.turnCompletedAt !== undefined) body.turn_completed_at = opts.turnCompletedAt;
|
||||
await this.request("POST", "/api/v1/_test/messages", body);
|
||||
const result = await this.request<{ message_id: string; turn_id?: string | null }>(
|
||||
"POST",
|
||||
"/api/v1/_test/messages",
|
||||
body,
|
||||
);
|
||||
return { messageId: result.message_id, turnId: result.turn_id ?? null };
|
||||
}
|
||||
|
||||
async updateSessionMessage(messageId: string, content: string): Promise<void> {
|
||||
await this.request("PATCH", `/api/v1/_test/messages/${messageId}`, { content });
|
||||
}
|
||||
|
||||
async deleteSessionMessage(messageId: string): Promise<void> {
|
||||
await this.request("DELETE", `/api/v1/_test/messages/${messageId}`);
|
||||
}
|
||||
|
||||
async completeSessionTurn(turnId: string): Promise<void> {
|
||||
await this.request("POST", `/api/v1/_test/turns/${turnId}/complete`);
|
||||
}
|
||||
|
||||
async seedToolCallMessages(
|
||||
@@ -2599,7 +2616,7 @@ export class ApiClient {
|
||||
}
|
||||
|
||||
async deleteSession(sessionId: string): Promise<void> {
|
||||
await this.request("DELETE", `/api/v1/task-sessions/${sessionId}`);
|
||||
await this.request("DELETE", `/api/v1/_test/task-sessions/${sessionId}`);
|
||||
}
|
||||
|
||||
async getTask(taskId: string): Promise<{
|
||||
|
||||
@@ -150,6 +150,23 @@ describe("watchWs().waitForEvent", () => {
|
||||
payload: { task_id: "t1", status: "cancelled" },
|
||||
});
|
||||
});
|
||||
it("resolves on an ordered session event using its legacy action alias", async () => {
|
||||
const { page, fake } = fakePage();
|
||||
const ws = watchWs(page);
|
||||
const socket = fake.openSocket(GATEWAY);
|
||||
const pending = ws.waitForEvent("session.message.added", {
|
||||
where: (payload) => payload.session_id === "session-1",
|
||||
});
|
||||
socket.emit("framereceived", {
|
||||
type: "session.event",
|
||||
event_type: "message.added",
|
||||
payload: { session_id: "session-1", message_id: "message-1" },
|
||||
});
|
||||
await expect(pending).resolves.toMatchObject({
|
||||
eventType: "message.added",
|
||||
payload: { session_id: "session-1", message_id: "message-1" },
|
||||
});
|
||||
});
|
||||
|
||||
it("skips notifications that fail the `where` predicate", async () => {
|
||||
const { page, fake } = fakePage();
|
||||
|
||||
@@ -124,6 +124,7 @@ export type WsFrame = {
|
||||
id?: string;
|
||||
type?: string;
|
||||
action?: string;
|
||||
eventType?: string;
|
||||
payload: Record<string, unknown>;
|
||||
};
|
||||
|
||||
@@ -160,6 +161,7 @@ function decodeFrame(payload: string | Buffer | Uint8Array): WsFrame | null {
|
||||
id: typeof parsed.id === "string" ? parsed.id : undefined,
|
||||
type: typeof parsed.type === "string" ? parsed.type : undefined,
|
||||
action: typeof parsed.action === "string" ? parsed.action : undefined,
|
||||
eventType: typeof parsed.event_type === "string" ? parsed.event_type : undefined,
|
||||
payload: body && typeof body === "object" ? (body as Record<string, unknown>) : {},
|
||||
};
|
||||
} catch {
|
||||
@@ -214,6 +216,12 @@ export function watchWs(page: Page): WsWatcher {
|
||||
};
|
||||
}
|
||||
|
||||
function matchesEventAction(frame: WsFrame, action: string): boolean {
|
||||
if (frame.action === action) return true;
|
||||
if (frame.type !== "session.event") return false;
|
||||
const orderedEventType = action.startsWith("session.") ? action.slice("session.".length) : action;
|
||||
return frame.eventType === action || frame.eventType === orderedEventType;
|
||||
}
|
||||
function waitForEvent(
|
||||
channels: Channels,
|
||||
action: string,
|
||||
@@ -227,14 +235,9 @@ function waitForEvent(
|
||||
reject,
|
||||
);
|
||||
wait.listen(channels.received, (frame) => {
|
||||
if (frame.action !== action) return;
|
||||
// Strict, not `frame.type && ...`: a frame carrying the right `action` but
|
||||
// no `type` is not a confirmed server push, and resolving on one would be
|
||||
// the exact false-positive this module exists to remove. Matches the
|
||||
// sibling guard in `waitForResponse`. Observed gateway traffic only ever
|
||||
// carries `notification` / `response` / `request`, so nothing legitimate
|
||||
// relies on the loose form.
|
||||
if (frame.type !== "notification") return;
|
||||
if (!matchesEventAction(frame, action)) return;
|
||||
// Strictly require a server-push frame, including ordered session events.
|
||||
if (frame.type !== "notification" && frame.type !== "session.event") return;
|
||||
if (where && !where(frame.payload)) return;
|
||||
wait.dispose();
|
||||
resolve(frame);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import path from "node:path";
|
||||
import { type Page, expect } from "@playwright/test";
|
||||
import type { ApiClient } from "./api-client";
|
||||
|
||||
export const PLUGIN_ID = "kandev-plugin-e2e";
|
||||
export const PACKAGE_PATH = path.resolve(
|
||||
@@ -7,6 +8,10 @@ export const PACKAGE_PATH = path.resolve(
|
||||
"../../../backend/.build/kandev-plugin-e2e-1.0.0.tar.gz",
|
||||
);
|
||||
|
||||
export async function uninstallFixturePlugin(apiClient: ApiClient): Promise<void> {
|
||||
await apiClient.rawRequest("DELETE", `/api/plugins/${PLUGIN_ID}`).catch(() => undefined);
|
||||
}
|
||||
|
||||
export async function installFixturePlugin(page: Page): Promise<void> {
|
||||
await page.goto("/settings/plugins");
|
||||
await page.getByTestId("install-plugin-trigger").click();
|
||||
|
||||
@@ -60,7 +60,10 @@ export function noisyReceivedFrames(
|
||||
frame.sessionId === sessionId &&
|
||||
// A subscriber present before the burst can receive its coalesced state
|
||||
// as an add; a subscriber joining mid-burst observes subsequent updates.
|
||||
(frame.action === "session.message.added" || frame.action === "session.message.updated"),
|
||||
(frame.action === "session.message.added" ||
|
||||
frame.action === "session.message.updated" ||
|
||||
(frame.type === "session.event" &&
|
||||
(frame.eventType === "message.added" || frame.eventType === "message.updated"))),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,12 @@ type MessageAddResponseDropController = {
|
||||
droppedCount: () => number;
|
||||
};
|
||||
|
||||
type ExpiredPluginSnapshotController = {
|
||||
expireNextPluginSnapshot: () => void;
|
||||
modifiedCount: () => number;
|
||||
pluginSubscribeCount: () => number;
|
||||
};
|
||||
|
||||
export type QueueAdmissionDropController = {
|
||||
dropNextQueueAddRequest: () => void;
|
||||
dropNextQueueAddResponse: (count?: number) => void;
|
||||
@@ -44,18 +50,34 @@ function parseJSONFrames(message: string | Buffer): Array<Record<string, unknown
|
||||
return frames;
|
||||
}
|
||||
|
||||
function targetMessagePayload(message: unknown, prompt: string): Record<string, unknown> | null {
|
||||
const envelope = asRecord(message);
|
||||
const payload = asRecord(envelope?.payload);
|
||||
const isLegacy = envelope?.action === "session.message.added" && payload?.author_type === "user";
|
||||
const isOrdered =
|
||||
envelope?.type === "session.event" &&
|
||||
envelope?.event_type === "message.added" &&
|
||||
payload?.author_type === "user";
|
||||
if (
|
||||
(!isLegacy && !isOrdered) ||
|
||||
typeof payload?.content !== "string" ||
|
||||
!payload.content.includes(prompt)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
function isTargetUserMessageAdded(
|
||||
message: unknown,
|
||||
prompt: string,
|
||||
): message is { payload: { content: string } } {
|
||||
return targetMessagePayload(message, prompt) !== null;
|
||||
}
|
||||
|
||||
function targetAction(message: unknown): string {
|
||||
const envelope = asRecord(message);
|
||||
const payload = asRecord(envelope?.payload);
|
||||
return (
|
||||
envelope?.action === "session.message.added" &&
|
||||
payload?.author_type === "user" &&
|
||||
typeof payload.content === "string" &&
|
||||
payload.content.includes(prompt)
|
||||
);
|
||||
return envelope?.type === "session.event" ? "session.event" : "session.message.added";
|
||||
}
|
||||
|
||||
function filterServerFrame(
|
||||
@@ -77,7 +99,7 @@ function filterServerFrame(
|
||||
const parsed = JSON.parse(trimmed) as unknown;
|
||||
if (isTargetUserMessageAdded(parsed, prompt)) {
|
||||
didDrop = true;
|
||||
dropped.push({ action: "session.message.added", content: parsed.payload.content });
|
||||
dropped.push({ action: targetAction(parsed), content: parsed.payload.content });
|
||||
continue;
|
||||
}
|
||||
} catch {
|
||||
@@ -95,12 +117,14 @@ export async function routeMainWebSocketWithPromptDrop(page: Page): Promise<Prom
|
||||
let promptToDrop: string | null = null;
|
||||
const dropped: DroppedMessage[] = [];
|
||||
const recoveryRequestIDs = new Set<string>();
|
||||
const orderedRecoveryRequestIDs = new Set<string>();
|
||||
let recoveryResponses = 0;
|
||||
|
||||
await page.routeWebSocket(/\/ws$/, (ws) => {
|
||||
const server = ws.connectToServer();
|
||||
ws.onMessage((message) => {
|
||||
for (const frame of parseJSONFrames(message)) {
|
||||
const payload = asRecord(frame.payload);
|
||||
if (
|
||||
promptToDrop !== null &&
|
||||
frame.type === "request" &&
|
||||
@@ -109,6 +133,16 @@ export async function routeMainWebSocketWithPromptDrop(page: Page): Promise<Prom
|
||||
) {
|
||||
recoveryRequestIDs.add(frame.id);
|
||||
}
|
||||
if (
|
||||
promptToDrop !== null &&
|
||||
frame.type === "request" &&
|
||||
frame.action === "session.subscribe" &&
|
||||
payload?.consumer_kind === "core" &&
|
||||
payload.replace_cursor === true &&
|
||||
typeof frame.id === "string"
|
||||
) {
|
||||
orderedRecoveryRequestIDs.add(frame.id);
|
||||
}
|
||||
}
|
||||
server.send(message);
|
||||
});
|
||||
@@ -124,6 +158,14 @@ export async function routeMainWebSocketWithPromptDrop(page: Page): Promise<Prom
|
||||
recoveryResponses += 1;
|
||||
}
|
||||
}
|
||||
if (
|
||||
frame.type === "response" &&
|
||||
frame.action === "session.subscribe" &&
|
||||
typeof frame.id === "string" &&
|
||||
orderedRecoveryRequestIDs.delete(frame.id)
|
||||
) {
|
||||
recoveryResponses += 1;
|
||||
}
|
||||
}
|
||||
const filtered = filterServerFrame(message, promptToDrop, dropped);
|
||||
if (filtered !== null) ws.send(filtered);
|
||||
@@ -135,6 +177,7 @@ export async function routeMainWebSocketWithPromptDrop(page: Page): Promise<Prom
|
||||
promptToDrop = prompt;
|
||||
dropped.length = 0;
|
||||
recoveryRequestIDs.clear();
|
||||
orderedRecoveryRequestIDs.clear();
|
||||
recoveryResponses = 0;
|
||||
},
|
||||
droppedCount: () => dropped.length,
|
||||
@@ -364,3 +407,85 @@ export async function routeMainWebSocketWithQueueAdmissionDrops(
|
||||
droppedResponseCount: () => state.droppedResponses.value,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrites one plugin subscription expiry in the browser transport. The
|
||||
* signed token remains server-valid, so the panel must exercise its normal
|
||||
* fresh rebind path instead of relying on a relaxed backend validation rule.
|
||||
*/
|
||||
export async function routeMainWebSocketWithExpiredPluginSnapshot(
|
||||
page: Page,
|
||||
): Promise<ExpiredPluginSnapshotController> {
|
||||
const requestIDs = new Set<string>();
|
||||
let armed = false;
|
||||
let modified = 0;
|
||||
let pluginSubscribeRequests = 0;
|
||||
|
||||
await page.routeWebSocket(/\/ws$/, (ws) => {
|
||||
const server = ws.connectToServer();
|
||||
ws.onMessage((message) => {
|
||||
for (const frame of parseJSONFrames(message)) {
|
||||
const payload = asRecord(frame.payload);
|
||||
if (
|
||||
frame.type === "request" &&
|
||||
frame.action === "session.subscribe" &&
|
||||
typeof frame.id === "string" &&
|
||||
payload?.consumer_kind === "plugin"
|
||||
) {
|
||||
pluginSubscribeRequests += 1;
|
||||
if (armed) requestIDs.add(frame.id);
|
||||
}
|
||||
}
|
||||
server.send(message);
|
||||
});
|
||||
server.onMessage((message) => {
|
||||
if (typeof message !== "string") {
|
||||
ws.send(message);
|
||||
return;
|
||||
}
|
||||
const rewritten: string[] = [];
|
||||
let didRewrite = false;
|
||||
for (const part of message.split("\n")) {
|
||||
const trimmed = part.trim();
|
||||
if (!trimmed) {
|
||||
rewritten.push(part);
|
||||
continue;
|
||||
}
|
||||
let frame: Record<string, unknown> | null = null;
|
||||
try {
|
||||
frame = asRecord(JSON.parse(trimmed));
|
||||
} catch {
|
||||
// Preserve non-JSON frames.
|
||||
}
|
||||
if (
|
||||
armed &&
|
||||
frame?.type === "response" &&
|
||||
frame.action === "session.subscribe" &&
|
||||
typeof frame.id === "string" &&
|
||||
requestIDs.delete(frame.id)
|
||||
) {
|
||||
const payload = asRecord(frame.payload);
|
||||
if (payload?.success === true) {
|
||||
frame.payload = { ...payload, expires_at: "2000-01-01T00:00:00Z" };
|
||||
rewritten.push(JSON.stringify(frame));
|
||||
armed = false;
|
||||
modified += 1;
|
||||
didRewrite = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
rewritten.push(part);
|
||||
}
|
||||
ws.send(didRewrite ? rewritten.join("\n") : message);
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
expireNextPluginSnapshot: () => {
|
||||
requestIDs.clear();
|
||||
armed = true;
|
||||
},
|
||||
modifiedCount: () => modified,
|
||||
pluginSubscribeCount: () => pluginSubscribeRequests,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ export type GatewayTrafficFrame = {
|
||||
direction: "sent" | "received";
|
||||
action?: string;
|
||||
type?: string;
|
||||
eventType?: string;
|
||||
sessionId?: string;
|
||||
taskId?: string;
|
||||
bytes: number;
|
||||
@@ -74,6 +75,7 @@ function frameFromPayload(
|
||||
return {
|
||||
direction,
|
||||
action: stringField(message?.action),
|
||||
eventType: stringField((message as Record<string, unknown> | null)?.event_type),
|
||||
type: stringField(message?.type),
|
||||
sessionId:
|
||||
stringField(messagePayload?.session_id) ??
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user