feat: remove nextjs production runtime (#1389)
This commit is contained in:
@@ -84,16 +84,15 @@ jobs:
|
||||
path: apps/backend/bin/
|
||||
retention-days: 1
|
||||
|
||||
# Tar the .next directory to preserve pnpm symlinks in the standalone build.
|
||||
# upload-artifact dereferences symlinks which breaks the Node.js module resolution.
|
||||
# Tar the Vite dist directory so shards can reuse the same static web build.
|
||||
- name: Package web build
|
||||
run: tar -cf apps/web/next-build.tar -C apps/web .next
|
||||
run: tar -cf apps/web/web-dist.tar -C apps/web dist
|
||||
|
||||
- name: Upload web build
|
||||
uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: e2e-web-build
|
||||
path: apps/web/next-build.tar
|
||||
path: apps/web/web-dist.tar
|
||||
retention-days: 1
|
||||
|
||||
e2e:
|
||||
@@ -122,15 +121,8 @@ jobs:
|
||||
defaults:
|
||||
run:
|
||||
shell: bash
|
||||
# Node 18+'s `dns.lookup` defaults to `verbatim: true`, returning the OS
|
||||
# resolver order. Inside this container `localhost` resolves to ::1 first,
|
||||
# so `net.Server.listen({ host: "localhost" })` (used by Next.js standalone
|
||||
# via $HOSTNAME) binds IPv6-only — and undici's `fetch("http://localhost…")`
|
||||
# in the test runner only tries the first resolved address (IPv4), so the
|
||||
# frontend health check times out and every spec fails. Forcing
|
||||
# IPv4-first ordering aligns both the bind and the fetch and matches the
|
||||
# effective default on the github-hosted ubuntu runner this workflow
|
||||
# previously ran on.
|
||||
# Keep localhost resolution deterministic for Node fetches used by the
|
||||
# Playwright fixtures and backend readiness polling.
|
||||
env:
|
||||
NODE_OPTIONS: --dns-result-order=ipv4first
|
||||
strategy:
|
||||
@@ -173,7 +165,7 @@ jobs:
|
||||
|
||||
- name: Extract web build
|
||||
working-directory: apps/web
|
||||
run: tar -xf next-build.tar && rm next-build.tar
|
||||
run: tar -xf web-dist.tar && rm web-dist.tar
|
||||
|
||||
# Pinned to chromium + mobile-chrome so the dedicated `e2e-containers`
|
||||
# job below owns the real-Docker project (which covers both the Docker
|
||||
@@ -334,7 +326,7 @@ jobs:
|
||||
|
||||
- name: Extract web build
|
||||
working-directory: apps/web
|
||||
run: tar -xf next-build.tar && rm next-build.tar
|
||||
run: tar -xf web-dist.tar && rm web-dist.tar
|
||||
|
||||
- name: Run container-backed E2E tests shard ${{ matrix.shard }}/6 (Docker executor + SSH executor)
|
||||
working-directory: apps/web
|
||||
|
||||
@@ -5,7 +5,7 @@ name: release
|
||||
#
|
||||
# Flow (one click → everything below):
|
||||
# 1. prepare — bump version, regen CHANGELOG, open + merge release PR, push tag vX.Y.Z
|
||||
# 2. build-web — build Next.js standalone bundle (shared across arches)
|
||||
# 2. build-web — build Vite SPA bundle (shared across arches)
|
||||
# 3. build-cli — build + esbuild-bundle CLI (shared across arches)
|
||||
# 4. build-bundles — assemble bin/ + web/ + cli/ tarballs for 5 platforms
|
||||
# 5. docker-{amd64,arm64} — native per-arch image builds (no QEMU), consuming
|
||||
@@ -251,7 +251,7 @@ jobs:
|
||||
echo "DRY RUN: CHANGELOG.md regenerated (in-runner only)."
|
||||
|
||||
build-web:
|
||||
name: Build web standalone
|
||||
name: Build web SPA
|
||||
needs: prepare
|
||||
if: ${{ !inputs.dry_run }}
|
||||
runs-on: ubuntu-latest
|
||||
@@ -269,16 +269,16 @@ jobs:
|
||||
cache-dependency-path: apps/pnpm-lock.yaml
|
||||
- name: Install dependencies
|
||||
run: pnpm -C apps install --frozen-lockfile
|
||||
- name: Build web (standalone)
|
||||
- name: Build web (Vite)
|
||||
env:
|
||||
KANDEV_VERSION: ${{ needs.prepare.outputs.tag }}
|
||||
VITE_KANDEV_API_PORT: ""
|
||||
VITE_KANDEV_DEBUG: ""
|
||||
run: pnpm -C apps --filter @kandev/web build
|
||||
- name: Package web bundle
|
||||
run: bash scripts/release/package-web.sh
|
||||
- uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: web-bundle
|
||||
path: dist/web
|
||||
path: apps/web/dist
|
||||
include-hidden-files: true
|
||||
|
||||
build-cli:
|
||||
@@ -351,7 +351,7 @@ jobs:
|
||||
- uses: actions/download-artifact@v5
|
||||
with:
|
||||
name: web-bundle
|
||||
path: dist/web
|
||||
path: apps/backend/internal/webapp/embedded/generated
|
||||
- uses: actions/download-artifact@v5
|
||||
with:
|
||||
name: cli-bundle
|
||||
@@ -377,6 +377,7 @@ jobs:
|
||||
CC: ${{ matrix.cc || '' }}
|
||||
KANDEV_VERSION: ${{ needs.prepare.outputs.tag }}
|
||||
run: |
|
||||
test -f apps/backend/internal/webapp/embedded/generated/index.html
|
||||
cd apps/backend
|
||||
mkdir -p bin
|
||||
EXT=${{ matrix.goos == 'windows' && '.exe' || '' }}
|
||||
@@ -408,8 +409,6 @@ jobs:
|
||||
cp apps/backend/bin/kandev${EXT} dist/kandev/bin/
|
||||
cp apps/backend/bin/agentctl${EXT} dist/kandev/bin/
|
||||
cp apps/backend/bin/agentctl-linux-amd64 dist/kandev/bin/
|
||||
mkdir -p dist/kandev/web
|
||||
cp -R dist/web/. dist/kandev/web/
|
||||
# cli/ was already placed by the build-cli job (downloaded above).
|
||||
# actions/upload-artifact + download-artifact zip-roundtrip strips the
|
||||
# executable bit, so re-apply it. The Homebrew formula's wrapper at
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
```
|
||||
apps/
|
||||
├── backend/ # Go backend (orchestrator, lifecycle, agentctl, WS gateway)
|
||||
├── web/ # Next.js frontend (SSR + WS + Zustand)
|
||||
├── web/ # Vite/React SPA frontend (Go boot payload + WS + Zustand)
|
||||
├── cli/ # CLI tool (TypeScript)
|
||||
├── landing/ # Landing page
|
||||
└── packages/ # Shared packages/types
|
||||
@@ -17,7 +17,7 @@ apps/
|
||||
|
||||
- **Package manager**: `pnpm` workspace (run from `apps/`, not repo root)
|
||||
- **Backend**: Go with Make (`make -C apps/backend test|lint|build`)
|
||||
- **Frontend**: Next.js (`cd apps && pnpm --filter @kandev/web dev|lint|typecheck`)
|
||||
- **Frontend**: Vite/React SPA (`cd apps && pnpm --filter @kandev/web dev|build:vite|lint|typecheck`)
|
||||
- **UI**: Shadcn components via `@kandev/ui`
|
||||
- **E2E**: Playwright (`cd apps/web && pnpm e2e`). The `containers` project (gated on `KANDEV_E2E_CONTAINERS=1`, formerly `docker`) covers both the Docker executor and the SSH executor — anything that needs a real Docker daemon on the host lives there. See `apps/web/e2e/README.md`.
|
||||
- **GitHub repo**: `https://github.com/kdlbs/kandev`
|
||||
@@ -37,7 +37,7 @@ Architecture notes and per-area conventions live alongside the code they describ
|
||||
- `apps/backend/internal/agentctl/AGENTS.md` — agentctl HTTP server: route groups, adapter model, ACP protocol.
|
||||
- `apps/backend/internal/agentctl/server/api/AGENTS.md` — reverse-proxy body rewriting (`Accept-Encoding`), iframe-blocking header stripping.
|
||||
- `apps/backend/internal/integrations/AGENTS.md` — adding a new third-party integration (Jira/Linear pattern, both backend and frontend halves). The `/add-integration` skill mirrors this for scaffolding new integrations.
|
||||
- `apps/web/AGENTS.md` — Next.js frontend: shadcn imports, SSR-hydrate-store data flow, store slice structure (incl. `office`), WS format, component conventions, TS lint limits.
|
||||
- `apps/web/AGENTS.md` — Vite/React SPA frontend: shadcn imports, Go boot-payload hydration, store slice structure (incl. `office`), WS format, component conventions, TS lint limits.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
# Kandev Root Makefile
|
||||
# Orchestrates both backend (Go) and web app (Next.js)
|
||||
# Orchestrates both backend (Go) and web app (Vite/React)
|
||||
|
||||
# Directories
|
||||
BACKEND_DIR := apps/backend
|
||||
WEB_DIR := apps/web
|
||||
APPS_DIR := apps
|
||||
EMBEDDED_WEB_DIR := $(BACKEND_DIR)/internal/webapp/embedded/generated
|
||||
|
||||
# Tools
|
||||
PNPM := pnpm
|
||||
@@ -194,7 +195,7 @@ dev-web:
|
||||
#
|
||||
|
||||
.PHONY: build
|
||||
build: build-backend build-web
|
||||
build: build-web sync-embedded-web build-backend
|
||||
@printf "\n$(GREEN)$(BOLD)✓ Build complete!$(RESET)\n"
|
||||
|
||||
#
|
||||
@@ -208,8 +209,9 @@ start:
|
||||
@$(MAKE) -s install-web
|
||||
$(call success,Dependencies installed)
|
||||
$(call phase,Building)
|
||||
@$(MAKE) -s build-backend-quiet
|
||||
@$(MAKE) -s build-web-quiet
|
||||
@$(MAKE) -s sync-embedded-web
|
||||
@$(MAKE) -s build-backend-quiet
|
||||
$(call success,Build complete)
|
||||
$(call phase,Starting Server)
|
||||
@cd $(APPS_DIR) && $(PNPM) -C cli dev -- start $(if $(filter 1 true yes,$(VERBOSE)),--verbose,) $(if $(filter 1 true yes,$(DEBUG)),--debug,)
|
||||
@@ -229,7 +231,6 @@ start-debug:
|
||||
.PHONY: service-bundle
|
||||
service-bundle: install build
|
||||
$(call phase,Packaging Service Bundle)
|
||||
@scripts/release/package-web.sh
|
||||
@scripts/release/package-cli.sh
|
||||
@test -n "$(SERVICE_BUNDLE_DIR)" || { echo "SERVICE_BUNDLE_DIR is empty; aborting."; exit 1; }
|
||||
@test "$(SERVICE_BUNDLE_DIR)" != "/" || { echo "SERVICE_BUNDLE_DIR must not be /; aborting."; exit 1; }
|
||||
@@ -299,12 +300,20 @@ build-backend-quiet:
|
||||
.PHONY: build-web
|
||||
build-web:
|
||||
@printf "$(CYAN)Building web app...$(RESET)\n"
|
||||
@cd $(APPS_DIR) && $(PNPM) --filter @kandev/web build
|
||||
@cd $(APPS_DIR) && VITE_KANDEV_API_PORT= VITE_KANDEV_DEBUG= $(PNPM) --filter @kandev/web build
|
||||
|
||||
.PHONY: build-web-quiet
|
||||
build-web-quiet:
|
||||
@printf " $(DIM)Web app$(RESET)\n"
|
||||
@cd $(APPS_DIR) && $(PNPM) --filter @kandev/web build 2>&1 | grep -v "Warning:" | grep -v "parseLineType" | grep -v "^$$" || true
|
||||
@cd $(APPS_DIR) && VITE_KANDEV_API_PORT= VITE_KANDEV_DEBUG= $(PNPM) --filter @kandev/web build 2>&1 | grep -v "Warning:" | grep -v "parseLineType" | grep -v "^$$" || true
|
||||
|
||||
.PHONY: sync-embedded-web
|
||||
sync-embedded-web:
|
||||
@test -f "$(WEB_DIR)/dist/index.html" || { echo "Missing $(WEB_DIR)/dist/index.html; run 'make build-web' first."; exit 1; }
|
||||
@mkdir -p "$(EMBEDDED_WEB_DIR)"
|
||||
@find "$(EMBEDDED_WEB_DIR)" -mindepth 1 ! -name .gitignore ! -name keep.txt -exec rm -rf {} +
|
||||
@cp -R "$(WEB_DIR)/dist/." "$(EMBEDDED_WEB_DIR)/"
|
||||
@printf " $(DIM)Embedded web assets$(RESET)\n"
|
||||
|
||||
#
|
||||
# Installation
|
||||
@@ -480,7 +489,7 @@ clean-backend:
|
||||
.PHONY: clean-web
|
||||
clean-web:
|
||||
@printf "$(CYAN)Cleaning web artifacts...$(RESET)\n"
|
||||
@$(RMDIR) $(WEB_DIR)/.next $(APPS_DIR)/node_modules
|
||||
@$(RMDIR) $(WEB_DIR)/dist $(WEB_DIR)/.next $(APPS_DIR)/node_modules
|
||||
@$(RMDIR) $(APPS_DIR)/packages/*/node_modules
|
||||
|
||||
.PHONY: clean-db
|
||||
|
||||
@@ -367,6 +367,11 @@ func (a *lifecycleAdapter) IsAgentRunningForSession(ctx context.Context, session
|
||||
return a.mgr.IsAgentRunningForSession(ctx, sessionID)
|
||||
}
|
||||
|
||||
// IsAgentReadyForPrompt checks if the session can accept a prompt immediately.
|
||||
func (a *lifecycleAdapter) IsAgentReadyForPrompt(ctx context.Context, sessionID string) bool {
|
||||
return a.mgr.IsAgentReadyForPrompt(ctx, sessionID)
|
||||
}
|
||||
|
||||
// IsPassthroughSession checks if the given session is running in passthrough (PTY) mode.
|
||||
func (a *lifecycleAdapter) IsPassthroughSession(ctx context.Context, sessionID string) bool {
|
||||
return a.mgr.IsPassthroughSession(ctx, sessionID)
|
||||
|
||||
@@ -0,0 +1,721 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
agentsettingsdto "github.com/kandev/kandev/internal/agent/settings/dto"
|
||||
taskdto "github.com/kandev/kandev/internal/task/dto"
|
||||
taskmodels "github.com/kandev/kandev/internal/task/models"
|
||||
taskservice "github.com/kandev/kandev/internal/task/service"
|
||||
userdto "github.com/kandev/kandev/internal/user/dto"
|
||||
"github.com/kandev/kandev/internal/webapp"
|
||||
v1 "github.com/kandev/kandev/pkg/api/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
activeWorkspaceCookie = "kandev-active-workspace"
|
||||
legacyOfficeWorkspaceCookie = "office-active-workspace"
|
||||
)
|
||||
|
||||
func bootInitialState(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
p routeParams,
|
||||
route webapp.RouteClassification,
|
||||
) map[string]any {
|
||||
builder := bootStateBuilder{p: p}
|
||||
state := map[string]any{
|
||||
"features": p.features,
|
||||
}
|
||||
|
||||
if route.Route == webapp.RouteSettings {
|
||||
builder.addWorkspaceState(ctx, state, nil)
|
||||
builder.addUserSettingsState(ctx, state, "")
|
||||
builder.addSettingsRouteState(ctx, state, route.Path)
|
||||
}
|
||||
if route.Route == webapp.RouteHome {
|
||||
builder.addHomeKanbanRouteState(ctx, req, state)
|
||||
}
|
||||
if route.Route == webapp.RouteTasks {
|
||||
tasksState, _ := builder.tasksPageBootData(ctx, req)
|
||||
mergeBootState(state, tasksState)
|
||||
}
|
||||
if isLocalContextRoute(route.Route) {
|
||||
contextState, _ := builder.routeContextBootData(ctx, req)
|
||||
mergeBootState(state, contextState)
|
||||
}
|
||||
if route.Route == webapp.RouteOffice {
|
||||
builder.addOfficeRouteState(ctx, req, state)
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
func bootRouteData(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
p routeParams,
|
||||
route webapp.RouteClassification,
|
||||
) map[string]any {
|
||||
builder := bootStateBuilder{p: p}
|
||||
switch route.Route {
|
||||
case webapp.RouteTaskDetail:
|
||||
return builder.taskDetailRouteData(ctx, route.Params["taskId"])
|
||||
case webapp.RouteTasks:
|
||||
_, routeData := builder.tasksPageBootData(ctx, req)
|
||||
if routeData == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{"tasksPage": routeData}
|
||||
case webapp.RouteGitHub, webapp.RouteGitLab, webapp.RouteJira, webapp.RouteLinear, webapp.RouteStats:
|
||||
_, routeData := builder.routeContextBootData(ctx, req)
|
||||
if routeData == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{"routeContext": routeData}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func isLocalContextRoute(route webapp.RouteName) bool {
|
||||
switch route {
|
||||
case webapp.RouteGitHub, webapp.RouteGitLab, webapp.RouteJira, webapp.RouteLinear, webapp.RouteStats:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
type bootStateBuilder struct {
|
||||
p routeParams
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addWorkspaceState(ctx context.Context, state map[string]any, activeID *string) {
|
||||
if b.p.taskSvc == nil {
|
||||
return
|
||||
}
|
||||
workspaces, err := b.p.taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("list workspaces", err)
|
||||
return
|
||||
}
|
||||
items := make([]taskdto.WorkspaceDTO, 0, len(workspaces))
|
||||
for _, workspace := range workspaces {
|
||||
if workspace == nil {
|
||||
continue
|
||||
}
|
||||
items = append(items, taskdto.FromWorkspace(workspace))
|
||||
}
|
||||
var active any
|
||||
if activeID != nil {
|
||||
active = *activeID
|
||||
}
|
||||
state["workspaces"] = map[string]any{
|
||||
"items": items,
|
||||
"activeId": active,
|
||||
}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addUserSettingsState(ctx context.Context, state map[string]any, workspaceID string) {
|
||||
if b.p.userCtrl == nil {
|
||||
return
|
||||
}
|
||||
response, err := b.p.userCtrl.GetUserSettings(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("get user settings", err)
|
||||
return
|
||||
}
|
||||
state["userSettings"] = mapUserSettingsState(response, workspaceID)
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addSettingsRouteState(ctx context.Context, state map[string]any, path string) {
|
||||
switch path {
|
||||
case "/settings/prompts":
|
||||
b.addPromptsState(ctx, state)
|
||||
case "/settings/general/editors":
|
||||
b.addEditorsState(ctx, state)
|
||||
}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addHomeKanbanRouteState(ctx context.Context, req *http.Request, state map[string]any) {
|
||||
if b.p.taskSvc == nil {
|
||||
return
|
||||
}
|
||||
workspaces, err := b.p.taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("list home workspaces", err)
|
||||
return
|
||||
}
|
||||
workspaceItems := make([]map[string]any, 0, len(workspaces))
|
||||
workspaceIDs := make(map[string]bool, len(workspaces))
|
||||
for _, workspace := range workspaces {
|
||||
if workspace == nil {
|
||||
continue
|
||||
}
|
||||
workspaceIDs[workspace.ID] = true
|
||||
workspaceItems = append(workspaceItems, mapWorkspaceItemState(taskdto.FromWorkspace(workspace)))
|
||||
}
|
||||
|
||||
settings, hasSettings := b.userSettings(ctx)
|
||||
settingsWorkspaceID := ""
|
||||
settingsWorkflowID := ""
|
||||
if hasSettings {
|
||||
settingsWorkspaceID = settings.Settings.WorkspaceID
|
||||
settingsWorkflowID = settings.Settings.WorkflowFilterID
|
||||
}
|
||||
activeWorkspaceID := firstValidID(
|
||||
workspaceIDs,
|
||||
queryValue(req, "workspaceId"),
|
||||
readActiveWorkspaceCookie(req),
|
||||
settingsWorkspaceID,
|
||||
firstWorkspaceID(workspaces),
|
||||
)
|
||||
state["workspaces"] = map[string]any{
|
||||
"items": workspaceItems,
|
||||
"activeId": nullString(activeWorkspaceID),
|
||||
}
|
||||
if hasSettings {
|
||||
state["userSettings"] = mapUserSettingsState(settings, activeWorkspaceID)
|
||||
}
|
||||
if activeWorkspaceID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
workflows, err := b.homeWorkflows(ctx, activeWorkspaceID)
|
||||
if err != nil {
|
||||
b.logBootError("list home workflows", err)
|
||||
return
|
||||
}
|
||||
workflowItems := make([]map[string]any, 0, len(workflows))
|
||||
for _, workflow := range workflows {
|
||||
if workflow == nil {
|
||||
continue
|
||||
}
|
||||
workflowItems = append(workflowItems, mapWorkflowItemState(taskdto.FromWorkflow(workflow)))
|
||||
}
|
||||
activeWorkflowID := resolveHomeWorkflowID(workflows, queryValue(req, "workflowId"), settingsWorkflowID, hasSettings)
|
||||
state["workflows"] = map[string]any{
|
||||
"items": workflowItems,
|
||||
"activeId": nullString(activeWorkflowID),
|
||||
}
|
||||
if hasSettings {
|
||||
state["userSettings"] = mapUserSettingsStateWithWorkflow(settings, activeWorkspaceID, activeWorkflowID)
|
||||
}
|
||||
b.addRepositoriesState(ctx, state, activeWorkspaceID)
|
||||
b.addKanbanSnapshotsState(ctx, state, workflows, activeWorkflowID)
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) userSettings(ctx context.Context) (userdto.UserSettingsResponse, bool) {
|
||||
if b.p.userCtrl == nil {
|
||||
return userdto.UserSettingsResponse{}, false
|
||||
}
|
||||
response, err := b.p.userCtrl.GetUserSettings(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("get user settings", err)
|
||||
return userdto.UserSettingsResponse{}, false
|
||||
}
|
||||
return response, true
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) homeWorkflows(ctx context.Context, workspaceID string) ([]*taskmodels.Workflow, error) {
|
||||
workflows, err := b.p.taskSvc.ListWorkflows(ctx, workspaceID, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
officeIDs := b.p.taskSvc.GetOfficeWorkflowIDs(ctx)
|
||||
filtered := make([]*taskmodels.Workflow, 0, len(workflows))
|
||||
for _, workflow := range workflows {
|
||||
if workflow == nil {
|
||||
continue
|
||||
}
|
||||
if _, isOffice := officeIDs[workflow.ID]; isOffice {
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, workflow)
|
||||
}
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addRepositoriesState(ctx context.Context, state map[string]any, workspaceID string) {
|
||||
repositories, err := b.p.taskSvc.ListRepositories(ctx, workspaceID)
|
||||
if err != nil {
|
||||
b.logBootError("list home repositories", err)
|
||||
return
|
||||
}
|
||||
items := make([]taskdto.RepositoryDTO, 0, len(repositories))
|
||||
for _, repository := range repositories {
|
||||
if repository == nil {
|
||||
continue
|
||||
}
|
||||
items = append(items, taskdto.FromRepository(repository))
|
||||
}
|
||||
state["repositories"] = map[string]any{
|
||||
"itemsByWorkspaceId": map[string]any{workspaceID: items},
|
||||
"loadingByWorkspaceId": map[string]any{
|
||||
workspaceID: false,
|
||||
},
|
||||
"loadedByWorkspaceId": map[string]any{
|
||||
workspaceID: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addKanbanSnapshotsState(
|
||||
ctx context.Context,
|
||||
state map[string]any,
|
||||
workflows []*taskmodels.Workflow,
|
||||
activeWorkflowID string,
|
||||
) {
|
||||
snapshots := make(map[string]any, len(workflows))
|
||||
var active map[string]any
|
||||
for _, workflow := range workflows {
|
||||
if workflow == nil {
|
||||
continue
|
||||
}
|
||||
snapshot, ok := b.workflowSnapshotState(ctx, workflow)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
snapshots[workflow.ID] = snapshot
|
||||
if workflow.ID == activeWorkflowID {
|
||||
active = snapshot
|
||||
}
|
||||
}
|
||||
state["kanbanMulti"] = map[string]any{
|
||||
"snapshots": snapshots,
|
||||
"isLoading": false,
|
||||
}
|
||||
if active != nil {
|
||||
state["kanban"] = map[string]any{
|
||||
"workflowId": active["workflowId"],
|
||||
"steps": active["steps"],
|
||||
"tasks": active["tasks"],
|
||||
"isLoading": false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) workflowSnapshotState(ctx context.Context, workflow *taskmodels.Workflow) (map[string]any, bool) {
|
||||
steps, err := b.workflowStepStates(ctx, workflow.ID)
|
||||
if err != nil {
|
||||
b.logBootError("list home workflow steps", err)
|
||||
return nil, false
|
||||
}
|
||||
tasks, err := b.p.taskSvc.ListTasks(ctx, workflow.ID)
|
||||
if err != nil {
|
||||
b.logBootError("list home workflow tasks", err)
|
||||
return nil, false
|
||||
}
|
||||
taskStates := make([]map[string]any, 0, len(tasks))
|
||||
for _, task := range tasks {
|
||||
if task == nil || task.IsEphemeral || task.WorkflowStepID == "" {
|
||||
continue
|
||||
}
|
||||
taskStates = append(taskStates, mapKanbanTaskState(taskdto.FromTask(task)))
|
||||
}
|
||||
return map[string]any{
|
||||
"workflowId": workflow.ID,
|
||||
"workflowName": workflow.Name,
|
||||
"steps": steps,
|
||||
"tasks": taskStates,
|
||||
}, true
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) workflowStepStates(ctx context.Context, workflowID string) ([]map[string]any, error) {
|
||||
if b.p.services == nil || b.p.services.Workflow == nil {
|
||||
return []map[string]any{}, nil
|
||||
}
|
||||
steps, err := b.p.services.Workflow.ListStepsByWorkflow(ctx, workflowID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]map[string]any, 0, len(steps))
|
||||
for _, step := range steps {
|
||||
if step == nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, mapKanbanStepState(taskdto.FromWorkflowStepWithTimestamps(step)))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) taskDetailRouteData(ctx context.Context, taskID string) map[string]any {
|
||||
if b.p.taskSvc == nil || taskID == "" {
|
||||
return nil
|
||||
}
|
||||
task, err := b.p.taskSvc.GetTask(ctx, taskID)
|
||||
if err != nil {
|
||||
b.logBootError("get task detail task", err)
|
||||
return nil
|
||||
}
|
||||
sessions, err := b.p.taskSvc.ListTaskSessions(ctx, task.ID)
|
||||
if err != nil {
|
||||
b.logBootError("list task detail sessions", err)
|
||||
sessions = nil
|
||||
}
|
||||
activeSessionID := resolveTaskDetailSessionID(task, sessions)
|
||||
taskDTO := b.taskDTOWithSessionInfo(ctx, task)
|
||||
initialState := b.taskDetailInitialState(ctx, task, taskDTO, sessions, activeSessionID)
|
||||
return map[string]any{
|
||||
"taskDetail": map[string]any{
|
||||
"task": taskDTO,
|
||||
"sessionId": nullString(activeSessionID),
|
||||
"initialState": initialState,
|
||||
"initialTerminals": []any{},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resolveTaskDetailSessionID(task *taskmodels.Task, sessions []*taskmodels.TaskSession) string {
|
||||
if task != nil {
|
||||
for _, session := range sessions {
|
||||
if session != nil && session.IsPrimary {
|
||||
return session.ID
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, session := range sessions {
|
||||
if session != nil && session.ID != "" {
|
||||
return session.ID
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) taskDTOWithSessionInfo(ctx context.Context, task *taskmodels.Task) taskdto.TaskDTO {
|
||||
if task == nil {
|
||||
return taskdto.TaskDTO{}
|
||||
}
|
||||
dtos := b.taskDTOsWithSessionInfo(ctx, []*taskmodels.Task{task})
|
||||
if len(dtos) == 0 {
|
||||
return taskdto.FromTask(task)
|
||||
}
|
||||
return dtos[0]
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) taskDTOsWithSessionInfo(ctx context.Context, tasks []*taskmodels.Task) []taskdto.TaskDTO {
|
||||
if len(tasks) == 0 {
|
||||
return []taskdto.TaskDTO{}
|
||||
}
|
||||
taskIDs := make([]string, 0, len(tasks))
|
||||
for _, task := range tasks {
|
||||
if task != nil {
|
||||
taskIDs = append(taskIDs, task.ID)
|
||||
}
|
||||
}
|
||||
sessionsByTask, err := b.p.taskSvc.BatchGetSessionsForTasks(ctx, taskIDs)
|
||||
if err != nil {
|
||||
b.logBootError("batch task detail sessions", err)
|
||||
return taskDTOs(tasks)
|
||||
}
|
||||
primaryInfoByTask, err := b.p.taskSvc.GetPrimarySessionInfoForTasks(ctx, taskIDs)
|
||||
if err != nil {
|
||||
b.logBootError("get task detail primary session info", err)
|
||||
return taskDTOs(tasks)
|
||||
}
|
||||
result := make([]taskdto.TaskDTO, 0, len(tasks))
|
||||
for _, task := range tasks {
|
||||
if task == nil {
|
||||
continue
|
||||
}
|
||||
sessions := sessionsByTask[task.ID]
|
||||
var primarySessionID *string
|
||||
for _, session := range sessions {
|
||||
if session != nil && session.IsPrimary {
|
||||
id := session.ID
|
||||
primarySessionID = &id
|
||||
break
|
||||
}
|
||||
}
|
||||
var sessionCount *int
|
||||
if len(sessions) > 0 {
|
||||
count := len(sessions)
|
||||
sessionCount = &count
|
||||
}
|
||||
info := bootSessionInfo(primaryInfoByTask[task.ID])
|
||||
result = append(result, taskdto.FromTaskWithSessionInfo(
|
||||
task,
|
||||
primarySessionID,
|
||||
sessionCount,
|
||||
info.reviewStatus,
|
||||
info.executorID,
|
||||
info.executorType,
|
||||
info.executorName,
|
||||
info.agentName,
|
||||
info.workingDirectory,
|
||||
info.sessionState,
|
||||
))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func taskDTOs(tasks []*taskmodels.Task) []taskdto.TaskDTO {
|
||||
result := make([]taskdto.TaskDTO, 0, len(tasks))
|
||||
for _, task := range tasks {
|
||||
if task != nil {
|
||||
result = append(result, taskdto.FromTask(task))
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type bootSessionInfoFields struct {
|
||||
reviewStatus taskmodels.ReviewStatus
|
||||
sessionState *string
|
||||
executorID *string
|
||||
executorType *string
|
||||
executorName *string
|
||||
agentName *string
|
||||
workingDirectory *string
|
||||
}
|
||||
|
||||
func bootSessionInfo(session *taskmodels.TaskSession) bootSessionInfoFields {
|
||||
var info bootSessionInfoFields
|
||||
if session == nil {
|
||||
return info
|
||||
}
|
||||
info.reviewStatus = session.ReviewStatus
|
||||
if session.State != "" {
|
||||
value := string(session.State)
|
||||
info.sessionState = &value
|
||||
}
|
||||
if session.ExecutorID != "" {
|
||||
value := session.ExecutorID
|
||||
info.executorID = &value
|
||||
}
|
||||
if session.ExecutorSnapshot != nil {
|
||||
if value, ok := session.ExecutorSnapshot["executor_type"].(string); ok && value != "" {
|
||||
info.executorType = &value
|
||||
}
|
||||
if value, ok := session.ExecutorSnapshot["executor_name"].(string); ok && value != "" {
|
||||
info.executorName = &value
|
||||
}
|
||||
}
|
||||
if session.AgentProfileSnapshot != nil {
|
||||
if value, ok := session.AgentProfileSnapshot["name"].(string); ok && value != "" {
|
||||
info.agentName = &value
|
||||
}
|
||||
}
|
||||
if session.RepositorySnapshot != nil {
|
||||
if value, ok := session.RepositorySnapshot["path"].(string); ok && value != "" {
|
||||
info.workingDirectory = &value
|
||||
}
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) taskDetailInitialState(
|
||||
ctx context.Context,
|
||||
task *taskmodels.Task,
|
||||
taskDTO taskdto.TaskDTO,
|
||||
sessions []*taskmodels.TaskSession,
|
||||
activeSessionID string,
|
||||
) map[string]any {
|
||||
state := map[string]any{}
|
||||
b.addTaskDetailResourceState(ctx, state, task)
|
||||
b.addTaskDetailKanbanState(ctx, state, task)
|
||||
b.addTaskDetailActiveTaskState(ctx, state, taskDTO, activeSessionID)
|
||||
b.addTaskDetailSessionsState(state, task.ID, sessions, activeSessionID)
|
||||
b.addTaskDetailAgentsState(ctx, state)
|
||||
return state
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addTaskDetailResourceState(ctx context.Context, state map[string]any, task *taskmodels.Task) {
|
||||
b.addWorkspaceState(ctx, state, &task.WorkspaceID)
|
||||
b.addUserSettingsState(ctx, state, task.WorkspaceID)
|
||||
workflows, err := b.p.taskSvc.ListWorkflows(ctx, task.WorkspaceID, true)
|
||||
if err != nil {
|
||||
b.logBootError("list task detail workflows", err)
|
||||
} else {
|
||||
state["workflows"] = map[string]any{
|
||||
"items": workflowItemStates(workflows),
|
||||
"activeId": nil,
|
||||
}
|
||||
}
|
||||
b.addRepositoriesState(ctx, state, task.WorkspaceID)
|
||||
}
|
||||
|
||||
func workflowItemStates(workflows []*taskmodels.Workflow) []map[string]any {
|
||||
items := make([]map[string]any, 0, len(workflows))
|
||||
for _, workflow := range workflows {
|
||||
if workflow != nil {
|
||||
items = append(items, mapWorkflowItemState(taskdto.FromWorkflow(workflow)))
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addTaskDetailKanbanState(ctx context.Context, state map[string]any, task *taskmodels.Task) {
|
||||
if task.WorkflowID == "" {
|
||||
state["kanban"] = map[string]any{"workflowId": "", "steps": []any{}, "tasks": []any{}, "isLoading": false}
|
||||
return
|
||||
}
|
||||
workflows, err := b.p.taskSvc.ListWorkflows(ctx, task.WorkspaceID, true)
|
||||
if err != nil {
|
||||
b.logBootError("list task detail kanban workflows", err)
|
||||
return
|
||||
}
|
||||
for _, workflow := range workflows {
|
||||
if workflow == nil || workflow.ID != task.WorkflowID {
|
||||
continue
|
||||
}
|
||||
snapshot, ok := b.workflowSnapshotState(ctx, workflow)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
state["kanban"] = map[string]any{
|
||||
"workflowId": snapshot["workflowId"],
|
||||
"steps": snapshot["steps"],
|
||||
"tasks": snapshot["tasks"],
|
||||
"isLoading": false,
|
||||
}
|
||||
state["kanbanMulti"] = map[string]any{
|
||||
"snapshots": map[string]any{workflow.ID: snapshot},
|
||||
"isLoading": false,
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addTaskDetailActiveTaskState(
|
||||
ctx context.Context,
|
||||
state map[string]any,
|
||||
task taskdto.TaskDTO,
|
||||
activeSessionID string,
|
||||
) {
|
||||
state["tasks"] = map[string]any{
|
||||
"activeTaskId": task.ID,
|
||||
"activeSessionId": nullString(activeSessionID),
|
||||
"pinnedSessionId": nil,
|
||||
"lastSessionByTaskId": lastSessionByTaskState(task.ID, activeSessionID),
|
||||
}
|
||||
if activeSessionID == "" {
|
||||
return
|
||||
}
|
||||
messages, hasMore, err := b.p.taskSvc.ListMessagesPaginated(ctx, taskservice.ListMessagesRequest{
|
||||
TaskSessionID: activeSessionID,
|
||||
Limit: 50,
|
||||
Sort: "desc",
|
||||
})
|
||||
if err != nil {
|
||||
b.logBootError("list task detail messages", err)
|
||||
return
|
||||
}
|
||||
apiMessages := make([]*v1.Message, 0, len(messages))
|
||||
for i := len(messages) - 1; i >= 0; i-- {
|
||||
if messages[i] != nil {
|
||||
apiMessages = append(apiMessages, messages[i].ToAPI())
|
||||
}
|
||||
}
|
||||
var oldest any
|
||||
if len(apiMessages) > 0 {
|
||||
oldest = apiMessages[0].ID
|
||||
}
|
||||
state["messages"] = map[string]any{
|
||||
"bySession": map[string]any{activeSessionID: apiMessages},
|
||||
"metaBySession": map[string]any{
|
||||
activeSessionID: map[string]any{
|
||||
"isLoading": false,
|
||||
"hasMore": hasMore,
|
||||
"oldestCursor": oldest,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func lastSessionByTaskState(taskID, sessionID string) map[string]string {
|
||||
if taskID == "" || sessionID == "" {
|
||||
return map[string]string{}
|
||||
}
|
||||
return map[string]string{taskID: sessionID}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addTaskDetailSessionsState(
|
||||
state map[string]any,
|
||||
taskID string,
|
||||
sessions []*taskmodels.TaskSession,
|
||||
activeSessionID string,
|
||||
) {
|
||||
sessionItems := make(map[string]taskdto.TaskSessionDTO, len(sessions))
|
||||
sessionList := make([]taskdto.TaskSessionDTO, 0, len(sessions))
|
||||
environmentBySession := make(map[string]string, len(sessions))
|
||||
worktrees := make(map[string]any)
|
||||
worktreesBySession := make(map[string]any)
|
||||
for _, session := range sessions {
|
||||
if session == nil {
|
||||
continue
|
||||
}
|
||||
dto := taskdto.FromTaskSession(session)
|
||||
sessionItems[session.ID] = dto
|
||||
sessionList = append(sessionList, dto)
|
||||
if session.TaskEnvironmentID != "" {
|
||||
environmentBySession[session.ID] = session.TaskEnvironmentID
|
||||
}
|
||||
if dto.WorktreeID != "" {
|
||||
worktrees[dto.WorktreeID] = map[string]any{
|
||||
"id": dto.WorktreeID,
|
||||
"sessionId": session.ID,
|
||||
"repositoryId": nullString(dto.RepositoryID),
|
||||
"path": nullString(dto.WorktreePath),
|
||||
"branch": nullString(dto.WorktreeBranch),
|
||||
}
|
||||
worktreesBySession[session.ID] = []string{dto.WorktreeID}
|
||||
}
|
||||
}
|
||||
state["taskSessions"] = map[string]any{"items": sessionItems}
|
||||
state["taskSessionsByTask"] = map[string]any{
|
||||
"itemsByTaskId": map[string]any{taskID: sessionList},
|
||||
"loadingByTaskId": map[string]any{taskID: false},
|
||||
"loadedByTaskId": map[string]any{taskID: true},
|
||||
}
|
||||
state["turns"] = map[string]any{
|
||||
"bySession": map[string]any{},
|
||||
"activeBySession": activeTurnBySessionState(activeSessionID),
|
||||
}
|
||||
state["environmentIdBySessionId"] = environmentBySession
|
||||
state["worktrees"] = map[string]any{"items": worktrees}
|
||||
state["sessionWorktreesBySessionId"] = map[string]any{"itemsBySessionId": worktreesBySession}
|
||||
}
|
||||
|
||||
func activeTurnBySessionState(sessionID string) map[string]any {
|
||||
if sessionID == "" {
|
||||
return map[string]any{}
|
||||
}
|
||||
return map[string]any{sessionID: nil}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addTaskDetailAgentsState(ctx context.Context, state map[string]any) {
|
||||
if b.p.agentSettingsController == nil {
|
||||
return
|
||||
}
|
||||
response, err := b.p.agentSettingsController.ListAgents(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("list task detail agents", err)
|
||||
return
|
||||
}
|
||||
state["settingsAgents"] = map[string]any{"items": response.Agents}
|
||||
state["settingsData"] = map[string]any{"agentsLoaded": true, "executorsLoaded": false}
|
||||
state["agentProfiles"] = map[string]any{
|
||||
"items": agentProfileOptionStates(response.Agents),
|
||||
"version": 0,
|
||||
}
|
||||
}
|
||||
|
||||
func agentProfileOptionStates(agents []agentsettingsdto.AgentDTO) []map[string]any {
|
||||
items := []map[string]any{}
|
||||
for _, agent := range agents {
|
||||
for _, profile := range agent.Profiles {
|
||||
items = append(items, map[string]any{
|
||||
"id": profile.ID,
|
||||
"label": profile.AgentDisplayName + " - " + profile.Name,
|
||||
"agent_id": agent.ID,
|
||||
"agent_name": agent.Name,
|
||||
"cli_passthrough": profile.CLIPassthrough,
|
||||
"capability_status": nullString(agent.CapabilityStatus),
|
||||
"capability_error": nullString(agent.CapabilityError),
|
||||
})
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
@@ -0,0 +1,714 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
officedashboard "github.com/kandev/kandev/internal/office/dashboard"
|
||||
taskdto "github.com/kandev/kandev/internal/task/dto"
|
||||
taskmodels "github.com/kandev/kandev/internal/task/models"
|
||||
userdto "github.com/kandev/kandev/internal/user/dto"
|
||||
usermodels "github.com/kandev/kandev/internal/user/models"
|
||||
)
|
||||
|
||||
func (b bootStateBuilder) tasksPageBootData(ctx context.Context, req *http.Request) (map[string]any, map[string]any) {
|
||||
if b.p.taskSvc == nil {
|
||||
return nil, nil
|
||||
}
|
||||
workspaces, err := b.p.taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("list tasks page workspaces", err)
|
||||
return nil, nil
|
||||
}
|
||||
settings, hasSettings := b.userSettings(ctx)
|
||||
settingsWorkspaceID := ""
|
||||
settingsWorkflowID := ""
|
||||
settingsRepositoryID := ""
|
||||
if hasSettings {
|
||||
settingsWorkspaceID = settings.Settings.WorkspaceID
|
||||
settingsWorkflowID = settings.Settings.WorkflowFilterID
|
||||
if len(settings.Settings.RepositoryIDs) > 0 {
|
||||
settingsRepositoryID = settings.Settings.RepositoryIDs[0]
|
||||
}
|
||||
}
|
||||
workspaceIDs := workspaceIDSet(workspaces)
|
||||
activeWorkspaceID := firstValidID(
|
||||
workspaceIDs,
|
||||
queryValue(req, "workspaceId"),
|
||||
queryValue(req, "workspace"),
|
||||
readActiveWorkspaceCookie(req),
|
||||
settingsWorkspaceID,
|
||||
firstWorkspaceID(workspaces),
|
||||
)
|
||||
state := map[string]any{
|
||||
"workspaces": map[string]any{
|
||||
"items": workspaceItemStates(workspaces),
|
||||
"activeId": nullString(activeWorkspaceID),
|
||||
},
|
||||
}
|
||||
if hasSettings {
|
||||
state["userSettings"] = mapUserSettingsState(settings, activeWorkspaceID)
|
||||
}
|
||||
if activeWorkspaceID == "" {
|
||||
return state, map[string]any{"activeWorkspaceId": nil, "workflows": []any{}, "steps": []any{}, "repositories": []any{}, "tasks": []any{}, "total": 0}
|
||||
}
|
||||
workflows, err := b.p.taskSvc.ListWorkflows(ctx, activeWorkspaceID, false)
|
||||
if err != nil {
|
||||
b.logBootError("list tasks page workflows", err)
|
||||
return state, nil
|
||||
}
|
||||
activeWorkflowID := validWorkflowOrEmpty(workflows, settingsWorkflowID)
|
||||
workflowItems := workflowItemStates(workflows)
|
||||
state["workflows"] = map[string]any{"items": workflowItems, "activeId": nullString(activeWorkflowID)}
|
||||
if hasSettings {
|
||||
state["userSettings"] = mapUserSettingsStateWithWorkflow(settings, activeWorkspaceID, activeWorkflowID)
|
||||
}
|
||||
repositories := b.repositoriesForState(ctx, activeWorkspaceID, state)
|
||||
steps := b.workflowStepsForWorkspace(ctx, activeWorkspaceID)
|
||||
tasks, total := b.tasksForWorkspace(ctx, activeWorkspaceID, activeWorkflowID, settingsRepositoryID)
|
||||
routeData := map[string]any{
|
||||
"activeWorkspaceId": activeWorkspaceID,
|
||||
"workflows": workflowsToDTOs(workflows),
|
||||
"steps": steps,
|
||||
"repositories": repositories,
|
||||
"tasks": tasks,
|
||||
"total": total,
|
||||
}
|
||||
return state, routeData
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) routeContextBootData(ctx context.Context, req *http.Request) (map[string]any, map[string]any) {
|
||||
if b.p.taskSvc == nil {
|
||||
return nil, nil
|
||||
}
|
||||
workspaces, err := b.p.taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("list route context workspaces", err)
|
||||
return nil, nil
|
||||
}
|
||||
settings, hasSettings := b.userSettings(ctx)
|
||||
settingsWorkspaceID := ""
|
||||
settingsWorkflowID := ""
|
||||
if hasSettings {
|
||||
settingsWorkspaceID = settings.Settings.WorkspaceID
|
||||
settingsWorkflowID = settings.Settings.WorkflowFilterID
|
||||
}
|
||||
activeWorkspaceID := firstValidID(
|
||||
workspaceIDSet(workspaces),
|
||||
queryValue(req, "workspaceId"),
|
||||
queryValue(req, "workspace"),
|
||||
readActiveWorkspaceCookie(req),
|
||||
settingsWorkspaceID,
|
||||
firstWorkspaceID(workspaces),
|
||||
)
|
||||
state := map[string]any{
|
||||
"workspaces": map[string]any{
|
||||
"items": workspaceItemStates(workspaces),
|
||||
"activeId": nullString(activeWorkspaceID),
|
||||
},
|
||||
}
|
||||
if hasSettings {
|
||||
state["userSettings"] = mapUserSettingsState(settings, activeWorkspaceID)
|
||||
}
|
||||
if activeWorkspaceID == "" {
|
||||
return state, map[string]any{"activeWorkspaceId": nil, "workflows": []any{}, "steps": []any{}, "repositories": []any{}}
|
||||
}
|
||||
workflows, err := b.p.taskSvc.ListWorkflows(ctx, activeWorkspaceID, false)
|
||||
if err != nil {
|
||||
b.logBootError("list route context workflows", err)
|
||||
return state, nil
|
||||
}
|
||||
activeWorkflowID := validWorkflowOrEmpty(workflows, settingsWorkflowID)
|
||||
state["workflows"] = map[string]any{
|
||||
"items": workflowItemStates(workflows),
|
||||
"activeId": nullString(activeWorkflowID),
|
||||
}
|
||||
if hasSettings {
|
||||
state["userSettings"] = mapUserSettingsStateWithWorkflow(settings, activeWorkspaceID, activeWorkflowID)
|
||||
}
|
||||
repositories := b.repositoriesForState(ctx, activeWorkspaceID, state)
|
||||
steps := b.workflowStepsForWorkspace(ctx, activeWorkspaceID)
|
||||
return state, map[string]any{
|
||||
"activeWorkspaceId": activeWorkspaceID,
|
||||
"workflows": workflowsToDTOs(workflows),
|
||||
"steps": steps,
|
||||
"repositories": repositories,
|
||||
}
|
||||
}
|
||||
|
||||
func workspaceIDSet(workspaces []*taskmodels.Workspace) map[string]bool {
|
||||
result := make(map[string]bool, len(workspaces))
|
||||
for _, workspace := range workspaces {
|
||||
if workspace != nil {
|
||||
result[workspace.ID] = true
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func workspaceItemStates(workspaces []*taskmodels.Workspace) []map[string]any {
|
||||
items := make([]map[string]any, 0, len(workspaces))
|
||||
for _, workspace := range workspaces {
|
||||
if workspace != nil {
|
||||
items = append(items, mapWorkspaceItemState(taskdto.FromWorkspace(workspace)))
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func resolveHomeWorkflowID(
|
||||
workflows []*taskmodels.Workflow,
|
||||
queryWorkflowID string,
|
||||
settingsWorkflowID string,
|
||||
hasSettings bool,
|
||||
) string {
|
||||
if activeWorkflowID := validWorkflowOrEmpty(workflows, queryWorkflowID); activeWorkflowID != "" {
|
||||
return activeWorkflowID
|
||||
}
|
||||
if hasSettings {
|
||||
return validWorkflowOrEmpty(workflows, settingsWorkflowID)
|
||||
}
|
||||
return firstWorkflowID(workflows)
|
||||
}
|
||||
|
||||
func validWorkflowOrEmpty(workflows []*taskmodels.Workflow, workflowID string) string {
|
||||
for _, workflow := range workflows {
|
||||
if workflow != nil && workflow.ID == workflowID {
|
||||
return workflowID
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) repositoriesForState(ctx context.Context, workspaceID string, state map[string]any) []taskdto.RepositoryDTO {
|
||||
repositories, err := b.p.taskSvc.ListRepositories(ctx, workspaceID)
|
||||
if err != nil {
|
||||
b.logBootError("list tasks page repositories", err)
|
||||
return []taskdto.RepositoryDTO{}
|
||||
}
|
||||
items := repositoriesToDTOs(repositories)
|
||||
state["repositories"] = map[string]any{
|
||||
"itemsByWorkspaceId": map[string]any{workspaceID: items},
|
||||
"loadingByWorkspaceId": map[string]any{workspaceID: false},
|
||||
"loadedByWorkspaceId": map[string]any{workspaceID: true},
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func repositoriesToDTOs(repositories []*taskmodels.Repository) []taskdto.RepositoryDTO {
|
||||
items := make([]taskdto.RepositoryDTO, 0, len(repositories))
|
||||
for _, repository := range repositories {
|
||||
if repository != nil {
|
||||
items = append(items, taskdto.FromRepository(repository))
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func workflowsToDTOs(workflows []*taskmodels.Workflow) []taskdto.WorkflowDTO {
|
||||
items := make([]taskdto.WorkflowDTO, 0, len(workflows))
|
||||
for _, workflow := range workflows {
|
||||
if workflow != nil {
|
||||
items = append(items, taskdto.FromWorkflow(workflow))
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) workflowStepsForWorkspace(ctx context.Context, workspaceID string) []taskdto.WorkflowStepDTO {
|
||||
if b.p.services == nil || b.p.services.Workflow == nil {
|
||||
return []taskdto.WorkflowStepDTO{}
|
||||
}
|
||||
steps, err := b.p.services.Workflow.ListStepsByWorkspaceID(ctx, workspaceID)
|
||||
if err != nil {
|
||||
b.logBootError("list tasks page workflow steps", err)
|
||||
return []taskdto.WorkflowStepDTO{}
|
||||
}
|
||||
items := make([]taskdto.WorkflowStepDTO, 0, len(steps))
|
||||
for _, step := range steps {
|
||||
if step != nil {
|
||||
items = append(items, taskdto.FromWorkflowStepWithTimestamps(step))
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) tasksForWorkspace(ctx context.Context, workspaceID, workflowID, repositoryID string) ([]taskdto.TaskDTO, int) {
|
||||
tasks, total, err := b.p.taskSvc.ListTasksByWorkspace(ctx, workspaceID, workflowID, repositoryID, "", 1, 25, false, false, false, false)
|
||||
if err != nil {
|
||||
b.logBootError("list tasks page tasks", err)
|
||||
return []taskdto.TaskDTO{}, 0
|
||||
}
|
||||
return b.taskDTOsWithSessionInfo(ctx, tasks), total
|
||||
}
|
||||
|
||||
func mergeBootState(dst map[string]any, src map[string]any) {
|
||||
for key, value := range src {
|
||||
dst[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addPromptsState(ctx context.Context, state map[string]any) {
|
||||
if b.p.promptCtrl == nil {
|
||||
return
|
||||
}
|
||||
response, err := b.p.promptCtrl.ListPrompts(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("list prompts", err)
|
||||
return
|
||||
}
|
||||
state["prompts"] = map[string]any{
|
||||
"items": response.Prompts,
|
||||
"loaded": true,
|
||||
"loading": false,
|
||||
}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addEditorsState(ctx context.Context, state map[string]any) {
|
||||
if b.p.editorCtrl == nil {
|
||||
return
|
||||
}
|
||||
response, err := b.p.editorCtrl.ListEditors(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("list editors", err)
|
||||
return
|
||||
}
|
||||
state["editors"] = map[string]any{
|
||||
"items": response.Editors,
|
||||
"loaded": true,
|
||||
"loading": false,
|
||||
}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) addOfficeRouteState(ctx context.Context, req *http.Request, state map[string]any) {
|
||||
if !b.p.features.Office || b.p.services == nil || b.p.services.OfficeSvcs == nil {
|
||||
return
|
||||
}
|
||||
officeSvcs := b.p.services.OfficeSvcs
|
||||
if officeSvcs.Onboarding != nil {
|
||||
onboarding, err := officeSvcs.Onboarding.GetOnboardingState(ctx)
|
||||
if err != nil {
|
||||
b.logBootError("get office onboarding", err)
|
||||
return
|
||||
}
|
||||
if onboarding != nil && !onboarding.Completed {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
workspaces, activeID, err := b.officeWorkspaces(ctx, req)
|
||||
if err != nil {
|
||||
b.logBootError("list office workspaces", err)
|
||||
return
|
||||
}
|
||||
state["workspaces"] = map[string]any{
|
||||
"items": workspaces,
|
||||
"activeId": activeID,
|
||||
}
|
||||
b.addUserSettingsState(ctx, state, activeID)
|
||||
state["office"] = b.officeState(ctx, activeID)
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) officeWorkspaces(ctx context.Context, req *http.Request) ([]taskdto.WorkspaceDTO, string, error) {
|
||||
if b.p.taskSvc == nil {
|
||||
return nil, "", nil
|
||||
}
|
||||
workspaces, err := b.p.taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
items := make([]taskdto.WorkspaceDTO, 0, len(workspaces))
|
||||
officeItems := make([]taskdto.WorkspaceDTO, 0, len(workspaces))
|
||||
for _, workspace := range workspaces {
|
||||
if workspace == nil {
|
||||
continue
|
||||
}
|
||||
item := taskdto.FromWorkspace(workspace)
|
||||
items = append(items, item)
|
||||
if item.OfficeWorkflowID != "" {
|
||||
officeItems = append(officeItems, item)
|
||||
}
|
||||
}
|
||||
return items, resolveActiveOfficeWorkspaceID(officeItems, readActiveWorkspaceCookie(req)), nil
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) officeState(ctx context.Context, activeID string) map[string]any {
|
||||
agents := b.officeAgents(ctx, activeID)
|
||||
projects := b.officeProjects(ctx, activeID)
|
||||
inboxItems, inboxCount := b.officeInbox(ctx, activeID)
|
||||
dashboard := b.officeDashboard(ctx, activeID)
|
||||
return map[string]any{
|
||||
"agentProfiles": agents,
|
||||
"skills": []any{},
|
||||
"projects": projects,
|
||||
"approvals": []any{},
|
||||
"activity": []any{},
|
||||
"costSummary": nil,
|
||||
"budgetPolicies": []any{},
|
||||
"routines": []any{},
|
||||
"inboxItems": inboxItems,
|
||||
"inboxCount": inboxCount,
|
||||
"runs": []any{},
|
||||
"dashboard": dashboard,
|
||||
"tasks": map[string]any{
|
||||
"items": []any{},
|
||||
"filters": map[string]any{"statuses": []any{}, "priorities": []any{}, "assigneeIds": []any{}, "projectIds": []any{}, "search": ""},
|
||||
"viewMode": "list",
|
||||
"sortField": "updated",
|
||||
"sortDir": "desc",
|
||||
"groupBy": "none",
|
||||
"nestingEnabled": true,
|
||||
"isLoading": false,
|
||||
},
|
||||
"meta": officedashboard.BuildMetaResponse(),
|
||||
"isLoading": false,
|
||||
"refetchTrigger": nil,
|
||||
"routing": map[string]any{"byWorkspace": map[string]any{}, "knownProviders": []any{}, "preview": map[string]any{"byWorkspace": map[string]any{}}},
|
||||
"providerHealth": map[string]any{"byWorkspace": map[string]any{}},
|
||||
"runAttempts": map[string]any{"byRunId": map[string]any{}},
|
||||
"agentRouting": map[string]any{"byAgentId": map[string]any{}},
|
||||
}
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) officeAgents(ctx context.Context, activeID string) any {
|
||||
if activeID == "" || b.p.services.OfficeSvcs.Agents == nil {
|
||||
return []any{}
|
||||
}
|
||||
result, err := b.p.services.OfficeSvcs.Agents.ListAgentsFromConfig(ctx, activeID)
|
||||
if err != nil {
|
||||
b.logBootError("list office agents", err)
|
||||
return []any{}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) officeProjects(ctx context.Context, activeID string) any {
|
||||
if activeID == "" || b.p.services.OfficeSvcs.Projects == nil {
|
||||
return []any{}
|
||||
}
|
||||
result, err := b.p.services.OfficeSvcs.Projects.ListProjectsWithCountsFromConfig(ctx, activeID)
|
||||
if err != nil {
|
||||
b.logBootError("list office projects", err)
|
||||
return []any{}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) officeInbox(ctx context.Context, activeID string) (any, int) {
|
||||
if activeID == "" || b.p.services.OfficeSvcs.Dashboard == nil {
|
||||
return []any{}, 0
|
||||
}
|
||||
result, err := b.p.services.OfficeSvcs.Dashboard.GetInboxItems(ctx, activeID)
|
||||
if err != nil {
|
||||
b.logBootError("get office inbox", err)
|
||||
return []any{}, 0
|
||||
}
|
||||
return result, len(result)
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) officeDashboard(ctx context.Context, activeID string) any {
|
||||
if activeID == "" || b.p.services.OfficeSvcs.Dashboard == nil {
|
||||
return nil
|
||||
}
|
||||
data, err := b.p.services.OfficeSvcs.Dashboard.GetDashboardData(ctx, activeID)
|
||||
if err != nil {
|
||||
b.logBootError("get office dashboard", err)
|
||||
return nil
|
||||
}
|
||||
summaries, err := b.p.services.OfficeSvcs.Dashboard.GetAgentSummaries(ctx, activeID)
|
||||
if err != nil {
|
||||
b.logBootError("get office agent summaries", err)
|
||||
summaries = []officedashboard.AgentSummary{}
|
||||
}
|
||||
return officedashboard.NewDashboardResponse(data, summaries)
|
||||
}
|
||||
|
||||
func mapUserSettingsState(response userdto.UserSettingsResponse, workspaceID string) map[string]any {
|
||||
settings := response.Settings
|
||||
effectiveWorkspaceID := nullString(settings.WorkspaceID)
|
||||
if workspaceID != "" {
|
||||
effectiveWorkspaceID = workspaceID
|
||||
}
|
||||
return map[string]any{
|
||||
"workspaceId": effectiveWorkspaceID,
|
||||
"kanbanViewMode": nullString(settings.KanbanViewMode),
|
||||
"workflowId": nullString(settings.WorkflowFilterID),
|
||||
"repositoryIds": stringSlice(settings.RepositoryIDs),
|
||||
"preferredShell": nullString(settings.PreferredShell),
|
||||
"shellOptions": response.ShellOptions,
|
||||
"defaultEditorId": nullString(settings.DefaultEditorID),
|
||||
"enablePreviewOnClick": settings.EnablePreviewOnClick,
|
||||
"chatSubmitKey": defaultString(settings.ChatSubmitKey, "cmd_enter"),
|
||||
"reviewAutoMarkOnScroll": settings.ReviewAutoMarkOnScroll,
|
||||
"showReleaseNotification": settings.ShowReleaseNotification,
|
||||
"releaseNotesLastSeenVersion": nullString(settings.ReleaseNotesLastSeenVersion),
|
||||
"lspAutoStartLanguages": stringSlice(settings.LspAutoStartLanguages),
|
||||
"lspAutoInstallLanguages": stringSlice(settings.LspAutoInstallLanguages),
|
||||
"lspServerConfigs": mapStringMap(settings.LspServerConfigs),
|
||||
"savedLayouts": settings.SavedLayouts,
|
||||
"sidebarViews": mapSidebarViews(settings.SidebarViews),
|
||||
"defaultUtilityAgentId": nullString(settings.DefaultUtilityAgentID),
|
||||
"keyboardShortcuts": mapStringAny(settings.KeyboardShortcuts),
|
||||
"terminalLinkBehavior": terminalLinkBehavior(settings.TerminalLinkBehavior),
|
||||
"terminalFontFamily": nullString(settings.TerminalFontFamily),
|
||||
"terminalFontSize": nullInt(settings.TerminalFontSize),
|
||||
"changesPanelLayout": changesPanelLayout(settings.ChangesPanelLayout),
|
||||
"systemMetricsDisplay": map[string]any{"showInTopbar": settings.SystemMetricsDisplay.ShowInTopbar},
|
||||
"voiceMode": mapVoiceMode(settings.VoiceMode),
|
||||
"loaded": true,
|
||||
}
|
||||
}
|
||||
|
||||
func mapUserSettingsStateWithWorkflow(response userdto.UserSettingsResponse, workspaceID, workflowID string) map[string]any {
|
||||
state := mapUserSettingsState(response, workspaceID)
|
||||
state["workflowId"] = nullString(workflowID)
|
||||
return state
|
||||
}
|
||||
|
||||
func mapWorkspaceItemState(workspace taskdto.WorkspaceDTO) map[string]any {
|
||||
return map[string]any{
|
||||
"id": workspace.ID,
|
||||
"name": workspace.Name,
|
||||
"description": workspace.Description,
|
||||
"owner_id": workspace.OwnerID,
|
||||
"default_executor_id": workspace.DefaultExecutorID,
|
||||
"default_environment_id": workspace.DefaultEnvironmentID,
|
||||
"default_agent_profile_id": workspace.DefaultAgentProfileID,
|
||||
"default_config_agent_profile_id": workspace.DefaultConfigAgentProfileID,
|
||||
"office_workflow_id": nullString(workspace.OfficeWorkflowID),
|
||||
"created_at": workspace.CreatedAt,
|
||||
"updated_at": workspace.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func mapWorkflowItemState(workflow taskdto.WorkflowDTO) map[string]any {
|
||||
return map[string]any{
|
||||
"id": workflow.ID,
|
||||
"workspaceId": workflow.WorkspaceID,
|
||||
"name": workflow.Name,
|
||||
"description": workflow.Description,
|
||||
"sortOrder": workflow.SortOrder,
|
||||
"agent_profile_id": nullString(workflow.AgentProfileID),
|
||||
"hidden": workflow.Hidden,
|
||||
"style": workflow.Style,
|
||||
}
|
||||
}
|
||||
|
||||
func mapKanbanStepState(step taskdto.WorkflowStepDTO) map[string]any {
|
||||
return map[string]any{
|
||||
"id": step.ID,
|
||||
"title": step.Name,
|
||||
"color": defaultString(step.Color, "bg-neutral-400"),
|
||||
"position": step.Position,
|
||||
"events": step.Events,
|
||||
"allow_manual_move": step.AllowManualMove,
|
||||
"prompt": step.Prompt,
|
||||
"is_start_step": step.IsStartStep,
|
||||
"show_in_command_panel": step.ShowInCommandPanel,
|
||||
"agent_profile_id": nullString(step.AgentProfileID),
|
||||
"stage_type": nullString(step.StageType),
|
||||
}
|
||||
}
|
||||
|
||||
func mapKanbanTaskState(task taskdto.TaskDTO) map[string]any {
|
||||
repositories := make([]map[string]any, 0, len(task.Repositories))
|
||||
var primaryRepositoryID any
|
||||
for i, repo := range task.Repositories {
|
||||
if i == 0 {
|
||||
primaryRepositoryID = repo.RepositoryID
|
||||
}
|
||||
repositories = append(repositories, map[string]any{
|
||||
"id": repo.ID,
|
||||
"repository_id": repo.RepositoryID,
|
||||
"base_branch": repo.BaseBranch,
|
||||
"checkout_branch": repo.CheckoutBranch,
|
||||
"position": repo.Position,
|
||||
})
|
||||
}
|
||||
return map[string]any{
|
||||
"id": task.ID,
|
||||
"workflowStepId": task.WorkflowStepID,
|
||||
"title": task.Title,
|
||||
"description": task.Description,
|
||||
"position": task.Position,
|
||||
"state": task.State,
|
||||
"repositoryId": primaryRepositoryID,
|
||||
"repositories": repositories,
|
||||
"primarySessionId": task.PrimarySessionID,
|
||||
"primarySessionState": task.PrimarySessionState,
|
||||
"sessionCount": task.SessionCount,
|
||||
"reviewStatus": nullString(string(task.ReviewStatus)),
|
||||
"parentTaskId": nullString(task.ParentID),
|
||||
"updatedAt": task.UpdatedAt,
|
||||
"createdAt": task.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func mapSidebarViews(views []usermodels.SidebarView) []map[string]any {
|
||||
if len(views) == 0 {
|
||||
return []map[string]any{}
|
||||
}
|
||||
result := make([]map[string]any, 0, len(views))
|
||||
for _, view := range views {
|
||||
result = append(result, map[string]any{
|
||||
"id": view.ID,
|
||||
"name": view.Name,
|
||||
"filters": view.Filters,
|
||||
"sort": view.Sort,
|
||||
"group": view.Group,
|
||||
"collapsedGroups": stringSlice(view.CollapsedGroups),
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func mapVoiceMode(value usermodels.VoiceModeSettings) map[string]any {
|
||||
return map[string]any{
|
||||
"enabled": value.Enabled,
|
||||
"engine": defaultString(value.Engine, "auto"),
|
||||
"language": defaultString(value.Language, "auto"),
|
||||
"mode": defaultString(value.Mode, "toggle"),
|
||||
"autoSend": value.AutoSend,
|
||||
"whisperWebModel": defaultString(value.WhisperWebModel, "base"),
|
||||
}
|
||||
}
|
||||
|
||||
func resolveActiveOfficeWorkspaceID(workspaces []taskdto.WorkspaceDTO, cookieWorkspaceID string) string {
|
||||
for _, workspace := range workspaces {
|
||||
if workspace.ID == cookieWorkspaceID {
|
||||
return workspace.ID
|
||||
}
|
||||
}
|
||||
if len(workspaces) > 0 {
|
||||
return workspaces[0].ID
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func firstValidID(valid map[string]bool, candidates ...string) string {
|
||||
for _, candidate := range candidates {
|
||||
value := strings.TrimSpace(candidate)
|
||||
if value != "" && valid[value] {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func firstWorkspaceID(workspaces []*taskmodels.Workspace) string {
|
||||
for _, workspace := range workspaces {
|
||||
if workspace != nil && workspace.ID != "" {
|
||||
return workspace.ID
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func firstWorkflowID(workflows []*taskmodels.Workflow) string {
|
||||
for _, workflow := range workflows {
|
||||
if workflow != nil && workflow.ID != "" {
|
||||
return workflow.ID
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func queryValue(req *http.Request, name string) string {
|
||||
if req == nil || req.URL == nil {
|
||||
return ""
|
||||
}
|
||||
if value := strings.TrimSpace(req.URL.Query().Get(name)); value != "" {
|
||||
return value
|
||||
}
|
||||
routePath := strings.TrimSpace(req.URL.Query().Get("path"))
|
||||
if routePath == "" {
|
||||
return ""
|
||||
}
|
||||
parsed, err := url.Parse(routePath)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(parsed.Query().Get(name))
|
||||
}
|
||||
|
||||
func readActiveWorkspaceCookie(req *http.Request) string {
|
||||
if req == nil {
|
||||
return ""
|
||||
}
|
||||
for _, name := range []string{activeWorkspaceCookie, legacyOfficeWorkspaceCookie} {
|
||||
cookie, err := req.Cookie(name)
|
||||
if err == nil {
|
||||
if value := strings.TrimSpace(cookie.Value); value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func nullString(value string) any {
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func nullInt(value int) any {
|
||||
if value == 0 {
|
||||
return nil
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func stringSlice(value []string) []string {
|
||||
if value == nil {
|
||||
return []string{}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func mapStringAny(value map[string]any) map[string]any {
|
||||
if value == nil {
|
||||
return map[string]any{}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func mapStringMap(value map[string]map[string]any) map[string]map[string]any {
|
||||
if value == nil {
|
||||
return map[string]map[string]any{}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func defaultString(value, fallback string) string {
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func terminalLinkBehavior(value string) string {
|
||||
if value == "browser_panel" {
|
||||
return "browser_panel"
|
||||
}
|
||||
return "new_tab"
|
||||
}
|
||||
|
||||
func changesPanelLayout(value string) string {
|
||||
if value == "flat" {
|
||||
return "flat"
|
||||
}
|
||||
return "tree"
|
||||
}
|
||||
|
||||
func (b bootStateBuilder) logBootError(operation string, err error) {
|
||||
if err == nil || b.p.log == nil {
|
||||
return
|
||||
}
|
||||
b.p.log.Debug("SPA boot state skipped optional data", zap.String("operation", operation), zap.Error(err))
|
||||
}
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -75,6 +75,8 @@ import (
|
||||
utilityhandlers "github.com/kandev/kandev/internal/utility/handlers"
|
||||
voicehandlers "github.com/kandev/kandev/internal/voice/handlers"
|
||||
"github.com/kandev/kandev/internal/voice/transcribe"
|
||||
"github.com/kandev/kandev/internal/webapp"
|
||||
webembedded "github.com/kandev/kandev/internal/webapp/embedded"
|
||||
workflowcontroller "github.com/kandev/kandev/internal/workflow/controller"
|
||||
workflowhandlers "github.com/kandev/kandev/internal/workflow/handlers"
|
||||
"github.com/kandev/kandev/internal/worktree"
|
||||
@@ -144,6 +146,9 @@ func appendAgentctlStatusMessage(
|
||||
if execution.TaskEnvironmentID != "" {
|
||||
payload["task_environment_id"] = execution.TaskEnvironmentID
|
||||
}
|
||||
if execution.WorkspacePath != "" {
|
||||
payload["worktree_path"] = execution.WorkspacePath
|
||||
}
|
||||
action := ws.ActionSessionAgentctlStarting
|
||||
if execution.GetWorkspaceStream() != nil {
|
||||
action = ws.ActionSessionAgentctlReady
|
||||
@@ -558,21 +563,31 @@ func registerRoutes(p routeParams) {
|
||||
p.router.GET("/api/v1/features", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, p.features)
|
||||
})
|
||||
p.router.GET("/api/v1/app-state", func(c *gin.Context) {
|
||||
routePath := c.Query("path")
|
||||
if routePath == "" {
|
||||
routePath = c.Request.URL.Path
|
||||
}
|
||||
route := webapp.ClassifyRoute(routePath)
|
||||
payload := bootPayload(c.Request.Context(), c.Request, p, route)
|
||||
c.JSON(http.StatusOK, payload)
|
||||
})
|
||||
|
||||
if p.webInternalURL == "" {
|
||||
if handler, distDir, ok := newWebAppHandler(p); ok {
|
||||
p.router.NoRoute(func(c *gin.Context) {
|
||||
handler.ServeHTTP(c.Writer, c.Request)
|
||||
})
|
||||
p.log.Info("Web SPA static serving enabled", zap.String("dist_dir", distDir))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if p.webInternalURL != "" {
|
||||
target, err := url.Parse(p.webInternalURL)
|
||||
handler, err := newWebDevHandler(p)
|
||||
if err != nil {
|
||||
p.log.Error("Invalid web internal URL, skipping reverse proxy", zap.String("url", p.webInternalURL), zap.Error(err))
|
||||
p.log.Error("Invalid web internal URL, skipping web dev handler", zap.String("url", p.webInternalURL), zap.Error(err))
|
||||
} else {
|
||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
||||
proxy.FlushInterval = -1
|
||||
// ErrorHandler catches upstream failures (e.g., connection refused)
|
||||
// that occur before any response headers are written, returning a
|
||||
// clean 502 instead of letting the default handler log an error.
|
||||
proxy.ErrorHandler = func(w http.ResponseWriter, _ *http.Request, err error) {
|
||||
p.log.Debug("Web proxy error", zap.Error(err))
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
}
|
||||
p.router.NoRoute(func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
if strings.HasPrefix(path, "/api/") || path == "/ws" || path == "/health" {
|
||||
@@ -592,13 +607,70 @@ func registerRoutes(p routeParams) {
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
proxy.ServeHTTP(c.Writer, c.Request)
|
||||
handler.ServeHTTP(c.Writer, c.Request)
|
||||
})
|
||||
p.log.Info("Web reverse proxy enabled", zap.String("target", p.webInternalURL))
|
||||
p.log.Info("Web dev handler enabled", zap.String("target", p.webInternalURL))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newWebAppHandler(p routeParams) (*webapp.Handler, string, bool) {
|
||||
assets, source, ok := webAssetsFS()
|
||||
if !ok {
|
||||
return nil, "", false
|
||||
}
|
||||
handler := webapp.NewHandler(assets, webAppHandlerOptions(p)...)
|
||||
return handler, source, true
|
||||
}
|
||||
|
||||
func newWebDevHandler(p routeParams) (*webapp.DevHandler, error) {
|
||||
return webapp.NewDevHandler(p.webInternalURL, webAppHandlerOptions(p)...)
|
||||
}
|
||||
|
||||
func webAppHandlerOptions(p routeParams) []webapp.HandlerOption {
|
||||
return []webapp.HandlerOption{
|
||||
webapp.WithRuntimeConfig(webapp.RuntimeConfig{APIPrefix: "/api/v1", WebSocketPath: "/ws"}),
|
||||
webapp.WithPayloadBuilder(func(req *http.Request, route webapp.RouteClassification) webapp.BootPayload {
|
||||
return bootPayload(req.Context(), req, p, route)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
func bootPayload(ctx context.Context, req *http.Request, p routeParams, route webapp.RouteClassification) webapp.BootPayload {
|
||||
payload := webapp.NewBootPayload(
|
||||
route,
|
||||
webapp.RuntimeConfig{APIPrefix: "/api/v1", WebSocketPath: "/ws"},
|
||||
bootInitialState(ctx, req, p, route),
|
||||
)
|
||||
payload.RouteData = bootRouteData(ctx, req, p, route)
|
||||
return payload
|
||||
}
|
||||
|
||||
func webAssetsFS() (fs.FS, string, bool) {
|
||||
distDir := os.Getenv("KANDEV_WEB_DIST_DIR")
|
||||
if distDir == "" {
|
||||
distDir = firstExistingDir("apps/web/dist", "../web/dist", "../../apps/web/dist")
|
||||
}
|
||||
if distDir != "" {
|
||||
return os.DirFS(distDir), distDir, true
|
||||
}
|
||||
assets, err := webembedded.FS()
|
||||
if err != nil {
|
||||
return nil, "", false
|
||||
}
|
||||
return assets, "embedded", true
|
||||
}
|
||||
|
||||
func firstExistingDir(candidates ...string) string {
|
||||
for _, candidate := range candidates {
|
||||
info, err := os.Stat(candidate)
|
||||
if err == nil && info.IsDir() {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// resolvePrimaryTaskRepositoryID returns the primary (lowest-position)
|
||||
// task_repositories.repository_id for a task, or "" if none / on error.
|
||||
// Used by PR-import callbacks where the PR is associated with the task's
|
||||
|
||||
@@ -1,14 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/kandev/kandev/internal/agent/runtime/lifecycle"
|
||||
"github.com/kandev/kandev/internal/common/config"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
taskdto "github.com/kandev/kandev/internal/task/dto"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
taskrepo "github.com/kandev/kandev/internal/task/repository"
|
||||
taskservice "github.com/kandev/kandev/internal/task/service"
|
||||
usercontroller "github.com/kandev/kandev/internal/user/controller"
|
||||
userservice "github.com/kandev/kandev/internal/user/service"
|
||||
userstore "github.com/kandev/kandev/internal/user/store"
|
||||
"github.com/kandev/kandev/internal/webapp"
|
||||
workflowrepo "github.com/kandev/kandev/internal/workflow/repository"
|
||||
workflowservice "github.com/kandev/kandev/internal/workflow/service"
|
||||
"github.com/kandev/kandev/internal/worktree"
|
||||
ws "github.com/kandev/kandev/pkg/websocket"
|
||||
)
|
||||
|
||||
@@ -56,6 +75,707 @@ func TestAppendSessionStateMessage_IncludesTaskEnvironmentID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendAgentctlStatusMessage_IncludesWorkspacePathForReload(t *testing.T) {
|
||||
log, err := logger.NewLogger(logger.LoggingConfig{
|
||||
Level: "error",
|
||||
Format: "console",
|
||||
OutputPath: "stdout",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewLogger: %v", err)
|
||||
}
|
||||
mgr := lifecycle.NewManager(nil, nil, nil, nil, nil, nil, lifecycle.ExecutorFallbackDeny, t.TempDir(), log)
|
||||
workspacePath := filepath.Join(t.TempDir(), "scratch")
|
||||
if err := mgr.ExecutionStoreForTesting().Add(&lifecycle.AgentExecution{
|
||||
ID: "exec-1",
|
||||
TaskID: "task-1",
|
||||
SessionID: "sess-1",
|
||||
TaskEnvironmentID: "env-1",
|
||||
WorkspacePath: workspacePath,
|
||||
}); err != nil {
|
||||
t.Fatalf("add execution: %v", err)
|
||||
}
|
||||
|
||||
msgs := appendAgentctlStatusMessage(context.Background(), mgr, "sess-1", nil, log)
|
||||
if len(msgs) != 1 {
|
||||
t.Fatalf("expected 1 message, got %d", len(msgs))
|
||||
}
|
||||
if msgs[0].Action != ws.ActionSessionAgentctlStarting {
|
||||
t.Fatalf("expected action %q, got %q", ws.ActionSessionAgentctlStarting, msgs[0].Action)
|
||||
}
|
||||
|
||||
payload := decodePayload(t, msgs[0].Payload)
|
||||
got, present := payload["worktree_path"]
|
||||
if !present {
|
||||
t.Fatalf("payload missing worktree_path — reload cannot hydrate repo-less scratch workspace path")
|
||||
}
|
||||
if got != workspacePath {
|
||||
t.Fatalf("expected worktree_path=%q, got %v", workspacePath, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootInitialStateIncludesFeatureFlags(t *testing.T) {
|
||||
state := bootInitialState(
|
||||
context.Background(),
|
||||
nil,
|
||||
routeParams{features: config.FeaturesConfig{Office: true}},
|
||||
webapp.ClassifyRoute("/"),
|
||||
)
|
||||
|
||||
raw, err := json.Marshal(state)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal state: %v", err)
|
||||
}
|
||||
var decoded struct {
|
||||
Features config.FeaturesConfig `json:"features"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal state: %v", err)
|
||||
}
|
||||
if !decoded.Features.Office {
|
||||
t.Fatal("features.office should hydrate true from the backend boot payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootInitialStateHomeIncludesKanbanFirstPaintState(t *testing.T) {
|
||||
taskSvc, workflowSvc := newBootStateTestServices(t)
|
||||
ctx := context.Background()
|
||||
|
||||
workspaces, err := taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListWorkspaces: %v", err)
|
||||
}
|
||||
if len(workspaces) == 0 {
|
||||
t.Fatal("expected seeded default workspace")
|
||||
}
|
||||
workflows, err := taskSvc.ListWorkflows(ctx, workspaces[0].ID, true)
|
||||
if err != nil {
|
||||
t.Fatalf("ListWorkflows: %v", err)
|
||||
}
|
||||
if len(workflows) == 0 {
|
||||
t.Fatal("expected seeded default workflow")
|
||||
}
|
||||
|
||||
state := bootInitialState(
|
||||
ctx,
|
||||
nil,
|
||||
routeParams{
|
||||
taskSvc: taskSvc,
|
||||
services: &Services{
|
||||
Workflow: workflowSvc,
|
||||
},
|
||||
},
|
||||
webapp.ClassifyRoute("/"),
|
||||
)
|
||||
|
||||
raw, err := json.Marshal(state)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal state: %v", err)
|
||||
}
|
||||
var decoded struct {
|
||||
Workspaces struct {
|
||||
Items []taskdto.WorkspaceDTO `json:"items"`
|
||||
ActiveID string `json:"activeId"`
|
||||
} `json:"workspaces"`
|
||||
Workflows struct {
|
||||
Items []struct {
|
||||
ID string `json:"id"`
|
||||
WorkspaceID string `json:"workspaceId"`
|
||||
Name string `json:"name"`
|
||||
} `json:"items"`
|
||||
ActiveID string `json:"activeId"`
|
||||
} `json:"workflows"`
|
||||
KanbanMulti struct {
|
||||
Snapshots map[string]struct {
|
||||
WorkflowID string `json:"workflowId"`
|
||||
WorkflowName string `json:"workflowName"`
|
||||
Steps []struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
} `json:"steps"`
|
||||
Tasks []struct {
|
||||
ID string `json:"id"`
|
||||
WorkflowStepID string `json:"workflowStepId"`
|
||||
} `json:"tasks"`
|
||||
} `json:"snapshots"`
|
||||
IsLoading bool `json:"isLoading"`
|
||||
} `json:"kanbanMulti"`
|
||||
Kanban struct {
|
||||
WorkflowID string `json:"workflowId"`
|
||||
Steps []struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
} `json:"steps"`
|
||||
Tasks []struct {
|
||||
ID string `json:"id"`
|
||||
WorkflowStepID string `json:"workflowStepId"`
|
||||
} `json:"tasks"`
|
||||
IsLoading bool `json:"isLoading"`
|
||||
} `json:"kanban"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal state: %v", err)
|
||||
}
|
||||
|
||||
if decoded.Workspaces.ActiveID != workspaces[0].ID {
|
||||
t.Fatalf("active workspace = %q, want %q", decoded.Workspaces.ActiveID, workspaces[0].ID)
|
||||
}
|
||||
if len(decoded.Workflows.Items) == 0 || decoded.Workflows.ActiveID == "" {
|
||||
t.Fatalf("expected workflows to hydrate first paint, got %+v", decoded.Workflows)
|
||||
}
|
||||
if _, ok := decoded.KanbanMulti.Snapshots[workflows[0].ID]; !ok {
|
||||
t.Fatalf("expected snapshot for workflow %q in boot payload", workflows[0].ID)
|
||||
}
|
||||
if decoded.Kanban.WorkflowID != workflows[0].ID {
|
||||
t.Fatalf("active kanban workflow = %q, want %q", decoded.Kanban.WorkflowID, workflows[0].ID)
|
||||
}
|
||||
if decoded.KanbanMulti.IsLoading || decoded.Kanban.IsLoading {
|
||||
t.Fatal("boot payload should mark kanban data loaded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootInitialStateHomePreservesSavedAllWorkflowsFilter(t *testing.T) {
|
||||
harness := newBootStateTestHarness(t)
|
||||
ctx := context.Background()
|
||||
|
||||
workspaces, err := harness.taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListWorkspaces: %v", err)
|
||||
}
|
||||
if len(workspaces) == 0 {
|
||||
t.Fatal("expected seeded default workspace")
|
||||
}
|
||||
workflowB, err := harness.taskSvc.CreateWorkflow(ctx, &taskservice.CreateWorkflowRequest{
|
||||
WorkspaceID: workspaces[0].ID,
|
||||
Name: "Workflow B",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateWorkflow: %v", err)
|
||||
}
|
||||
|
||||
allWorkflows := ""
|
||||
repositoryIDs := []string{}
|
||||
if _, err := harness.userSvc.UpdateUserSettings(ctx, &userservice.UpdateUserSettingsRequest{
|
||||
WorkspaceID: &workspaces[0].ID,
|
||||
WorkflowFilterID: &allWorkflows,
|
||||
RepositoryIDs: &repositoryIDs,
|
||||
}); err != nil {
|
||||
t.Fatalf("UpdateUserSettings: %v", err)
|
||||
}
|
||||
|
||||
state := bootInitialState(
|
||||
ctx,
|
||||
nil,
|
||||
routeParams{
|
||||
taskSvc: harness.taskSvc,
|
||||
userCtrl: harness.userCtrl,
|
||||
services: &Services{
|
||||
Workflow: harness.workflowSvc,
|
||||
},
|
||||
},
|
||||
webapp.ClassifyRoute("/"),
|
||||
)
|
||||
|
||||
raw, err := json.Marshal(state)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal state: %v", err)
|
||||
}
|
||||
var decoded struct {
|
||||
Workflows struct {
|
||||
ActiveID *string `json:"activeId"`
|
||||
} `json:"workflows"`
|
||||
UserSettings struct {
|
||||
WorkflowID *string `json:"workflowId"`
|
||||
} `json:"userSettings"`
|
||||
KanbanMulti struct {
|
||||
Snapshots map[string]json.RawMessage `json:"snapshots"`
|
||||
} `json:"kanbanMulti"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal state: %v", err)
|
||||
}
|
||||
|
||||
if decoded.Workflows.ActiveID != nil {
|
||||
t.Fatalf("active workflow = %q, want nil for All Workflows", *decoded.Workflows.ActiveID)
|
||||
}
|
||||
if decoded.UserSettings.WorkflowID != nil {
|
||||
t.Fatalf("user settings workflow = %q, want nil for All Workflows", *decoded.UserSettings.WorkflowID)
|
||||
}
|
||||
if _, ok := decoded.KanbanMulti.Snapshots[workflowB.ID]; !ok {
|
||||
t.Fatalf("expected boot snapshots to include second workflow %q", workflowB.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootRouteDataTaskDetailIncludesTaskPageData(t *testing.T) {
|
||||
taskSvc, workflowSvc := newBootStateTestServices(t)
|
||||
ctx := context.Background()
|
||||
workspaces, err := taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListWorkspaces: %v", err)
|
||||
}
|
||||
workflows, err := taskSvc.ListWorkflows(ctx, workspaces[0].ID, true)
|
||||
if err != nil {
|
||||
t.Fatalf("ListWorkflows: %v", err)
|
||||
}
|
||||
steps, err := workflowSvc.ListStepsByWorkflow(ctx, workflows[0].ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListStepsByWorkflow: %v", err)
|
||||
}
|
||||
task, err := taskSvc.CreateTask(ctx, &taskservice.CreateTaskRequest{
|
||||
WorkspaceID: workspaces[0].ID,
|
||||
WorkflowID: workflows[0].ID,
|
||||
WorkflowStepID: steps[0].ID,
|
||||
Title: "Boot detail task",
|
||||
Description: "Should be present before React mounts",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateTask: %v", err)
|
||||
}
|
||||
|
||||
payload := webapp.NewBootPayload(
|
||||
webapp.ClassifyRoute("/t/"+task.ID),
|
||||
webapp.RuntimeConfig{APIPrefix: "/api/v1", WebSocketPath: "/ws"},
|
||||
bootInitialState(ctx, nil, routeParams{
|
||||
taskSvc: taskSvc,
|
||||
services: &Services{
|
||||
Workflow: workflowSvc,
|
||||
},
|
||||
}, webapp.ClassifyRoute("/t/"+task.ID)),
|
||||
)
|
||||
payload.RouteData = bootRouteData(ctx, nil, routeParams{
|
||||
taskSvc: taskSvc,
|
||||
services: &Services{
|
||||
Workflow: workflowSvc,
|
||||
},
|
||||
}, webapp.ClassifyRoute("/t/"+task.ID))
|
||||
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal payload: %v", err)
|
||||
}
|
||||
var decoded struct {
|
||||
RouteData struct {
|
||||
TaskDetail struct {
|
||||
Task struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
} `json:"task"`
|
||||
SessionID *string `json:"sessionId"`
|
||||
InitialState map[string]interface{} `json:"initialState"`
|
||||
} `json:"taskDetail"`
|
||||
} `json:"routeData"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal payload: %v", err)
|
||||
}
|
||||
|
||||
if decoded.RouteData.TaskDetail.Task.ID != task.ID {
|
||||
t.Fatalf("route task id = %q, want %q", decoded.RouteData.TaskDetail.Task.ID, task.ID)
|
||||
}
|
||||
if decoded.RouteData.TaskDetail.Task.Title != "Boot detail task" {
|
||||
t.Fatalf("route task title = %q", decoded.RouteData.TaskDetail.Task.Title)
|
||||
}
|
||||
if decoded.RouteData.TaskDetail.SessionID != nil {
|
||||
t.Fatalf("new task should not have a boot session id, got %q", *decoded.RouteData.TaskDetail.SessionID)
|
||||
}
|
||||
if _, ok := decoded.RouteData.TaskDetail.InitialState["kanban"]; !ok {
|
||||
t.Fatal("task detail route data should include initialState.kanban")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootRouteDataTasksIncludesFirstPageRows(t *testing.T) {
|
||||
taskSvc, workflowSvc := newBootStateTestServices(t)
|
||||
ctx := context.Background()
|
||||
workspaces, err := taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListWorkspaces: %v", err)
|
||||
}
|
||||
workflows, err := taskSvc.ListWorkflows(ctx, workspaces[0].ID, true)
|
||||
if err != nil {
|
||||
t.Fatalf("ListWorkflows: %v", err)
|
||||
}
|
||||
steps, err := workflowSvc.ListStepsByWorkflow(ctx, workflows[0].ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListStepsByWorkflow: %v", err)
|
||||
}
|
||||
created, err := taskSvc.CreateTask(ctx, &taskservice.CreateTaskRequest{
|
||||
WorkspaceID: workspaces[0].ID,
|
||||
WorkflowID: workflows[0].ID,
|
||||
WorkflowStepID: steps[0].ID,
|
||||
Title: "Tasks table row",
|
||||
Description: "Visible on first paint",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateTask: %v", err)
|
||||
}
|
||||
|
||||
route := webapp.ClassifyRoute("/tasks")
|
||||
payload := bootPayload(ctx, nil, routeParams{
|
||||
taskSvc: taskSvc,
|
||||
services: &Services{
|
||||
Workflow: workflowSvc,
|
||||
},
|
||||
}, route)
|
||||
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal payload: %v", err)
|
||||
}
|
||||
var decoded struct {
|
||||
InitialState struct {
|
||||
Workspaces struct {
|
||||
ActiveID string `json:"activeId"`
|
||||
} `json:"workspaces"`
|
||||
Workflows struct {
|
||||
ActiveID *string `json:"activeId"`
|
||||
Items []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"items"`
|
||||
} `json:"workflows"`
|
||||
} `json:"initialState"`
|
||||
RouteData struct {
|
||||
TasksPage struct {
|
||||
ActiveWorkspaceID string `json:"activeWorkspaceId"`
|
||||
Tasks []struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
} `json:"tasks"`
|
||||
Total int `json:"total"`
|
||||
} `json:"tasksPage"`
|
||||
} `json:"routeData"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal payload: %v", err)
|
||||
}
|
||||
|
||||
if decoded.InitialState.Workspaces.ActiveID != workspaces[0].ID {
|
||||
t.Fatalf("active workspace = %q, want %q", decoded.InitialState.Workspaces.ActiveID, workspaces[0].ID)
|
||||
}
|
||||
if len(decoded.InitialState.Workflows.Items) == 0 {
|
||||
t.Fatal("tasks route should hydrate workflow items")
|
||||
}
|
||||
if decoded.RouteData.TasksPage.ActiveWorkspaceID != workspaces[0].ID {
|
||||
t.Fatalf("route active workspace = %q, want %q", decoded.RouteData.TasksPage.ActiveWorkspaceID, workspaces[0].ID)
|
||||
}
|
||||
if decoded.RouteData.TasksPage.Total != 1 {
|
||||
t.Fatalf("tasks total = %d, want 1", decoded.RouteData.TasksPage.Total)
|
||||
}
|
||||
if len(decoded.RouteData.TasksPage.Tasks) != 1 || decoded.RouteData.TasksPage.Tasks[0].ID != created.ID {
|
||||
t.Fatalf("route tasks = %+v, want task %q", decoded.RouteData.TasksPage.Tasks, created.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootRouteDataTasksUsesActiveWorkspaceCookie(t *testing.T) {
|
||||
taskSvc, workflowSvc := newBootStateTestServices(t)
|
||||
ctx := context.Background()
|
||||
if _, err := taskSvc.CreateWorkspace(ctx, &taskservice.CreateWorkspaceRequest{
|
||||
Name: "Cookie Workspace",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateWorkspace: %v", err)
|
||||
}
|
||||
workspace := nonFallbackWorkspace(t, ctx, taskSvc)
|
||||
if _, err := taskSvc.CreateWorkflow(ctx, &taskservice.CreateWorkflowRequest{
|
||||
WorkspaceID: workspace.ID,
|
||||
Name: "Cookie Workflow",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateWorkflow: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/app-state?path=%2Ftasks", nil)
|
||||
req.AddCookie(&http.Cookie{Name: "kandev-active-workspace", Value: workspace.ID})
|
||||
payload := bootPayload(ctx, req, routeParams{
|
||||
taskSvc: taskSvc,
|
||||
services: &Services{
|
||||
Workflow: workflowSvc,
|
||||
},
|
||||
}, webapp.ClassifyRoute("/tasks"))
|
||||
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal payload: %v", err)
|
||||
}
|
||||
var decoded struct {
|
||||
InitialState struct {
|
||||
Workspaces struct {
|
||||
ActiveID string `json:"activeId"`
|
||||
} `json:"workspaces"`
|
||||
} `json:"initialState"`
|
||||
RouteData struct {
|
||||
TasksPage struct {
|
||||
ActiveWorkspaceID string `json:"activeWorkspaceId"`
|
||||
} `json:"tasksPage"`
|
||||
} `json:"routeData"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal payload: %v", err)
|
||||
}
|
||||
if decoded.InitialState.Workspaces.ActiveID != workspace.ID {
|
||||
t.Fatalf("initial active workspace = %q, want %q", decoded.InitialState.Workspaces.ActiveID, workspace.ID)
|
||||
}
|
||||
if decoded.RouteData.TasksPage.ActiveWorkspaceID != workspace.ID {
|
||||
t.Fatalf("route active workspace = %q, want %q", decoded.RouteData.TasksPage.ActiveWorkspaceID, workspace.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootRouteDataIntegrationRouteIncludesOnlyLocalContext(t *testing.T) {
|
||||
taskSvc, workflowSvc := newBootStateTestServices(t)
|
||||
ctx := context.Background()
|
||||
workspaces, err := taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListWorkspaces: %v", err)
|
||||
}
|
||||
|
||||
payload := bootPayload(ctx, nil, routeParams{
|
||||
taskSvc: taskSvc,
|
||||
services: &Services{
|
||||
Workflow: workflowSvc,
|
||||
},
|
||||
}, webapp.ClassifyRoute("/github"))
|
||||
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal payload: %v", err)
|
||||
}
|
||||
var decoded struct {
|
||||
RouteData map[string]json.RawMessage `json:"routeData"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal payload: %v", err)
|
||||
}
|
||||
if _, ok := decoded.RouteData["github"]; ok {
|
||||
t.Fatal("integration boot data must not include provider GitHub results")
|
||||
}
|
||||
var contextData struct {
|
||||
ActiveWorkspaceID string `json:"activeWorkspaceId"`
|
||||
Workflows []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"workflows"`
|
||||
Steps []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"steps"`
|
||||
Repositories []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"repositories"`
|
||||
}
|
||||
if err := json.Unmarshal(decoded.RouteData["routeContext"], &contextData); err != nil {
|
||||
t.Fatalf("Unmarshal routeContext: %v", err)
|
||||
}
|
||||
if contextData.ActiveWorkspaceID != workspaces[0].ID {
|
||||
t.Fatalf("active workspace = %q, want %q", contextData.ActiveWorkspaceID, workspaces[0].ID)
|
||||
}
|
||||
if len(contextData.Workflows) == 0 || len(contextData.Steps) == 0 {
|
||||
t.Fatalf("expected local workflow context, got %+v", contextData)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootRouteDataIntegrationRouteUsesActiveWorkspaceCookie(t *testing.T) {
|
||||
taskSvc, workflowSvc := newBootStateTestServices(t)
|
||||
ctx := context.Background()
|
||||
if _, err := taskSvc.CreateWorkspace(ctx, &taskservice.CreateWorkspaceRequest{
|
||||
Name: "Cookie Workspace",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateWorkspace: %v", err)
|
||||
}
|
||||
workspace := nonFallbackWorkspace(t, ctx, taskSvc)
|
||||
if _, err := taskSvc.CreateWorkflow(ctx, &taskservice.CreateWorkflowRequest{
|
||||
WorkspaceID: workspace.ID,
|
||||
Name: "Cookie Workflow",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateWorkflow: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/app-state?path=%2Fgithub", nil)
|
||||
req.AddCookie(&http.Cookie{Name: "kandev-active-workspace", Value: workspace.ID})
|
||||
payload := bootPayload(ctx, req, routeParams{
|
||||
taskSvc: taskSvc,
|
||||
services: &Services{
|
||||
Workflow: workflowSvc,
|
||||
},
|
||||
}, webapp.ClassifyRoute("/github"))
|
||||
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal payload: %v", err)
|
||||
}
|
||||
var decoded struct {
|
||||
RouteData struct {
|
||||
RouteContext struct {
|
||||
ActiveWorkspaceID string `json:"activeWorkspaceId"`
|
||||
} `json:"routeContext"`
|
||||
} `json:"routeData"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal payload: %v", err)
|
||||
}
|
||||
if decoded.RouteData.RouteContext.ActiveWorkspaceID != workspace.ID {
|
||||
t.Fatalf("route active workspace = %q, want %q", decoded.RouteData.RouteContext.ActiveWorkspaceID, workspace.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func nonFallbackWorkspace(t *testing.T, ctx context.Context, taskSvc *taskservice.Service) *models.Workspace {
|
||||
t.Helper()
|
||||
workspaces, err := taskSvc.ListWorkspaces(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListWorkspaces: %v", err)
|
||||
}
|
||||
if len(workspaces) < 2 {
|
||||
t.Fatalf("expected at least two workspaces, got %d", len(workspaces))
|
||||
}
|
||||
fallbackID := workspaces[0].ID
|
||||
for _, workspace := range workspaces {
|
||||
if workspace != nil && workspace.ID != fallbackID {
|
||||
return workspace
|
||||
}
|
||||
}
|
||||
t.Fatal("expected a workspace different from the default fallback")
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestBootInitialStateSettingsWithNilServicesDoesNotPanic(t *testing.T) {
|
||||
state := bootInitialState(
|
||||
context.Background(),
|
||||
nil,
|
||||
routeParams{features: config.FeaturesConfig{Office: true}},
|
||||
webapp.ClassifyRoute("/settings/prompts"),
|
||||
)
|
||||
|
||||
if _, ok := state["features"]; !ok {
|
||||
t.Fatal("features should always be present even when optional services are unavailable")
|
||||
}
|
||||
if _, ok := state["prompts"]; ok {
|
||||
t.Fatal("prompts should not be marked loaded when the prompts controller is unavailable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryValueReadsRouteQueryFromAppStatePath(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/app-state?path=%2F%3FworkspaceId%3Dws-1%26workflowId%3Dwf-1", nil)
|
||||
|
||||
if got := queryValue(req, "workspaceId"); got != "ws-1" {
|
||||
t.Fatalf("workspaceId = %q, want ws-1", got)
|
||||
}
|
||||
if got := queryValue(req, "workflowId"); got != "wf-1" {
|
||||
t.Fatalf("workflowId = %q, want wf-1", got)
|
||||
}
|
||||
}
|
||||
|
||||
type bootStateTestHarness struct {
|
||||
taskSvc *taskservice.Service
|
||||
workflowSvc *workflowservice.Service
|
||||
userCtrl *usercontroller.Controller
|
||||
userSvc *userservice.Service
|
||||
}
|
||||
|
||||
func newBootStateTestServices(t *testing.T) (*taskservice.Service, *workflowservice.Service) {
|
||||
harness := newBootStateTestHarness(t)
|
||||
return harness.taskSvc, harness.workflowSvc
|
||||
}
|
||||
|
||||
func newBootStateTestHarness(t *testing.T) bootStateTestHarness {
|
||||
t.Helper()
|
||||
dbConn, err := db.OpenSQLite(filepath.Join(t.TempDir(), "test.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("OpenSQLite: %v", err)
|
||||
}
|
||||
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
||||
taskRepo, cleanup, err := taskrepo.Provide(sqlxDB, sqlxDB, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("task repository: %v", err)
|
||||
}
|
||||
if _, err := worktree.NewSQLiteStore(sqlxDB, sqlxDB); err != nil {
|
||||
t.Fatalf("worktree store: %v", err)
|
||||
}
|
||||
workflowRepo, err := workflowrepo.NewWithDB(sqlxDB, sqlxDB, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("workflow repository: %v", err)
|
||||
}
|
||||
userRepo, userCleanup, err := userstore.Provide(sqlxDB, sqlxDB)
|
||||
if err != nil {
|
||||
t.Fatalf("user repository: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := sqlxDB.Close(); err != nil {
|
||||
t.Errorf("close db: %v", err)
|
||||
}
|
||||
if err := cleanup(); err != nil {
|
||||
t.Errorf("close task repo: %v", err)
|
||||
}
|
||||
if err := userCleanup(); err != nil {
|
||||
t.Errorf("close user repo: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
log, _ := logger.NewLogger(logger.LoggingConfig{Level: "error", Format: "json", OutputPath: "stdout"})
|
||||
eventBus := bus.NewMemoryEventBus(log)
|
||||
userSvc := userservice.NewService(userRepo, eventBus, log)
|
||||
workflowSvc := workflowservice.NewService(workflowRepo, log)
|
||||
taskSvc := taskservice.NewService(
|
||||
taskservice.Repos{
|
||||
Workspaces: taskRepo,
|
||||
Tasks: taskRepo,
|
||||
TaskRepos: taskRepo,
|
||||
Workflows: taskRepo,
|
||||
Messages: taskRepo,
|
||||
Turns: taskRepo,
|
||||
Sessions: taskRepo,
|
||||
GitSnapshots: taskRepo,
|
||||
RepoEntities: taskRepo,
|
||||
Executors: taskRepo,
|
||||
Environments: taskRepo,
|
||||
TaskEnvironments: taskRepo,
|
||||
Reviews: taskRepo,
|
||||
},
|
||||
eventBus,
|
||||
log,
|
||||
taskservice.RepositoryDiscoveryConfig{},
|
||||
)
|
||||
taskSvc.SetWorkflowStepCreator(workflowSvc)
|
||||
taskSvc.SetWorkflowStepGetter(&workflowStepGetterAdapter{svc: workflowSvc})
|
||||
taskSvc.SetStartStepResolver(&startStepResolverAdapter{svc: workflowSvc})
|
||||
workflowSvc.SetWorkflowProvider(&workflowProviderAdapter{svc: taskSvc})
|
||||
return bootStateTestHarness{
|
||||
taskSvc: taskSvc,
|
||||
workflowSvc: workflowSvc,
|
||||
userCtrl: usercontroller.NewController(userSvc),
|
||||
userSvc: userSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewWebAppHandlerUsesEmbeddedAssetsWithoutDistDir(t *testing.T) {
|
||||
t.Setenv("KANDEV_WEB_DIST_DIR", "")
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
handler, source, ok := newWebAppHandler(routeParams{})
|
||||
if !ok {
|
||||
t.Fatal("expected embedded web app handler when no filesystem dist dir exists")
|
||||
}
|
||||
if source != "embedded" {
|
||||
t.Fatalf("source = %q, want embedded", source)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "window.__KANDEV_BOOT_PAYLOAD__") {
|
||||
t.Fatal("embedded shell should receive boot payload injection")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveActiveOfficeWorkspaceIDPrefersCookie(t *testing.T) {
|
||||
workspaces := []taskdto.WorkspaceDTO{
|
||||
{ID: "ws-a", OfficeWorkflowID: "office-a"},
|
||||
{ID: "ws-b", OfficeWorkflowID: "office-b"},
|
||||
}
|
||||
|
||||
got := resolveActiveOfficeWorkspaceID(workspaces, "ws-b")
|
||||
if got != "ws-b" {
|
||||
t.Fatalf("expected cookie workspace to win, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendSessionStateMessage_IncludesUpdatedAt(t *testing.T) {
|
||||
updatedAt := time.Date(2026, 1, 2, 12, 0, 0, 0, time.UTC)
|
||||
session := &models.TaskSession{
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -9,10 +12,25 @@ import (
|
||||
// corsMiddleware returns a CORS middleware for HTTP and WebSocket connections.
|
||||
func corsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
if origin := c.Request.Header.Get("Origin"); origin != "" {
|
||||
if !isAllowedCORSOrigin(origin, c.Request.Host) {
|
||||
if c.Request.Method == http.MethodOptions {
|
||||
c.AbortWithStatus(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("Access-Control-Allow-Origin", origin)
|
||||
c.Header("Vary", "Origin")
|
||||
c.Header("Access-Control-Allow-Credentials", "true")
|
||||
} else {
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
c.Header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS")
|
||||
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Authorization, Upgrade, Connection, Sec-WebSocket-Key, Sec-WebSocket-Version, Sec-WebSocket-Protocol")
|
||||
c.Header("Access-Control-Allow-Credentials", "true")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
@@ -22,3 +40,38 @@ func corsMiddleware() gin.HandlerFunc {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func isAllowedCORSOrigin(origin string, requestHost string) bool {
|
||||
parsed, err := url.Parse(origin)
|
||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||
return false
|
||||
}
|
||||
if parsed.Scheme != "http" && parsed.Scheme != "https" {
|
||||
return false
|
||||
}
|
||||
|
||||
originHost := normalizeCORSHost(parsed.Hostname())
|
||||
host := normalizeCORSHost(requestHost)
|
||||
if originHost == "" || host == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return originHost == host || (isLoopbackCORSHost(originHost) && isLoopbackCORSHost(host))
|
||||
}
|
||||
|
||||
func normalizeCORSHost(host string) string {
|
||||
if parsedHost, _, err := net.SplitHostPort(host); err == nil {
|
||||
host = parsedHost
|
||||
}
|
||||
|
||||
return strings.ToLower(strings.Trim(host, "[]"))
|
||||
}
|
||||
|
||||
func isLoopbackCORSHost(host string) bool {
|
||||
if host == "localhost" {
|
||||
return true
|
||||
}
|
||||
|
||||
ip := net.ParseIP(host)
|
||||
return ip != nil && ip.IsLoopback()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestCORSMiddlewareEchoesOriginForCredentialedRequests(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
router.Use(corsMiddleware())
|
||||
router.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "pong")
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ping", nil)
|
||||
req.Host = "localhost:38429"
|
||||
req.Header.Set("Origin", "http://localhost:37429")
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "http://localhost:37429" {
|
||||
t.Fatalf("Access-Control-Allow-Origin = %q, want request origin", got)
|
||||
}
|
||||
if got := rec.Header().Get("Access-Control-Allow-Credentials"); got != "true" {
|
||||
t.Fatalf("Access-Control-Allow-Credentials = %q, want true", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCORSMiddlewareAllowsLoopbackAliasOriginForCredentialedRequests(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
router.Use(corsMiddleware())
|
||||
router.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "pong")
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ping", nil)
|
||||
req.Host = "127.0.0.1:38429"
|
||||
req.Header.Set("Origin", "http://localhost:37429")
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "http://localhost:37429" {
|
||||
t.Fatalf("Access-Control-Allow-Origin = %q, want loopback origin", got)
|
||||
}
|
||||
if got := rec.Header().Get("Access-Control-Allow-Credentials"); got != "true" {
|
||||
t.Fatalf("Access-Control-Allow-Credentials = %q, want true", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCORSMiddlewareRejectsCredentialedRequestsFromUnrelatedOrigins(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
router.Use(corsMiddleware())
|
||||
router.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "pong")
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ping", nil)
|
||||
req.Host = "localhost:38429"
|
||||
req.Header.Set("Origin", "https://example.invalid")
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "" {
|
||||
t.Fatalf("Access-Control-Allow-Origin = %q, want no credentialed CORS", got)
|
||||
}
|
||||
if got := rec.Header().Get("Access-Control-Allow-Credentials"); got != "" {
|
||||
t.Fatalf("Access-Control-Allow-Credentials = %q, want no credentialed CORS", got)
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ func copyFile(src, dst string, mode fs.FileMode) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildWeb runs the Next.js production build. When skipInstall is true the
|
||||
// buildWeb runs the Vite production build. When skipInstall is true the
|
||||
// pnpm install step is skipped (CI already runs it before invoking the CLI).
|
||||
func buildWeb(ctx context.Context, skipInstall bool) error {
|
||||
steps := []struct {
|
||||
@@ -157,9 +157,7 @@ func buildCLI(ctx context.Context) error {
|
||||
// packageBundle creates a tar.gz matching the Docker container layout:
|
||||
//
|
||||
// app/apps/backend/bin/{kandev,agentctl,mock-agent}
|
||||
// app/apps/web/.next/standalone/ (Next.js server + node_modules)
|
||||
// app/apps/web/.next/standalone/web/.next/static/ (static assets inside standalone)
|
||||
// app/apps/web/.next/standalone/web/public/ (public assets inside standalone)
|
||||
// app/apps/web/dist/ (Vite SPA assets)
|
||||
// usr/local/lib/kandev-cli/ (CLI launcher bundle)
|
||||
func packageBundle(binDir, tarPath string) error {
|
||||
f, err := os.Create(tarPath)
|
||||
@@ -184,24 +182,11 @@ func packageBundle(binDir, tarPath string) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Add Next.js standalone output.
|
||||
standaloneDir := filepath.Join(appsDir, "web", ".next", "standalone")
|
||||
if err := addDirToTar(tw, standaloneDir, filepath.Join("app", "apps", "web", ".next", "standalone")); err != nil {
|
||||
return fmt.Errorf("add standalone: %w", err)
|
||||
}
|
||||
|
||||
// Add static assets into the standalone web dir (mirrors the Dockerfile COPY).
|
||||
staticDir := filepath.Join(appsDir, "web", ".next", "static")
|
||||
staticDst := filepath.Join("app", "apps", "web", ".next", "standalone", "web", ".next", "static")
|
||||
if err := addDirToTar(tw, staticDir, staticDst); err != nil {
|
||||
return fmt.Errorf("add static: %w", err)
|
||||
}
|
||||
|
||||
// Add public directory into the standalone web dir.
|
||||
publicDir := filepath.Join(appsDir, "web", "public")
|
||||
publicDst := filepath.Join("app", "apps", "web", ".next", "standalone", "web", "public")
|
||||
if err := addDirToTar(tw, publicDir, publicDst); err != nil {
|
||||
return fmt.Errorf("add public: %w", err)
|
||||
// Add Vite SPA assets. `kandev start` resolves these from /app/apps/web/dist
|
||||
// and passes KANDEV_WEB_DIST_DIR to the backend.
|
||||
webDistDir := filepath.Join(appsDir, "web", "dist")
|
||||
if err := addDirToTar(tw, webDistDir, filepath.Join("app", "apps", "web", "dist")); err != nil {
|
||||
return fmt.Errorf("add web dist: %w", err)
|
||||
}
|
||||
|
||||
cliDir := filepath.Join(repoRootDir, "dist", "kandev", "cli")
|
||||
@@ -249,7 +234,7 @@ func addDirToTar(tw *tar.Writer, srcDir, dstPrefix string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Preserve symlinks as-is (Next.js standalone relies on them for node_modules).
|
||||
// Preserve symlinks as-is.
|
||||
if info.Mode()&fs.ModeSymlink != 0 {
|
||||
target, err := os.Readlink(path)
|
||||
if err != nil {
|
||||
|
||||
@@ -122,9 +122,8 @@ mkdir -p /data
|
||||
|
||||
# Kill any agentctl orphans from previous runs.
|
||||
pkill -f agentctl || true
|
||||
# Kill any stale web (node) process from older preview deployments, where the
|
||||
# web server was nohup'd outside the service process group.
|
||||
pkill -f '/app/apps/web/.next/standalone/web/server.js' || true
|
||||
# Kill any stale web (node) process from older preview deployments.
|
||||
pkill -f '/app/apps/web/.*/server.js' || true
|
||||
sleep 1
|
||||
cd /app
|
||||
|
||||
@@ -280,7 +279,7 @@ func fetchSpriteLogs(ctx context.Context, sprite *sprites.Sprite) string {
|
||||
logCtx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
script := `echo "=== /var/log/kandev.log ==="; tail -50 /var/log/kandev.log 2>/dev/null || echo "(empty)"; echo "=== /var/log/kandev-web.log ==="; tail -20 /var/log/kandev-web.log 2>/dev/null || echo "(empty)"`
|
||||
script := `echo "=== /var/log/kandev.log ==="; tail -50 /var/log/kandev.log 2>/dev/null || echo "(empty)"`
|
||||
out, err := sprite.CommandContext(logCtx, "bash", "-c", script).CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Sprintf("[log fetch error: %v]\n%s", err, string(out))
|
||||
|
||||
@@ -29,6 +29,9 @@ func TestBuildExtractScript(t *testing.T) {
|
||||
if strings.Contains(script, "nohup node") {
|
||||
t.Errorf("script should not start web outside the CLI supervisor")
|
||||
}
|
||||
if strings.Contains(script, ".next") {
|
||||
t.Errorf("script should not refer to Next.js build output")
|
||||
}
|
||||
if strings.Contains(script, "/app/apps/backend/bin/kandev >") {
|
||||
t.Errorf("script should not launch the backend binary directly")
|
||||
}
|
||||
|
||||
@@ -963,6 +963,29 @@ func (m *Manager) IsAgentRunningForSession(ctx context.Context, sessionID string
|
||||
return status.IsAgentRunning()
|
||||
}
|
||||
|
||||
// IsAgentReadyForPrompt reports whether the session can accept an ACP prompt
|
||||
// immediately. A process can be "running" while its update stream is still
|
||||
// reconnecting after resume; PromptAgent needs the stream-backed request path.
|
||||
func (m *Manager) IsAgentReadyForPrompt(ctx context.Context, sessionID string) bool {
|
||||
execution, exists := m.GetExecutionBySessionID(sessionID)
|
||||
if !exists {
|
||||
return false
|
||||
}
|
||||
|
||||
if execution.PassthroughProcessID != "" || execution.IsPassthrough {
|
||||
return m.IsAgentRunningForSession(ctx, sessionID)
|
||||
}
|
||||
|
||||
if execution.Status != v1.AgentStatusReady || execution.agentctl == nil {
|
||||
return false
|
||||
}
|
||||
if !execution.sessionInitialized || execution.ACPSessionID == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return execution.agentctl.HasAgentStream()
|
||||
}
|
||||
|
||||
// UpdateStatus updates the status of an execution
|
||||
func (m *Manager) UpdateStatus(executionID string, status v1.AgentStatus) error {
|
||||
if err := m.executionStore.WithLock(executionID, func(execution *AgentExecution) {
|
||||
|
||||
@@ -734,6 +734,72 @@ func TestIsAgentRunningForSession(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIsAgentReadyForPrompt(t *testing.T) {
|
||||
t.Run("ACP execution requires ready status, initialized session, and update stream", func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
mock := newMockAgentServer(t)
|
||||
t.Cleanup(mock.Close)
|
||||
|
||||
client := createTestClient(t, mock.server.URL)
|
||||
t.Cleanup(client.Close)
|
||||
|
||||
store := NewExecutionStore()
|
||||
execution := &AgentExecution{
|
||||
ID: "exec-acp-ready",
|
||||
SessionID: "session-acp-ready",
|
||||
ACPSessionID: "acp-session-1",
|
||||
Status: v1.AgentStatusReady,
|
||||
agentctl: client,
|
||||
}
|
||||
err := store.Add(execution)
|
||||
require.NoError(t, err)
|
||||
|
||||
mgr := &Manager{
|
||||
executionStore: store,
|
||||
logger: newTestLogger().WithFields(),
|
||||
}
|
||||
require.False(t, mgr.IsAgentReadyForPrompt(ctx, "session-acp-ready"))
|
||||
|
||||
err = client.StreamUpdates(ctx, func(agentctl.AgentEvent) {}, nil, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-mock.wsConnected:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("timed out waiting for updates stream")
|
||||
}
|
||||
|
||||
require.False(t, mgr.IsAgentReadyForPrompt(ctx, "session-acp-ready"))
|
||||
|
||||
err = store.WithLock("exec-acp-ready", func(exec *AgentExecution) {
|
||||
exec.sessionInitialized = true
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, mgr.IsAgentReadyForPrompt(ctx, "session-acp-ready"))
|
||||
|
||||
err = store.WithLock("exec-acp-ready", func(exec *AgentExecution) {
|
||||
exec.ACPSessionID = ""
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.False(t, mgr.IsAgentReadyForPrompt(ctx, "session-acp-ready"))
|
||||
|
||||
err = store.WithLock("exec-acp-ready", func(exec *AgentExecution) {
|
||||
exec.ACPSessionID = "acp-session-1"
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
store.UpdateStatus("exec-acp-ready", v1.AgentStatusRunning)
|
||||
require.False(t, mgr.IsAgentReadyForPrompt(ctx, "session-acp-ready"))
|
||||
})
|
||||
|
||||
t.Run("missing execution returns false", func(t *testing.T) {
|
||||
mgr := &Manager{executionStore: NewExecutionStore(), logger: newTestLogger().WithFields()}
|
||||
require.False(t, mgr.IsAgentReadyForPrompt(context.Background(), "missing"))
|
||||
})
|
||||
}
|
||||
|
||||
// TestEffectiveSessionMode covers the fresh-launch mode propagation for issue
|
||||
// #1183: a persisted session_mode (e.g. from a set_session_mode workflow step)
|
||||
// must override the profile default at ACP session init, while a missing
|
||||
|
||||
@@ -686,18 +686,46 @@ func (sm *SessionManager) retryPromptAfterReconnect(
|
||||
prompt string,
|
||||
attachments []v1.MessageAttachment,
|
||||
) error {
|
||||
ready := make(chan struct{})
|
||||
sm.streamManager.connectUpdatesStreamAsync(execution, ready)
|
||||
reconnectCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
select {
|
||||
case <-ready:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(5 * time.Second):
|
||||
return fmt.Errorf("timed out waiting for updates stream reconnect")
|
||||
var lastErr error
|
||||
for {
|
||||
if !execution.agentctl.HasAgentStream() {
|
||||
ready := make(chan struct{})
|
||||
sm.streamManager.connectUpdatesStreamAsync(execution, ready)
|
||||
|
||||
select {
|
||||
case <-ready:
|
||||
case <-reconnectCtx.Done():
|
||||
if lastErr != nil {
|
||||
return fmt.Errorf("timed out waiting for updates stream reconnect: %w", lastErr)
|
||||
}
|
||||
return reconnectCtx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
if execution.agentctl.HasAgentStream() {
|
||||
if err := execution.agentctl.Prompt(reconnectCtx, prompt, attachments); err == nil {
|
||||
return nil
|
||||
} else if !isAgentStreamNotConnectedErr(err) {
|
||||
return err
|
||||
} else {
|
||||
lastErr = err
|
||||
}
|
||||
} else {
|
||||
lastErr = fmt.Errorf("agent stream not connected")
|
||||
}
|
||||
|
||||
select {
|
||||
case <-reconnectCtx.Done():
|
||||
if lastErr != nil {
|
||||
return fmt.Errorf("timed out waiting for updates stream reconnect: %w", lastErr)
|
||||
}
|
||||
return reconnectCtx.Err()
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
|
||||
return execution.agentctl.Prompt(ctx, prompt, attachments)
|
||||
}
|
||||
|
||||
// jsonRPCMethodNotFound is the JSON-RPC 2.0 error code for "Method not found".
|
||||
|
||||
@@ -43,12 +43,13 @@ func newTestStopCh(t *testing.T) chan struct{} {
|
||||
// mockAgentServer creates a test WebSocket server simulating agentctl.
|
||||
// It responds to agent stream requests and tracks which actions were called and in what order.
|
||||
type mockAgentServer struct {
|
||||
server *httptest.Server
|
||||
mu sync.Mutex
|
||||
actionLog []string // ordered log of actions received
|
||||
upgrader websocket.Upgrader
|
||||
handler func(msg ws.Message) *ws.Message
|
||||
wsConnected chan struct{} // closed when WS stream connects
|
||||
server *httptest.Server
|
||||
mu sync.Mutex
|
||||
actionLog []string // ordered log of actions received
|
||||
rejectStreamAttempts int
|
||||
upgrader websocket.Upgrader
|
||||
handler func(msg ws.Message) *ws.Message
|
||||
wsConnected chan struct{} // closed when WS stream connects
|
||||
}
|
||||
|
||||
func newMockAgentServer(t *testing.T) *mockAgentServer {
|
||||
@@ -64,6 +65,15 @@ func newMockAgentServer(t *testing.T) *mockAgentServer {
|
||||
|
||||
// Agent stream WebSocket endpoint
|
||||
mux.HandleFunc("/api/v1/agent/stream", func(w http.ResponseWriter, r *http.Request) {
|
||||
m.mu.Lock()
|
||||
if m.rejectStreamAttempts > 0 {
|
||||
m.rejectStreamAttempts--
|
||||
m.mu.Unlock()
|
||||
http.Error(w, "stream temporarily unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
m.mu.Unlock()
|
||||
|
||||
conn, err := m.upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -140,6 +150,12 @@ func (m *mockAgentServer) buildResponse(msg ws.Message) *ws.Message {
|
||||
return m.defaultHandler(msg)
|
||||
}
|
||||
|
||||
func (m *mockAgentServer) rejectNextStreamAttempts(count int) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.rejectStreamAttempts = count
|
||||
}
|
||||
|
||||
func (m *mockAgentServer) defaultHandler(msg ws.Message) *ws.Message {
|
||||
switch msg.Action {
|
||||
case "agent.initialize":
|
||||
@@ -761,6 +777,50 @@ func waitForWSConnected(t *testing.T, mock *mockAgentServer) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendPrompt_RetriesUntilUpdateStreamReconnects(t *testing.T) {
|
||||
mock := newMockAgentServer(t)
|
||||
defer mock.Close()
|
||||
mock.rejectNextStreamAttempts(1)
|
||||
|
||||
log := newSessionTestLogger()
|
||||
stopCh := newTestStopCh(t)
|
||||
sm := NewSessionManager(log, stopCh)
|
||||
|
||||
streamMgr := NewStreamManager(log, StreamCallbacks{
|
||||
OnAgentEvent: func(execution *AgentExecution, event agentctl.AgentEvent) {},
|
||||
}, nil, stopCh)
|
||||
cleanupStreamManager(t, stopCh, streamMgr)
|
||||
sm.SetDependencies(nil, streamMgr, nil, nil)
|
||||
|
||||
client := createTestClient(t, mock.server.URL)
|
||||
defer client.Close()
|
||||
|
||||
execution := &AgentExecution{
|
||||
ID: "test-exec",
|
||||
TaskID: "test-task",
|
||||
SessionID: "test-session",
|
||||
WorkspacePath: "/workspace",
|
||||
agentctl: client,
|
||||
promptDoneCh: make(chan PromptCompletionSignal, 1),
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := sm.SendPrompt(ctx, execution, "hello after resume", false, nil, true)
|
||||
if err != nil {
|
||||
t.Fatalf("SendPrompt should retry after a transient stream reconnect failure: %v", err)
|
||||
}
|
||||
if result == nil || result.StopReason != PromptStopReasonDispatched {
|
||||
t.Fatalf("expected StopReason=%q, got %+v", PromptStopReasonDispatched, result)
|
||||
}
|
||||
|
||||
actions := mock.getActionLog()
|
||||
if len(actions) != 1 || actions[0] != "agent.prompt" {
|
||||
t.Fatalf("expected one prompt after reconnect, got actions: %v", actions)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSendPrompt_DispatchOnlyReturnsWithoutWaiting verifies that dispatch-only
|
||||
// mode returns immediately after agentctl.Prompt succeeds, without blocking on
|
||||
// the agent's complete event. This is what message_task_kandev relies on so the
|
||||
|
||||
@@ -11,7 +11,7 @@ const (
|
||||
// Backend is the default HTTP port for the kandev Go backend.
|
||||
Backend = 38429
|
||||
|
||||
// Web is the default HTTP port for the Next.js web server.
|
||||
// Web is the default HTTP port for the optional Vite dev server.
|
||||
Web = 37429
|
||||
|
||||
// AgentCtl is the default control port for the agentctl sidecar.
|
||||
|
||||
@@ -339,21 +339,22 @@ func (c *MockController) ensureMockPRForRequest(ctx context.Context, req *associ
|
||||
return
|
||||
}
|
||||
c.mock.AddPR(&PR{
|
||||
Number: req.PRNumber,
|
||||
Title: req.PRTitle,
|
||||
URL: req.PRURL,
|
||||
HTMLURL: req.PRURL,
|
||||
State: req.State,
|
||||
HeadBranch: req.HeadBranch,
|
||||
HeadSHA: mockHeadSHA(req.Owner, req.Repo, req.PRNumber),
|
||||
BaseBranch: req.BaseBranch,
|
||||
AuthorLogin: req.AuthorLogin,
|
||||
RepoOwner: req.Owner,
|
||||
RepoName: req.Repo,
|
||||
Additions: req.Additions,
|
||||
Deletions: req.Deletions,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
Number: req.PRNumber,
|
||||
Title: req.PRTitle,
|
||||
URL: req.PRURL,
|
||||
HTMLURL: req.PRURL,
|
||||
State: req.State,
|
||||
HeadBranch: req.HeadBranch,
|
||||
HeadSHA: mockHeadSHA(req.Owner, req.Repo, req.PRNumber),
|
||||
BaseBranch: req.BaseBranch,
|
||||
AuthorLogin: req.AuthorLogin,
|
||||
MergeableState: req.MergeableState,
|
||||
RepoOwner: req.Owner,
|
||||
RepoName: req.Repo,
|
||||
Additions: req.Additions,
|
||||
Deletions: req.Deletions,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestEnsureMockPRForRequestCopiesMergeableState(t *testing.T) {
|
||||
mock := NewMockClient()
|
||||
controller := &MockController{mock: mock}
|
||||
req := &associateTaskPRRequest{
|
||||
Owner: "testorg",
|
||||
Repo: "testrepo",
|
||||
PRNumber: 102,
|
||||
PRURL: "https://github.com/testorg/testrepo/pull/102",
|
||||
PRTitle: "Ready to ship",
|
||||
HeadBranch: "feat/ready",
|
||||
BaseBranch: "main",
|
||||
AuthorLogin: "test-user",
|
||||
State: "open",
|
||||
MergeableState: "clean",
|
||||
}
|
||||
|
||||
controller.ensureMockPRForRequest(context.Background(), req, time.Now().UTC())
|
||||
|
||||
pr, err := mock.GetPR(context.Background(), req.Owner, req.Repo, req.PRNumber)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPR: %v", err)
|
||||
}
|
||||
if pr == nil {
|
||||
t.Fatal("expected synthetic PR")
|
||||
}
|
||||
if pr.MergeableState != "clean" {
|
||||
t.Fatalf("MergeableState = %q, want clean", pr.MergeableState)
|
||||
}
|
||||
}
|
||||
@@ -396,6 +396,10 @@ func (s *SimulatedAgentManagerClient) IsAgentRunningForSession(ctx context.Conte
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *SimulatedAgentManagerClient) IsAgentReadyForPrompt(ctx context.Context, sessionID string) bool {
|
||||
return s.IsAgentRunningForSession(ctx, sessionID)
|
||||
}
|
||||
|
||||
// CancelAgent cancels the current agent turn for a session.
|
||||
// Returns lifecycle.ErrNoExecutionForSession when no live execution exists for the session
|
||||
// (for example, after CrashAgentForSession has been used to simulate a crash), matching the
|
||||
|
||||
@@ -80,6 +80,64 @@ type DashboardResponse struct {
|
||||
AgentSummaries []AgentSummary `json:"agent_summaries"`
|
||||
}
|
||||
|
||||
// NewDashboardResponse maps the internal dashboard model to the public JSON
|
||||
// shape consumed by both the HTTP endpoint and the Go-rendered SPA boot payload.
|
||||
func NewDashboardResponse(data *models.DashboardData, summaries []AgentSummary) DashboardResponse {
|
||||
if summaries == nil {
|
||||
summaries = []AgentSummary{}
|
||||
}
|
||||
if data == nil {
|
||||
return DashboardResponse{AgentSummaries: summaries}
|
||||
}
|
||||
|
||||
runActivity := make([]RunActivityDay, len(data.RunActivity))
|
||||
for i, d := range data.RunActivity {
|
||||
runActivity[i] = RunActivityDay{
|
||||
Date: d.Date,
|
||||
Succeeded: d.Succeeded,
|
||||
Failed: d.Failed,
|
||||
Other: d.Other,
|
||||
}
|
||||
}
|
||||
|
||||
recentTasks := make([]RecentTaskDTO, len(data.RecentTasks))
|
||||
for i, t := range data.RecentTasks {
|
||||
recentTasks[i] = RecentTaskDTO{
|
||||
ID: t.ID,
|
||||
Identifier: t.Identifier,
|
||||
Title: t.Title,
|
||||
Status: dbStateToOfficeStatus(t.Status),
|
||||
AssigneeAgentProfileID: t.AssigneeAgentProfileID,
|
||||
UpdatedAt: t.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
return DashboardResponse{
|
||||
AgentCount: data.AgentCount,
|
||||
RunningCount: data.RunningCount,
|
||||
PausedCount: data.PausedCount,
|
||||
ErrorCount: data.ErrorCount,
|
||||
TasksInProgress: data.TasksInProgress,
|
||||
OpenTasks: data.OpenTasks,
|
||||
BlockedTasks: data.BlockedTasks,
|
||||
MonthSpendSubcents: data.MonthSpendSubcents,
|
||||
PendingApprovals: data.PendingApprovals,
|
||||
RecentActivity: data.RecentActivity,
|
||||
TaskCount: data.TaskCount,
|
||||
SkillCount: data.SkillCount,
|
||||
RoutineCount: data.RoutineCount,
|
||||
RunActivity: runActivity,
|
||||
TaskBreakdown: TaskBreakdown{
|
||||
Open: data.TaskBreakdown.Open,
|
||||
InProgress: data.TaskBreakdown.InProgress,
|
||||
Blocked: data.TaskBreakdown.Blocked,
|
||||
Done: data.TaskBreakdown.Done,
|
||||
},
|
||||
RecentTasks: recentTasks,
|
||||
AgentSummaries: summaries,
|
||||
}
|
||||
}
|
||||
|
||||
// InboxResponse wraps inbox items. TotalCount mirrors len(Items) and is
|
||||
// included so the sidebar badge can read its count from the same payload
|
||||
// that drives the inbox page (Stream F of office optimization), avoiding a
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/office/models"
|
||||
)
|
||||
|
||||
func TestNewDashboardResponseUsesPublicJSONShape(t *testing.T) {
|
||||
resp := NewDashboardResponse(&models.DashboardData{
|
||||
AgentCount: 2,
|
||||
RunningCount: 1,
|
||||
PausedCount: 1,
|
||||
ErrorCount: 0,
|
||||
TasksInProgress: 3,
|
||||
OpenTasks: 4,
|
||||
BlockedTasks: 5,
|
||||
MonthSpendSubcents: 123,
|
||||
PendingApprovals: 6,
|
||||
TaskCount: 7,
|
||||
SkillCount: 8,
|
||||
RoutineCount: 9,
|
||||
RunActivity: []models.RunActivityDay{{
|
||||
Date: "2026-06-16",
|
||||
Succeeded: 2,
|
||||
Failed: 1,
|
||||
Other: 0,
|
||||
}},
|
||||
TaskBreakdown: models.TaskBreakdown{Open: 1, InProgress: 2, Blocked: 3, Done: 4},
|
||||
RecentTasks: []models.RecentTask{{
|
||||
ID: "task-1",
|
||||
Identifier: "KAN-1",
|
||||
Title: "Ship dashboard",
|
||||
Status: stateCompleted,
|
||||
UpdatedAt: "2026-06-16T12:00:00Z",
|
||||
}},
|
||||
}, nil)
|
||||
|
||||
raw, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal response: %v", err)
|
||||
}
|
||||
var decoded map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal response: %v", err)
|
||||
}
|
||||
for _, key := range []string{
|
||||
"agent_count",
|
||||
"task_count",
|
||||
"skill_count",
|
||||
"routine_count",
|
||||
"run_activity",
|
||||
"task_breakdown",
|
||||
"recent_tasks",
|
||||
"agent_summaries",
|
||||
} {
|
||||
if _, ok := decoded[key]; !ok {
|
||||
t.Fatalf("expected %q in dashboard response: %s", key, raw)
|
||||
}
|
||||
}
|
||||
|
||||
var recent []RecentTaskDTO
|
||||
if err := json.Unmarshal(decoded["recent_tasks"], &recent); err != nil {
|
||||
t.Fatalf("Unmarshal recent_tasks: %v", err)
|
||||
}
|
||||
if len(recent) != 1 || recent[0].Status != statusDoneLowercase {
|
||||
t.Fatalf("recent task status = %#v, want %q", recent, statusDoneLowercase)
|
||||
}
|
||||
|
||||
var summaries []AgentSummary
|
||||
if err := json.Unmarshal(decoded["agent_summaries"], &summaries); err != nil {
|
||||
t.Fatalf("Unmarshal agent_summaries: %v", err)
|
||||
}
|
||||
if summaries == nil {
|
||||
t.Fatal("agent_summaries should encode as an empty array, not null")
|
||||
}
|
||||
}
|
||||
@@ -144,49 +144,7 @@ func (h *Handler) getDashboard(c *gin.Context) {
|
||||
summaries = []AgentSummary{}
|
||||
}
|
||||
|
||||
runActivity := make([]RunActivityDay, len(data.RunActivity))
|
||||
for i, d := range data.RunActivity {
|
||||
runActivity[i] = RunActivityDay{
|
||||
Date: d.Date,
|
||||
Succeeded: d.Succeeded,
|
||||
Failed: d.Failed,
|
||||
Other: d.Other,
|
||||
}
|
||||
}
|
||||
|
||||
recentTasks := make([]RecentTaskDTO, len(data.RecentTasks))
|
||||
for i, t := range data.RecentTasks {
|
||||
recentTasks[i] = RecentTaskDTO{
|
||||
ID: t.ID,
|
||||
Identifier: t.Identifier,
|
||||
Title: t.Title,
|
||||
Status: dbStateToOfficeStatus(t.Status),
|
||||
AssigneeAgentProfileID: t.AssigneeAgentProfileID,
|
||||
UpdatedAt: t.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, DashboardResponse{
|
||||
AgentCount: data.AgentCount,
|
||||
RunningCount: data.RunningCount,
|
||||
PausedCount: data.PausedCount,
|
||||
ErrorCount: data.ErrorCount,
|
||||
MonthSpendSubcents: data.MonthSpendSubcents,
|
||||
PendingApprovals: data.PendingApprovals,
|
||||
RecentActivity: data.RecentActivity,
|
||||
TaskCount: data.TaskCount,
|
||||
SkillCount: data.SkillCount,
|
||||
RoutineCount: data.RoutineCount,
|
||||
RunActivity: runActivity,
|
||||
TaskBreakdown: TaskBreakdown{
|
||||
Open: data.TaskBreakdown.Open,
|
||||
InProgress: data.TaskBreakdown.InProgress,
|
||||
Blocked: data.TaskBreakdown.Blocked,
|
||||
Done: data.TaskBreakdown.Done,
|
||||
},
|
||||
RecentTasks: recentTasks,
|
||||
AgentSummaries: summaries,
|
||||
})
|
||||
c.JSON(http.StatusOK, NewDashboardResponse(data, summaries))
|
||||
}
|
||||
|
||||
// -- Live runs --
|
||||
|
||||
@@ -104,7 +104,13 @@ func (h *Handler) gitStatus(c *gin.Context) {
|
||||
// -- Meta --
|
||||
|
||||
func (h *Handler) getMeta(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, MetaResponse{
|
||||
c.JSON(http.StatusOK, BuildMetaResponse())
|
||||
}
|
||||
|
||||
// BuildMetaResponse returns the static Office metadata payload used by both
|
||||
// the HTTP endpoint and the SPA boot payload.
|
||||
func BuildMetaResponse() MetaResponse {
|
||||
return MetaResponse{
|
||||
Statuses: models.AllStatuses(),
|
||||
Priorities: models.AllPriorities(),
|
||||
Roles: models.AllRoles(),
|
||||
@@ -116,7 +122,7 @@ func (h *Handler) getMeta(c *gin.Context) {
|
||||
InboxItemTypes: models.AllInboxItemTypes(),
|
||||
Permissions: allPermissionMeta(),
|
||||
PermissionDefaults: allPermissionDefaults(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func allPermissionMeta() []PermissionMeta {
|
||||
|
||||
@@ -285,6 +285,15 @@ func seedReviewSession(t *testing.T, repo *sqliterepo.Repository, now time.Time)
|
||||
// outside the LaunchAgent call; mirroring that timing here lets the resume
|
||||
// path complete in unit tests without spawning a real subprocess.
|
||||
func wireBootReadySimulator(svc *Service, agentMgr *mockAgentManager, newExecID string) {
|
||||
promptReady := make(chan struct{})
|
||||
agentMgr.isAgentReadyFn = func(_ context.Context, _ string) bool {
|
||||
select {
|
||||
case <-promptReady:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
agentMgr.launchAgentFunc = func(_ context.Context, req *executor.LaunchAgentRequest) (*executor.LaunchAgentResponse, error) {
|
||||
// Simulate the lifecycle manager's persistExecutorRunning: in production
|
||||
// the row is upserted in lockstep with executionStore.Add; here we mirror
|
||||
@@ -308,6 +317,7 @@ func wireBootReadySimulator(svc *Service, agentMgr *mockAgentManager, newExecID
|
||||
AgentExecutionID: newExecID,
|
||||
AgentProfileID: req.AgentProfileID,
|
||||
})
|
||||
close(promptReady)
|
||||
}()
|
||||
return &executor.LaunchAgentResponse{
|
||||
AgentExecutionID: newExecID,
|
||||
|
||||
@@ -150,6 +150,7 @@ type mockAgentManager struct {
|
||||
// IsAgentRunningForSession. Lets tests model state changes mid-sequence
|
||||
// (e.g. stream disconnect between PromptAgent call and queue write).
|
||||
isAgentRunningFn func(context.Context, string) bool
|
||||
isAgentReadyFn func(context.Context, string) bool
|
||||
resolveProfileErr error
|
||||
restartProcessCalls []string // tracks execution IDs passed to RestartAgentProcess
|
||||
restartProcessErr error
|
||||
@@ -305,6 +306,12 @@ func (m *mockAgentManager) IsAgentRunningForSession(ctx context.Context, session
|
||||
}
|
||||
return m.isAgentRunning
|
||||
}
|
||||
func (m *mockAgentManager) IsAgentReadyForPrompt(ctx context.Context, sessionID string) bool {
|
||||
if m.isAgentReadyFn != nil {
|
||||
return m.isAgentReadyFn(ctx, sessionID)
|
||||
}
|
||||
return m.IsAgentRunningForSession(ctx, sessionID)
|
||||
}
|
||||
func (m *mockAgentManager) ResolveAgentProfile(_ context.Context, _ string) (*executor.AgentProfileInfo, error) {
|
||||
if m.resolveProfileErr != nil {
|
||||
return nil, m.resolveProfileErr
|
||||
|
||||
@@ -113,6 +113,11 @@ type AgentManagerClient interface {
|
||||
// This probes the actual agent (Docker container or standalone process) rather than relying on cached state
|
||||
IsAgentRunningForSession(ctx context.Context, sessionID string) bool
|
||||
|
||||
// IsAgentReadyForPrompt checks if the session can accept an immediate prompt.
|
||||
// This is stricter than IsAgentRunningForSession because ACP sessions can be
|
||||
// running before session initialization and stream setup have completed.
|
||||
IsAgentReadyForPrompt(ctx context.Context, sessionID string) bool
|
||||
|
||||
// ResolveAgentProfile resolves an agent profile ID to profile information
|
||||
ResolveAgentProfile(ctx context.Context, profileID string) (*AgentProfileInfo, error)
|
||||
|
||||
|
||||
@@ -128,6 +128,10 @@ func (m *mockAgentManager) IsAgentRunningForSession(ctx context.Context, session
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *mockAgentManager) IsAgentReadyForPrompt(ctx context.Context, sessionID string) bool {
|
||||
return m.IsAgentRunningForSession(ctx, sessionID)
|
||||
}
|
||||
|
||||
func (m *mockAgentManager) WasSessionInitialized(_ string) bool { return false }
|
||||
func (m *mockAgentManager) GetSessionAuthMethods(_ string) []streams.AuthMethodInfo {
|
||||
return nil
|
||||
|
||||
@@ -172,6 +172,9 @@ func TestEnsureSessionRunning_IdleSessionTriggersResume(t *testing.T) {
|
||||
agentMgr := &mockAgentManager{
|
||||
isAgentRunning: false,
|
||||
repoForExecutionLookup: repo,
|
||||
isAgentReadyFn: func(_ context.Context, _ string) bool {
|
||||
return true
|
||||
},
|
||||
launchAgentFunc: func(_ context.Context, req *executor.LaunchAgentRequest) (*executor.LaunchAgentResponse, error) {
|
||||
select {
|
||||
case launchCalled <- struct{}{}:
|
||||
@@ -230,3 +233,170 @@ func TestEnsureSessionRunning_IdleSessionTriggersResume(t *testing.T) {
|
||||
t.Fatal("expected ResumeSession to call LaunchAgent on IDLE session, but it never fired")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureSessionRunning_WaitsForPromptReadyAfterResume(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := setupTestRepo(t)
|
||||
|
||||
seedTaskAndSession(t, repo, "task1", "session1", models.TaskSessionStateIdle)
|
||||
session, err := repo.GetTaskSession(ctx, "session1")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load session: %v", err)
|
||||
}
|
||||
session.AgentExecutionID = "exec-idle-1"
|
||||
session.AgentProfileID = "profile1"
|
||||
if err := repo.UpdateTaskSession(ctx, session); err != nil {
|
||||
t.Fatalf("failed to update session: %v", err)
|
||||
}
|
||||
seedExecutorRunning(t, repo, session.ID, session.TaskID, "exec-idle-1")
|
||||
|
||||
ready := make(chan struct{})
|
||||
checked := make(chan struct{}, 1)
|
||||
agentMgr := &mockAgentManager{
|
||||
isAgentRunning: false,
|
||||
repoForExecutionLookup: repo,
|
||||
isAgentReadyFn: func(_ context.Context, _ string) bool {
|
||||
select {
|
||||
case checked <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-ready:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
},
|
||||
launchAgentFunc: func(_ context.Context, req *executor.LaunchAgentRequest) (*executor.LaunchAgentResponse, error) {
|
||||
go func(sessID string) {
|
||||
tick := time.NewTicker(5 * time.Millisecond)
|
||||
defer tick.Stop()
|
||||
timeout := time.After(5 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
sess, err := repo.GetTaskSession(context.Background(), sessID)
|
||||
if err == nil && sess != nil && sess.State == models.TaskSessionStateStarting {
|
||||
sess.State = models.TaskSessionStateWaitingForInput
|
||||
sess.UpdatedAt = time.Now().UTC()
|
||||
_ = repo.UpdateTaskSession(context.Background(), sess)
|
||||
return
|
||||
}
|
||||
case <-timeout:
|
||||
return
|
||||
}
|
||||
}
|
||||
}(req.SessionID)
|
||||
return &executor.LaunchAgentResponse{AgentExecutionID: "exec-resumed-1"}, nil
|
||||
},
|
||||
}
|
||||
|
||||
taskRepo := newMockTaskRepo()
|
||||
taskRepo.tasks["task1"] = &v1.Task{
|
||||
ID: "task1",
|
||||
Title: "Test Task",
|
||||
State: v1.TaskStateInProgress,
|
||||
}
|
||||
svc := createTestServiceWithAgent(repo, newMockStepGetter(), taskRepo, agentMgr)
|
||||
svc.executor = executor.NewExecutor(agentMgr, repo, testLogger(), executor.ExecutorConfig{})
|
||||
|
||||
session, err = repo.GetTaskSession(ctx, "session1")
|
||||
if err != nil {
|
||||
t.Fatalf("reload: %v", err)
|
||||
}
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- svc.ensureSessionRunning(ctx, "session1", session)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-checked:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("expected ensureSessionRunning to check prompt readiness after resume")
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
t.Fatalf("ensureSessionRunning returned before prompt readiness: %v", err)
|
||||
default:
|
||||
}
|
||||
|
||||
close(ready)
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("ensureSessionRunning failed after prompt readiness: %v", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("ensureSessionRunning did not return after prompt readiness")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureSessionRunning_WaitsForPromptReadyWhenExecutionExists(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := setupTestRepo(t)
|
||||
|
||||
seedTaskAndSession(t, repo, "task1", "session1", models.TaskSessionStateWaitingForInput)
|
||||
session, err := repo.GetTaskSession(ctx, "session1")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load session: %v", err)
|
||||
}
|
||||
session.AgentExecutionID = "exec-ready-race-1"
|
||||
session.AgentProfileID = "profile1"
|
||||
if err := repo.UpdateTaskSession(ctx, session); err != nil {
|
||||
t.Fatalf("failed to update session: %v", err)
|
||||
}
|
||||
seedExecutorRunning(t, repo, session.ID, session.TaskID, "exec-ready-race-1")
|
||||
|
||||
ready := make(chan struct{})
|
||||
checked := make(chan struct{}, 1)
|
||||
agentMgr := &mockAgentManager{
|
||||
isAgentRunning: true,
|
||||
repoForExecutionLookup: repo,
|
||||
isAgentReadyFn: func(_ context.Context, _ string) bool {
|
||||
select {
|
||||
case checked <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-ready:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
svc := createTestServiceWithAgent(repo, newMockStepGetter(), newMockTaskRepo(), agentMgr)
|
||||
svc.executor = executor.NewExecutor(agentMgr, repo, testLogger(), executor.ExecutorConfig{})
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- svc.ensureSessionRunning(ctx, "session1", session)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-checked:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("expected ensureSessionRunning to check prompt readiness")
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
t.Fatalf("ensureSessionRunning returned before prompt readiness: %v", err)
|
||||
default:
|
||||
}
|
||||
|
||||
close(ready)
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("ensureSessionRunning failed after prompt readiness: %v", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("ensureSessionRunning did not return after prompt readiness")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +99,10 @@ func (m *mockAgentManager) IsAgentRunningForSession(ctx context.Context, session
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *mockAgentManager) IsAgentReadyForPrompt(ctx context.Context, sessionID string) bool {
|
||||
return m.IsAgentRunningForSession(ctx, sessionID)
|
||||
}
|
||||
|
||||
func (m *mockAgentManager) WasSessionInitialized(_ string) bool { return false }
|
||||
func (m *mockAgentManager) GetSessionAuthMethods(_ string) []streams.AuthMethodInfo {
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package orchestrator
|
||||
|
||||
import "context"
|
||||
|
||||
func (m *sessionUpdatingAgentManager) IsAgentReadyForPrompt(_ context.Context, _ string) bool {
|
||||
return m.onStartCalled != nil && *m.onStartCalled
|
||||
}
|
||||
@@ -47,6 +47,11 @@ var ErrSessionResetInProgress = errors.New("session reset in progress")
|
||||
// the two misleads the UI and any caller doing errors.Is checks.
|
||||
var ErrSessionNotPromptable = errors.New("session not promptable")
|
||||
|
||||
const (
|
||||
agentPromptReadyTimeout = 10 * time.Second
|
||||
agentPromptReadyInterval = 100 * time.Millisecond
|
||||
)
|
||||
|
||||
func isAgentPromptInProgressError(err error) bool {
|
||||
return err != nil && errors.Is(err, ErrAgentPromptInProgress)
|
||||
}
|
||||
@@ -298,6 +303,7 @@ func (s *Service) StartCreatedSession(ctx context.Context, taskID, sessionID, ag
|
||||
"model": profileInfo.Model,
|
||||
}
|
||||
}
|
||||
s.promoteSessionIfTaskHasNoPrimary(ctx, taskID, session)
|
||||
if err := s.repo.UpdateTaskSession(ctx, session); err != nil {
|
||||
s.logger.Warn("failed to update session agent profile",
|
||||
zap.String("session_id", sessionID),
|
||||
@@ -400,6 +406,33 @@ func (s *Service) StartCreatedSession(ctx context.Context, taskID, sessionID, ag
|
||||
return execution, nil
|
||||
}
|
||||
|
||||
func (s *Service) promoteSessionIfTaskHasNoPrimary(ctx context.Context, taskID string, session *models.TaskSession) {
|
||||
if session == nil || session.IsPrimary {
|
||||
return
|
||||
}
|
||||
sessions, err := s.repo.ListTaskSessions(ctx, taskID)
|
||||
if err != nil {
|
||||
s.logger.Warn("failed to inspect task sessions for missing primary",
|
||||
zap.String("task_id", taskID),
|
||||
zap.String("session_id", session.ID),
|
||||
zap.Error(err))
|
||||
return
|
||||
}
|
||||
for _, existing := range sessions {
|
||||
if existing.IsPrimary {
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := s.SetPrimarySession(ctx, session.ID); err != nil {
|
||||
s.logger.Warn("failed to promote workflow session as primary",
|
||||
zap.String("task_id", taskID),
|
||||
zap.String("session_id", session.ID),
|
||||
zap.Error(err))
|
||||
return
|
||||
}
|
||||
session.IsPrimary = true
|
||||
}
|
||||
|
||||
// postLaunchCreated handles post-launch bookkeeping for a created session:
|
||||
// records the initial user message (unless skipped) and sets plan mode metadata.
|
||||
// It does NOT modify session state — the executor sets STARTING, and event handlers
|
||||
@@ -975,7 +1008,11 @@ func (s *Service) ResumeTaskSession(ctx context.Context, taskID, sessionID strin
|
||||
// If the execution is already running (duplicate resume request), return it as success.
|
||||
if errors.Is(err, executor.ErrExecutionAlreadyRunning) {
|
||||
if existing, ok := s.executor.GetExecutionBySession(sessionID); ok && existing != nil {
|
||||
existing.SessionState = v1.TaskSessionState(session.State)
|
||||
readySession, waitErr := s.waitForResumedSessionReady(ctx, sessionID)
|
||||
if waitErr != nil {
|
||||
return nil, waitErr
|
||||
}
|
||||
existing.SessionState = v1.TaskSessionState(readySession.State)
|
||||
return existing, nil
|
||||
}
|
||||
}
|
||||
@@ -1003,8 +1040,11 @@ func (s *Service) ResumeTaskSession(ctx context.Context, taskID, sessionID strin
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
// Preserve persisted task/session state; resume should not mutate state/columns.
|
||||
execution.SessionState = v1.TaskSessionState(session.State)
|
||||
readySession, err := s.waitForResumedSessionReady(ctx, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
execution.SessionState = v1.TaskSessionState(readySession.State)
|
||||
|
||||
// Backfill the initial user message when a prior failed launch never got
|
||||
// to recordInitialMessage. Without this, the resume can succeed and the
|
||||
@@ -1035,6 +1075,38 @@ func (s *Service) ResumeTaskSession(ctx context.Context, taskID, sessionID strin
|
||||
return execution, nil
|
||||
}
|
||||
|
||||
func (s *Service) waitForResumedSessionReady(ctx context.Context, sessionID string) (*models.TaskSession, error) {
|
||||
return s.waitForSessionAndAgentReady(ctx, sessionID, "after resume")
|
||||
}
|
||||
|
||||
func (s *Service) waitForSessionAndAgentReady(ctx context.Context, sessionID, waitContext string) (*models.TaskSession, error) {
|
||||
if err := s.waitForSessionReady(ctx, sessionID); err != nil {
|
||||
return nil, fmt.Errorf("session not ready %s: %w", waitContext, err)
|
||||
}
|
||||
if err := s.waitForAgentPromptReady(ctx, sessionID); err != nil {
|
||||
return nil, fmt.Errorf("agent not ready %s: %w", waitContext, err)
|
||||
}
|
||||
session, err := s.repo.GetTaskSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to reload session %s: %w", waitContext, err)
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (s *Service) waitForStartingSessionPromptable(ctx context.Context, taskID, sessionID string) (*models.TaskSession, error) {
|
||||
s.logger.Debug("waiting for starting session to become promptable",
|
||||
zap.String("task_id", taskID),
|
||||
zap.String("session_id", sessionID))
|
||||
session, err := s.waitForSessionAndAgentReady(ctx, sessionID, "for prompt")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.checkSessionPromptable(taskID, sessionID, session.State); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// StartSessionForWorkflowStep starts an existing session with a workflow step's prompt configuration.
|
||||
// If the session is not running, it will be resumed first. Then a prompt is sent using the
|
||||
// step's prompt_prefix, prompt_suffix, and plan_mode settings combined with the task description.
|
||||
@@ -1125,6 +1197,9 @@ func (s *Service) advanceTaskWorkflowStep(ctx context.Context, task *models.Task
|
||||
func (s *Service) ensureSessionRunning(ctx context.Context, sessionID string, session *models.TaskSession) error {
|
||||
// Check if agent is genuinely running (in-memory execution store, not just DB state)
|
||||
if exec, ok := s.executor.GetExecutionBySession(sessionID); ok && exec != nil {
|
||||
if err := s.waitForAgentPromptReady(ctx, sessionID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1177,11 +1252,38 @@ func (s *Service) ensureSessionRunning(ctx context.Context, sessionID string, se
|
||||
if err := s.waitForSessionReady(ctx, sessionID); err != nil {
|
||||
return fmt.Errorf("session not ready after resume: %w", err)
|
||||
}
|
||||
if err := s.waitForAgentPromptReady(ctx, sessionID); err != nil {
|
||||
return fmt.Errorf("agent not ready after resume: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Debug("session resumed and ready for prompt")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) waitForAgentPromptReady(ctx context.Context, sessionID string) error {
|
||||
if s.agentManager == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
readyCtx, cancel := context.WithTimeout(ctx, agentPromptReadyTimeout)
|
||||
defer cancel()
|
||||
|
||||
ticker := time.NewTicker(agentPromptReadyInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
if s.agentManager.IsAgentReadyForPrompt(readyCtx, sessionID) {
|
||||
return nil
|
||||
}
|
||||
|
||||
select {
|
||||
case <-readyCtx.Done():
|
||||
return fmt.Errorf("agent not ready for prompt: %w", readyCtx.Err())
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// startAgentOnPreparedWorkspace starts the agent subprocess on a session whose workspace
|
||||
// was prepared (agentctl running) but whose agent process was never started. This avoids
|
||||
// the "session already has an agent running" error from ResumeSession which tries a full
|
||||
@@ -1210,6 +1312,9 @@ func (s *Service) startAgentOnPreparedWorkspace(ctx context.Context, sessionID s
|
||||
if err := s.waitForSessionReady(ctx, sessionID); err != nil {
|
||||
return fmt.Errorf("session not ready after starting agent: %w", err)
|
||||
}
|
||||
if err := s.waitForAgentPromptReady(ctx, sessionID); err != nil {
|
||||
return fmt.Errorf("agent not ready after starting agent: %w", err)
|
||||
}
|
||||
s.logger.Debug("agent started on prepared workspace and ready for prompt")
|
||||
return nil
|
||||
}
|
||||
@@ -1967,7 +2072,14 @@ func (s *Service) PromptTask(ctx context.Context, taskID, sessionID string, prom
|
||||
return nil, fmt.Errorf("failed to get session: %w", err)
|
||||
}
|
||||
if err := s.checkSessionPromptable(taskID, sessionID, session.State); err != nil {
|
||||
return nil, err
|
||||
if !errors.Is(err, ErrSessionNotPromptable) || session.State != models.TaskSessionStateStarting {
|
||||
return nil, err
|
||||
}
|
||||
readySession, waitErr := s.waitForStartingSessionPromptable(ctx, taskID, sessionID)
|
||||
if waitErr != nil {
|
||||
return nil, waitErr
|
||||
}
|
||||
session = readySession
|
||||
}
|
||||
|
||||
// Inject config context for config-mode sessions (dedicated settings chat, not plan mode)
|
||||
|
||||
@@ -321,3 +321,111 @@ func TestGetTaskSessionStatus_NoAutoResumeWithResumeTokenOnError(t *testing.T) {
|
||||
t.Fatalf("expected ResumeReason=%q, got %q", resumeReasonErrorRecovery, resp.ResumeReason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResumeTaskSession_WaitsForPromptReady(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := setupTestRepo(t)
|
||||
seedTaskAndSession(t, repo, "task1", "session1", models.TaskSessionStateWaitingForInput)
|
||||
|
||||
session, err := repo.GetTaskSession(ctx, "session1")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load session: %v", err)
|
||||
}
|
||||
session.AgentExecutionID = "exec-old-1"
|
||||
session.AgentProfileID = "profile1"
|
||||
if err := repo.UpdateTaskSession(ctx, session); err != nil {
|
||||
t.Fatalf("failed to update session: %v", err)
|
||||
}
|
||||
seedExecutorRunning(t, repo, session.ID, session.TaskID, "exec-old-1")
|
||||
|
||||
ready := make(chan struct{})
|
||||
checked := make(chan struct{}, 1)
|
||||
agentMgr := &mockAgentManager{
|
||||
isAgentRunning: false,
|
||||
repoForExecutionLookup: repo,
|
||||
isAgentReadyFn: func(_ context.Context, _ string) bool {
|
||||
select {
|
||||
case checked <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-ready:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
},
|
||||
launchAgentFunc: func(_ context.Context, req *executor.LaunchAgentRequest) (*executor.LaunchAgentResponse, error) {
|
||||
go func(sessID string) {
|
||||
tick := time.NewTicker(5 * time.Millisecond)
|
||||
defer tick.Stop()
|
||||
timeout := time.After(5 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
sess, err := repo.GetTaskSession(context.Background(), sessID)
|
||||
if err == nil && sess != nil && sess.State == models.TaskSessionStateStarting {
|
||||
sess.State = models.TaskSessionStateWaitingForInput
|
||||
sess.UpdatedAt = time.Now().UTC()
|
||||
_ = repo.UpdateTaskSession(context.Background(), sess)
|
||||
return
|
||||
}
|
||||
case <-timeout:
|
||||
return
|
||||
}
|
||||
}
|
||||
}(req.SessionID)
|
||||
return &executor.LaunchAgentResponse{AgentExecutionID: "exec-resumed-1"}, nil
|
||||
},
|
||||
}
|
||||
|
||||
taskRepo := newMockTaskRepo()
|
||||
taskRepo.tasks["task1"] = &v1.Task{
|
||||
ID: "task1",
|
||||
Title: "Test Task",
|
||||
State: v1.TaskStateInProgress,
|
||||
}
|
||||
svc := createTestServiceWithAgent(repo, newMockStepGetter(), taskRepo, agentMgr)
|
||||
svc.executor = executor.NewExecutor(agentMgr, repo, testLogger(), executor.ExecutorConfig{})
|
||||
|
||||
done := make(chan struct {
|
||||
exec *executor.TaskExecution
|
||||
err error
|
||||
}, 1)
|
||||
go func() {
|
||||
exec, err := svc.ResumeTaskSession(ctx, "task1", "session1")
|
||||
done <- struct {
|
||||
exec *executor.TaskExecution
|
||||
err error
|
||||
}{exec: exec, err: err}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-checked:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("expected ResumeTaskSession to check prompt readiness")
|
||||
}
|
||||
|
||||
select {
|
||||
case result := <-done:
|
||||
t.Fatalf("ResumeTaskSession returned before prompt readiness: %v", result.err)
|
||||
default:
|
||||
}
|
||||
|
||||
close(ready)
|
||||
|
||||
select {
|
||||
case result := <-done:
|
||||
if result.err != nil {
|
||||
t.Fatalf("ResumeTaskSession failed after prompt readiness: %v", result.err)
|
||||
}
|
||||
if result.exec == nil {
|
||||
t.Fatal("ResumeTaskSession returned nil execution")
|
||||
}
|
||||
if result.exec.SessionState != v1.TaskSessionStateWaitingForInput {
|
||||
t.Fatalf("expected WAITING_FOR_INPUT response state, got %s", result.exec.SessionState)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("ResumeTaskSession did not return after prompt readiness")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"github.com/kandev/kandev/internal/orchestrator/scheduler"
|
||||
"github.com/kandev/kandev/internal/sysprompt"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
wfmodels "github.com/kandev/kandev/internal/workflow/models"
|
||||
v1 "github.com/kandev/kandev/pkg/api/v1"
|
||||
)
|
||||
|
||||
@@ -83,6 +84,116 @@ func TestPromptTask_SessionAlreadyRunning(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptTask_WaitsForStartingSessionBeforePrompt(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := setupTestRepo(t)
|
||||
seedTaskAndSession(t, repo, "task1", "session1", models.TaskSessionStateStarting)
|
||||
|
||||
session, err := repo.GetTaskSession(ctx, "session1")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load session: %v", err)
|
||||
}
|
||||
session.AgentExecutionID = "exec-resumed-1"
|
||||
session.AgentProfileID = "profile1"
|
||||
if err := repo.UpdateTaskSession(ctx, session); err != nil {
|
||||
t.Fatalf("failed to update session: %v", err)
|
||||
}
|
||||
seedExecutorRunning(t, repo, session.ID, session.TaskID, "exec-resumed-1")
|
||||
|
||||
promptReady := make(chan struct{})
|
||||
readinessChecked := make(chan struct{}, 1)
|
||||
agentMgr := &mockAgentManager{
|
||||
isAgentRunning: true,
|
||||
repoForExecutionLookup: repo,
|
||||
promptResult: &executor.PromptResult{
|
||||
StopReason: "end_turn",
|
||||
AgentMessage: "simple mock response",
|
||||
},
|
||||
isAgentReadyFn: func(_ context.Context, _ string) bool {
|
||||
select {
|
||||
case readinessChecked <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-promptReady:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
},
|
||||
}
|
||||
taskRepo := newMockTaskRepo()
|
||||
taskRepo.tasks["task1"] = &v1.Task{ID: "task1", State: v1.TaskStateInProgress}
|
||||
svc := createTestServiceWithAgent(repo, newMockStepGetter(), taskRepo, agentMgr)
|
||||
svc.executor = executor.NewExecutor(agentMgr, repo, testLogger(), executor.ExecutorConfig{})
|
||||
|
||||
done := make(chan struct {
|
||||
result *PromptResult
|
||||
err error
|
||||
}, 1)
|
||||
go func() {
|
||||
result, err := svc.PromptTask(ctx, "task1", "session1", "/e2e:simple-message", "", false, nil, false)
|
||||
done <- struct {
|
||||
result *PromptResult
|
||||
err error
|
||||
}{result: result, err: err}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
readySession, err := repo.GetTaskSession(context.Background(), "session1")
|
||||
if err != nil || readySession == nil {
|
||||
return
|
||||
}
|
||||
readySession.State = models.TaskSessionStateWaitingForInput
|
||||
readySession.UpdatedAt = time.Now().UTC()
|
||||
_ = repo.UpdateTaskSession(context.Background(), readySession)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-readinessChecked:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("expected PromptTask to wait for agent prompt readiness")
|
||||
}
|
||||
|
||||
select {
|
||||
case result := <-done:
|
||||
t.Fatalf("PromptTask returned before prompt readiness: result=%#v err=%v", result.result, result.err)
|
||||
default:
|
||||
}
|
||||
|
||||
close(promptReady)
|
||||
|
||||
select {
|
||||
case result := <-done:
|
||||
if result.err != nil {
|
||||
t.Fatalf("PromptTask failed after prompt readiness: %v", result.err)
|
||||
}
|
||||
if result.result == nil {
|
||||
t.Fatal("PromptTask returned nil result")
|
||||
}
|
||||
if result.result.AgentMessage != "simple mock response" {
|
||||
t.Fatalf("unexpected agent message: %q", result.result.AgentMessage)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("PromptTask did not return after prompt readiness")
|
||||
}
|
||||
|
||||
agentMgr.mu.Lock()
|
||||
prompts := append([]string(nil), agentMgr.capturedPrompts...)
|
||||
calls := append([]promptCall(nil), agentMgr.capturedPromptCalls...)
|
||||
agentMgr.mu.Unlock()
|
||||
if len(prompts) != 1 {
|
||||
t.Fatalf("expected one prompt after readiness, got %d", len(prompts))
|
||||
}
|
||||
if prompts[0] != "/e2e:simple-message" {
|
||||
t.Fatalf("unexpected prompt: %q", prompts[0])
|
||||
}
|
||||
if len(calls) != 1 || calls[0].ExecutionID != "exec-resumed-1" {
|
||||
t.Fatalf("unexpected prompt calls: %#v", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrySwitchModelUpdatesRuntimeModelCache(t *testing.T) {
|
||||
repo := setupTestRepo(t)
|
||||
agentMgr := &mockAgentManager{
|
||||
@@ -552,6 +663,97 @@ func TestStartCreatedSession_NotInCreatedState(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartCreatedSession_WorkflowOverridePromotesPreparedWhenTaskHasNoPrimary(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := setupTestRepo(t)
|
||||
now := time.Now().UTC()
|
||||
|
||||
if err := repo.CreateWorkspace(ctx, &models.Workspace{ID: "ws1", Name: "Test", CreatedAt: now, UpdatedAt: now}); err != nil {
|
||||
t.Fatalf("create workspace: %v", err)
|
||||
}
|
||||
if err := repo.CreateWorkflow(ctx, &models.Workflow{ID: "wf1", WorkspaceID: "ws1", Name: "Workflow", CreatedAt: now, UpdatedAt: now}); err != nil {
|
||||
t.Fatalf("create workflow: %v", err)
|
||||
}
|
||||
if err := repo.CreateTask(ctx, &models.Task{
|
||||
ID: "task1",
|
||||
WorkspaceID: "ws1",
|
||||
WorkflowID: "wf1",
|
||||
WorkflowStepID: "step1",
|
||||
Title: "Task",
|
||||
Description: "desc",
|
||||
State: v1.TaskStateInProgress,
|
||||
Metadata: map[string]interface{}{models.MetaKeyAgentProfileID: "profile-a"},
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}); err != nil {
|
||||
t.Fatalf("create task: %v", err)
|
||||
}
|
||||
|
||||
stepGetter := newMockStepGetter()
|
||||
stepGetter.steps["step1"] = &wfmodels.WorkflowStep{
|
||||
ID: "step1",
|
||||
WorkflowID: "wf1",
|
||||
Name: "Step 1",
|
||||
AgentProfileID: "profile-b",
|
||||
}
|
||||
taskRepo := newMockTaskRepo()
|
||||
taskRepo.tasks["task1"] = &v1.Task{
|
||||
ID: "task1",
|
||||
WorkspaceID: "ws1",
|
||||
WorkflowID: "wf1",
|
||||
Title: "Task",
|
||||
Description: "desc",
|
||||
State: v1.TaskStateInProgress,
|
||||
Metadata: map[string]interface{}{models.MetaKeyAgentProfileID: "profile-a"},
|
||||
}
|
||||
|
||||
var launchedProfile string
|
||||
agentMgr := &mockAgentManager{
|
||||
launchAgentFunc: func(_ context.Context, req *executor.LaunchAgentRequest) (*executor.LaunchAgentResponse, error) {
|
||||
launchedProfile = req.AgentProfileID
|
||||
return &executor.LaunchAgentResponse{AgentExecutionID: "exec-1"}, nil
|
||||
},
|
||||
}
|
||||
svc := createTestServiceWithScheduler(repo, stepGetter, taskRepo, agentMgr)
|
||||
|
||||
sessionID, err := svc.PrepareTaskSession(ctx, "task1", "profile-a", "", "", "step1", false)
|
||||
if err != nil {
|
||||
t.Fatalf("PrepareTaskSession: %v", err)
|
||||
}
|
||||
prepared, err := repo.GetTaskSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskSession after prepare: %v", err)
|
||||
}
|
||||
if !prepared.IsPrimary {
|
||||
t.Fatal("prepared first session should start as primary")
|
||||
}
|
||||
prepared.IsPrimary = false
|
||||
if err := repo.UpdateTaskSession(ctx, prepared); err != nil {
|
||||
t.Fatalf("clear prepared primary flag: %v", err)
|
||||
}
|
||||
|
||||
if _, err := svc.StartCreatedSession(ctx, "task1", sessionID, "profile-a", "desc", true, false, true, nil); err != nil {
|
||||
t.Fatalf("StartCreatedSession: %v", err)
|
||||
}
|
||||
|
||||
updated, err := repo.GetTaskSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskSession after start: %v", err)
|
||||
}
|
||||
if updated.AgentProfileID != "profile-b" {
|
||||
t.Fatalf("agent profile = %q, want profile-b", updated.AgentProfileID)
|
||||
}
|
||||
if !updated.IsPrimary {
|
||||
t.Fatal("workflow profile override must promote prepared session when task has no primary")
|
||||
}
|
||||
if got := updated.Metadata[models.SessionMetaKeyCreatedBy]; got != models.SessionCreatedByWorkflowSwitch {
|
||||
t.Fatalf("created_by metadata = %v, want %q", got, models.SessionCreatedByWorkflowSwitch)
|
||||
}
|
||||
if launchedProfile != "profile-b" {
|
||||
t.Fatalf("launched profile = %q, want profile-b", launchedProfile)
|
||||
}
|
||||
}
|
||||
|
||||
// --- recordInitialMessage ---
|
||||
|
||||
// mockMessageCreator implements MessageCreator for testing.
|
||||
@@ -1125,13 +1327,20 @@ func TestResumeTaskSession_FailedKeepsResumeToken(t *testing.T) {
|
||||
|
||||
// Agent launch succeeds so the resume path does not unwind and mark the task
|
||||
// FAILED, which would exercise a separate state-mutation code path.
|
||||
agentMgr := &mockAgentManager{
|
||||
launchAgentFunc: func(_ context.Context, _ *executor.LaunchAgentRequest) (*executor.LaunchAgentResponse, error) {
|
||||
return &executor.LaunchAgentResponse{
|
||||
AgentExecutionID: "exec-new",
|
||||
Status: v1.AgentStatusStarting,
|
||||
}, nil
|
||||
startAgentProcessCalled := false
|
||||
agentMgr := &sessionUpdatingAgentManager{
|
||||
mockAgentManager: &mockAgentManager{
|
||||
launchAgentFunc: func(_ context.Context, _ *executor.LaunchAgentRequest) (*executor.LaunchAgentResponse, error) {
|
||||
return &executor.LaunchAgentResponse{
|
||||
AgentExecutionID: "exec-new",
|
||||
Status: v1.AgentStatusStarting,
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
repo: repo,
|
||||
sessionID: "session1",
|
||||
taskID: "task1",
|
||||
onStartCalled: &startAgentProcessCalled,
|
||||
}
|
||||
svc := createTestServiceWithAgent(repo, newMockStepGetter(), taskRepo, agentMgr)
|
||||
svc.executor = executor.NewExecutor(agentMgr, repo, testLogger(), executor.ExecutorConfig{})
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"maps"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/agentruntime"
|
||||
@@ -137,6 +138,25 @@ func (e LastAgentError) Stamp() string {
|
||||
return e.OccurredAt.UTC().Format(time.RFC3339Nano) + ":" + e.Message
|
||||
}
|
||||
|
||||
func (e LastAgentError) MatchesStamp(stamp string) bool {
|
||||
if stamp == e.Stamp() {
|
||||
return true
|
||||
}
|
||||
suffix := ":" + e.Message
|
||||
if !strings.HasSuffix(stamp, suffix) {
|
||||
return false
|
||||
}
|
||||
rawOccurredAt := strings.TrimSuffix(stamp, suffix)
|
||||
if rawOccurredAt == "" {
|
||||
return e.OccurredAt.IsZero()
|
||||
}
|
||||
occurredAt, err := time.Parse(time.RFC3339Nano, rawOccurredAt)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return occurredAt.Equal(e.OccurredAt)
|
||||
}
|
||||
|
||||
func (e LastAgentError) IsDismissed() bool {
|
||||
return e.DismissedAt != nil && !e.DismissedAt.IsZero()
|
||||
}
|
||||
|
||||
@@ -647,8 +647,11 @@ func (r *Repository) DismissLastAgentError(ctx context.Context, sessionID string
|
||||
updated_at = ?
|
||||
WHERE id = ?
|
||||
AND json_extract(metadata, '$.last_agent_error.message') = ?
|
||||
AND json_extract(metadata, '$.last_agent_error.occurred_at') = ?
|
||||
`), string(valueJSON), now, sessionID, expected.Message, expected.OccurredAt.UTC().Format(time.RFC3339Nano))
|
||||
AND (
|
||||
json_extract(metadata, '$.last_agent_error.occurred_at') = ?
|
||||
OR julianday(json_extract(metadata, '$.last_agent_error.occurred_at')) = julianday(?)
|
||||
)
|
||||
`), string(valueJSON), now, sessionID, expected.Message, expected.OccurredAt.UTC().Format(time.RFC3339Nano), expected.OccurredAt.UTC().Format(time.RFC3339Nano))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
@@ -551,6 +551,46 @@ func TestDismissLastAgentErrorDoesNotOverwriteNewerError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDismissLastAgentErrorMatchesEquivalentTimestampText(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
|
||||
seedForMsgTest(t, repo, "task-error", "sess-error", "turn-error")
|
||||
occurredAt, err := time.Parse(time.RFC3339Nano, "2026-06-14T12:00:00.310Z")
|
||||
if err != nil {
|
||||
t.Fatalf("parse occurred_at: %v", err)
|
||||
}
|
||||
lastErr := models.LastAgentError{
|
||||
Message: "peer disconnected before response",
|
||||
OccurredAt: occurredAt,
|
||||
}
|
||||
if err := repo.SetSessionMetadataKey(ctx, "sess-error", models.SessionMetaKeyLastAgentError, map[string]any{
|
||||
"message": lastErr.Message,
|
||||
"occurred_at": "2026-06-14T12:00:00.310Z",
|
||||
}); err != nil {
|
||||
t.Fatalf("seed last agent error: %v", err)
|
||||
}
|
||||
|
||||
updated, err := repo.DismissLastAgentError(ctx, "sess-error", lastErr, time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatalf("dismiss last agent error: %v", err)
|
||||
}
|
||||
if !updated {
|
||||
t.Fatalf("expected equivalent timestamp text to match")
|
||||
}
|
||||
session, err := repo.GetTaskSession(ctx, "sess-error")
|
||||
if err != nil {
|
||||
t.Fatalf("get session: %v", err)
|
||||
}
|
||||
got, ok := models.LoadLastAgentError(session.Metadata)
|
||||
if !ok {
|
||||
t.Fatalf("expected last agent error metadata")
|
||||
}
|
||||
if !got.IsDismissed() {
|
||||
t.Fatalf("last agent error = %#v, want dismissed", got)
|
||||
}
|
||||
}
|
||||
|
||||
func sessionCancellationMetadata(t *testing.T, repo *Repository, sessionID string) (string, sql.NullTime, time.Time) {
|
||||
t.Helper()
|
||||
var errorMessage string
|
||||
|
||||
@@ -72,7 +72,7 @@ func (s *Service) DismissLastAgentError(ctx context.Context, sessionID, stamp st
|
||||
if !ok {
|
||||
return session, nil
|
||||
}
|
||||
if stamp != "" && lastErr.Stamp() != stamp {
|
||||
if stamp != "" && !lastErr.MatchesStamp(stamp) {
|
||||
return session, nil
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
|
||||
@@ -691,6 +691,36 @@ func TestService_DismissLastAgentError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_DismissLastAgentErrorAcceptsBrowserTimestampStamp(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
ctx := context.Background()
|
||||
setupTestTask(t, repo)
|
||||
sessionID := setupTestSession(t, repo)
|
||||
occurredAt, err := time.Parse(time.RFC3339Nano, "2026-06-14T12:00:00.310Z")
|
||||
if err != nil {
|
||||
t.Fatalf("parse occurred_at: %v", err)
|
||||
}
|
||||
lastErr := models.LastAgentError{
|
||||
Message: "peer disconnected before response",
|
||||
OccurredAt: occurredAt,
|
||||
}
|
||||
if err := repo.SetSessionMetadataKey(ctx, sessionID, models.SessionMetaKeyLastAgentError, lastErr); err != nil {
|
||||
t.Fatalf("seed last agent error: %v", err)
|
||||
}
|
||||
|
||||
session, err := svc.DismissLastAgentError(ctx, sessionID, "2026-06-14T12:00:00.310Z:"+lastErr.Message)
|
||||
if err != nil {
|
||||
t.Fatalf("dismiss last agent error: %v", err)
|
||||
}
|
||||
got, ok := models.LoadLastAgentError(session.Metadata)
|
||||
if !ok {
|
||||
t.Fatalf("expected last agent error metadata after dismiss")
|
||||
}
|
||||
if !got.IsDismissed() {
|
||||
t.Fatalf("expected dismissed last agent error, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_DismissLastAgentErrorIgnoresStaleStamp(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DevHandler fronts a Vite dev server with the same Go boot-payload shell used
|
||||
// in production. Vite remains responsible for module transforms and HMR.
|
||||
type DevHandler struct {
|
||||
viteIndexURL string
|
||||
proxy *httputil.ReverseProxy
|
||||
client *http.Client
|
||||
payloadFor PayloadBuilder
|
||||
}
|
||||
|
||||
func NewDevHandler(viteBaseURL string, opts ...HandlerOption) (*DevHandler, error) {
|
||||
target, err := url.Parse(viteBaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse Vite dev server URL: %w", err)
|
||||
}
|
||||
if target.Scheme == "" || target.Host == "" {
|
||||
return nil, fmt.Errorf("parse Vite dev server URL: missing scheme or host")
|
||||
}
|
||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
||||
proxy.FlushInterval = -1
|
||||
proxy.ErrorHandler = func(w http.ResponseWriter, _ *http.Request, _ error) {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
}
|
||||
|
||||
config := newHandlerDefaults(nil)
|
||||
for _, opt := range opts {
|
||||
opt(config)
|
||||
}
|
||||
|
||||
indexURL := *target
|
||||
indexURL.Path = "/"
|
||||
indexURL.RawQuery = ""
|
||||
indexURL.Fragment = ""
|
||||
|
||||
return &DevHandler{
|
||||
viteIndexURL: indexURL.String(),
|
||||
proxy: proxy,
|
||||
client: http.DefaultClient,
|
||||
payloadFor: config.payloadFor,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *DevHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if h == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if shouldProxyToVite(r) {
|
||||
h.proxy.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
route := ClassifyRoute(r.URL.Path)
|
||||
if route.Kind != RouteKindSPA {
|
||||
if route.Kind == RouteKindStatic {
|
||||
h.proxy.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if !acceptsHTML(r) {
|
||||
h.proxy.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
indexHTML, err := h.fetchViteIndex(r)
|
||||
if err != nil {
|
||||
http.Error(w, "Vite dev server unavailable", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
payload := h.payloadFor(r, route)
|
||||
html, err := RenderShellHTML(indexHTML, payload)
|
||||
if err != nil {
|
||||
http.Error(w, "web app shell unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(html)
|
||||
}
|
||||
|
||||
func (h *DevHandler) fetchViteIndex(r *http.Request) ([]byte, error) {
|
||||
req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, h.viteIndexURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "text/html")
|
||||
|
||||
resp, err := h.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("vite index status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
func shouldProxyToVite(r *http.Request) bool {
|
||||
if isWebSocketUpgrade(r) {
|
||||
return true
|
||||
}
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
return true
|
||||
}
|
||||
return isViteDevPath(r.URL.Path)
|
||||
}
|
||||
|
||||
func acceptsHTML(r *http.Request) bool {
|
||||
accept := r.Header.Get("Accept")
|
||||
return accept == "" || strings.Contains(accept, "text/html")
|
||||
}
|
||||
|
||||
func isWebSocketUpgrade(r *http.Request) bool {
|
||||
return strings.EqualFold(r.Header.Get("Upgrade"), "websocket") &&
|
||||
strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade")
|
||||
}
|
||||
|
||||
func isViteDevPath(requestPath string) bool {
|
||||
switch {
|
||||
case strings.HasPrefix(requestPath, "/@vite/"),
|
||||
strings.HasPrefix(requestPath, "/@id/"),
|
||||
strings.HasPrefix(requestPath, "/@react-refresh"),
|
||||
strings.HasPrefix(requestPath, "/@fs/"),
|
||||
strings.HasPrefix(requestPath, "/__vite"),
|
||||
strings.HasPrefix(requestPath, "/src/"),
|
||||
strings.HasPrefix(requestPath, "/app/"),
|
||||
strings.HasPrefix(requestPath, "/components/"),
|
||||
strings.HasPrefix(requestPath, "/hooks/"),
|
||||
strings.HasPrefix(requestPath, "/lib/"),
|
||||
strings.HasPrefix(requestPath, "/generated/"),
|
||||
strings.HasPrefix(requestPath, "/node_modules/"):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDevHandlerInjectsBootPayloadIntoViteIndexForSPARoutes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
vite := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
t.Fatalf("vite received path %q, want /", r.URL.Path)
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
_, _ = w.Write([]byte(`<html><head></head><body><script type="module" src="/src/main.tsx"></script></body></html>`))
|
||||
}))
|
||||
t.Cleanup(vite.Close)
|
||||
|
||||
handler, err := NewDevHandler(vite.URL, WithPayloadBuilder(func(_ *http.Request, route RouteClassification) BootPayload {
|
||||
payload := NewBootPayload(route, RuntimeConfig{APIPrefix: "/api/v1", WebSocketPath: "/ws"}, nil)
|
||||
payload.RouteData = map[string]any{"from": "go"}
|
||||
return payload
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("NewDevHandler: %v", err)
|
||||
}
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/tasks", nil)
|
||||
req.Header.Set("Accept", "text/html")
|
||||
handler.ServeHTTP(resp, req)
|
||||
|
||||
body := resp.Body.String()
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d body=%s", resp.Code, http.StatusOK, body)
|
||||
}
|
||||
if !strings.Contains(body, bootPayloadGlobal) {
|
||||
t.Fatalf("body missing boot payload: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, `"route":"tasks"`) {
|
||||
t.Fatalf("body missing route metadata: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, `"from":"go"`) {
|
||||
t.Fatalf("body missing route data: %s", body)
|
||||
}
|
||||
if got := resp.Header().Get("Cache-Control"); got != "no-store" {
|
||||
t.Fatalf("Cache-Control = %q, want no-store", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevHandlerProxiesViteModuleRequests(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
vite := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/src/main.tsx" {
|
||||
t.Fatalf("vite received path %q, want /src/main.tsx", r.URL.Path)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/javascript")
|
||||
_, _ = w.Write([]byte(`console.log("vite module")`))
|
||||
}))
|
||||
t.Cleanup(vite.Close)
|
||||
|
||||
handler, err := NewDevHandler(vite.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewDevHandler: %v", err)
|
||||
}
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/src/main.tsx", nil)
|
||||
req.Header.Set("Accept", "*/*")
|
||||
handler.ServeHTTP(resp, req)
|
||||
|
||||
body := resp.Body.String()
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d body=%s", resp.Code, http.StatusOK, body)
|
||||
}
|
||||
if got := strings.TrimSpace(body); got != `console.log("vite module")` {
|
||||
t.Fatalf("body = %q, want proxied module", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package embedded
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
//go:embed fallback generated
|
||||
var assets embed.FS
|
||||
|
||||
// FS returns embedded Vite web assets. Generated assets are embedded when the
|
||||
// root build syncs apps/web/dist before compiling the backend; backend-only
|
||||
// builds fall back to a minimal shell so the binary remains buildable.
|
||||
func FS() (fs.FS, error) {
|
||||
if _, err := assets.Open("generated/index.html"); err == nil {
|
||||
return fs.Sub(assets, "generated")
|
||||
}
|
||||
return fs.Sub(assets, "fallback")
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Kandev</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,3 @@
|
||||
*
|
||||
!.gitignore
|
||||
!keep.txt
|
||||
@@ -0,0 +1 @@
|
||||
Generated Vite assets are copied here by `make sync-embedded-web`.
|
||||
@@ -0,0 +1,132 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const IndexHTML = "index.html"
|
||||
|
||||
type PayloadBuilder func(*http.Request, RouteClassification) BootPayload
|
||||
|
||||
type Handler struct {
|
||||
assets fs.FS
|
||||
payloadFor PayloadBuilder
|
||||
runtime RuntimeConfig
|
||||
indexPath string
|
||||
cacheControl string
|
||||
}
|
||||
|
||||
type HandlerOption func(*Handler)
|
||||
|
||||
func NewHandler(assets fs.FS, opts ...HandlerOption) *Handler {
|
||||
h := newHandlerDefaults(assets)
|
||||
for _, opt := range opts {
|
||||
opt(h)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func newHandlerDefaults(assets fs.FS) *Handler {
|
||||
h := &Handler{
|
||||
assets: assets,
|
||||
runtime: RuntimeConfig{APIPrefix: "/api/v1", WebSocketPath: "/ws"},
|
||||
indexPath: IndexHTML,
|
||||
cacheControl: "public, max-age=31536000, immutable",
|
||||
}
|
||||
h.payloadFor = func(_ *http.Request, route RouteClassification) BootPayload {
|
||||
return NewBootPayload(route, h.runtime, nil)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func WithRuntimeConfig(runtime RuntimeConfig) HandlerOption {
|
||||
return func(h *Handler) {
|
||||
h.runtime = runtime
|
||||
}
|
||||
}
|
||||
|
||||
func WithPayloadBuilder(builder PayloadBuilder) HandlerOption {
|
||||
return func(h *Handler) {
|
||||
if builder != nil {
|
||||
h.payloadFor = builder
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if h == nil || h.assets == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if h.serveAsset(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
route := ClassifyRoute(r.URL.Path)
|
||||
if route.Kind != RouteKindSPA {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
payload := h.payloadFor(r, route)
|
||||
html, err := RenderShell(h.assets, h.indexPath, payload)
|
||||
if err != nil {
|
||||
http.Error(w, "web app shell unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(html)
|
||||
}
|
||||
|
||||
func (h *Handler) serveAsset(w http.ResponseWriter, r *http.Request) bool {
|
||||
name := assetName(r.URL.Path)
|
||||
if name == "" || name == h.indexPath {
|
||||
return false
|
||||
}
|
||||
|
||||
file, err := h.assets.Open(name)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
}()
|
||||
|
||||
stat, err := file.Stat()
|
||||
if err != nil || stat.IsDir() {
|
||||
return false
|
||||
}
|
||||
|
||||
if contentType := mime.TypeByExtension(path.Ext(name)); contentType != "" {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
w.Header().Set("Cache-Control", h.cacheControl)
|
||||
seeker, ok := file.(io.ReadSeeker)
|
||||
if !ok {
|
||||
data, readErr := io.ReadAll(file)
|
||||
if readErr != nil {
|
||||
http.Error(w, "read web asset", http.StatusInternalServerError)
|
||||
return true
|
||||
}
|
||||
_, _ = w.Write(data)
|
||||
return true
|
||||
}
|
||||
http.ServeContent(w, r, stat.Name(), stat.ModTime(), seeker)
|
||||
return true
|
||||
}
|
||||
|
||||
func assetName(requestPath string) string {
|
||||
cleanPath := path.Clean("/" + strings.TrimPrefix(requestPath, "/"))
|
||||
if cleanPath == "/" {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimPrefix(cleanPath, "/")
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func TestHandlerServesStaticAssetBeforeRouteClassification(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler := NewHandler(fstest.MapFS{
|
||||
"index.html": {Data: []byte("<html><head></head><body></body></html>")},
|
||||
"fonts/seti/seti.woff": {Data: []byte("font")},
|
||||
"assets/index-abc123.js": {Data: []byte("console.log('ok')")},
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/fonts/seti/seti.woff", nil)
|
||||
handler.ServeHTTP(resp, req)
|
||||
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", resp.Code, http.StatusOK)
|
||||
}
|
||||
if got := strings.TrimSpace(resp.Body.String()); got != "font" {
|
||||
t.Fatalf("body = %q, want font", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerInjectsBootPayloadForSPARoutes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler := NewHandler(fstest.MapFS{
|
||||
"index.html": {Data: []byte("<html><head></head><body><div id=\"root\"></div></body></html>")},
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/t/task-1", nil)
|
||||
handler.ServeHTTP(resp, req)
|
||||
|
||||
body := resp.Body.String()
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d body=%s", resp.Code, http.StatusOK, body)
|
||||
}
|
||||
if !strings.Contains(body, bootPayloadGlobal) {
|
||||
t.Fatalf("body missing boot payload: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, `"taskId":"task-1"`) {
|
||||
t.Fatalf("body missing task route params: %s", body)
|
||||
}
|
||||
if got := resp.Header().Get("Cache-Control"); got != "no-store" {
|
||||
t.Fatalf("Cache-Control = %q, want no-store", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerReturnsNotFoundForAPIMiss(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler := NewHandler(fstest.MapFS{
|
||||
"index.html": {Data: []byte("<html><head></head><body></body></html>")},
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/unknown", nil)
|
||||
handler.ServeHTTP(resp, req)
|
||||
|
||||
if resp.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want %d", resp.Code, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package webapp
|
||||
|
||||
const BootPayloadVersion = 1
|
||||
|
||||
// BootPayload is the JSON-safe data blob the Go server will embed in the SPA
|
||||
// shell before React hydrates.
|
||||
type BootPayload struct {
|
||||
Version int `json:"version"`
|
||||
Route RouteClassification `json:"route"`
|
||||
Runtime RuntimeConfig `json:"runtime"`
|
||||
InitialState map[string]any `json:"initialState"`
|
||||
RouteData map[string]any `json:"routeData,omitempty"`
|
||||
Errors []BootError `json:"errors,omitempty"`
|
||||
}
|
||||
|
||||
// RuntimeConfig contains browser-facing runtime endpoints for the SPA.
|
||||
type RuntimeConfig struct {
|
||||
APIPrefix string `json:"apiPrefix"`
|
||||
WebSocketPath string `json:"webSocketPath"`
|
||||
}
|
||||
|
||||
// BootError is a serializable non-fatal boot-data error for partial hydration.
|
||||
type BootError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func NewBootPayload(
|
||||
route RouteClassification,
|
||||
runtime RuntimeConfig,
|
||||
initialState map[string]any,
|
||||
) BootPayload {
|
||||
if initialState == nil {
|
||||
initialState = map[string]any{}
|
||||
}
|
||||
|
||||
return BootPayload{
|
||||
Version: BootPayloadVersion,
|
||||
Route: route,
|
||||
Runtime: runtime,
|
||||
InitialState: initialState,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
pathpkg "path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// RouteKind describes how the backend should handle an incoming request path.
|
||||
type RouteKind string
|
||||
|
||||
const (
|
||||
RouteKindSPA RouteKind = "spa"
|
||||
RouteKindAPI RouteKind = "api"
|
||||
RouteKindWebSocket RouteKind = "websocket"
|
||||
RouteKindStatic RouteKind = "static"
|
||||
RouteKindHealth RouteKind = "health"
|
||||
)
|
||||
|
||||
// RouteName identifies SPA routes that need route-aware boot data.
|
||||
type RouteName string
|
||||
|
||||
const (
|
||||
RouteUnknown RouteName = "unknown"
|
||||
RouteHome RouteName = "home"
|
||||
RouteTasks RouteName = "tasks"
|
||||
RouteTaskDetail RouteName = "taskDetail"
|
||||
RouteOffice RouteName = "office"
|
||||
RouteSettings RouteName = "settings"
|
||||
RouteGitHub RouteName = "github"
|
||||
RouteGitLab RouteName = "gitlab"
|
||||
RouteJira RouteName = "jira"
|
||||
RouteLinear RouteName = "linear"
|
||||
RouteStats RouteName = "stats"
|
||||
)
|
||||
|
||||
// RouteClassification is the backend contract for SPA-vs-backend routing.
|
||||
type RouteClassification struct {
|
||||
Kind RouteKind `json:"kind"`
|
||||
Route RouteName `json:"route,omitempty"`
|
||||
Path string `json:"path"`
|
||||
Params map[string]string `json:"params,omitempty"`
|
||||
}
|
||||
|
||||
// ClassifyRoute returns the future webapp handling class for a request path.
|
||||
func ClassifyRoute(rawPath string) RouteClassification {
|
||||
requestPath := normalizePath(rawPath)
|
||||
if kind, ok := classifyNonSPA(requestPath); ok {
|
||||
return RouteClassification{Kind: kind, Path: requestPath}
|
||||
}
|
||||
|
||||
route, params := classifySPARoute(requestPath)
|
||||
return RouteClassification{
|
||||
Kind: RouteKindSPA,
|
||||
Route: route,
|
||||
Path: requestPath,
|
||||
Params: params,
|
||||
}
|
||||
}
|
||||
|
||||
// IsSPARoute reports whether rawPath should serve the SPA shell.
|
||||
func IsSPARoute(rawPath string) bool {
|
||||
return ClassifyRoute(rawPath).Kind == RouteKindSPA
|
||||
}
|
||||
|
||||
func classifyNonSPA(requestPath string) (RouteKind, bool) {
|
||||
switch {
|
||||
case requestPath == "/api" || strings.HasPrefix(requestPath, "/api/"):
|
||||
return RouteKindAPI, true
|
||||
case requestPath == "/ws" || strings.HasPrefix(requestPath, "/ws/"):
|
||||
return RouteKindWebSocket, true
|
||||
case requestPath == "/health":
|
||||
return RouteKindHealth, true
|
||||
case isStaticPath(requestPath):
|
||||
return RouteKindStatic, true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
func classifySPARoute(requestPath string) (RouteName, map[string]string) {
|
||||
switch {
|
||||
case requestPath == "/":
|
||||
return RouteHome, nil
|
||||
case requestPath == "/tasks":
|
||||
return RouteTasks, nil
|
||||
case requestPath == "/github":
|
||||
return RouteGitHub, nil
|
||||
case requestPath == "/gitlab":
|
||||
return RouteGitLab, nil
|
||||
case requestPath == "/jira":
|
||||
return RouteJira, nil
|
||||
case requestPath == "/linear":
|
||||
return RouteLinear, nil
|
||||
case requestPath == "/stats":
|
||||
return RouteStats, nil
|
||||
case requestPath == "/office" || strings.HasPrefix(requestPath, "/office/"):
|
||||
return RouteOffice, nil
|
||||
case requestPath == "/settings" || strings.HasPrefix(requestPath, "/settings/"):
|
||||
return RouteSettings, nil
|
||||
default:
|
||||
return classifyTaskRoute(requestPath)
|
||||
}
|
||||
}
|
||||
|
||||
func classifyTaskRoute(requestPath string) (RouteName, map[string]string) {
|
||||
taskID, ok := cutSingleSegment(requestPath, "/t/")
|
||||
if !ok {
|
||||
taskID, ok = cutSingleSegment(requestPath, "/tasks/")
|
||||
}
|
||||
if !ok {
|
||||
return RouteUnknown, nil
|
||||
}
|
||||
return RouteTaskDetail, map[string]string{"taskId": taskID}
|
||||
}
|
||||
|
||||
func cutSingleSegment(requestPath, prefix string) (string, bool) {
|
||||
value, ok := strings.CutPrefix(requestPath, prefix)
|
||||
if !ok || value == "" || strings.Contains(value, "/") {
|
||||
return "", false
|
||||
}
|
||||
return value, true
|
||||
}
|
||||
|
||||
func isStaticPath(requestPath string) bool {
|
||||
if strings.HasPrefix(requestPath, "/assets/") || strings.HasPrefix(requestPath, "/_next/") {
|
||||
return true
|
||||
}
|
||||
|
||||
switch requestPath {
|
||||
case "/favicon.ico", "/robots.txt", "/manifest.json", "/manifest.webmanifest", "/vite.svg":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func normalizePath(rawPath string) string {
|
||||
if rawPath == "" {
|
||||
return "/"
|
||||
}
|
||||
cleanInput := stripSuffix(rawPath, "?")
|
||||
cleanInput = stripSuffix(cleanInput, "#")
|
||||
if !strings.HasPrefix(cleanInput, "/") {
|
||||
cleanInput = "/" + cleanInput
|
||||
}
|
||||
return pathpkg.Clean(cleanInput)
|
||||
}
|
||||
|
||||
func stripSuffix(value, marker string) string {
|
||||
if idx := strings.Index(value, marker); idx >= 0 {
|
||||
return value[:idx]
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClassifyRouteSPARoutes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
wantRoute RouteName
|
||||
wantParams map[string]string
|
||||
}{
|
||||
{name: "home", path: "/", wantRoute: RouteHome},
|
||||
{name: "tasks", path: "/tasks", wantRoute: RouteTasks},
|
||||
{name: "task detail", path: "/t/task-123", wantRoute: RouteTaskDetail, wantParams: map[string]string{"taskId": "task-123"}},
|
||||
{name: "task detail compat", path: "/tasks/task-123", wantRoute: RouteTaskDetail, wantParams: map[string]string{"taskId": "task-123"}},
|
||||
{name: "office root", path: "/office", wantRoute: RouteOffice},
|
||||
{name: "office nested", path: "/office/agents/agent-1", wantRoute: RouteOffice},
|
||||
{name: "settings root", path: "/settings", wantRoute: RouteSettings},
|
||||
{name: "settings nested", path: "/settings/integrations/github", wantRoute: RouteSettings},
|
||||
{name: "github", path: "/github", wantRoute: RouteGitHub},
|
||||
{name: "gitlab", path: "/gitlab", wantRoute: RouteGitLab},
|
||||
{name: "jira", path: "/jira", wantRoute: RouteJira},
|
||||
{name: "linear", path: "/linear", wantRoute: RouteLinear},
|
||||
{name: "stats", path: "/stats", wantRoute: RouteStats},
|
||||
{name: "query stripped", path: "/tasks?view=all", wantRoute: RouteTasks},
|
||||
{name: "trailing slash normalized", path: "/settings/", wantRoute: RouteSettings},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := ClassifyRoute(tc.path)
|
||||
if got.Kind != RouteKindSPA {
|
||||
t.Fatalf("Kind = %q, want %q", got.Kind, RouteKindSPA)
|
||||
}
|
||||
if got.Route != tc.wantRoute {
|
||||
t.Fatalf("Route = %q, want %q", got.Route, tc.wantRoute)
|
||||
}
|
||||
assertParams(t, got.Params, tc.wantParams)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyRouteNonSPARoutes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
wantKind RouteKind
|
||||
}{
|
||||
{name: "api root", path: "/api", wantKind: RouteKindAPI},
|
||||
{name: "api nested", path: "/api/v1/tasks", wantKind: RouteKindAPI},
|
||||
{name: "websocket", path: "/ws", wantKind: RouteKindWebSocket},
|
||||
{name: "websocket nested", path: "/ws/tasks", wantKind: RouteKindWebSocket},
|
||||
{name: "health", path: "/health", wantKind: RouteKindHealth},
|
||||
{name: "vite assets", path: "/assets/app.js", wantKind: RouteKindStatic},
|
||||
{name: "favicon", path: "/favicon.ico", wantKind: RouteKindStatic},
|
||||
{name: "robots", path: "/robots.txt", wantKind: RouteKindStatic},
|
||||
{name: "manifest", path: "/manifest.webmanifest", wantKind: RouteKindStatic},
|
||||
{name: "legacy next static", path: "/_next/static/chunk.js", wantKind: RouteKindStatic},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := ClassifyRoute(tc.path)
|
||||
if got.Kind != tc.wantKind {
|
||||
t.Fatalf("Kind = %q, want %q", got.Kind, tc.wantKind)
|
||||
}
|
||||
if got.Route != "" {
|
||||
t.Fatalf("Route = %q, want empty for non-SPA route", got.Route)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyRouteUnknownPathFallsThroughToSPA(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := ClassifyRoute("/future/client/route")
|
||||
if got.Kind != RouteKindSPA {
|
||||
t.Fatalf("Kind = %q, want %q", got.Kind, RouteKindSPA)
|
||||
}
|
||||
if got.Route != RouteUnknown {
|
||||
t.Fatalf("Route = %q, want %q", got.Route, RouteUnknown)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootPayloadMarshalShapeAndScriptSafety(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
payload := NewBootPayload(
|
||||
ClassifyRoute("/t/task-123"),
|
||||
RuntimeConfig{APIPrefix: "/api/v1", WebSocketPath: "/ws"},
|
||||
map[string]any{
|
||||
"task": map[string]any{
|
||||
"id": "task-123",
|
||||
"title": "</script><script>alert(1)</script>",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal BootPayload: %v", err)
|
||||
}
|
||||
if strings.Contains(string(data), "</script>") {
|
||||
t.Fatalf("payload contains raw script terminator: %s", data)
|
||||
}
|
||||
|
||||
var decoded struct {
|
||||
Version int `json:"version"`
|
||||
Route json.RawMessage `json:"route"`
|
||||
Runtime json.RawMessage `json:"runtime"`
|
||||
InitialState json.RawMessage `json:"initialState"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &decoded); err != nil {
|
||||
t.Fatalf("Unmarshal BootPayload: %v", err)
|
||||
}
|
||||
if decoded.Version != BootPayloadVersion {
|
||||
t.Fatalf("Version = %d, want %d", decoded.Version, BootPayloadVersion)
|
||||
}
|
||||
assertRawJSONPresent(t, "route", decoded.Route)
|
||||
assertRawJSONPresent(t, "runtime", decoded.Runtime)
|
||||
assertRawJSONPresent(t, "initialState", decoded.InitialState)
|
||||
}
|
||||
|
||||
func assertParams(t *testing.T, got, want map[string]string) {
|
||||
t.Helper()
|
||||
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("Params = %#v, want %#v", got, want)
|
||||
}
|
||||
for key, wantValue := range want {
|
||||
if got[key] != wantValue {
|
||||
t.Fatalf("Params[%q] = %q, want %q", key, got[key], wantValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertRawJSONPresent(t *testing.T, name string, raw json.RawMessage) {
|
||||
t.Helper()
|
||||
|
||||
if len(raw) == 0 || string(raw) == "null" {
|
||||
t.Fatalf("%s missing from payload", name)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
const bootPayloadGlobal = "window.__KANDEV_BOOT_PAYLOAD__"
|
||||
const maxInt = int(^uint(0) >> 1)
|
||||
|
||||
var headCloseTag = []byte("</head>")
|
||||
|
||||
// RenderShell reads indexPath from assets and injects the boot payload before
|
||||
// </head>. The assets FS can be an embedded Vite dist in production or an
|
||||
// in-memory filesystem in tests.
|
||||
func RenderShell(assets fs.FS, indexPath string, payload BootPayload) ([]byte, error) {
|
||||
indexHTML, err := fs.ReadFile(assets, indexPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read web shell: %w", err)
|
||||
}
|
||||
|
||||
return RenderShellHTML(indexHTML, payload)
|
||||
}
|
||||
|
||||
// RenderShellHTML injects the boot payload into an already-loaded HTML shell.
|
||||
// Dev mode uses this with Vite's in-memory index.html.
|
||||
func RenderShellHTML(indexHTML []byte, payload BootPayload) ([]byte, error) {
|
||||
script, err := BootPayloadScript(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return injectBeforeHeadClose(indexHTML, script), nil
|
||||
}
|
||||
|
||||
// BootPayloadScript serializes payload into an inline script safe to embed in
|
||||
// HTML. encoding/json escapes '<', '>', and '&', which prevents a state value
|
||||
// from closing the script tag.
|
||||
func BootPayloadScript(payload BootPayload) ([]byte, error) {
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal boot payload: %w", err)
|
||||
}
|
||||
script := make([]byte, 0, bytesCapacity(len(data), len(bootPayloadGlobal), 32))
|
||||
script = append(script, "<script>"...)
|
||||
script = append(script, bootPayloadGlobal...)
|
||||
script = append(script, "="...)
|
||||
script = append(script, data...)
|
||||
script = append(script, ";</script>"...)
|
||||
return script, nil
|
||||
}
|
||||
|
||||
func injectBeforeHeadClose(indexHTML, script []byte) []byte {
|
||||
idx := bytes.Index(indexHTML, headCloseTag)
|
||||
if idx < 0 {
|
||||
out := make([]byte, 0, bytesCapacity(len(indexHTML), len(script)))
|
||||
out = append(out, script...)
|
||||
out = append(out, indexHTML...)
|
||||
return out
|
||||
}
|
||||
|
||||
out := make([]byte, 0, bytesCapacity(len(indexHTML), len(script)))
|
||||
out = append(out, indexHTML[:idx]...)
|
||||
out = append(out, script...)
|
||||
out = append(out, indexHTML[idx:]...)
|
||||
return out
|
||||
}
|
||||
|
||||
func bytesCapacity(parts ...int) int {
|
||||
total := 0
|
||||
for _, part := range parts {
|
||||
if part > maxInt-total {
|
||||
return 0
|
||||
}
|
||||
total += part
|
||||
}
|
||||
return total
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func TestRenderShellInjectsBootPayloadBeforeHeadClose(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assets := fstest.MapFS{
|
||||
"index.html": {
|
||||
Data: []byte("<!doctype html><html><head><title>Kandev</title></head><body></body></html>"),
|
||||
},
|
||||
}
|
||||
payload := NewBootPayload(
|
||||
ClassifyRoute("/t/task-1"),
|
||||
RuntimeConfig{APIPrefix: "/api/v1", WebSocketPath: "/ws"},
|
||||
map[string]any{"title": "Task"},
|
||||
)
|
||||
|
||||
html, err := RenderShell(assets, "index.html", payload)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderShell: %v", err)
|
||||
}
|
||||
|
||||
got := string(html)
|
||||
scriptIdx := strings.Index(got, bootPayloadGlobal)
|
||||
headCloseIdx := strings.Index(got, "</head>")
|
||||
if scriptIdx < 0 {
|
||||
t.Fatalf("rendered shell missing boot payload script: %s", got)
|
||||
}
|
||||
if headCloseIdx < 0 || scriptIdx > headCloseIdx {
|
||||
t.Fatalf("boot payload script should be injected before </head>: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, `"taskId":"task-1"`) {
|
||||
t.Fatalf("rendered shell missing route params: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootPayloadScriptEscapesScriptTerminators(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
payload := NewBootPayload(
|
||||
ClassifyRoute("/"),
|
||||
RuntimeConfig{},
|
||||
map[string]any{"title": "</script><script>alert(1)</script>"},
|
||||
)
|
||||
|
||||
script, err := BootPayloadScript(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("BootPayloadScript: %v", err)
|
||||
}
|
||||
if strings.Contains(string(script), "</script><script>") {
|
||||
t.Fatalf("script contains unescaped script terminator: %s", script)
|
||||
}
|
||||
if !strings.HasPrefix(string(script), "<script>window.__KANDEV_BOOT_PAYLOAD__=") {
|
||||
t.Fatalf("script has unexpected prefix: %s", script)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderShellPrependsScriptWhenHeadCloseIsMissing(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assets := fstest.MapFS{
|
||||
"index.html": {Data: []byte("<div id=\"root\"></div>")},
|
||||
}
|
||||
|
||||
html, err := RenderShell(assets, "index.html", NewBootPayload(ClassifyRoute("/"), RuntimeConfig{}, nil))
|
||||
if err != nil {
|
||||
t.Fatalf("RenderShell: %v", err)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(string(html), "<script>") {
|
||||
t.Fatalf("expected script prefix for shell without </head>: %s", html)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBytesCapacityReturnsZeroOnOverflow(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if got := bytesCapacity(3, 4, 5); got != 12 {
|
||||
t.Fatalf("bytesCapacity returned %d, want 12", got)
|
||||
}
|
||||
if got := bytesCapacity(maxInt, 1); got != 0 {
|
||||
t.Fatalf("bytesCapacity overflow returned %d, want 0", got)
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -17,7 +17,7 @@ kandev
|
||||
npx kandev@latest
|
||||
```
|
||||
|
||||
Either install path resolves a platform-matched runtime (Go backend, agentctl, Next.js standalone web), launches the backend + web, and opens your browser. Data (worktrees, SQLite DB) is stored in `~/.kandev` by default.
|
||||
Either install path resolves a platform-matched runtime (Go backend, agentctl, and static Vite web assets), launches the backend, and opens your browser. Data (worktrees, SQLite DB) is stored in `~/.kandev` by default.
|
||||
|
||||
## Version and Updates
|
||||
|
||||
@@ -71,7 +71,7 @@ See [docs/run-as-a-service.md](../../docs/run-as-a-service.md) for the full guid
|
||||
|
||||
## Requirements
|
||||
|
||||
- Node.js (for `npx`)
|
||||
- Node.js (only for `npx` / npm-based installs and development tooling)
|
||||
- Git
|
||||
- Docker (optional - needed for container runtimes)
|
||||
|
||||
|
||||
@@ -19,24 +19,24 @@ flowchart TD
|
||||
D5 -->|yes| D6["Download from GitHub Releases (cache)"]
|
||||
D5 -->|no| D7["Throw: 'No runtime found'"]
|
||||
|
||||
D2 --> E["Start backend + Next.js standalone"]
|
||||
D2 --> E["Start Go backend serving static SPA"]
|
||||
D4 --> E
|
||||
D6 --> E
|
||||
|
||||
F --> F1["make dev + next dev"]
|
||||
G --> G1["binary + next start"]
|
||||
F --> F1["make dev + web dev server"]
|
||||
G --> G1["backend binary serving Vite dist"]
|
||||
```
|
||||
|
||||
## Overview
|
||||
|
||||
This package provides the `kandev` CLI launcher. The runtime bundle (Go backend, agentctl, Next.js standalone web) is **installed by the package manager** — there is no first-run download.
|
||||
This package provides the `kandev` CLI launcher. The runtime bundle (Go backend, agentctl, and static Vite web assets) is **installed by the package manager** — there is no first-run download.
|
||||
|
||||
Two install channels share the same release artifacts:
|
||||
|
||||
- **npm/npx**: `kandev@X.Y.Z` declares `optionalDependencies` for `@kdlbs/runtime-{platform}@X.Y.Z`. npm 7+ filters by `os`/`cpu` and installs only the matching one.
|
||||
- **Homebrew**: `kdlbs/homebrew-kandev` formula downloads the GitHub release tarball into the Cellar and sets `KANDEV_BUNDLE_DIR`.
|
||||
|
||||
Both channels resolve to the same runtime bundle layout (`bin/kandev`, `bin/agentctl`, `web/server.js`). The CLI launcher (`runtime.ts`) abstracts over them.
|
||||
Both channels resolve to the same runtime bundle layout (`bin/kandev`, `bin/agentctl`, `web/index.html` plus assets). The CLI launcher (`runtime.ts`) abstracts over them.
|
||||
|
||||
## Artifact shapes
|
||||
|
||||
@@ -46,7 +46,8 @@ The GitHub release bundle and the npm runtime package are **different shapes** b
|
||||
# GitHub release bundle (used by Homebrew + manual installs)
|
||||
kandev/
|
||||
├── bin/{kandev,agentctl}
|
||||
├── web/server.js
|
||||
├── web/index.html
|
||||
├── web/assets/
|
||||
└── cli/
|
||||
├── bin/cli.js # #!/usr/bin/env node + chmod +x
|
||||
└── dist/cli.bundle.js # esbuild single-file bundle (deps inlined)
|
||||
@@ -54,10 +55,11 @@ kandev/
|
||||
# npm runtime package (@kdlbs/runtime-{platform})
|
||||
@kdlbs/runtime-{platform}/
|
||||
├── bin/{kandev,agentctl}
|
||||
└── web/server.js
|
||||
├── web/index.html
|
||||
└── web/assets/
|
||||
```
|
||||
|
||||
The `cli/` directory is only in the GitHub bundle (Homebrew needs the launcher). For npm installs, the main `kandev` package already provides the CLI; the runtime package only ships the binaries + web bundle.
|
||||
The `cli/` directory is only in the GitHub bundle (Homebrew needs the launcher). For npm installs, the main `kandev` package already provides the CLI; the runtime package only ships the binaries + web assets.
|
||||
|
||||
## Commands
|
||||
|
||||
@@ -73,7 +75,7 @@ The `cli/` directory is only in the GitHub bundle (Homebrew needs the launcher).
|
||||
| -------------------------- | ---------------------------------------------------------- |
|
||||
| `--version`, `-V` | Print CLI version and exit |
|
||||
| `--port`, `--backend-port` | Backend port |
|
||||
| `--web-internal-port` | Override internal Next.js port |
|
||||
| `--web-internal-port` | Override internal dev web port |
|
||||
| `--verbose`, `-v` | Show info logs |
|
||||
| `--debug` | Show debug logs + agent message dumps |
|
||||
| `--runtime-version <tag>` | **Advanced/debug**: download a specific GitHub runtime tag |
|
||||
|
||||
@@ -3,7 +3,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { findBundleRoot, resolveWebServerPath } from "./bundle";
|
||||
import { findBundleRoot } from "./bundle";
|
||||
|
||||
describe("findBundleRoot", () => {
|
||||
let tmpDir: string;
|
||||
@@ -26,50 +26,3 @@ describe("findBundleRoot", () => {
|
||||
expect(findBundleRoot(tmpDir)).toBe(tmpDir);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveWebServerPath", () => {
|
||||
let tmpDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "kandev-web-test-"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("finds web/server.js (non-monorepo)", () => {
|
||||
const serverPath = path.join(tmpDir, "web", "server.js");
|
||||
fs.mkdirSync(path.dirname(serverPath), { recursive: true });
|
||||
fs.writeFileSync(serverPath, "");
|
||||
expect(resolveWebServerPath(tmpDir)).toBe(serverPath);
|
||||
});
|
||||
|
||||
it("finds web/web/server.js (monorepo, apps root)", () => {
|
||||
const serverPath = path.join(tmpDir, "web", "web", "server.js");
|
||||
fs.mkdirSync(path.dirname(serverPath), { recursive: true });
|
||||
fs.writeFileSync(serverPath, "");
|
||||
expect(resolveWebServerPath(tmpDir)).toBe(serverPath);
|
||||
});
|
||||
|
||||
it("finds web/apps/web/server.js (monorepo, project root)", () => {
|
||||
const serverPath = path.join(tmpDir, "web", "apps", "web", "server.js");
|
||||
fs.mkdirSync(path.dirname(serverPath), { recursive: true });
|
||||
fs.writeFileSync(serverPath, "");
|
||||
expect(resolveWebServerPath(tmpDir)).toBe(serverPath);
|
||||
});
|
||||
|
||||
it("returns null when no server.js found", () => {
|
||||
expect(resolveWebServerPath(tmpDir)).toBeNull();
|
||||
});
|
||||
|
||||
it("prefers first match when multiple exist", () => {
|
||||
const first = path.join(tmpDir, "web", "server.js");
|
||||
const second = path.join(tmpDir, "web", "web", "server.js");
|
||||
fs.mkdirSync(path.dirname(first), { recursive: true });
|
||||
fs.writeFileSync(first, "");
|
||||
fs.mkdirSync(path.dirname(second), { recursive: true });
|
||||
fs.writeFileSync(second, "");
|
||||
expect(resolveWebServerPath(tmpDir)).toBe(first);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -22,21 +22,3 @@ export function findBundleRoot(cacheDir: string): string {
|
||||
}
|
||||
return cacheDir;
|
||||
}
|
||||
|
||||
export function resolveWebServerPath(bundleDir: string): string | null {
|
||||
// Next.js standalone output location depends on workspace structure:
|
||||
// - non-monorepo: web/server.js
|
||||
// - monorepo (apps/ root): web/web/server.js
|
||||
// - monorepo (project root): web/apps/web/server.js
|
||||
const candidates = [
|
||||
path.join(bundleDir, "web", "server.js"),
|
||||
path.join(bundleDir, "web", "web", "server.js"),
|
||||
path.join(bundleDir, "web", "apps", "web", "server.js"),
|
||||
];
|
||||
for (const candidate of candidates) {
|
||||
if (fs.existsSync(candidate)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+4
-4
@@ -32,8 +32,8 @@ Examples:
|
||||
kandev --debug
|
||||
|
||||
Options:
|
||||
dev Use local repo for dev (make dev + next dev) if available.
|
||||
start Use local production build (make build + next start).
|
||||
dev Use local repo for dev (Go backend + Vite dev server).
|
||||
start Use local production build (Go backend serves Vite dist).
|
||||
run Use installed runtime bundle (default).
|
||||
service Manage kandev as an OS service (systemd / launchd).
|
||||
Run 'kandev service --help' for details.
|
||||
@@ -49,8 +49,8 @@ Options:
|
||||
|
||||
Advanced:
|
||||
--backend-port Same as --port.
|
||||
--web-internal-port Override the internal Next.js port. The Go backend
|
||||
reverse-proxies to it; users hit the backend port.
|
||||
--web-internal-port Override the internal dev web port. The Go backend
|
||||
reverse-proxies to it in dev; start/run serve from Go.
|
||||
Also reads KANDEV_WEB_PORT.
|
||||
--web-port Deprecated alias for --web-internal-port.
|
||||
--runtime-version <tag> Download and use a specific release tag instead of
|
||||
|
||||
+3
-3
@@ -41,11 +41,11 @@ export async function runDev({ repoRoot, backendPort, webPort }: DevOptions): Pr
|
||||
const logLevel =
|
||||
process.env.KANDEV_LOGGING_LEVEL?.trim() || process.env.KANDEV_LOG_LEVEL?.trim() || "info";
|
||||
const webUrl = `http://localhost:${ports.webPort}`;
|
||||
const appUrl = ports.backendUrl;
|
||||
|
||||
logStartupInfo({
|
||||
header: "dev mode: using local repo",
|
||||
ports,
|
||||
primary: "web",
|
||||
dbPath,
|
||||
logLevel,
|
||||
});
|
||||
@@ -85,8 +85,8 @@ export async function runDev({ repoRoot, backendPort, webPort }: DevOptions): Pr
|
||||
label: "web",
|
||||
});
|
||||
await waitForUrlReady(webUrl, webProc, healthTimeoutMs);
|
||||
console.log(`[kandev] open: ${webUrl}`);
|
||||
openBrowser(webUrl);
|
||||
console.log(`[kandev] open: ${appUrl}`);
|
||||
openBrowser(appUrl);
|
||||
}
|
||||
|
||||
type DevBackendEnv = {
|
||||
|
||||
+6
-36
@@ -2,7 +2,7 @@ import { spawn } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
import { ensureExtracted, findBundleRoot, resolveWebServerPath } from "./bundle";
|
||||
import { ensureExtracted, findBundleRoot } from "./bundle";
|
||||
import {
|
||||
DEFAULT_AGENTCTL_PORT,
|
||||
DEFAULT_BACKEND_PORT,
|
||||
@@ -20,9 +20,9 @@ import { sortVersionsDesc } from "./version";
|
||||
import { pickAvailablePort } from "./ports";
|
||||
import { createProcessSupervisor } from "./process";
|
||||
import { resolveRuntime, validateBundle } from "./runtime";
|
||||
import { buildBackendEnv, buildWebEnv, logStartupInfo } from "./shared";
|
||||
import { buildBackendEnv, logStartupInfo } from "./shared";
|
||||
import { launchRestartableBackend } from "./supervisor/backend";
|
||||
import { launchWebApp, openBrowser } from "./web";
|
||||
import { openBrowser } from "./web";
|
||||
|
||||
export type RunOptions = {
|
||||
runtimeVersion?: string;
|
||||
@@ -41,7 +41,6 @@ type PreparedBundle = {
|
||||
backendBin: string;
|
||||
backendUrl: string;
|
||||
backendEnv: NodeJS.ProcessEnv;
|
||||
webEnv: NodeJS.ProcessEnv;
|
||||
releaseTag: string;
|
||||
webPort: number;
|
||||
agentctlPort: number;
|
||||
@@ -205,29 +204,18 @@ async function prepareBundleForLaunch({
|
||||
backendUrl,
|
||||
},
|
||||
logLevel,
|
||||
webProxy: false,
|
||||
extra: {
|
||||
KANDEV_DATABASE_PATH: dbPath,
|
||||
...(debug ? { KANDEV_DEBUG_AGENT_MESSAGES: "true", KANDEV_DEBUG_PPROF_ENABLED: "true" } : {}),
|
||||
},
|
||||
});
|
||||
|
||||
const webEnv = buildWebEnv({
|
||||
ports: {
|
||||
backendPort: actualBackendPort,
|
||||
webPort: actualWebPort,
|
||||
agentctlPort,
|
||||
backendUrl,
|
||||
},
|
||||
production: true,
|
||||
debug,
|
||||
});
|
||||
|
||||
return {
|
||||
bundleDir,
|
||||
backendBin,
|
||||
backendUrl,
|
||||
backendEnv,
|
||||
webEnv,
|
||||
releaseTag,
|
||||
webPort: actualWebPort,
|
||||
agentctlPort,
|
||||
@@ -259,7 +247,6 @@ export function attachRingBuffer(
|
||||
async function launchBundle(prepared: PreparedBundle): Promise<{
|
||||
supervisor: ReturnType<typeof createProcessSupervisor>;
|
||||
backendProc: ReturnType<typeof spawn>;
|
||||
webServerPath: string;
|
||||
dumpBackendLogs: () => void;
|
||||
}> {
|
||||
logStartupInfo({
|
||||
@@ -307,12 +294,7 @@ async function launchBundle(prepared: PreparedBundle): Promise<{
|
||||
console.error("[kandev] --- end backend stdout ---");
|
||||
};
|
||||
|
||||
const webServerPath = resolveWebServerPath(prepared.bundleDir);
|
||||
if (!webServerPath) {
|
||||
throw new Error("Web server entry (server.js) not found in bundle");
|
||||
}
|
||||
|
||||
return { supervisor, backendProc, webServerPath, dumpBackendLogs };
|
||||
return { supervisor, backendProc, dumpBackendLogs };
|
||||
}
|
||||
|
||||
export async function runRelease({
|
||||
@@ -330,24 +312,12 @@ export async function runRelease({
|
||||
verbose,
|
||||
debug,
|
||||
});
|
||||
const { supervisor, backendProc, webServerPath, dumpBackendLogs } = await launchBundle(prepared);
|
||||
const { backendProc, dumpBackendLogs } = await launchBundle(prepared);
|
||||
const healthTimeoutMs = resolveHealthTimeoutMs(HEALTH_TIMEOUT_MS_RELEASE);
|
||||
console.log("[kandev] starting backend...");
|
||||
await waitForHealth(prepared.backendUrl, backendProc, healthTimeoutMs, dumpBackendLogs);
|
||||
console.log(`[kandev] backend ready at ${prepared.backendUrl}`);
|
||||
|
||||
const webUrl = `http://localhost:${prepared.webPort}`;
|
||||
console.log("[kandev] starting web...");
|
||||
const webProc = launchWebApp({
|
||||
command: "node",
|
||||
args: [webServerPath],
|
||||
cwd: path.dirname(webServerPath),
|
||||
env: prepared.webEnv,
|
||||
supervisor,
|
||||
label: "web",
|
||||
quiet: !prepared.showOutput,
|
||||
});
|
||||
await waitForUrlReady(webUrl, webProc, healthTimeoutMs);
|
||||
if (headless) {
|
||||
console.log(`[kandev] ready (headless) at ${prepared.backendUrl}`);
|
||||
return;
|
||||
|
||||
@@ -17,7 +17,9 @@ function createFakeBundle(dir: string) {
|
||||
fs.writeFileSync(path.join(dir, "bin", "kandev"), "fake");
|
||||
fs.writeFileSync(path.join(dir, "bin", "agentctl"), "fake");
|
||||
fs.mkdirSync(path.join(dir, "web"), { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, "web", "server.js"), "fake");
|
||||
fs.writeFileSync(path.join(dir, "web", "index.html"), '<div id="root"></div>');
|
||||
fs.mkdirSync(path.join(dir, "web", "assets"), { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, "web", "assets", "index.js"), "fake");
|
||||
}
|
||||
|
||||
describe("validateBundle", () => {
|
||||
@@ -40,7 +42,7 @@ describe("validateBundle", () => {
|
||||
fs.mkdirSync(path.join(tmpDir, "bin"), { recursive: true });
|
||||
fs.writeFileSync(path.join(tmpDir, "bin", "agentctl"), "fake");
|
||||
fs.mkdirSync(path.join(tmpDir, "web"), { recursive: true });
|
||||
fs.writeFileSync(path.join(tmpDir, "web", "server.js"), "fake");
|
||||
fs.writeFileSync(path.join(tmpDir, "web", "index.html"), "");
|
||||
expect(() => validateBundle(tmpDir)).toThrow(/Backend binary not found/);
|
||||
});
|
||||
|
||||
@@ -48,15 +50,20 @@ describe("validateBundle", () => {
|
||||
fs.mkdirSync(path.join(tmpDir, "bin"), { recursive: true });
|
||||
fs.writeFileSync(path.join(tmpDir, "bin", "kandev"), "fake");
|
||||
fs.mkdirSync(path.join(tmpDir, "web"), { recursive: true });
|
||||
fs.writeFileSync(path.join(tmpDir, "web", "server.js"), "fake");
|
||||
fs.writeFileSync(path.join(tmpDir, "web", "index.html"), "");
|
||||
expect(() => validateBundle(tmpDir)).toThrow(/agentctl binary not found/);
|
||||
});
|
||||
|
||||
it("throws when web server.js is missing", () => {
|
||||
it("does not require external web assets", () => {
|
||||
fs.mkdirSync(path.join(tmpDir, "bin"), { recursive: true });
|
||||
fs.writeFileSync(path.join(tmpDir, "bin", "kandev"), "fake");
|
||||
fs.writeFileSync(path.join(tmpDir, "bin", "agentctl"), "fake");
|
||||
expect(() => validateBundle(tmpDir)).toThrow(/Web server.*not found/);
|
||||
expect(() => validateBundle(tmpDir)).not.toThrow();
|
||||
});
|
||||
|
||||
it("does not require a Node web server in the runtime bundle", () => {
|
||||
createFakeBundle(tmpDir);
|
||||
expect(() => validateBundle(tmpDir)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
|
||||
import { resolveWebServerPath } from "./bundle";
|
||||
import { getBinaryName, getPlatformDir, type PlatformDir } from "./platform";
|
||||
|
||||
const PLATFORM_TO_NPM_PACKAGE: Record<PlatformDir, string> = {
|
||||
@@ -76,8 +75,4 @@ export function validateBundle(bundleDir: string): void {
|
||||
if (!fs.existsSync(agentctlBin)) {
|
||||
throw new Error(`agentctl binary not found in bundle at ${bundleDir}`);
|
||||
}
|
||||
const webServerPath = resolveWebServerPath(bundleDir);
|
||||
if (!webServerPath) {
|
||||
throw new Error(`Web server (server.js) not found in bundle at ${bundleDir}`);
|
||||
}
|
||||
}
|
||||
|
||||
+26
-123
@@ -3,6 +3,7 @@ import os from "node:os";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
buildBackendEnv,
|
||||
buildWebEnv,
|
||||
listHostNetworkAddresses,
|
||||
logStartupInfo,
|
||||
@@ -18,41 +19,41 @@ const ports: PortConfig = {
|
||||
};
|
||||
|
||||
describe("buildWebEnv", () => {
|
||||
const originalApiPort = process.env.NEXT_PUBLIC_KANDEV_API_PORT;
|
||||
const originalViteApiPort = process.env.VITE_KANDEV_API_PORT;
|
||||
afterEach(() => {
|
||||
if (originalApiPort === undefined) {
|
||||
delete process.env.NEXT_PUBLIC_KANDEV_API_PORT;
|
||||
if (originalViteApiPort === undefined) {
|
||||
delete process.env.VITE_KANDEV_API_PORT;
|
||||
} else {
|
||||
process.env.NEXT_PUBLIC_KANDEV_API_PORT = originalApiPort;
|
||||
process.env.VITE_KANDEV_API_PORT = originalViteApiPort;
|
||||
}
|
||||
});
|
||||
|
||||
it("sets NEXT_PUBLIC_KANDEV_API_PORT in dev mode so the browser knows the backend port", () => {
|
||||
it("does not set browser API port env vars in dev mode", () => {
|
||||
const env = buildWebEnv({ ports });
|
||||
|
||||
expect(env.NEXT_PUBLIC_KANDEV_API_PORT).toBe("38429");
|
||||
expect(env.VITE_KANDEV_API_PORT).toBeUndefined();
|
||||
expect(env.NODE_ENV).not.toBe("production");
|
||||
});
|
||||
|
||||
it("does not set NEXT_PUBLIC_KANDEV_API_PORT in production single-port mode", () => {
|
||||
// The Go backend reverse-proxies Next.js, so the client must use same-origin.
|
||||
it("does not set browser API port env vars in production single-port mode", () => {
|
||||
// The Go backend serves the SPA, so the client must use same-origin.
|
||||
// A non-empty value would cause the client to build cross-origin URLs like
|
||||
// `https://host:38429/...` that aren't reachable behind a reverse proxy.
|
||||
const env = buildWebEnv({ ports, production: true });
|
||||
|
||||
expect(env.NEXT_PUBLIC_KANDEV_API_PORT).toBeUndefined();
|
||||
expect(env.VITE_KANDEV_API_PORT).toBeUndefined();
|
||||
expect(env.NODE_ENV).toBe("production");
|
||||
});
|
||||
|
||||
it("strips a host-level NEXT_PUBLIC_KANDEV_API_PORT in production", () => {
|
||||
it("strips host-level browser API port env vars in production", () => {
|
||||
// If an operator has the var in their shell/Docker env, the process.env
|
||||
// spread would otherwise leak it into the Next.js process and reintroduce
|
||||
// spread would otherwise leak it into the web process and reintroduce
|
||||
// the cross-origin URL problem this fix addresses.
|
||||
process.env.NEXT_PUBLIC_KANDEV_API_PORT = "99999";
|
||||
process.env.VITE_KANDEV_API_PORT = "99999";
|
||||
|
||||
const env = buildWebEnv({ ports, production: true });
|
||||
|
||||
expect(env.NEXT_PUBLIC_KANDEV_API_PORT).toBeUndefined();
|
||||
expect(env.VITE_KANDEV_API_PORT).toBeUndefined();
|
||||
});
|
||||
|
||||
it("always sets KANDEV_API_BASE_URL for SSR fetches", () => {
|
||||
@@ -63,121 +64,24 @@ describe("buildWebEnv", () => {
|
||||
});
|
||||
|
||||
it("enables debug flag when requested", () => {
|
||||
expect(buildWebEnv({ ports, debug: true }).NEXT_PUBLIC_KANDEV_DEBUG).toBe("true");
|
||||
expect(buildWebEnv({ ports, debug: true }).VITE_KANDEV_DEBUG).toBe("true");
|
||||
expect(buildWebEnv({ ports, debug: true }).KANDEV_DEBUG).toBe("true");
|
||||
expect(buildWebEnv({ ports }).NEXT_PUBLIC_KANDEV_DEBUG).toBeUndefined();
|
||||
expect(buildWebEnv({ ports }).VITE_KANDEV_DEBUG).toBeUndefined();
|
||||
expect(buildWebEnv({ ports }).KANDEV_DEBUG).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildWebEnv allowedDevOrigins", () => {
|
||||
const originalAllowed = process.env.NEXT_ALLOWED_DEV_ORIGINS;
|
||||
describe("buildBackendEnv", () => {
|
||||
it("points backend non-API routes at the dev web server by default", () => {
|
||||
const env = buildBackendEnv({ ports });
|
||||
|
||||
beforeEach(() => {
|
||||
vi.spyOn(os, "networkInterfaces").mockReturnValue({
|
||||
lo: [
|
||||
{
|
||||
address: "127.0.0.1",
|
||||
netmask: "255.0.0.0",
|
||||
family: "IPv4",
|
||||
mac: "00:00:00:00:00:00",
|
||||
internal: true,
|
||||
cidr: "127.0.0.1/8",
|
||||
},
|
||||
],
|
||||
eth0: [
|
||||
{
|
||||
address: "192.168.1.34",
|
||||
netmask: "255.255.252.0",
|
||||
family: "IPv4",
|
||||
mac: "aa:bb:cc:dd:ee:ff",
|
||||
internal: false,
|
||||
cidr: "192.168.1.34/22",
|
||||
},
|
||||
{
|
||||
address: "fe80::1",
|
||||
netmask: "ffff:ffff:ffff:ffff::",
|
||||
family: "IPv6",
|
||||
mac: "aa:bb:cc:dd:ee:ff",
|
||||
internal: false,
|
||||
cidr: "fe80::1/64",
|
||||
scopeid: 2,
|
||||
},
|
||||
],
|
||||
tailscale0: [
|
||||
{
|
||||
address: "100.94.173.104",
|
||||
netmask: "255.255.255.255",
|
||||
family: "IPv4",
|
||||
mac: "00:00:00:00:00:00",
|
||||
internal: false,
|
||||
cidr: "100.94.173.104/32",
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(env.KANDEV_WEB_INTERNAL_URL).toBe("http://localhost:37429");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
if (originalAllowed === undefined) {
|
||||
delete process.env.NEXT_ALLOWED_DEV_ORIGINS;
|
||||
} else {
|
||||
process.env.NEXT_ALLOWED_DEV_ORIGINS = originalAllowed;
|
||||
}
|
||||
});
|
||||
it("omits the web proxy in production single-process mode", () => {
|
||||
const env = buildBackendEnv({ ports, webProxy: false });
|
||||
|
||||
it("populates NEXT_ALLOWED_DEV_ORIGINS with LAN + Tailscale IPs in dev mode", () => {
|
||||
delete process.env.NEXT_ALLOWED_DEV_ORIGINS;
|
||||
const env = buildWebEnv({ ports });
|
||||
const origins = (env.NEXT_ALLOWED_DEV_ORIGINS ?? "").split(",");
|
||||
expect(origins).toContain("192.168.1.34");
|
||||
expect(origins).toContain("100.94.173.104");
|
||||
});
|
||||
|
||||
it("excludes loopback and link-local IPv6 addresses", () => {
|
||||
delete process.env.NEXT_ALLOWED_DEV_ORIGINS;
|
||||
const env = buildWebEnv({ ports });
|
||||
const origins = (env.NEXT_ALLOWED_DEV_ORIGINS ?? "").split(",");
|
||||
expect(origins).not.toContain("127.0.0.1");
|
||||
expect(origins).not.toContain("fe80::1");
|
||||
});
|
||||
|
||||
it("merges with a user-supplied NEXT_ALLOWED_DEV_ORIGINS, deduped", () => {
|
||||
process.env.NEXT_ALLOWED_DEV_ORIGINS = "dev.example, 192.168.1.34";
|
||||
const env = buildWebEnv({ ports });
|
||||
const origins = (env.NEXT_ALLOWED_DEV_ORIGINS ?? "").split(",");
|
||||
expect(origins).toContain("dev.example");
|
||||
expect(origins).toContain("192.168.1.34");
|
||||
expect(origins).toContain("100.94.173.104");
|
||||
expect(origins.filter((s) => s === "192.168.1.34")).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("omits NEXT_ALLOWED_DEV_ORIGINS entirely when there's nothing to allow", () => {
|
||||
delete process.env.NEXT_ALLOWED_DEV_ORIGINS;
|
||||
vi.restoreAllMocks();
|
||||
vi.spyOn(os, "networkInterfaces").mockReturnValue({
|
||||
lo: [
|
||||
{
|
||||
address: "127.0.0.1",
|
||||
netmask: "255.0.0.0",
|
||||
family: "IPv4",
|
||||
mac: "00:00:00:00:00:00",
|
||||
internal: true,
|
||||
cidr: "127.0.0.1/8",
|
||||
},
|
||||
],
|
||||
});
|
||||
const env = buildWebEnv({ ports });
|
||||
// Better than setting NEXT_ALLOWED_DEV_ORIGINS="" — keeps the env clean for
|
||||
// the loopback-only case so a downstream consumer that distinguishes
|
||||
// "unset" from "empty" doesn't get confused.
|
||||
expect(env.NEXT_ALLOWED_DEV_ORIGINS).toBeUndefined();
|
||||
});
|
||||
|
||||
it("does not set NEXT_ALLOWED_DEV_ORIGINS in production", () => {
|
||||
delete process.env.NEXT_ALLOWED_DEV_ORIGINS;
|
||||
const env = buildWebEnv({ ports, production: true });
|
||||
expect(env.NEXT_ALLOWED_DEV_ORIGINS).toBeUndefined();
|
||||
expect(env.KANDEV_WEB_INTERNAL_URL).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -387,16 +291,15 @@ describe("logStartupInfo", () => {
|
||||
expect(lines.some((l) => l.includes("agentctl"))).toBe(false);
|
||||
});
|
||||
|
||||
it('prints the web URL and network URLs when primary="web" (dev mode)', () => {
|
||||
it('prints the web URL and network URLs when primary="web" is explicitly requested', () => {
|
||||
mockTwoInterfaces();
|
||||
logStartupInfo({ header: "test", ports, primary: "web" });
|
||||
const lines = log.mock.calls.map((c) => c.join(" "));
|
||||
expect(lines).toContain("[kandev] url: http://localhost:37429");
|
||||
expect(lines).toContain("[kandev] network: http://192.168.1.34:37429");
|
||||
expect(lines).toContain("[kandev] network: http://100.94.173.104:37429");
|
||||
// The backend port is internal in dev mode (only the browser's API calls
|
||||
// hit it, via NEXT_PUBLIC_KANDEV_API_PORT), so it doesn't show up as a URL
|
||||
// for the user. The mcp endpoint still appears because MCP clients use it.
|
||||
// Explicit web-primary mode is for diagnostics; normal dev/start modes
|
||||
// use the backend URL as the browser entrypoint.
|
||||
expect(lines.some((l) => l === "[kandev] url: http://localhost:38429")).toBe(false);
|
||||
expect(lines.some((l) => l.includes("network:") && l.includes(":38429"))).toBe(false);
|
||||
});
|
||||
|
||||
+26
-56
@@ -43,6 +43,8 @@ export type BackendEnvOptions = {
|
||||
ports: PortConfig;
|
||||
/** Log level: debug, info, warn, error (default: info) */
|
||||
logLevel?: string;
|
||||
/** Route browser pages through an external dev web server. Production serves the SPA from Go. */
|
||||
webProxy?: boolean;
|
||||
/** Additional environment variables to merge */
|
||||
extra?: Record<string, string>;
|
||||
};
|
||||
@@ -54,22 +56,26 @@ export type BackendEnvOptions = {
|
||||
* @returns Environment object for the backend process
|
||||
*/
|
||||
export function buildBackendEnv(options: BackendEnvOptions): NodeJS.ProcessEnv {
|
||||
const { ports, logLevel, extra } = options;
|
||||
return {
|
||||
const { ports, logLevel, webProxy = true, extra } = options;
|
||||
const env: NodeJS.ProcessEnv = {
|
||||
...process.env,
|
||||
KANDEV_SERVER_PORT: String(ports.backendPort),
|
||||
KANDEV_WEB_INTERNAL_URL: `http://localhost:${ports.webPort}`,
|
||||
...(webProxy ? { KANDEV_WEB_INTERNAL_URL: `http://localhost:${ports.webPort}` } : {}),
|
||||
KANDEV_AGENT_STANDALONE_PORT: String(ports.agentctlPort),
|
||||
...(logLevel ? { KANDEV_LOG_LEVEL: logLevel } : {}),
|
||||
...extra,
|
||||
};
|
||||
if (!webProxy) {
|
||||
delete env.KANDEV_WEB_INTERNAL_URL;
|
||||
}
|
||||
return env;
|
||||
}
|
||||
|
||||
export type WebEnvOptions = {
|
||||
ports: PortConfig;
|
||||
/** Set NODE_ENV to production */
|
||||
production?: boolean;
|
||||
/** Enable debug mode (NEXT_PUBLIC_KANDEV_DEBUG) */
|
||||
/** Enable debug mode */
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
@@ -84,45 +90,24 @@ export function buildWebEnv(options: WebEnvOptions): NodeJS.ProcessEnv {
|
||||
|
||||
const env: NodeJS.ProcessEnv = {
|
||||
...process.env,
|
||||
// Server-side: full localhost URL for SSR fetches (Next.js SSR → Go backend)
|
||||
// Internal Vite/server tooling config. Browser traffic enters through Go
|
||||
// in every mode so frontend code should use same-origin API/WS URLs.
|
||||
KANDEV_API_BASE_URL: ports.backendUrl,
|
||||
PORT: String(ports.webPort),
|
||||
// Ensure Next.js standalone server binds to 127.0.0.1 so localhost health checks work.
|
||||
// Without this, HOSTNAME from the host environment can cause binding issues.
|
||||
HOSTNAME: "127.0.0.1",
|
||||
};
|
||||
|
||||
if (production) {
|
||||
(env as Record<string, string>).NODE_ENV = "production";
|
||||
// Explicitly unset so a host-level NEXT_PUBLIC_KANDEV_API_PORT (from a .env
|
||||
// file, Docker env, or CI variable) cannot leak through the process.env
|
||||
// spread above and reintroduce the cross-origin URL problem.
|
||||
delete env.NEXT_PUBLIC_KANDEV_API_PORT;
|
||||
} else {
|
||||
// Dev mode only: browser hits the web port directly, so the client needs to
|
||||
// know the backend port for API calls. In production the Go backend
|
||||
// reverse-proxies Next.js on a single port, so the client uses same-origin
|
||||
// and this var must NOT be set — otherwise the client builds cross-origin
|
||||
// URLs like `https://host:38429/...` that aren't reachable behind a
|
||||
// reverse proxy / ingress / Cloudflare tunnel.
|
||||
env.NEXT_PUBLIC_KANDEV_API_PORT = String(ports.backendPort);
|
||||
|
||||
// Auto-allow the host's own LAN / VPN addresses so a dev hitting the dev
|
||||
// server from another device on the same network (Tailscale, LAN IP, WSL
|
||||
// mirrored mode, etc.) passes Next.js's allowedDevOrigins check and HMR
|
||||
// works. The user can still extend the list via NEXT_ALLOWED_DEV_ORIGINS.
|
||||
// Skip the assignment when there's nothing to add — keeps the env clean
|
||||
// for the loopback-only case.
|
||||
const merged = mergeAllowedDevOrigins(
|
||||
process.env.NEXT_ALLOWED_DEV_ORIGINS,
|
||||
listHostNetworkAddresses(),
|
||||
);
|
||||
if (merged) env.NEXT_ALLOWED_DEV_ORIGINS = merged;
|
||||
}
|
||||
// Explicitly unset so a host-level VITE_KANDEV_API_PORT (from a .env file,
|
||||
// Docker env, or CI variable) cannot leak through the process.env spread and
|
||||
// reintroduce cross-origin browser API calls.
|
||||
delete env.VITE_KANDEV_API_PORT;
|
||||
|
||||
if (debug) {
|
||||
env.KANDEV_DEBUG = "true";
|
||||
env.NEXT_PUBLIC_KANDEV_DEBUG = "true";
|
||||
env.VITE_KANDEV_DEBUG = "true";
|
||||
}
|
||||
|
||||
return env;
|
||||
@@ -130,8 +115,7 @@ export function buildWebEnv(options: WebEnvOptions): NodeJS.ProcessEnv {
|
||||
|
||||
/**
|
||||
* Returns the host's non-loopback, non-internal IPv4/IPv6 addresses. Used to
|
||||
* auto-populate Next.js `allowedDevOrigins` so the dev server accepts
|
||||
* connections from LAN / Tailscale / SSH-forwarded clients.
|
||||
* print LAN / Tailscale / SSH-forwarded browser URLs in the startup banner.
|
||||
*/
|
||||
export function listHostNetworkAddresses(): string[] {
|
||||
const v4: string[] = [];
|
||||
@@ -160,27 +144,15 @@ export function listHostNetworkAddresses(): string[] {
|
||||
return [...v4, ...v6];
|
||||
}
|
||||
|
||||
function mergeAllowedDevOrigins(existing: string | undefined, extra: string[]): string {
|
||||
const set = new Set<string>();
|
||||
if (existing) {
|
||||
for (const s of existing.split(",")) {
|
||||
const trimmed = s.trim();
|
||||
if (trimmed) set.add(trimmed);
|
||||
}
|
||||
}
|
||||
for (const s of extra) set.add(s);
|
||||
return [...set].join(",");
|
||||
}
|
||||
|
||||
export type StartupInfoOptions = {
|
||||
/** Mode header line, e.g. "dev mode: using local repo" or "release: v0.0.12 (github latest)" */
|
||||
header: string;
|
||||
ports: PortConfig;
|
||||
/**
|
||||
* Which port a user actually opens in a browser. In `start`/`run` the Go
|
||||
* backend reverse-proxies Next.js so the entry point is the backend port;
|
||||
* in `dev` the browser hits Next.js directly. Network URLs are only listed
|
||||
* under this port — the other one is internal-only. Defaults to "backend".
|
||||
* Which port a user actually opens in a browser. Normal `dev`/`start`/`run`
|
||||
* use the Go backend as the entrypoint; `primary: "web"` is retained for
|
||||
* diagnostics. Network URLs are only listed under this port — the other one
|
||||
* is internal-only. Defaults to "backend".
|
||||
*/
|
||||
primary?: "backend" | "web";
|
||||
/** Database file path */
|
||||
@@ -192,12 +164,10 @@ export type StartupInfoOptions = {
|
||||
/**
|
||||
* Logs a unified startup info block to the console.
|
||||
*
|
||||
* Shows only the URL the user actually opens — start/run modes have the Go
|
||||
* backend reverse-proxy Next.js on a single port, dev mode hits Next.js
|
||||
* directly. The other port and the agentctl port are internal plumbing and
|
||||
* would only mislead. Below the URL, lists the same port on each non-loopback
|
||||
* interface (LAN, Tailscale) so a user opening the app remotely sees the
|
||||
* right address.
|
||||
* Shows only the URL the user actually opens. The other web/Vite port and the
|
||||
* agentctl port are internal plumbing and would only mislead. Below the URL,
|
||||
* lists the same port on each non-loopback interface (LAN, Tailscale) so a
|
||||
* user opening the app remotely sees the right address.
|
||||
*/
|
||||
export function logStartupInfo(options: StartupInfoOptions): void {
|
||||
const { header, ports, primary = "backend", dbPath, logLevel } = options;
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { resolveStandaloneServerPath } from "./start";
|
||||
|
||||
function makeRepo(): string {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), "kandev-start-test-"));
|
||||
}
|
||||
|
||||
function writeFile(filePath: string, contents = "") {
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
fs.writeFileSync(filePath, contents);
|
||||
}
|
||||
|
||||
describe("resolveStandaloneServerPath", () => {
|
||||
let repoRoot: string;
|
||||
|
||||
beforeEach(() => {
|
||||
repoRoot = makeRepo();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fs.rmSync(repoRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("returns the expected path when .next/standalone/web/server.js exists", () => {
|
||||
const expected = path.join(repoRoot, "apps", "web", ".next", "standalone", "web", "server.js");
|
||||
writeFile(expected);
|
||||
|
||||
expect(resolveStandaloneServerPath(repoRoot)).toBe(expected);
|
||||
});
|
||||
|
||||
it("finds server.js at a deeper path (Turbopack root mismatch)", () => {
|
||||
const nested = path.join(
|
||||
repoRoot,
|
||||
"apps",
|
||||
"web",
|
||||
".next",
|
||||
"standalone",
|
||||
"Users",
|
||||
"alice",
|
||||
"projects",
|
||||
"kandev",
|
||||
"apps",
|
||||
"web",
|
||||
"server.js",
|
||||
);
|
||||
writeFile(nested);
|
||||
|
||||
expect(resolveStandaloneServerPath(repoRoot)).toBe(nested);
|
||||
});
|
||||
|
||||
it("returns null when .next/standalone/ does not exist", () => {
|
||||
expect(resolveStandaloneServerPath(repoRoot)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when standalone/ exists but has no web/server.js", () => {
|
||||
const standaloneDir = path.join(repoRoot, "apps", "web", ".next", "standalone");
|
||||
fs.mkdirSync(standaloneDir, { recursive: true });
|
||||
// Stray server.js that is NOT inside a `web/` directory.
|
||||
writeFile(path.join(standaloneDir, "node_modules", "next", "server.js"));
|
||||
|
||||
expect(resolveStandaloneServerPath(repoRoot)).toBeNull();
|
||||
});
|
||||
|
||||
it("ignores server.js inside node_modules/web/", () => {
|
||||
const standaloneDir = path.join(repoRoot, "apps", "web", ".next", "standalone");
|
||||
writeFile(path.join(standaloneDir, "node_modules", "web", "server.js"));
|
||||
|
||||
expect(resolveStandaloneServerPath(repoRoot)).toBeNull();
|
||||
});
|
||||
});
|
||||
+8
-108
@@ -2,13 +2,12 @@
|
||||
* Production start command for running local builds.
|
||||
*
|
||||
* This module implements the `kandev start` command, which runs the locally
|
||||
* built backend binary and web app in production mode. Unlike `kandev dev`
|
||||
* built backend binary in production mode. Unlike `kandev dev`
|
||||
* which uses hot-reloading, this runs the optimized production builds.
|
||||
*
|
||||
* Prerequisites:
|
||||
* - Backend must be built: `make build-backend`
|
||||
* - Web app must be built: `make build-web`
|
||||
* - Or simply: `make build` (builds both)
|
||||
* - Or simply: `make build`
|
||||
*/
|
||||
|
||||
import fs from "node:fs";
|
||||
@@ -20,55 +19,12 @@ import {
|
||||
resolveDatabasePath,
|
||||
resolveKandevHomeDir,
|
||||
} from "./constants";
|
||||
import { resolveHealthTimeoutMs, waitForHealth, waitForUrlReady } from "./health";
|
||||
import { resolveHealthTimeoutMs, waitForHealth } from "./health";
|
||||
import { getBinaryName } from "./platform";
|
||||
import { createProcessSupervisor } from "./process";
|
||||
import { buildBackendEnv, buildWebEnv, logStartupInfo, pickPorts } from "./shared";
|
||||
import { buildBackendEnv, logStartupInfo, pickPorts } from "./shared";
|
||||
import { launchRestartableBackend } from "./supervisor/backend";
|
||||
import { launchWebApp, openBrowser } from "./web";
|
||||
|
||||
/**
|
||||
* Locates the standalone Next.js `server.js` inside `apps/web/.next/standalone/`.
|
||||
*
|
||||
* Normally this sits at `.next/standalone/web/server.js`, but Turbopack may
|
||||
* place it under a deeper path (e.g. `.next/standalone/Users/.../web/server.js`)
|
||||
* when it detects the wrong project root (typically caused by a stray
|
||||
* `package-lock.json` in a parent directory). We look for any `web/server.js`
|
||||
* below the standalone directory so `kandev start` keeps working.
|
||||
*
|
||||
* @returns absolute path to `server.js`, or null if it cannot be found.
|
||||
*/
|
||||
export function resolveStandaloneServerPath(repoRoot: string): string | null {
|
||||
const standaloneDir = path.join(repoRoot, "apps", "web", ".next", "standalone");
|
||||
const expected = path.join(standaloneDir, "web", "server.js");
|
||||
if (fs.existsSync(expected)) return expected;
|
||||
if (!fs.existsSync(standaloneDir)) return null;
|
||||
|
||||
return findWebServerJs(standaloneDir);
|
||||
}
|
||||
|
||||
// Walks `dir` manually instead of relying on `Dirent.parentPath` (Node 20.12+),
|
||||
// so the CLI keeps working on older Node 20.x runtimes installed via npx.
|
||||
function findWebServerJs(dir: string): string | null {
|
||||
const stack: string[] = [dir];
|
||||
while (stack.length > 0) {
|
||||
const current = stack.pop() as string;
|
||||
let entries: fs.Dirent[];
|
||||
try {
|
||||
entries = fs.readdirSync(current, { withFileTypes: true });
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
for (const entry of entries) {
|
||||
if (entry.isDirectory() && entry.name !== "node_modules") {
|
||||
stack.push(path.join(current, entry.name));
|
||||
} else if (entry.isFile() && entry.name === "server.js" && path.basename(current) === "web") {
|
||||
return path.join(current, entry.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
import { openBrowser } from "./web";
|
||||
|
||||
export type StartOptions = {
|
||||
/** Path to the repository root directory */
|
||||
@@ -92,11 +48,10 @@ export type StartOptions = {
|
||||
* 1. Validates that build artifacts exist
|
||||
* 2. Picks available ports for all services
|
||||
* 3. Starts the backend binary (with warn log level for clean output)
|
||||
* 4. Starts the web app via `pnpm start`
|
||||
* 5. Waits for the backend to be healthy before announcing readiness
|
||||
* 4. Waits for the backend to be healthy before announcing readiness
|
||||
*
|
||||
* @param options - Configuration for the start command
|
||||
* @throws Error if backend binary or web build is not found
|
||||
* @throws Error if backend binary is not found
|
||||
*/
|
||||
export async function runStart({
|
||||
repoRoot,
|
||||
@@ -113,47 +68,6 @@ export async function runStart({
|
||||
throw new Error("Backend binary not found. Run `make build` first.");
|
||||
}
|
||||
|
||||
// Check for standalone build (Next.js standalone output)
|
||||
const webServerPath = resolveStandaloneServerPath(repoRoot);
|
||||
if (!webServerPath) {
|
||||
const standaloneDir = path.join(repoRoot, "apps", "web", ".next", "standalone");
|
||||
if (!fs.existsSync(standaloneDir)) {
|
||||
throw new Error("Web standalone build not found. Run `make build` first.");
|
||||
}
|
||||
throw new Error(
|
||||
`Web standalone build is missing server.js under ${standaloneDir}. ` +
|
||||
"This can happen when Next.js/Turbopack detects a different project root " +
|
||||
"(e.g. a stray package-lock.json in a parent directory). " +
|
||||
"Remove the stray lockfile and re-run `make build`.",
|
||||
);
|
||||
}
|
||||
const webStandaloneDir = path.dirname(webServerPath);
|
||||
const webStaticDir = path.join(repoRoot, "apps", "web", ".next", "static");
|
||||
const standaloneStaticDir = path.join(webStandaloneDir, ".next", "static");
|
||||
if (fs.existsSync(webStaticDir) && !fs.existsSync(standaloneStaticDir)) {
|
||||
fs.mkdirSync(path.dirname(standaloneStaticDir), { recursive: true });
|
||||
try {
|
||||
fs.symlinkSync(webStaticDir, standaloneStaticDir, "junction");
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
`[kandev] failed to link Next.js static assets: ${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Link public directory (fonts, images, etc.) into standalone output
|
||||
const webPublicDir = path.join(repoRoot, "apps", "web", "public");
|
||||
const standalonePublicDir = path.join(webStandaloneDir, "public");
|
||||
if (fs.existsSync(webPublicDir) && !fs.existsSync(standalonePublicDir)) {
|
||||
try {
|
||||
fs.symlinkSync(webPublicDir, standalonePublicDir, "junction");
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
`[kandev] failed to link public assets: ${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Production mode: use warn log level for clean output unless verbose/debug
|
||||
const showOutput = verbose || debug;
|
||||
fs.mkdirSync(resolveDataDir(), { recursive: true });
|
||||
@@ -166,12 +80,12 @@ export async function runStart({
|
||||
const backendEnv = buildBackendEnv({
|
||||
ports,
|
||||
logLevel,
|
||||
webProxy: false,
|
||||
extra: {
|
||||
KANDEV_DATABASE_PATH: dbPath,
|
||||
...(debug ? { KANDEV_DEBUG_AGENT_MESSAGES: "true", KANDEV_DEBUG_PPROF_ENABLED: "true" } : {}),
|
||||
},
|
||||
});
|
||||
const webEnv = buildWebEnv({ ports, production: true, debug });
|
||||
|
||||
logStartupInfo({
|
||||
header: "start mode: using local build",
|
||||
@@ -202,20 +116,6 @@ export async function runStart({
|
||||
await waitForHealth(ports.backendUrl, backend.proc, healthTimeoutMs);
|
||||
console.log(`[kandev] backend ready at ${ports.backendUrl}`);
|
||||
|
||||
// Use standalone server.js directly (not pnpm start)
|
||||
const webUrl = `http://localhost:${ports.webPort}`;
|
||||
console.log("[kandev] starting web...");
|
||||
const webProc = launchWebApp({
|
||||
command: "node",
|
||||
args: [webServerPath],
|
||||
cwd: webStandaloneDir,
|
||||
env: webEnv,
|
||||
supervisor,
|
||||
label: "web",
|
||||
quiet: !showOutput,
|
||||
});
|
||||
|
||||
await waitForUrlReady(webUrl, webProc, healthTimeoutMs);
|
||||
console.log("[kandev] open: " + ports.backendUrl);
|
||||
if (headless) {
|
||||
console.log(`[kandev] ready (headless) at ${ports.backendUrl}`);
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
"cmdk": "^1.1.1",
|
||||
"embla-carousel-react": "^8.6.0",
|
||||
"input-otp": "^1.4.2",
|
||||
"next-themes": "^0.4.6",
|
||||
"radix-ui": "^1.4.3",
|
||||
"react-day-picker": "^9.13.0",
|
||||
"react-resizable-panels": "^4.4.1",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useTheme } from "next-themes";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Toaster as Sonner, type ToasterProps } from "sonner";
|
||||
import {
|
||||
IconCircleCheck,
|
||||
@@ -10,12 +10,23 @@ import {
|
||||
IconLoader,
|
||||
} from "@tabler/icons-react";
|
||||
|
||||
function readDocumentTheme(): ToasterProps["theme"] {
|
||||
if (typeof document === "undefined") return "system";
|
||||
return document.documentElement.classList.contains("dark") ? "dark" : "light";
|
||||
}
|
||||
|
||||
const Toaster = ({ ...props }: ToasterProps) => {
|
||||
const { theme = "system" } = useTheme();
|
||||
const [theme, setTheme] = useState<ToasterProps["theme"]>(() => readDocumentTheme());
|
||||
|
||||
useEffect(() => {
|
||||
const observer = new MutationObserver(() => setTheme(readDocumentTheme()));
|
||||
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
|
||||
return () => observer.disconnect();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Sonner
|
||||
theme={theme as ToasterProps["theme"]}
|
||||
theme={theme}
|
||||
className="toaster group"
|
||||
icons={{
|
||||
success: <IconCircleCheck className="size-4" />,
|
||||
|
||||
Generated
+448
-1706
File diff suppressed because it is too large
Load Diff
+13
-3
@@ -1,4 +1,4 @@
|
||||
# Frontend (Next.js) — architecture and conventions
|
||||
# Frontend (Vite/React SPA) — architecture and conventions
|
||||
|
||||
Scoped guidance for `apps/web/`. Repo-wide rules (commit format, code-quality limits, etc.) live in the root `AGENTS.md`.
|
||||
|
||||
@@ -23,7 +23,7 @@ import { Dialog } from "@kandev/ui/dialog";
|
||||
## Data Flow Pattern (Critical)
|
||||
|
||||
```text
|
||||
SSR Fetch -> Hydrate Store -> Components Read Store -> Hooks Subscribe
|
||||
Go Boot Payload -> Hydrate Store -> Components Read Store -> Hooks Subscribe
|
||||
```
|
||||
|
||||
**Never fetch data directly in components.**
|
||||
@@ -59,7 +59,10 @@ lib/api/domains/ # API clients
|
||||
- `tasks.activeTaskId`, `tasks.activeSessionId`, `workspaces.activeId`
|
||||
- `repositories.byWorkspace`, `repositoryBranches.byRepository`
|
||||
|
||||
**Hydration:** `lib/state/hydration/merge-strategies.ts` has `deepMerge()`, `mergeSessionMap()`, `mergeLoadingState()` to avoid overwriting live client state. Pass `activeSessionId` to protect active sessions.
|
||||
**Hydration:** Go injects `window.__KANDEV_BOOT_PAYLOAD__` into the SPA shell before React mounts. `lib/state/hydration/merge-strategies.ts` has `deepMerge()`, `mergeSessionMap()`, `mergeLoadingState()` to avoid overwriting live client state. Pass `activeSessionId` to protect active sessions.
|
||||
|
||||
For rebasing or finishing PRs written against the old Next.js runtime, follow
|
||||
[`docs/nextjs-spa-migration.md`](../../docs/nextjs-spa-migration.md).
|
||||
|
||||
**Hooks Pattern:** Hooks in `hooks/domains/` encapsulate WS subscription + store selection. WS client deduplicates subscriptions automatically.
|
||||
|
||||
@@ -71,6 +74,13 @@ Use subscription hooks only; the WS client auto-deduplicates.
|
||||
|
||||
## Component conventions
|
||||
|
||||
- **Framework adapters during Next removal:** Client components should import
|
||||
links, router hooks, dynamic imports, images, and theme hooks from the local
|
||||
adapter modules (`components/routing/*`, `lib/routing/*`,
|
||||
`components/theme/app-theme`) instead of importing `next/*` or
|
||||
`next-themes` directly. The routing/image/dynamic adapters now provide
|
||||
browser-native behavior for the Vite SPA while legacy Next entrypoints are
|
||||
phased out.
|
||||
- Components: <200 lines, extract to domain components, composition over props.
|
||||
- Hooks: domain-organized in `hooks/domains/`, encapsulate subscription + selection.
|
||||
- **Interactivity:** all buttons and links with actions must have `cursor-pointer` class.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { IconBrandGithub, IconMenu2 } from "@tabler/icons-react";
|
||||
import { Alert, AlertDescription } from "@kandev/ui/alert";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { IconBrandGitlab, IconMenu2 } from "@tabler/icons-react";
|
||||
import { Alert, AlertDescription } from "@kandev/ui/alert";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { IconTicket } from "@tabler/icons-react";
|
||||
import { Alert, AlertDescription } from "@kandev/ui/alert";
|
||||
|
||||
+5
-19
@@ -1,4 +1,3 @@
|
||||
import type { Metadata, Viewport } from "next";
|
||||
import "./globals.css";
|
||||
import { ThemeProvider } from "@/components/theme-provider";
|
||||
import { StateProvider } from "@/components/state-provider";
|
||||
@@ -19,12 +18,12 @@ import { SidebarViewsSyncBridge } from "@/components/sidebar-views-sync-bridge";
|
||||
import { LogBufferBridge } from "@/components/log-buffer-bridge";
|
||||
import { getFeatureFlagsAction, getRuntimeDebugModeAction } from "@/app/actions/features";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
export const metadata = {
|
||||
title: "Kandev - AI Kanban",
|
||||
description: "AI-powered workflow management for developers",
|
||||
};
|
||||
|
||||
export const viewport: Viewport = {
|
||||
export const viewport = {
|
||||
// Enable safe area insets for iOS devices (notch, home indicator)
|
||||
viewportFit: "cover",
|
||||
// Prevent iOS auto-zoom on input focus (for app-like experience)
|
||||
@@ -37,12 +36,7 @@ export default async function RootLayout({
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
// API port injection for dev mode (browser opens at web port, API on backend port).
|
||||
// In production single-port mode, this is not needed — the client uses
|
||||
// window.location.origin (same-origin, works for any domain / reverse proxy).
|
||||
const apiPort = process.env.NEXT_PUBLIC_KANDEV_API_PORT ?? null;
|
||||
const envDebugMode =
|
||||
process.env.KANDEV_DEBUG === "true" || process.env.NEXT_PUBLIC_KANDEV_DEBUG === "true";
|
||||
const envDebugMode = process.env.KANDEV_DEBUG === "true";
|
||||
|
||||
// SSR-fetch the deployment's feature flags so the entire client tree
|
||||
// (including the sidebar nav and gated routes) renders with the correct
|
||||
@@ -54,21 +48,13 @@ export default async function RootLayout({
|
||||
]);
|
||||
const debugMode = envDebugMode || runtimeDebugMode;
|
||||
|
||||
const runtimeConfigScript =
|
||||
apiPort || debugMode
|
||||
? [
|
||||
apiPort ? `window.__KANDEV_API_PORT = ${JSON.stringify(apiPort)};` : "",
|
||||
debugMode ? `window.__KANDEV_DEBUG = true;` : "",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n")
|
||||
: null;
|
||||
const runtimeConfigScript = debugMode ? "window.__KANDEV_DEBUG = true;" : null;
|
||||
|
||||
return (
|
||||
<html lang="en" suppressHydrationWarning>
|
||||
<head>
|
||||
<meta name="apple-mobile-web-app-title" content="Kandev" />
|
||||
{/* Inject runtime config before Next.js async chunks so debug UI flags
|
||||
{/* Inject runtime config before app chunks so debug UI flags
|
||||
are visible when client modules first evaluate. */}
|
||||
{runtimeConfigScript ? (
|
||||
<script dangerouslySetInnerHTML={{ __html: runtimeConfigScript }} />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { IconExternalLink, IconHexagon, IconPlus, IconSearch } from "@tabler/icons-react";
|
||||
import { Alert, AlertDescription } from "@kandev/ui/alert";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState, useCallback } from "react";
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { toast } from "sonner";
|
||||
import { Badge } from "@kandev/ui/badge";
|
||||
import { Button } from "@kandev/ui/button";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@kandev/ui/card";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@kandev/ui/table";
|
||||
import type { AgentCostAggregate, AgentRunCost } from "@/lib/api/domains/office-extended-api";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { IconChevronRight } from "@tabler/icons-react";
|
||||
import { Badge } from "@kandev/ui/badge";
|
||||
import { Card, CardContent } from "@kandev/ui/card";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { Badge } from "@kandev/ui/badge";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@kandev/ui/card";
|
||||
import type { AgentRecentTask } from "@/lib/api/domains/office-extended-api";
|
||||
|
||||
@@ -24,10 +24,9 @@ type Props = {
|
||||
* `agents` and `tasks` channels both impact the dashboard) — matching
|
||||
* the project's reactive-only convention.
|
||||
*
|
||||
* The chart components are pure SSR-safe presentational pieces; this
|
||||
* The chart components are pure route-safe presentational pieces; this
|
||||
* shell exists so a future "Refresh" / "Date range" UI has somewhere
|
||||
* to live without lifting state up into the parent page (which is a
|
||||
* Server Component and therefore can't hold state).
|
||||
* to live without lifting state up into the parent route loader.
|
||||
*/
|
||||
export function DashboardView({ agentId, initial, days }: Props) {
|
||||
const [summary, setSummary] = useState<AgentSummaryResponse>(initial);
|
||||
|
||||
@@ -7,15 +7,13 @@ type Props = { params: Promise<{ id: string }> };
|
||||
const DEFAULT_DAYS = 14;
|
||||
|
||||
/**
|
||||
* Server Component for `/office/agents/[id]/dashboard`. Fetches the
|
||||
* summary on the server so the chart values appear in the initial
|
||||
* HTML (no JS required for first paint), then hands the snapshot to
|
||||
* the Client Component for interactivity + WS-driven refetches.
|
||||
* Route loader for `/office/agents/[id]/dashboard`. Fetches the summary
|
||||
* before rendering, then hands the snapshot to the view for interactivity
|
||||
* and WS-driven refetches.
|
||||
*
|
||||
* Errors bubble up to the Next.js error boundary; we don't catch
|
||||
* them here. A 5xx from the backend produces an error page rather
|
||||
* than a half-empty dashboard, which matches the live-data nature
|
||||
* of the office surface.
|
||||
* Errors bubble up to the route error surface; we don't catch them here.
|
||||
* A 5xx from the backend produces an error page rather than a half-empty
|
||||
* dashboard, which matches the live-data nature of the office surface.
|
||||
*/
|
||||
export default async function AgentDashboardPage({ params }: Props) {
|
||||
const { id } = await params;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { use, useCallback, useEffect, type ReactNode } from "react";
|
||||
import Link from "next/link";
|
||||
import { usePathname } from "next/navigation";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { usePathname } from "@/lib/routing/client-router";
|
||||
import { IconInfoCircle } from "@tabler/icons-react";
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { redirect } from "@/lib/routing/server-navigation";
|
||||
|
||||
type AgentDetailPageProps = {
|
||||
params: Promise<{ id: string }>;
|
||||
|
||||
@@ -5,11 +5,10 @@ import { RunDetailView } from "./run-detail-view";
|
||||
type Props = { params: Promise<{ id: string; runId: string }> };
|
||||
|
||||
/**
|
||||
* Run detail Server Component — parallel-fetches the run aggregate
|
||||
* and the recent-runs sidebar window so the page returns with both
|
||||
* the main panel and the sidebar populated. Errors bubble to the
|
||||
* Next.js error boundary; live-mode WS subscription is added by
|
||||
* Wave 2.E inside the Client Component.
|
||||
* Run detail route loader: parallel-fetches the run aggregate and the
|
||||
* recent-runs sidebar window so the page returns with both the main panel
|
||||
* and the sidebar populated. Errors bubble to the route error surface;
|
||||
* live-mode WS subscription is added by the view.
|
||||
*/
|
||||
export default async function AgentRunDetailPage({ params }: Props) {
|
||||
const { id, runId } = await params;
|
||||
|
||||
@@ -21,13 +21,13 @@ type Props = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Run detail client shell. The Server Component delivers `initial`
|
||||
* (the run aggregate) and `recent` (the sidebar window) in one
|
||||
* round-trip; this component owns interactivity (collapsibles,
|
||||
* action buttons, the embedded conversation). While the run is
|
||||
* `claimed`, `useRunLiveSync` subscribes to `run.subscribe` over the
|
||||
* WS and feeds appended events into the EventsLog plus updates the
|
||||
* status badge on terminal events — no whole-snapshot refetch.
|
||||
* Run detail client shell. The route loader delivers `initial` (the run
|
||||
* aggregate) and `recent` (the sidebar window) in one round-trip; this
|
||||
* component owns interactivity (collapsibles, action buttons, the embedded
|
||||
* conversation). While the run is `claimed`, `useRunLiveSync` subscribes to
|
||||
* `run.subscribe` over the WS and feeds appended events into the EventsLog
|
||||
* plus updates the status badge on terminal events, with no whole-snapshot
|
||||
* refetch.
|
||||
*/
|
||||
export function RunDetailView({ agentId, initial, recent }: Props) {
|
||||
const taskId = initial.task_id ?? "";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { usePathname } from "next/navigation";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { usePathname } from "@/lib/routing/client-router";
|
||||
import { IconLoader2 } from "@tabler/icons-react";
|
||||
import { Badge } from "@kandev/ui/badge";
|
||||
import { cn } from "@/lib/utils";
|
||||
@@ -36,8 +36,8 @@ function deriveActiveRunId(pathname: string | null, fallback: string): string {
|
||||
* navigation between sibling runs updates the highlight without
|
||||
* re-rendering the parent.
|
||||
* - In-progress (`claimed`) rows show an animated spinner icon.
|
||||
* - Each row is a Next.js `<Link>` with `aria-current="page"` on the
|
||||
* active one so it's keyboard-navigable + screen-reader friendly.
|
||||
* - Each row is an app link with `aria-current="page"` on the active one
|
||||
* so it's keyboard-navigable + screen-reader friendly.
|
||||
*/
|
||||
export function RecentRunsSidebar({ runs, agentId, activeRunId }: Props) {
|
||||
const pathname = usePathname();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@kandev/ui/card";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@kandev/ui/table";
|
||||
|
||||
@@ -5,9 +5,9 @@ import { RunsListView } from "./runs-list-view";
|
||||
type Props = { params: Promise<{ id: string }> };
|
||||
|
||||
/**
|
||||
* Per-agent paginated runs list. Server Component fetches page 1 via
|
||||
* the SSR helper and hands the result to the client view; subsequent
|
||||
* pages are loaded on click via `listAgentRuns`.
|
||||
* Per-agent paginated runs list. The route loader fetches page 1 and
|
||||
* hands the result to the view; subsequent pages are loaded on click via
|
||||
* `listAgentRuns`.
|
||||
*/
|
||||
export default async function AgentRunsPage({ params }: Props) {
|
||||
const { id } = await params;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import {
|
||||
IconClock,
|
||||
IconLoader2,
|
||||
@@ -49,9 +49,9 @@ function formatReason(reason: string): string {
|
||||
|
||||
/**
|
||||
* Client-side runs list with cursor-based "Load more" pagination.
|
||||
* The Server Component delivers page 1; subsequent pages are
|
||||
* fetched via `listAgentRuns` and appended to a flat array. Scroll
|
||||
* position is preserved when a new page is appended.
|
||||
* The route loader delivers page 1; subsequent pages are fetched via
|
||||
* `listAgentRuns` and appended to a flat array. Scroll position is
|
||||
* preserved when a new page is appended.
|
||||
*/
|
||||
export function RunsListView({ initial, agentId }: Props) {
|
||||
const [pages, setPages] = useState<AgentRunsListPage[]>([initial]);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { Badge } from "@kandev/ui/badge";
|
||||
import { Card, CardContent } from "@kandev/ui/card";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, afterEach, vi } from "vitest";
|
||||
import { render, screen, cleanup, within } from "@testing-library/react";
|
||||
|
||||
vi.mock("next/link", () => ({
|
||||
vi.mock("@/components/routing/app-link", () => ({
|
||||
default: ({ children, href, ...rest }: { children: React.ReactNode; href: string }) => (
|
||||
<a href={href} {...rest}>
|
||||
{children}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { memo } from "react";
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { IconExternalLink, IconLoader2 } from "@tabler/icons-react";
|
||||
import { Card } from "@kandev/ui/card";
|
||||
import type { AgentSummary, SessionSummary } from "@/lib/api/domains/office-api";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { usePathname } from "next/navigation";
|
||||
import { usePathname } from "@/lib/routing/client-router";
|
||||
|
||||
const PAGE_TITLES: Record<string, string> = {
|
||||
"/office": "Dashboard",
|
||||
@@ -27,7 +27,7 @@ function resolveTitle(pathname: string): string | null {
|
||||
function isDetailPage(pathname: string): boolean {
|
||||
return (
|
||||
/^\/office\/tasks\/[^/]+$/.test(pathname) ||
|
||||
/^\/office\/agents\/[^/]+$/.test(pathname) ||
|
||||
/^\/office\/agents\/[^/]+(?:\/.*)?$/.test(pathname) ||
|
||||
/^\/office\/projects\/[^/]+$/.test(pathname) ||
|
||||
/^\/office\/routines\/[^/]+$/.test(pathname)
|
||||
);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { Card } from "@kandev/ui/card";
|
||||
import { Badge } from "@kandev/ui/badge";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import Link from "@/components/routing/app-link";
|
||||
import { useState } from "react";
|
||||
import {
|
||||
IconShieldCheck,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { notFound } from "next/navigation";
|
||||
import { cookies } from "next/headers";
|
||||
import { notFound } from "@/lib/routing/server-navigation";
|
||||
import { TooltipProvider } from "@kandev/ui/tooltip";
|
||||
import { StateHydrator } from "@/components/state-hydrator";
|
||||
import { getFeatureFlagsAction } from "@/app/actions/features";
|
||||
@@ -12,6 +11,7 @@ import {
|
||||
listProjects,
|
||||
} from "@/lib/api/domains/office-api";
|
||||
import { mapUserSettingsResponse } from "@/lib/ssr/user-settings";
|
||||
import { readCookies } from "@/lib/server/cookies";
|
||||
import type { AppState } from "@/lib/state/store";
|
||||
import { OfficeTopbar } from "./components/office-topbar";
|
||||
|
||||
@@ -82,7 +82,7 @@ export default async function OfficeLayout({ children }: { children: React.React
|
||||
listWorkspaces({ cache: "no-store" }).catch(() => ({ workspaces: [] })),
|
||||
fetchUserSettings({ cache: "no-store" }).catch(() => null),
|
||||
getMeta({ cache: "no-store" }).catch(() => null),
|
||||
cookies().catch(() => null),
|
||||
readCookies().catch(() => null),
|
||||
]);
|
||||
|
||||
// Only office workspaces can be active inside /office, but hydrate every
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
|
||||
vi.mock("next/headers", () => ({
|
||||
cookies: vi.fn(),
|
||||
vi.mock("@/lib/server/cookies", () => ({
|
||||
readCookies: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/api", () => ({
|
||||
@@ -13,12 +13,12 @@ vi.mock("@/lib/api/domains/settings-api", () => ({
|
||||
updateUserSettings: vi.fn(),
|
||||
}));
|
||||
|
||||
import { cookies } from "next/headers";
|
||||
import { readCookies } from "@/lib/server/cookies";
|
||||
import { listWorkspaces, fetchUserSettings } from "@/lib/api";
|
||||
import { updateUserSettings } from "@/lib/api/domains/settings-api";
|
||||
import { getActiveWorkspaceId } from "./get-active-workspace";
|
||||
|
||||
const mockCookies = vi.mocked(cookies);
|
||||
const mockReadCookies = vi.mocked(readCookies);
|
||||
const mockListWorkspaces = vi.mocked(listWorkspaces);
|
||||
const mockFetchUserSettings = vi.mocked(fetchUserSettings);
|
||||
const mockUpdateUserSettings = vi.mocked(updateUserSettings);
|
||||
@@ -42,13 +42,13 @@ function makeCookieStore(workspaceId: string | null) {
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
mockCookies.mockReset();
|
||||
mockReadCookies.mockReset();
|
||||
mockListWorkspaces.mockReset();
|
||||
mockFetchUserSettings.mockReset();
|
||||
mockUpdateUserSettings.mockReset();
|
||||
mockUpdateUserSettings.mockResolvedValue({} as never);
|
||||
// Default: no cookie set
|
||||
mockCookies.mockResolvedValue(makeCookieStore(null) as never);
|
||||
mockReadCookies.mockResolvedValue(makeCookieStore(null) as never);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -81,7 +81,7 @@ describe("getActiveWorkspaceId", () => {
|
||||
|
||||
describe("cookie wins after URL param misses", () => {
|
||||
it("returns the cookie workspace and does NOT call updateUserSettings", async () => {
|
||||
mockCookies.mockResolvedValue(makeCookieStore(OFFICE_WS_ID_2) as never);
|
||||
mockReadCookies.mockResolvedValue(makeCookieStore(OFFICE_WS_ID_2) as never);
|
||||
mockListWorkspaces.mockResolvedValue({ workspaces: [OFFICE_WS, OFFICE_WS_2] } as never);
|
||||
mockFetchUserSettings.mockResolvedValue(makeSettings(KANBAN_WS_ID) as never);
|
||||
|
||||
@@ -92,7 +92,7 @@ describe("getActiveWorkspaceId", () => {
|
||||
});
|
||||
|
||||
it("ignores cookie when it does not match any office workspace", async () => {
|
||||
mockCookies.mockResolvedValue(makeCookieStore("ws-stale") as never);
|
||||
mockReadCookies.mockResolvedValue(makeCookieStore("ws-stale") as never);
|
||||
mockListWorkspaces.mockResolvedValue({ workspaces: [OFFICE_WS] } as never);
|
||||
mockFetchUserSettings.mockResolvedValue(makeSettings(OFFICE_WS_ID) as never);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { listWorkspaces, fetchUserSettings } from "@/lib/api";
|
||||
import { readCookies } from "@/lib/server/cookies";
|
||||
|
||||
/**
|
||||
* Server-side helper to resolve the active workspace ID.
|
||||
@@ -19,7 +19,7 @@ export async function getActiveWorkspaceId(urlWorkspaceId?: string): Promise<str
|
||||
const [workspacesRes, settingsRes, cookieStore] = await Promise.all([
|
||||
listWorkspaces({ cache: "no-store" }).catch(() => ({ workspaces: [] })),
|
||||
fetchUserSettings({ cache: "no-store" }).catch(() => null),
|
||||
cookies().catch(() => null),
|
||||
readCookies().catch(() => null),
|
||||
]);
|
||||
|
||||
// Only consider office workspaces (those with office_workflow_id set).
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import { cleanup, render, waitFor } from "@testing-library/react";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { DashboardData } from "@/lib/state/slices/office/types";
|
||||
|
||||
const getDashboardMock = vi.hoisted(() => vi.fn());
|
||||
const setDashboardMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
const state = {
|
||||
workspaces: { activeId: "workspace-1" },
|
||||
office: {
|
||||
dashboard: null as DashboardData | null,
|
||||
agentProfiles: [],
|
||||
routing: {
|
||||
byWorkspace: {},
|
||||
knownProviders: [],
|
||||
preview: { byWorkspace: {} },
|
||||
},
|
||||
providerHealth: { byWorkspace: {} },
|
||||
},
|
||||
setDashboard: setDashboardMock,
|
||||
};
|
||||
|
||||
vi.mock("@/components/state-provider", () => ({
|
||||
useAppStore: (selector: (s: typeof state) => unknown) => selector(state),
|
||||
}));
|
||||
|
||||
vi.mock("@/hooks/use-office-refetch", () => ({
|
||||
useOfficeRefetch: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/api/domains/office-api", () => ({
|
||||
getDashboard: getDashboardMock,
|
||||
}));
|
||||
|
||||
import { OfficePageClient } from "./page-client";
|
||||
|
||||
function dashboard(): DashboardData {
|
||||
return {
|
||||
agent_count: 1,
|
||||
running_count: 0,
|
||||
paused_count: 0,
|
||||
error_count: 0,
|
||||
tasks_in_progress: 0,
|
||||
open_tasks: 0,
|
||||
blocked_tasks: 0,
|
||||
month_spend_subcents: 0,
|
||||
pending_approvals: 0,
|
||||
recent_activity: [],
|
||||
task_count: 2,
|
||||
skill_count: 3,
|
||||
routine_count: 4,
|
||||
run_activity: [],
|
||||
task_breakdown: { open: 0, in_progress: 0, blocked: 0, done: 0 },
|
||||
recent_tasks: [],
|
||||
agent_summaries: [],
|
||||
};
|
||||
}
|
||||
|
||||
describe("OfficePageClient boot hydration", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
state.workspaces.activeId = "workspace-1";
|
||||
state.office.dashboard = null;
|
||||
});
|
||||
|
||||
it("does not fetch dashboard data when Go boot state already hydrated it", async () => {
|
||||
state.office.dashboard = dashboard();
|
||||
|
||||
render(<OfficePageClient initialDashboard={null} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getDashboardMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("fetches dashboard data when neither SSR props nor boot state provided it", async () => {
|
||||
const data = dashboard();
|
||||
getDashboardMock.mockResolvedValue(data);
|
||||
|
||||
render(<OfficePageClient initialDashboard={null} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getDashboardMock).toHaveBeenCalledWith("workspace-1");
|
||||
});
|
||||
await waitFor(() => {
|
||||
expect(setDashboardMock).toHaveBeenCalledWith(data);
|
||||
});
|
||||
});
|
||||
|
||||
it("refetches dashboard data when the active workspace changes", async () => {
|
||||
state.office.dashboard = dashboard();
|
||||
getDashboardMock.mockResolvedValue({ ...dashboard(), agent_count: 2 });
|
||||
|
||||
const { rerender } = render(<OfficePageClient initialDashboard={null} />);
|
||||
|
||||
expect(getDashboardMock).not.toHaveBeenCalled();
|
||||
|
||||
state.workspaces.activeId = "workspace-2";
|
||||
rerender(<OfficePageClient initialDashboard={null} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getDashboardMock).toHaveBeenCalledWith("workspace-2");
|
||||
});
|
||||
});
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user