Skip to content
Merged
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
58 changes: 29 additions & 29 deletions packages/runner/src/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,6 @@ function followPointer<S extends BaseMemoryAddress>(
const target: BaseMemoryAddress = (link.id !== undefined)
? { id: link.id, type: "application/json" }
: doc.address;
const targetDoc = {
address: doc.address,
value: doc.value,
};
if (selector !== undefined) {
// We'll need to re-root the selector for the target doc
// Remove the portions of doc.path from selector.path, limiting schema if
Expand All @@ -492,43 +488,47 @@ function followPointer<S extends BaseMemoryAddress>(
// Cycle detected - treat this as notFound to avoid traversal
return [notFound(doc.address), selector];
}
// We may access portions of the doc outside what we have in our doc
// attestation, so reload the top level doc from the manager.
const valueEntry = manager.load(target);
if (valueEntry === null) {
return [notFound(doc.address), selector];
}
if (link.id !== undefined) {
// We have a reference to a different cell, so track the dependency
// We have a reference to a different doc, so track the dependency
// and update our targetDoc
const valueEntry = manager.load(target);
if (valueEntry === null) {
return [notFound(doc.address), selector];
}
if (schemaTracker !== undefined && selector !== undefined) {
schemaTracker.add(manager.toKey(target), selector);
}
// If the object we're pointing to is a retracted fact, just return undefined.
// We can't do a better match, but we do want to include the result so we watch this doc
if (valueEntry.value === undefined) {
return [notFound(target), selector];
// Load the sources/recipes recursively unless we're a retracted fact.
if (valueEntry.value !== undefined) {
loadSource(
manager,
valueEntry,
new Set<string>(),
schemaTracker,
);
}
// Otherwise, we can continue with the target.
// an assertion fact.is will be an object with a value property, and
// that's what our schema is relative to.
targetDoc.address = { ...target, path: ["value"] };
targetDoc.value = (valueEntry.value as Immutable<JSONObject>)["value"];
// Load any sources (recursively) if they exist and any linked recipes
loadSource(
manager,
valueEntry,
new Set<string>(),
schemaTracker,
);
}
// If the object we're pointing to is a retracted fact, just return undefined.
// We can't do a better match, but we do want to include the result so we watch this doc
if (valueEntry.value === undefined) {
return [notFound(target), selector];
}
// We can continue with the target, but provide the top level target doc
// to getAtPath.
// An assertion fact.is will be an object with a value property, and
// that's what our schema is relative to, so we'll grab the value part.
const targetDoc = {
address: { ...target, path: ["value"] },
value: (valueEntry.value as Immutable<JSONObject>)["value"],
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential runtime error: valueEntry.value may be null or non-object; guard with isObject before accessing ["value"].

Prompt for AI agents
Address the following comment on packages/runner/src/traverse.ts at line 524:

<comment>Potential runtime error: valueEntry.value may be null or non-object; guard with isObject before accessing [&quot;value&quot;].</comment>

<file context>
@@ -492,43 +488,47 @@ function followPointer&lt;S extends BaseMemoryAddress&gt;(
+  // that&#39;s what our schema is relative to, so we&#39;ll grab the value part.
+  const targetDoc = {
+    address: { ...target, path: [&quot;value&quot;] },
+    value: (valueEntry.value as Immutable&lt;JSONObject&gt;)[&quot;value&quot;],
+  };
 
</file context>
Suggested change
value: (valueEntry.value as Immutable<JSONObject>)["value"],
value: isObject(valueEntry.value) ? (valueEntry.value as Immutable<JSONObject>)["value"] : undefined,
Fix with Cubic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps an argument for a better return type for manager.load, where we indicate that if the valueEntry returned has a defined value, that it will be an object with a value property.
However, I'm not going to address that in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion—refining manager.load’s return type to encode the value shape (e.g., via a discriminated union or type guard) would help avoid unsafe property access. Understood that it won’t be changed in this PR; I’ll keep this in mind for future reviews.

};

// We've loaded the linked doc, so walk the path to get to the right part of that doc (or whatever doc that path leads to),
// then the provided path from the arguments.
return getAtPath(
manager,
{
address: targetDoc.address,
value: targetDoc.value,
},
targetDoc,
[...link.path, ...path] as string[],
tracker,
schemaTracker,
Expand Down
105 changes: 105 additions & 0 deletions packages/runner/test/traverse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { describe, it } from "@std/testing/bdd";
import { expect } from "@std/expect";
import { refer } from "merkle-reference/json";
import type {
Entity,
Revision,
State,
URI,
} from "@commontools/memory/interface";
import { SchemaObjectTraverser } from "../src/traverse.ts";
import { StoreObjectManager } from "../src/storage/query.ts";

describe("SchemaObjectTraverser.traverseDAG", () => {
it("follows legacy cell links when traversing", () => {
const store = new Map<string, Revision<State>>();
const type = "application/json" as const;
const doc1Uri = "of:doc-1" as URI;
const doc2Uri = "of:doc-2" as URI;
const doc1Entity = doc1Uri as Entity;
const doc2Entity = doc2Uri as Entity;

const doc1Value = { employees: [{ name: "Bob" }] };
const doc1EntityId = { "/": doc1Uri };

const doc1Revision: Revision<State> = {
the: type,
of: doc1Entity,
is: { value: doc1Value },
cause: refer({ the: type, of: doc1Entity }),
since: 1,
};
store.set(
`${doc1Revision.of}/${doc1Revision.the}`,
doc1Revision,
);

const doc2Value = {
employeeName: {
cell: doc1EntityId,
path: ["employees", "0", "name"],
},
argument: {
tools: {
search_web: {
pattern: {
result: {
$alias: {
path: ["internal", "__#0"],
},
},
},
},
},
},
internal: {
"__#0": {
name: "Foo",
},
},
};

const doc2Revision: Revision<State> = {
the: type,
of: doc2Entity,
is: { value: doc2Value },
cause: refer({ the: type, of: doc2Entity }),
since: 2,
};
store.set(
`${doc2Revision.of}/${doc2Revision.the}`,
doc2Revision,
);

const manager = new StoreObjectManager(store);
const traverser = new SchemaObjectTraverser(manager, {
path: [],
schemaContext: { schema: true, rootSchema: true },
});

const result = traverser.traverse({
address: { id: doc2Uri, type, path: ["value"] },
value: doc2Value,
});

expect(result).toEqual({
argument: {
tools: {
search_web: {
pattern: {
result: {
name: "Foo",
},
},
},
},
},
employeeName: "Bob",
internal: {
"__#0": {
name: "Foo",
},
},
});
});
});