Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(issues): bump issue updated_at when a comment is added (MUL-5009) (#5667)

Bohan Jiang9961db15ed63
diff --git a/server/internal/daemon/gc.go b/server/internal/daemon/gc.go
index 6010b3c8c6..374973233a 100644
--- a/server/internal/daemon/gc.go
+++ b/server/internal/daemon/gc.go
@@ -282,10 +282,11 @@ const (
// follow the parent record that actually governs their lifecycle.
func (d *Daemon) shouldCleanTaskDir(ctx context.Context, taskDir string) gcAction {
// A task currently running on this env root must never be reclaimed —
- // not even on the done/cancelled or orphan-404 paths. A new comment on
- // an already-done issue can dispatch a follow-up task that reuses the
- // prior workdir without bumping the issue's updated_at, so the regular
- // TTL check alone wouldn't notice the resumed activity.
+ // not even on the done/cancelled or orphan-404 paths. A re-dispatched or
+ // still-running task can reuse the prior workdir of an already-done issue
+ // whose updated_at is older than the TTL (a task re-claim doesn't advance
+ // updated_at), so the regular TTL check alone wouldn't notice the resumed
+ // activity.
if d.isActiveEnvRoot(taskDir) {
return gcActionSkip
}
diff --git a/server/internal/daemon/gc_test.go b/server/internal/daemon/gc_test.go
index 12939b5415..d594f8b3a0 100644
--- a/server/internal/daemon/gc_test.go
+++ b/server/internal/daemon/gc_test.go
@@ -631,9 +631,10 @@ func TestShouldCleanTaskDir_ActiveEnvRootSkipsFullCleanup(t *testing.T) {
mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Done long enough ago to satisfy GCTTL — this would normally return
- // gcActionClean. But the env root is in use (e.g. follow-up comment
- // dispatched a task that reuses the prior workdir), and CreateComment
- // does not bump issue.updated_at. Active-root guard must override.
+ // gcActionClean. But the env root is in use (e.g. a re-dispatched task
+ // reuses the prior workdir of an already-done issue whose updated_at is
+ // still stale, since a task re-claim doesn't advance it). Active-root
+ // guard must override.
json.NewEncoder(w).Encode(map[string]any{
"status": "done",
"updated_at": time.Now().Add(-30 * 24 * time.Hour),
diff --git a/server/internal/handler/comment_touch_issue_test.go b/server/internal/handler/comment_touch_issue_test.go
new file mode 100644
index 0000000000..8b645a3469
--- /dev/null
+++ b/server/internal/handler/comment_touch_issue_test.go
@@ -0,0 +1,107 @@
+package handler
+
+import (
+ "context"
+ "errors"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+
+ "github.com/jackc/pgx/v5"
+
+ db "github.com/multica-ai/multica/server/pkg/db/generated"
+)
+
+// TestCreateComment_BumpsIssueUpdatedAt pins MUL-5009: a new comment counts as
+// activity on the issue, so updated_at advances. This is what lets the
+// "Updated date" Kanban/list sort surface recently-discussed cards, not only
+// cards whose status changed.
+func TestCreateComment_BumpsIssueUpdatedAt(t *testing.T) {
+ if testHandler == nil || testPool == nil {
+ t.Skip("database not available")
+ }
+ ctx := context.Background()
+
+ issueID := createCommentTriggerPreviewIssue(t, "comment bumps updated_at", "", "")
+
+ var before time.Time
+ if err := testPool.QueryRow(ctx, `SELECT updated_at FROM issue WHERE id = $1`, issueID).Scan(&before); err != nil {
+ t.Fatalf("read updated_at before: %v", err)
+ }
+
+ // Guarantee a measurable wall-clock gap so the bump is unambiguous; now() is
+ // evaluated per statement and the issue was inserted moments ago.
+ time.Sleep(10 * time.Millisecond)
+
+ w := httptest.NewRecorder()
+ r := withURLParam(
+ newRequest(http.MethodPost, "/api/issues/"+issueID+"/comments", map[string]any{"content": "a fresh comment"}),
+ "id", issueID,
+ )
+ testHandler.CreateComment(w, r)
+ if w.Code != http.StatusCreated {
+ t.Fatalf("CreateComment: expected 201, got %d: %s", w.Code, w.Body.String())
+ }
+
+ var after time.Time
+ if err := testPool.QueryRow(ctx, `SELECT updated_at FROM issue WHERE id = $1`, issueID).Scan(&after); err != nil {
+ t.Fatalf("read updated_at after: %v", err)
+ }
+
+ if !after.After(before) {
+ t.Fatalf("issue updated_at was not bumped by a new comment: before=%s after=%s", before, after)
+ }
+}
+
+// TestCreateComment_WorkspaceMismatchPersistsNothing pins the tenant-integrity
+// guarantee of the CreateComment CTE (MUL-5009 nit2): CreateComment is the
+// single carrier of "a comment always belongs to an issue in the same
+// workspace and always bumps it". If the passed workspace does not match the
+// target issue's workspace, the leading UPDATE matches no issue row, the
+// dependent INSERT selects nothing, and the :one query returns pgx.ErrNoRows —
+// so no mis-attributed comment is written and the issue is not touched.
+func TestCreateComment_WorkspaceMismatchPersistsNothing(t *testing.T) {
+ if testHandler == nil || testPool == nil {
+ t.Skip("database not available")
+ }
+ ctx := context.Background()
+
+ issueID := createCommentTriggerPreviewIssue(t, "comment workspace guard", "", "")
+
+ var before time.Time
+ if err := testPool.QueryRow(ctx, `SELECT updated_at FROM issue WHERE id = $1`, issueID).Scan(&before); err != nil {
+ t.Fatalf("read updated_at before: %v", err)
+ }
+
+ // A workspace that is NOT the issue's workspace: the issue exists, but the
+ // (issue, workspace) pair matches no row.
+ wrongWorkspace := parseUUID("11111111-1111-1111-1111-111111111111")
+ _, err := testHandler.Queries.CreateComment(ctx, db.CreateCommentParams{
+ IssueID: parseUUID(issueID),
+ WorkspaceID: wrongWorkspace,
+ AuthorType: "member",
+ AuthorID: parseUUID(testUserID),
+ Content: "should never persist",
+ Type: "comment",
+ })
+ if !errors.Is(err, pgx.ErrNoRows) {
+ t.Fatalf("expected pgx.ErrNoRows on workspace mismatch, got %v", err)
+ }
+
+ var commentCount int
+ if err := testPool.QueryRow(ctx, `SELECT count(*) FROM comment WHERE issue_id = $1`, issueID).Scan(&commentCount); err != nil {
+ t.Fatalf("count comments: %v", err)
+ }
+ if commentCount != 0 {
+ t.Fatalf("workspace mismatch must persist no comment, found %d", commentCount)
+ }
+
+ var after time.Time
+ if err := testPool.QueryRow(ctx, `SELECT updated_at FROM issue WHERE id = $1`, issueID).Scan(&after); err != nil {
+ t.Fatalf("read updated_at after: %v", err)
+ }
+ if !after.Equal(before) {
+ t.Fatalf("workspace mismatch must not bump updated_at: before=%s after=%s", before, after)
+ }
+}
diff --git a/server/pkg/db/generated/comment.sql.go b/server/pkg/db/generated/comment.sql.go
index 2d7f601a3c..b605e3e2a0 100644
--- a/server/pkg/db/generated/comment.sql.go
+++ b/server/pkg/db/generated/comment.sql.go
@@ -155,32 +155,54 @@ func (q *Queries) CountNewCommentsSince(ctx context.Context, arg CountNewComment
}
const createComment = `-- name: CreateComment :one
+WITH touched_issue AS (
+ UPDATE issue SET updated_at = now()
+ WHERE issue.id = $7 AND issue.workspace_id = $8
+ RETURNING issue.id, issue.workspace_id
+)
INSERT INTO comment (issue_id, workspace_id, author_type, author_id, content, type, parent_id, source_task_id)
-VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
+SELECT ti.id, ti.workspace_id, $1, $2, $3, $4, $5, $6
+FROM touched_issue ti
RETURNING id, issue_id, author_type, author_id, content, type, created_at, updated_at, parent_id, workspace_id, resolved_at, resolved_by_type, resolved_by_id, source_task_id
`
type CreateCommentParams struct {
- IssueID pgtype.UUID `json:"issue_id"`
- WorkspaceID pgtype.UUID `json:"workspace_id"`
AuthorType string `json:"author_type"`
AuthorID pgtype.UUID `json:"author_id"`
Content string `json:"content"`
Type string `json:"type"`
ParentID pgtype.UUID `json:"parent_id"`
SourceTaskID pgtype.UUID `json:"source_task_id"`
+ IssueID pgtype.UUID `json:"issue_id"`
+ WorkspaceID pgtype.UUID `json:"workspace_id"`
}
+// A new comment counts as activity on its issue, so the same statement bumps
+// the parent issue's updated_at. The touch is a leading data-modifying CTE and
+// the INSERT selects the issue/workspace back out of it, which makes the two
+// inseparable and gives two query-level guarantees:
+// - atomicity — the insert and the timestamp bump commit or roll back
+// together, so an issue is never left with a stale updated_at after a
+// comment persists; and
+// - tenant integrity — the comment can only be created against an issue that
+// actually exists in the given workspace. A mismatched (issue, workspace)
+// pair matches 0 rows in the CTE, the dependent INSERT then selects nothing,
+// and the :one query returns pgx.ErrNoRows. A wrong workspace can therefore
+// never leave a mis-attributed comment or a silently un-touched issue.
+//
+// Centralizing this here means every comment entrypoint inherits both
+// guarantees regardless of what a caller passes. The "Updated date" sort and
+// the daemon GC TTL both read updated_at, so this consistency is load-bearing.
func (q *Queries) CreateComment(ctx context.Context, arg CreateCommentParams) (Comment, error) {
row := q.db.QueryRow(ctx, createComment,
- arg.IssueID,
- arg.WorkspaceID,
arg.AuthorType,
arg.AuthorID,
arg.Content,
arg.Type,
arg.ParentID,
arg.SourceTaskID,
+ arg.IssueID,
+ arg.WorkspaceID,
)
var i Comment
err := row.Scan(
diff --git a/server/pkg/db/queries/comment.sql b/server/pkg/db/queries/comment.sql
index 5c045d9378..ff6132fef8 100644
--- a/server/pkg/db/queries/comment.sql
+++ b/server/pkg/db/queries/comment.sql
@@ -393,8 +393,29 @@ SELECT c.* FROM comment c
WHERE c.id = (SELECT id FROM root_of WHERE parent_id IS NULL LIMIT 1);
-- name: CreateComment :one
+-- A new comment counts as activity on its issue, so the same statement bumps
+-- the parent issue's updated_at. The touch is a leading data-modifying CTE and
+-- the INSERT selects the issue/workspace back out of it, which makes the two
+-- inseparable and gives two query-level guarantees:
+-- * atomicity — the insert and the timestamp bump commit or roll back
+-- together, so an issue is never left with a stale updated_at after a
+-- comment persists; and
+-- * tenant integrity — the comment can only be created against an issue that
+-- actually exists in the given workspace. A mismatched (issue, workspace)
+-- pair matches 0 rows in the CTE, the dependent INSERT then selects nothing,
+-- and the :one query returns pgx.ErrNoRows. A wrong workspace can therefore
+-- never leave a mis-attributed comment or a silently un-touched issue.
+-- Centralizing this here means every comment entrypoint inherits both
+-- guarantees regardless of what a caller passes. The "Updated date" sort and
+-- the daemon GC TTL both read updated_at, so this consistency is load-bearing.
+WITH touched_issue AS (
+ UPDATE issue SET updated_at = now()
+ WHERE issue.id = sqlc.arg(issue_id) AND issue.workspace_id = sqlc.arg(workspace_id)
+ RETURNING issue.id, issue.workspace_id
+)
INSERT INTO comment (issue_id, workspace_id, author_type, author_id, content, type, parent_id, source_task_id)
-VALUES ($1, $2, $3, $4, $5, $6, sqlc.narg(parent_id), sqlc.narg(source_task_id))
+SELECT ti.id, ti.workspace_id, sqlc.arg(author_type), sqlc.arg(author_id), sqlc.arg(content), sqlc.arg(type), sqlc.narg(parent_id), sqlc.narg(source_task_id)
+FROM touched_issue ti
RETURNING *;
-- name: UpdateComment :one