Skip to content

Commit cf7b488

Browse files
authored
fix(workspace-source): locate every finding, hint nested folder creates, and plan managed rows as stored (#10558)
* fix(workspace-source): locate every finding, hint nested folder creates, and plan managed rows as stored - kindFinding defaults file to the kind's declarationPath; rows and doc findings point at their file - E_TABLE_FOLDER/E_KB_FOLDER hint one folders create per missing level, parent first - E_PLAN_REF_DELETED and hold findings point at the workflow line using the resource - a workspace plan reads secrets.ts names as known, so E_SECRET_MISSING (at the read) replaces W_ENV_UNKNOWN - E_NAME_TAKEN names the refused declaration - planRows compares coerced declared cells and reports +added ~patched -removed - docs: a get prints only secrets that have a value * fix(workspace-source): keep reference spans through id resolution and locate E_PLAN_REF_HELD
1 parent 5b518c2 commit cf7b488

41 files changed

Lines changed: 374 additions & 192 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/docs/content/docs/workflows/code/diagnostics.mdx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ Workspace source reports every workflow code above for the files of `workflows/<
239239
| Code | Meaning |
240240
| --- | --- |
241241
| `E_SECRET_NAME` | A declared name outside the secret name format. |
242-
| `E_SECRET_MISSING` | A workflow uses a declared secret the workspace holds no value for: it is held back. |
242+
| `E_SECRET_MISSING` | A workflow reads a declared secret the workspace holds no value for: it is held back. The finding points at the first line that reads it, and replaces `W_ENV_UNKNOWN` for that name. |
243243
| `W_SECRET_MISSING` | A declared secret the workspace holds no value for. |
244244

245245
#### Credentials (`credentials.ts`)
@@ -260,7 +260,7 @@ Workspace source reports every workflow code above for the files of `workflows/<
260260
| `E_TABLE_TOO_MANY_COLUMNS` | More columns than a table may hold: a size limit, classed like `E_LIMIT_EXCEEDED`. |
261261
| `E_TABLE_OPTIONS` | A select column without options, an option declared twice, or options on another type. |
262262
| `E_TABLE_TYPE_CONVERSION` | A type change the stored cells cannot convert to. |
263-
| `E_TABLE_FOLDER` | A table folder that does not exist in the workspace. |
263+
| `E_TABLE_FOLDER` | A table folder that does not exist in the workspace. The hint creates each missing folder, parent first. |
264264
| `E_TABLE_ROWS` | The declaration's `rows` or its `tables/<key>.rows.jsonl`: a bad mode or key column, a line that is not a row of declared columns, a managed row without its key or repeating one. |
265265
| `I_TABLE_SEED_SKIPPED` | Seed rows declared for a table that already holds rows: only a new or empty table is seeded. |
266266
| `I_TABLE_ROWS_UNKEYED` | Managed rows whose key cell is empty (a row the table's "add row" made and nobody keyed): no file line can name them, so a get leaves them out and an apply keeps them as they are. |
@@ -311,7 +311,7 @@ Workspace source reports every workflow code above for the files of `workflows/<
311311
| `E_KB_DOC_NAME` | A document file name or type the knowledge base cannot hold. |
312312
| `E_KB_DOC_EMPTY` | An empty document, which a knowledge base cannot index. |
313313
| `E_KB_CHUNKING` | Chunking settings outside what a knowledge base accepts. |
314-
| `E_KB_FOLDER` | A knowledge base folder that does not exist in the workspace. |
314+
| `E_KB_FOLDER` | A knowledge base folder that does not exist in the workspace. The hint creates each missing folder, parent first. |
315315
| `W_KB_DOC_UNCHECKED` | A stored document whose bytes this request had no budget left to download and compare. |
316316
| `W_KB_DOC_FAILED` | Documents whose indexing failed: search does not find them until they are indexed again. |
317317
| `I_KB_REEMBED` | How many documents the apply indexes (embeds) after it writes them. |
@@ -346,7 +346,7 @@ Workspace source reports every workflow code above for the files of `workflows/<
346346
| `E_APPLY_REFUSED` | A change a child operation refused, with its message. |
347347
| `E_APPLY_FAILED` | A write failed for a reason that is not the source's (logged on the server). |
348348
| `E_PLAN_TOO_LARGE` | The apply would make more changes or touch more workflows than one request may. |
349-
| `E_PLAN_REF_DELETED` | A declared resource references one this apply deletes. |
349+
| `E_PLAN_REF_DELETED` | A declared resource references one this apply deletes. For an edited workflow, the finding points at the line that uses it. |
350350
| `E_PLAN_REF_HELD` | A declared resource references one this apply holds back, so it is held back too. |
351351
| `W_RESOURCE_IN_USE` | A resource this apply deletes is still used by stored workflows. |
352352
| `W_TABLE_COLUMN_RENAMED_IN_USE` | A table column this apply renames is still written, filtered or sorted by its old name in a stored workflow's Table block, which the run then drops. |

‎apps/docs/content/docs/workflows/code/directory.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ A resource's key is its file or folder name, and a workflow names it by that key
5454

5555
{/* workflow-code-docs:directory-kinds:begin — generated by `bun run generate:workflow-code-docs`; do not edit between markers */}
5656

57-
- `secrets.ts`: `export default defineSecrets({ NAME: { description? } })`, the workspace secrets workflows read, by name and description only (never a value: set one with `sim secrets set NAME --scope workspace`). A workflow: `env.NAME`; one using a declared secret the workspace has no value for is held back, and the rest apply.
57+
- `secrets.ts`: `export default defineSecrets({ NAME: { description? } })`, the workspace secrets workflows read, by name and description only (never a value: set one with `sim secrets set NAME --scope workspace`). A workflow: `env.NAME`; one using a declared secret the workspace has no value for is held back, and the rest apply. A get prints only the secrets the workspace has a value for, so a declared secret without one stays only in your directory.
5858
- `credentials.ts`: `export default defineCredentials({ <key>: { provider, name? } })`, binding each key to this workspace's credential for that provider (never a token): the one bound already, else the only one, else the one whose display name is `name`. A workflow: `credential('<key>')`; one using a key no credential binds to is held back, and the rest apply.
5959
- `tools/<key>.ts` (`defineTool({ title, schema, code: codeFile('./<key>.js') })`) and its code in `tools/<key>.js`, where each schema parameter is a variable. Code holding a literal credential prints as `code: stored()`, which keeps the stored code. A workflow: `customTool('<key>')`.
6060
- `skills/<key>.md`: a frontmatter block of `name` and `description`, then the content. A workflow: `skills: [skill('<key>')]` on an agent.

‎apps/sim/lib/workflows/application/source-pipeline/evaluate/resolve.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function withPendingResources(
8686

8787
/**
8888
* Every `(kind, key)` resource reference in a compiled model, for ident lookup, and the tool each
89-
* MCP agent tool (`mcp(key).tool(name)`) names on its server.
89+
* MCP agent tool (`mcp(key).tool(name)`) names on its server, each at `span`.
9090
*/
9191
function collectResourceKeys(
9292
value: unknown,
@@ -105,7 +105,7 @@ function collectResourceKeys(
105105
isResourceKind(record.kind) &&
106106
typeof record.key === 'string'
107107
) {
108-
refs.push({ kind: record.kind, key: record.key })
108+
refs.push({ kind: record.kind, key: record.key, ...(span ? { span } : {}) })
109109
}
110110
const tool = mcpToolReference(record)
111111
if (tool) refs.push(span ? { ...tool, span } : tool)
@@ -183,10 +183,13 @@ export async function resolveSubmission(
183183

184184
const compiledKeys: SourceReference[] = []
185185
for (const [key, block] of Object.entries(compiledWorkflow.blocks)) {
186-
const span =
187-
compiled.spans[encodeAnchor({ kind: 'param', block: key, param: 'tools' })] ??
188-
compiled.spans[encodeAnchor({ kind: 'block', block: key })]
189-
collectResourceKeys(block, compiledKeys, span)
186+
const blockSpan = compiled.spans[encodeAnchor({ kind: 'block', block: key })]
187+
const { params, ...rest } = block
188+
for (const [param, value] of Object.entries(params)) {
189+
const span = compiled.spans[encodeAnchor({ kind: 'param', block: key, param })] ?? blockSpan
190+
collectResourceKeys(value, compiledKeys, span)
191+
}
192+
collectResourceKeys(rest, compiledKeys, blockSpan)
190193
}
191194
const storedResources = resources
192195
let filesWithheld: Promise<boolean> | undefined

‎apps/sim/lib/workflows/application/source-pipeline/evaluate/types.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
CanonicalWorkflow,
55
CanonicalWorkflowMeta,
66
ChildResultFields,
7+
EnvReference,
78
LockIdentity,
89
ResourceKeyResolver,
910
ResourceKind,
@@ -77,7 +78,7 @@ export interface SourceEvaluationInput {
7778
*/
7879
export interface SourceReference extends ResourceKeyRef {
7980
member?: string
80-
/** Where the entry file writes the reference, for an MCP tool: the agent's `tools`. */
81+
/** Where the source writes the reference: the block param that holds it, else the block. */
8182
span?: SourceSpan
8283
}
8384

@@ -138,8 +139,8 @@ export interface SourcePlan {
138139
resources: ResourceKeyResolver
139140
/** Every resource the submitted source names by key, as the compiler read it. */
140141
references: SourceReference[]
141-
/** Every env name the submitted source reads, as the compiler read it. */
142-
envReferences: string[]
142+
/** Every env name the submitted source reads, and where, as the compiler read it. */
143+
envReferences: EnvReference[]
143144
/** The child result fields the compile typed with, which every print of the plan types with. */
144145
childResults?: ReadonlyMap<string, ChildResultFields>
145146
}

‎apps/sim/lib/workflows/application/source-pipeline/load.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ export type SourceTarget =
7777
export interface SourceLoads {
7878
catalogGate?: Promise<CatalogGate>
7979
envNames?: Promise<string[]>
80+
/**
81+
* The env names the workspace directory's `secrets.ts` declares, which a plan reads as known, as
82+
* the offline check does: a workflow that reads one the workspace holds no value for is held
83+
* back (`E_SECRET_MISSING`, at the read) rather than warned about (`W_ENV_UNKNOWN`).
84+
*/
85+
declaredEnvNames?: readonly string[]
8086
/** The child workflows read to type their callers' results (`loadChildResults`). */
8187
childResults?: ChildResultsMemo
8288
/** The workflow folders the workspace source apply writes, which its callers are typed by. */
@@ -174,7 +180,7 @@ export async function loadSourceTarget(
174180
storedCanonical.canonical,
175181
storedCanonical.resources
176182
),
177-
envNames,
183+
envNames: [...envNames, ...(loads.declaredEnvNames ?? [])],
178184
scope,
179185
}
180186
})

‎apps/sim/lib/workflows/application/source-pipeline/plan.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Principal } from '@sim/auth/principal'
22
import type {
33
BlockCatalog,
4+
EnvReference,
45
ResourceKind,
56
SourceBase,
67
SourceFingerprint,
@@ -113,7 +114,7 @@ export interface SourcePlanSummary {
113114
diff: SourceDiffSummary
114115
/** What the source references, as the compiler read it; empty when it did not compile. */
115116
references: readonly SourceReference[]
116-
envReferences: readonly string[]
117+
envReferences: readonly EnvReference[]
117118
/** Whether a graph no-op still changes source meta (idents, order or comments). */
118119
sourceMetaChanged: boolean
119120
/** The workflow's name after the write; absent for a refusal. */

‎apps/sim/lib/workspace-source/application/apply.integration.ts‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,36 @@ describe('workspace source apply', () => {
715715
const taken = await refusal(
716716
apply(workspaceId, { 'tables/other.ts': LEADS }, { kinds: ['table'], only: ['table'] })
717717
)
718-
expect(codes(taken.diagnostics)).toContain('E_NAME_TAKEN')
718+
expect(taken.diagnostics).toContainEqual(
719+
expect.objectContaining({
720+
code: 'E_NAME_TAKEN',
721+
file: 'tables/other.ts',
722+
message:
723+
'tables/other declares the table name "leads", which belongs to the table under key "leads".',
724+
})
725+
)
726+
}
727+
)
728+
729+
check(
730+
'a missing nested table folder hints one create per missing level, parent first',
731+
async () => {
732+
const workspaceId = await newWorkspace()
733+
const nested = LEADS.replace("name: 'leads',", "name: 'leads',\n folder: '/qa/sub',")
734+
735+
const refused = await refusal(
736+
apply(workspaceId, { 'tables/leads.ts': nested }, { kinds: ['table'], only: ['table'] })
737+
)
738+
739+
expect(refused.diagnostics).toEqual([
740+
expect.objectContaining({
741+
code: 'E_TABLE_FOLDER',
742+
file: 'tables/leads.ts',
743+
hint: expect.stringContaining(
744+
'Create it first (`sim tables folders create "qa"`, then `sim tables folders create "qa/sub"`)'
745+
),
746+
}),
747+
])
719748
}
720749
)
721750

@@ -754,7 +783,13 @@ describe('workspace source apply', () => {
754783
)
755784

756785
expect(codes(refused.diagnostics)).toEqual(['E_PLAN_REF_DELETED'])
757-
expect(refused.diagnostics[0]).toMatchObject({ kind: 'workflow', key: 'ingest' })
786+
const source = files['workflows/ingest/workflow.ts'].split('\n')
787+
expect(refused.diagnostics[0]).toMatchObject({
788+
kind: 'workflow',
789+
key: 'ingest',
790+
file: 'workflows/ingest/workflow.ts',
791+
line: source.findIndex((line) => line.includes("'Save Lead'")) + 1,
792+
})
758793
expect(await tables(workspaceId)).toHaveLength(1)
759794
}
760795
)

‎apps/sim/lib/workspace-source/application/files.integration.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,9 @@ export default workflow('Reader', {}, () => {
10181018
.filter((finding) => finding.severity === 'error')
10191019
.map((finding) => `${finding.code} ${finding.kind}:${finding.key}`)
10201020
).toEqual(['E_PLAN_REF_DELETED workflow:reader'])
1021+
expect(
1022+
refused.diagnostics.find((finding) => finding.code === 'E_PLAN_REF_DELETED')
1023+
).toMatchObject({ file: 'workflows/reader/workflow.ts', line: expect.any(Number) })
10211024
}
10221025
)
10231026
check(

0 commit comments

Comments
 (0)