Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs-site/src/content/docs/reference/experiments.md

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions pkg/experiments/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,12 @@ const Triggers = "hub.triggers"

// MessagePolicies gates project-scoped message admission policies
// (ptone/scion#8047): /api/v1/projects/{id}/message-policies[/{policyId}[/test|/decisions]],
// scion message-policy create|list|get|delete|test|decisions, and the H2
// outbound agent message gate (agent->user DMs, agent conversation posts and
// choice prompts; shadow and enforce modes). While it is off, the message
// policy routes answer 404, the outbound gate evaluates and records nothing,
// and stored policies and decision records are kept.
// scion message-policy create|update|list|get|delete|test|decisions, and the outbound
// agent message gate (agent_to_user, agent_to_conversation, agent_dm,
// mention_delivery; shadow and enforce modes). Server layer: while it is off,
// the message policy routes answer 404, the outbound gate evaluates and records
// nothing, and stored policies and decision records are kept. Web layer:
// exposes the flag for UI presentation.
const MessagePolicies = "hub.message_policies"

// EffectivePermissions gates the admin effective-permissions viewer
Expand Down Expand Up @@ -1432,9 +1433,9 @@ var compiled = []Experiment{
{
Name: MessagePolicies,
Title: "Project message policies",
Description: "Enables project-scoped CEL message policies on outbound agent messages to users, conversations and choice prompts (/api/v1/projects/{id}/message-policies[/{policyId}[/test|/decisions]] and scion message-policy create|list|get|delete|test|decisions), with shadow and enforce modes and content-free decision records. Agent-to-agent DMs, group-set fan-out and mention deliveries are not covered yet and are reported in coverage.uncovered_agent_paths. While off, the routes answer 404, the gate evaluates nothing and stored policies and decisions are kept.",
Description: "Enables project-scoped message policies on outbound agent messages to users, conversations, choice prompts, agent-to-agent DMs, and mention deliveries (agent_to_user, agent_to_conversation, agent_dm, mention_delivery) (/api/v1/projects/{id}/message-policies[/{policyId}[/test|/decisions]] and scion message-policy), with shadow and enforce modes and content-free decision records. Uncovered agent paths (group_post, group_set, mention_fanout, broadcast) are reported in coverage.uncovered_agent_paths. Server layer: while off, the routes answer 404, the gate evaluates nothing and stored policies and decisions are kept. Web layer: exposes the flag for UI presentation.",
Default: false,
Layers: []Layer{LayerServer},
Layers: []Layer{LayerServer, LayerWeb},
Stage: StageAlpha,
Issue: "ptone/scion#8047",
Owner: "system-one",
Expand Down
6 changes: 3 additions & 3 deletions pkg/experiments/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ func TestTriggersExperimentRegistered(t *testing.T) {
}

// TestMessagePoliciesExperimentRegistered pins hub.message_policies
// (ptone/scion#8047): server-layer only and ships off by default.
// (ptone/scion#8047): server+web layers and ships off by default.
func TestMessagePoliciesExperimentRegistered(t *testing.T) {
e, ok := Default().Lookup(MessagePolicies)
if !ok {
Expand All @@ -959,8 +959,8 @@ func TestMessagePoliciesExperimentRegistered(t *testing.T) {
if e.Default {
t.Fatal("hub.message_policies must default to off")
}
if !e.HasLayer(LayerServer) || e.HasLayer(LayerWeb) {
t.Fatalf("must be a server-layer (only) experiment, got %v", e.Layers)
if !e.HasLayer(LayerServer) || !e.HasLayer(LayerWeb) {
t.Fatalf("must have both LayerServer and LayerWeb, got %v", e.Layers)
}
if e.Issue != "ptone/scion#8047" || e.Stage != StageAlpha || e.Owner != "system-one" {
t.Fatalf("unexpected registration: %+v", e)
Expand Down
120 changes: 120 additions & 0 deletions pkg/hub/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,3 +957,123 @@ func TestResourceBuilders(t *testing.T) {
assert.Equal(t, Resource{}, gcpServiceAccountResource(nil))
})
}

// TestProjectCapabilities_MessagePolicyAndSystemOneActions pins the exact
// project _capabilities.actions names and built-in role mapping relied on by
// the message-policy and System One project UI (A5).
func TestProjectCapabilities_MessagePolicyAndSystemOneActions(t *testing.T) {
const (
actionManagePolicies = "manage_message_policies"
actionReadPolicies = "read_message_policies"
actionManageS1 = "manage_system_one_instances"
)

// Pin the exact action names on ResourceActions["project"].
assert.Contains(t, ResourceActions["project"], Action(actionManagePolicies))
assert.Contains(t, ResourceActions["project"], Action(actionReadPolicies))
assert.Contains(t, ResourceActions["project"], Action(actionManageS1))

srv, s := testServer(t)
ctx := context.Background()

project := &store.Project{
ID: tid("mp-cap-project"),
Name: "MP Cap Project",
Slug: "mp-cap-project",
}
require.NoError(t, s.CreateProject(ctx, project))

type roleCase struct {
name string
userID string
email string
systemRole string
projectRole string
wantActions []string
forbidAction []string
}

cases := []roleCase{
{
name: "project-owner",
userID: tid("mp-cap-owner"),
email: "mp-cap-owner@example.com",
projectRole: store.ProjectRoleOwner,
wantActions: []string{actionManagePolicies, actionReadPolicies, actionManageS1},
},
{
name: "project-admin",
userID: tid("mp-cap-admin"),
email: "mp-cap-admin@example.com",
projectRole: store.ProjectRoleAdmin,
wantActions: []string{actionManagePolicies, actionReadPolicies, actionManageS1},
},
{
name: "hub-admin",
userID: tid("mp-cap-hubadmin"),
email: "mp-cap-hubadmin@example.com",
systemRole: store.SystemRoleHubAdmin,
wantActions: []string{actionManagePolicies, actionReadPolicies, actionManageS1},
},
{
name: "project-member",
userID: tid("mp-cap-member"),
email: "mp-cap-member@example.com",
projectRole: store.ProjectRoleMember,
wantActions: []string{actionReadPolicies},
forbidAction: []string{actionManagePolicies, actionManageS1},
},
{
name: "project-viewer",
userID: tid("mp-cap-viewer"),
email: "mp-cap-viewer@example.com",
projectRole: store.ProjectRoleViewer,
wantActions: []string{actionReadPolicies},
forbidAction: []string{actionManagePolicies, actionManageS1},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if tc.systemRole != "" {
createTestUserWithRole(t, s, tc.userID, tc.email, store.UserRoleAdmin, tc.systemRole)
} else {
require.NoError(t, s.CreateUser(ctx, &store.User{
ID: tc.userID,
Email: tc.email,
DisplayName: tc.name,
Role: store.UserRoleMember,
Status: "active",
}))
projectOpsGrant(t, s, tc.userID, project.ID, tc.projectRole)
}

userRole := store.UserRoleMember
if tc.systemRole != "" {
userRole = store.UserRoleAdmin
}
ident := NewAuthenticatedUser(tc.userID, tc.email, tc.name, userRole, "session")

rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/projects/"+project.ID, nil).
WithContext(contextWithIdentity(ctx, ident))
srv.getProject(rec, req, project.ID)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())

var resp struct {
Capabilities *Capabilities `json:"_capabilities"`
}
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp))
require.NotNil(t, resp.Capabilities, "GET /api/v1/projects/{id} must populate _capabilities")

for _, want := range tc.wantActions {
assert.Contains(t, resp.Capabilities.Actions, want,
"%s must receive %q in project _capabilities.actions", tc.name, want)
}
for _, forbid := range tc.forbidAction {
assert.NotContains(t, resp.Capabilities.Actions, forbid,
"%s must not receive %q in project _capabilities.actions", tc.name, forbid)
}
})
}
}
24 changes: 24 additions & 0 deletions pkg/hub/experiments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,30 @@ func TestResolvedExperiments_IncludesProjectVisitors(t *testing.T) {
}
}

// TestResolvedExperiments_IncludesMessagePolicies: the browser reads
// GET /api/v1/experiments to gate the project Message Policies UI, so
// hub.message_policies must appear in the resolved web-layer map with its
// effective value (off by default, true when overridden on).
func TestResolvedExperiments_IncludesMessagePolicies(t *testing.T) {
srv := &Server{}
got := srv.resolvedExperiments()
if v, ok := got[experiments.MessagePolicies]; !ok || v {
t.Errorf("resolvedExperiments()[%q] = %v (present=%v), want false and present", experiments.MessagePolicies, v, ok)
}

fakeStore := newFakeHubSettingStore()
fakeStore.seed("experiments", json.RawMessage(`{"overrides":{"hub.message_policies":true}}`))
ops := NewOperationalSettings(fakeStore, emptyKoanf(), emptyKoanf())
if _, err := ops.Refresh(context.Background()); err != nil {
t.Fatalf("Refresh: %v", err)
}
on := &Server{}
on.SetOperationalSettings(ops)
if v, ok := on.resolvedExperiments()[experiments.MessagePolicies]; !ok || !v {
t.Errorf("with the override on: resolvedExperiments()[%q] = %v (present=%v), want true", experiments.MessagePolicies, v, ok)
}
}

// --- requireExperiment ---

func TestRequireExperiment_OffReturns404(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions web/e2e-perf/budgets/fixture-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"experiments.hub.flat_runtime_brokers:boolean",
"experiments.hub.github_triggers:boolean",
"experiments.hub.k8s_workload_identity_setup:boolean",
"experiments.hub.message_policies:boolean",
"experiments.hub.system_one:boolean",
"experiments.hub.triggers:boolean",
"experiments.hub.user_delete_force_cascade:boolean",
Expand Down
1 change: 1 addition & 0 deletions web/e2e-perf/budgets/fixture.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export function buildFixture() {
'hub.flat_runtime_brokers': true,
'hub.github_triggers': false,
'hub.k8s_workload_identity_setup': false,
'hub.message_policies': false,
'hub.system_one': false,
'hub.triggers': false,
'hub.user_delete_force_cascade': true,
Expand Down
14 changes: 14 additions & 0 deletions web/src/utils/feature-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,17 @@ describe('idleSuspendEnabled (lifecycle.idle_suspend)', () => {
expect(idleSuspendEnabled()).toBe(false);
});
});

describe('messagePoliciesEnabled (hub.message_policies)', () => {
it('defaults to false and follows server/explicit flags', async () => {
const { MESSAGE_POLICIES_FLAG, messagePoliciesEnabled } = await import('./feature-flags.js');
expect(MESSAGE_POLICIES_FLAG).toBe('hub.message_policies');
expect(messagePoliciesEnabled()).toBe(false);

setServerFlags({ 'hub.message_policies': true });
expect(messagePoliciesEnabled()).toBe(true);

setFeatureFlag('hub.message_policies', false);
expect(messagePoliciesEnabled()).toBe(false);
});
});
14 changes: 14 additions & 0 deletions web/src/utils/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ export function systemOneEnabled(): boolean {
return isFeatureEnabled(SYSTEM_ONE_FLAG);
}

/**
* Project message policies (hub.message_policies, ptone/scion#8047):
* exposes the message-policies experiment flag for UI presentation.
* Presentation only: the hub enforces the experiment (404 when off) and
* permissions on every /api/v1/projects/{id}/message-policies* route.
* Default OFF.
*/
export const MESSAGE_POLICIES_FLAG = 'hub.message_policies';

/** Whether project message policies UI surfaces are enabled in this session. */
export function messagePoliciesEnabled(): boolean {
return isFeatureEnabled(MESSAGE_POLICIES_FLAG);
}

/**
* Effective permissions viewer (hub.effective_permissions, ptone/scion#8439):
* shows the two-tab Roles dialog (Effective roles + Effective permissions) on
Expand Down
Loading