f87b954f27
- internal/extsvc: circuit breaker (§11.16) + retrying bearer JSON client - atomizer client: §5.1 contract incl. defensive coefficient normalization and extension coefficient range (0,2] - persisted jobs collection: atomic claim, panic recovery, exponential backoff (max 3), stale-running requeue, shutdown-safe bookkeeping - subdivide (async 202, atomizing status, re-run replaces unpublished children after confirm) and extend (sibling task, source budget) - failure path reverts status and notifies the consultant on final attempt - PATCH task editing with bounty recompute, budget cascade to descendants - publish single + bulk; task detail endpoint role-scoped - coder/websocket hub: multiplexed channels, origin check, 30s heartbeats; client ws.js with reconnect + 15s polling fallback - consultant atomization board: tree by root, coefficient sliders with live per-parent sum indicator, bounty preview, modals, shimmer, atomizer-down gating via /api/v1/service-health - fix: tasks external-ref unique index is partial, not sparse (compound sparse indexed every task through customerId and broke child inserts) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
82 lines
3.5 KiB
Go
82 lines
3.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
)
|
|
|
|
// EnsureIndexes creates every index from spec §4.9. CreateMany is idempotent:
|
|
// existing identical indexes are no-ops, so this runs safely at every startup.
|
|
func EnsureIndexes(ctx context.Context, db *mongo.Database) error {
|
|
plan := map[string][]mongo.IndexModel{
|
|
"users": {
|
|
{Keys: bson.D{{Key: "email", Value: 1}}, Options: options.Index().SetUnique(true)},
|
|
{Keys: bson.D{{Key: "auth.oidc.issuer", Value: 1}, {Key: "auth.oidc.subject", Value: 1}},
|
|
Options: options.Index().SetUnique(true).SetSparse(true)},
|
|
},
|
|
"customers": {
|
|
{Keys: bson.D{{Key: "name", Value: 1}}, Options: options.Index().SetUnique(true)},
|
|
{Keys: bson.D{{Key: "consultantIds", Value: 1}}},
|
|
},
|
|
"pools": {
|
|
{Keys: bson.D{{Key: "consultantId", Value: 1}, {Key: "developerId", Value: 1}},
|
|
Options: options.Index().SetUnique(true)},
|
|
{Keys: bson.D{{Key: "developerId", Value: 1}}},
|
|
},
|
|
"tasks": {
|
|
{Keys: bson.D{{Key: "customerId", Value: 1}, {Key: "status", Value: 1}, {Key: "updatedAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "consultantId", Value: 1}, {Key: "status", Value: 1}, {Key: "updatedAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "assignee.userId", Value: 1}, {Key: "status", Value: 1}}},
|
|
{Keys: bson.D{{Key: "rootId", Value: 1}}},
|
|
{Keys: bson.D{{Key: "parentId", Value: 1}}},
|
|
// The spec calls this "unique sparse", but a compound sparse index
|
|
// matches any doc where customerId exists (i.e. every task), which
|
|
// breaks subdivided children. A partial index expresses the intent:
|
|
// uniqueness only for tasks that actually have an external ref.
|
|
{Keys: bson.D{{Key: "external.system", Value: 1}, {Key: "external.key", Value: 1}, {Key: "customerId", Value: 1}},
|
|
Options: options.Index().SetUnique(true).SetName("uniq_external_ref").
|
|
SetPartialFilterExpression(bson.M{"external.key": bson.M{"$exists": true}})},
|
|
{Keys: bson.D{{Key: "title", Value: "text"}, {Key: "description", Value: "text"}}},
|
|
},
|
|
"bountyAwards": {
|
|
{Keys: bson.D{{Key: "developerId", Value: 1}, {Key: "awardedAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "consultantId", Value: 1}, {Key: "awardedAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "customerId", Value: 1}, {Key: "awardedAt", Value: -1}}},
|
|
},
|
|
"conversations": {
|
|
{Keys: bson.D{{Key: "participantIds", Value: 1}, {Key: "lastMessageAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "kind", Value: 1}, {Key: "customerId", Value: 1}}},
|
|
},
|
|
"messages": {
|
|
// ULID _id is time-ordered, so this serves history pagination.
|
|
{Keys: bson.D{{Key: "conversationId", Value: 1}, {Key: "_id", Value: 1}}},
|
|
},
|
|
"notifications": {
|
|
{Keys: bson.D{{Key: "userId", Value: 1}, {Key: "readAt", Value: 1}, {Key: "createdAt", Value: -1}}},
|
|
},
|
|
"sessions": {
|
|
{Keys: bson.D{{Key: "expiresAt", Value: 1}}, Options: options.Index().SetExpireAfterSeconds(0)},
|
|
{Keys: bson.D{{Key: "userId", Value: 1}}},
|
|
},
|
|
"auditLog": {
|
|
{Keys: bson.D{{Key: "at", Value: -1}}},
|
|
{Keys: bson.D{{Key: "entityId", Value: 1}, {Key: "at", Value: -1}}},
|
|
},
|
|
}
|
|
|
|
// migration: an earlier build created the external-ref index as sparse
|
|
// under the auto-generated name; drop it so the partial variant applies
|
|
_ = db.Collection("tasks").Indexes().DropOne(ctx, "external.system_1_external.key_1_customerId_1")
|
|
|
|
for coll, models := range plan {
|
|
if _, err := db.Collection(coll).Indexes().CreateMany(ctx, models); err != nil {
|
|
return fmt.Errorf("create indexes for %s: %w", coll, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|