Skip to content

Commit 62c8308

Browse files
chrisstIsaac
andauthored
Add experimental skill/<name> user-agent pairs from DATABRICKS_SKILLS_INVOKED (#7730)
## Changes **Experimental skill-usage tracking.** When `DATABRICKS_SKILLS_INVOKED=databricks-core,databricks-jobs` is set, the CLI adds one `skill/<name>` pair per skill to its user agent: `... skill/databricks-core skill/databricks-jobs`. Databricks agent skills will prefix their CLI calls with this env var (databricks/databricks-agent-skills#311). - **Env var only, no flag.** It can be removed later without changing the CLI's interface. Older CLIs also ignore unknown env vars, so skills need no version gate. - **Respects the telemetry opt-out.** Nothing is added when `DATABRICKS_CLI_DISABLE_TELEMETRY` is set. - **Normalization.** Entries are trimmed, lowercased and deduplicated. Entries not matching `^[a-z0-9][a-z0-9_.-]*$` (a subset of what the SDK allows in a user agent value) or longer than 64 bytes are dropped. Names are kept in order while all ` skill/<name>` pairs fit in 225 bytes (five of the longest current skill names), so the SDK's `auth/`, `agent/` and other pairs stay within the 512-character usage-log cap. Bad input never fails or warns a command; dropped entries are only logged at debug level. - **Placement.** It lives in `cmd/root/user_agent_skills.go` next to the other user-agent dimensions and is applied in the root `PersistentPreRunE`. Every request that already carries `cmd/` carries the skills too. The DECO usage pipeline (`deco/bundles/http_access_logs` → `deco_usage_logs_normalized`) parses repeated keys into `unified_user_agent.other_info_multi`, as it already does for the repeated `aitools/` pairs. So the skills land in `other_info_multi['skill']` with no pipeline change. **Known gap:** the files/v2 client used by `fs` on `/Volumes` builds its own user agent without any of the CLI's context pairs (no `cli/` or `cmd/` either), so those requests carry no skills. ## Why To measure which Databricks agent skills drive CLI usage, per request, from the existing HTTP access logs, with no new header or log column. ## Tests - Unit (`cmd/root_test`): parsing table (normalization, disallowed characters including `:`, name length, and 225-byte budget boundaries), and the user-agent pairs with and without the telemetry opt-out. Both env vars are always set in the context so a host value can't leak in. - Acceptance (`acceptance/cmd/skills-invoked`): asserts `skill/` pairs in the recorded user agent for `current-user me`, `api get`, and `bundle validate`. It also covers normalization, no pairs when unset, and no pairs with telemetry disabled. A host `DATABRICKS_SKILLS_INVOKED` is unset in `acceptance_test.go`. This pull request and its description were written by Isaac. --------- Co-authored-by: Isaac <no-reply@databricks.com>
1 parent e4e790c commit 62c8308

10 files changed

Lines changed: 269 additions & 0 deletions

File tree

‎acceptance/acceptance_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ func testAccept(t *testing.T, inprocessMode bool, selectedTests []string, skipTo
293293
// from the host shell would move the venv the localenv tests expect.
294294
os.Unsetenv("UV_PROJECT_ENVIRONMENT") //nolint:usetesting // t.Setenv cannot unset
295295

296+
// Agent skills set DATABRICKS_SKILLS_INVOKED (experimental skill tracking),
297+
// so a host value would add skill/ pairs to recorded user agents.
298+
os.Unsetenv("DATABRICKS_SKILLS_INVOKED") //nolint:usetesting // t.Setenv cannot unset
299+
296300
if !requirePrerequisites(t) {
297301
// Don't run the suite against a stale toolchain; the failed subtest
298302
// has already marked the parent test as failed.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bundle:
2+
name: skills-invoked
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cloud = false
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
=== Env var adds the skills to the user agent on a workspace GET
3+
4+
>>> [CLI] current-user me
5+
6+
>>> print_requests.py --get //scim/v2/Me
7+
8+
=== Env var adds the skills on databricks api
9+
10+
>>> [CLI] api get /api/2.1/clusters/list
11+
12+
>>> print_requests.py --get //clusters/list
13+
14+
=== Env var adds the skills from bundle commands
15+
16+
>>> [CLI] bundle validate
17+
18+
>>> print_requests.py --get //scim/v2/Me
19+
20+
=== Invalid entries are dropped and the rest is normalized
21+
22+
>>> [CLI] current-user me
23+
24+
>>> print_requests.py --get //scim/v2/Me
25+
26+
=== No skills when unset
27+
28+
>>> [CLI] current-user me
29+
30+
>>> print_requests.py --get //scim/v2/Me
31+
32+
=== No skills when telemetry is disabled
33+
34+
>>> [CLI] current-user me
35+
36+
>>> print_requests.py --get //scim/v2/Me
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Experimental skill-usage tracking: DATABRICKS_SKILLS_INVOKED adds skill/<name> pairs to the user agent.
2+
# The rest of the user agent varies by machine, so each request dump goes to a LOG.* file (kept for
3+
# diagnosis, excluded from the diff) and only the pairs are asserted.
4+
5+
title "Env var adds the skills to the user agent on a workspace GET"
6+
DATABRICKS_SKILLS_INVOKED=databricks-core,databricks-jobs trace $CLI current-user me > /dev/null
7+
trace print_requests.py --get //scim/v2/Me > LOG.me
8+
contains.py " skill/databricks-core skill/databricks-jobs" < LOG.me > /dev/null
9+
10+
title "Env var adds the skills on databricks api"
11+
DATABRICKS_SKILLS_INVOKED=databricks-core MSYS_NO_PATHCONV=1 trace $CLI api get /api/2.1/clusters/list > /dev/null
12+
trace print_requests.py --get //clusters/list > LOG.api
13+
contains.py " skill/databricks-core" < LOG.api > /dev/null
14+
15+
title "Env var adds the skills from bundle commands"
16+
DATABRICKS_SKILLS_INVOKED=databricks-dabs trace $CLI bundle validate > /dev/null
17+
trace print_requests.py --get //scim/v2/Me > LOG.bundle
18+
contains.py " skill/databricks-dabs" < LOG.bundle > /dev/null
19+
20+
title "Invalid entries are dropped and the rest is normalized"
21+
DATABRICKS_SKILLS_INVOKED=' Databricks-Core , bad name!,,databricks-core,databricks:databricks-jobs ' trace $CLI current-user me > /dev/null
22+
trace print_requests.py --get //scim/v2/Me > LOG.normalized
23+
contains.py " skill/databricks-core" "!skill/databricks-core skill/" "!bad" "!databricks:" < LOG.normalized > /dev/null
24+
25+
title "No skills when unset"
26+
trace $CLI current-user me > /dev/null
27+
trace print_requests.py --get //scim/v2/Me > LOG.unset
28+
contains.py "!skill/" < LOG.unset > /dev/null
29+
30+
title "No skills when telemetry is disabled"
31+
DATABRICKS_SKILLS_INVOKED=databricks-core DATABRICKS_CLI_DISABLE_TELEMETRY=1 trace $CLI current-user me > /dev/null
32+
trace print_requests.py --get //scim/v2/Me > LOG.disabled
33+
contains.py "!skill/" < LOG.disabled > /dev/null
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RecordRequests = true
2+
IncludeRequestHeaders = ["User-Agent"]
3+
Ignore = [".databricks"]
4+
# A host opt-out would otherwise remove the skills the positive cases assert.
5+
Env.DATABRICKS_CLI_DISABLE_TELEMETRY = ""

‎cmd/root/export_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9+
// Experimental skill-usage tracking, see user_agent_skills.go.
10+
var (
11+
ParseSkills = parseSkills
12+
WithSkillsInUserAgent = withSkillsInUserAgent
13+
SkillsInvokedEnvVar = skillsInvokedEnvVar
14+
MaxSkillsUserAgentLength = maxSkillsUserAgentLength
15+
)
16+
917
// BuildAll builds every command in t and returns the root command, for tests
1018
// that walk the whole tree.
1119
func BuildAll(ctx context.Context, t *Tree) *cobra.Command {

‎cmd/root/root.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func newRootCommand(ctx context.Context) *cobra.Command {
8888
ctx = installer.WithAiDevKitInUserAgent(ctx)
8989
ctx = installer.WithCallingAgentToolingInUserAgent(ctx)
9090
ctx = InjectTestPidToUserAgent(ctx)
91+
ctx = withSkillsInUserAgent(ctx)
9192
cmd.SetContext(ctx)
9293

9394
// Recommend installing Databricks AI tooling to the calling coding agent

‎cmd/root/user_agent_skills.go‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package root
2+
3+
import (
4+
"context"
5+
"regexp"
6+
"slices"
7+
"strings"
8+
"sync"
9+
10+
"github.com/databricks/cli/libs/env"
11+
"github.com/databricks/cli/libs/log"
12+
"github.com/databricks/cli/libs/telemetry"
13+
"github.com/databricks/databricks-sdk-go/useragent"
14+
)
15+
16+
// Experimental skill-usage tracking: the databricks-core agent skill
17+
// (https://github.com/databricks/databricks-agent-skills/tree/main/skills/databricks-core)
18+
// tells agents to prefix CLI calls with this env var, set to the comma-separated
19+
// bare names of the skills they loaded. It may change or be removed without
20+
// notice. It is an env var rather than a flag so removing it doesn't change the
21+
// CLI's interface, and because older CLIs ignore unknown env vars but reject
22+
// unknown flags.
23+
const skillsInvokedEnvVar = "DATABRICKS_SKILLS_INVOKED"
24+
25+
// skillUserAgentKey is repeated once per skill: "skill/databricks-core skill/databricks-jobs".
26+
const skillUserAgentKey = "skill"
27+
28+
const (
29+
maxSkillLength = 64
30+
31+
// maxSkillsUserAgentLength bounds all " skill/<name>" pairs together. It
32+
// fits five of the longest current skill names (45-byte pairs). The usage
33+
// logs that feed skill analytics cap the user agent at 512 characters, and
34+
// the rest of the CLI's user agent (auth/, agent/ and other pairs, some
35+
// appended after these) takes about 200, so the skills must leave room for it.
36+
maxSkillsUserAgentLength = 225
37+
)
38+
39+
// skillNamePattern is a subset of what the SDK accepts in a user agent value,
40+
// so a valid name can never make useragent.InContext panic.
41+
var skillNamePattern = sync.OnceValue(func() *regexp.Regexp {
42+
return regexp.MustCompile(`^[a-z0-9][a-z0-9_.-]*$`)
43+
})
44+
45+
// parseSkills normalizes a comma-separated list of skill names: entries are
46+
// trimmed, lowercased and deduplicated, invalid ones are dropped, and names are
47+
// kept in order while their pairs fit in maxSkillsUserAgentLength. Invalid input is only logged at debug level because this
48+
// is telemetry and must never fail or warn a command.
49+
func parseSkills(ctx context.Context, raw string) []string {
50+
var skills []string
51+
size := 0
52+
for entry := range strings.SplitSeq(raw, ",") {
53+
name := strings.ToLower(strings.TrimSpace(entry))
54+
pairSize := len(" "+skillUserAgentKey+"/") + len(name)
55+
switch {
56+
case name == "":
57+
case len(name) > maxSkillLength || !skillNamePattern().MatchString(name):
58+
log.Debugf(ctx, "dropping invalid %s entry %q", skillsInvokedEnvVar, entry)
59+
case slices.Contains(skills, name):
60+
case size+pairSize > maxSkillsUserAgentLength:
61+
log.Debugf(ctx, "dropping %s entry %q: skills exceed %d user agent bytes", skillsInvokedEnvVar, name, maxSkillsUserAgentLength)
62+
default:
63+
skills = append(skills, name)
64+
size += pairSize
65+
}
66+
}
67+
return skills
68+
}
69+
70+
// withSkillsInUserAgent adds one skill/<name> pair per skill in
71+
// skillsInvokedEnvVar (experimental tracking), unless the user opted out of
72+
// telemetry.
73+
func withSkillsInUserAgent(ctx context.Context) context.Context {
74+
if telemetry.Disabled(ctx) {
75+
return ctx
76+
}
77+
for _, skill := range parseSkills(ctx, env.Get(ctx, skillsInvokedEnvVar)) {
78+
// Each skill adds its own pair to the same context; this is accumulation,
79+
// not the accidental nesting fatcontext flags.
80+
ctx = useragent.InContext(ctx, skillUserAgentKey, skill) //nolint:fatcontext
81+
}
82+
return ctx
83+
}

‎cmd/root/user_agent_skills_test.go‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package root_test
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/databricks/cli/cmd/root"
9+
"github.com/databricks/cli/libs/env"
10+
"github.com/databricks/databricks-sdk-go/useragent"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
// skillNames returns n distinct skill names of exactly length bytes.
15+
func skillNames(n, length int) []string {
16+
names := make([]string, 0, n)
17+
for i := range n {
18+
prefix := fmt.Sprintf("s%02d", i)
19+
names = append(names, prefix+strings.Repeat("x", length-len(prefix)))
20+
}
21+
return names
22+
}
23+
24+
// pairsSize is the length of " skill/<name>" for every name.
25+
func pairsSize(names []string) int {
26+
size := 0
27+
for _, name := range names {
28+
size += len(" skill/") + len(name)
29+
}
30+
return size
31+
}
32+
33+
func TestParseSkills(t *testing.T) {
34+
// 38-byte names (the longest current skill) make 45-byte pairs: five fill
35+
// 225 bytes exactly and a sixth doesn't fit.
36+
fiveFit := skillNames(5, 38)
37+
sixTooMany := skillNames(6, 38)
38+
// 64-, 64-, 64- and 22-byte pairs make 214 bytes, so " skill/abcd" (11)
39+
// lands exactly on 225 and nothing more fits.
40+
exact := []string{strings.Repeat("a", 57), strings.Repeat("b", 57), strings.Repeat("c", 57), "databricks-core", "abcd"}
41+
42+
tests := []struct {
43+
name string
44+
raw string
45+
want []string
46+
}{
47+
{"empty", "", nil},
48+
{"single", "databricks-core", []string{"databricks-core"}},
49+
{"multiple", "a,b,c", []string{"a", "b", "c"}},
50+
{"trims and lowercases", " Databricks-Core , JOBS ", []string{"databricks-core", "jobs"}},
51+
{"drops empty entries", ",,a,,", []string{"a"}},
52+
{"allows underscore and dot", "a_b.c-d", []string{"a_b.c-d"}},
53+
{"drops disallowed skill-name characters", "ok,bad name,a:b,a/b,a+b", []string{"ok"}},
54+
{"drops leading punctuation", "-a,.b,_c", nil},
55+
{"dedupes preserving first occurrence", "b,a,b,a,c", []string{"b", "a", "c"}},
56+
{"dedupes after normalization", "Core,core, CORE", []string{"core"}},
57+
{"drops entry longer than 64 bytes", strings.Repeat("a", 65) + ",ok", []string{"ok"}},
58+
{"keeps entry of exactly 64 bytes", strings.Repeat("a", 64), []string{strings.Repeat("a", 64)}},
59+
{"keeps five of the longest skill names", strings.Join(fiveFit, ","), fiveFit},
60+
{"drops pairs over the budget", strings.Join(sixTooMany, ","), sixTooMany[:5]},
61+
{"fills the budget exactly", strings.Join(exact, ",") + ",d", exact},
62+
}
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
got := root.ParseSkills(t.Context(), tt.raw)
66+
assert.Equal(t, tt.want, got)
67+
assert.LessOrEqual(t, pairsSize(got), root.MaxSkillsUserAgentLength)
68+
})
69+
}
70+
}
71+
72+
func TestWithSkillsInUserAgent(t *testing.T) {
73+
tests := []struct {
74+
name string
75+
skills string
76+
disableTelemetry string
77+
want string
78+
}{
79+
{"unset", "", "", ""},
80+
{"set", "databricks-core,databricks-jobs", "", "skill/databricks-core skill/databricks-jobs"},
81+
{"telemetry disabled", "databricks-core", "1", ""},
82+
}
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
// Always set both: an empty value in the context overrides the host environment.
86+
ctx := env.Set(t.Context(), root.SkillsInvokedEnvVar, tt.skills)
87+
ctx = env.Set(ctx, "DATABRICKS_CLI_DISABLE_TELEMETRY", tt.disableTelemetry)
88+
ua := useragent.FromContext(root.WithSkillsInUserAgent(ctx))
89+
if tt.want == "" {
90+
assert.NotContains(t, ua, "skill/")
91+
} else {
92+
assert.Contains(t, ua, tt.want)
93+
}
94+
})
95+
}
96+
}

0 commit comments

Comments
 (0)