Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose planner API via mahler/planner #23

Merged
merged 2 commits into from
Aug 9, 2023
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
1 change: 0 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './agent';
export * from './planner';
export * from './observable';
export * from './sensor';
export * from './task';
Expand Down
22 changes: 4 additions & 18 deletions lib/planner/findPlan.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Context } from '../context';
import { Diff } from '../diff';
import { createHash } from 'crypto';
import { Operation } from '../operation';
import { Path } from '../path';
import { Pointer } from '../pointer';
import { Action, Instruction, Method, Task } from '../task';
import { PlannerConfig, Plan, Node } from './types';
import { PlannerConfig } from './types';
import { Plan, Node } from './plan';
import { isTaskApplicable } from './utils';
import assert from '../assert';

Expand All @@ -30,21 +30,6 @@ function planHasId<T>(id: string, node: Node<T> | null): boolean {
return false;
}

function createNodeId<TState>(s: TState, a: Action<TState, any, any>): string {
// md5 should be good enough for this purpose
// and it's the fastest of the available options
return createHash('md5')
.update(
JSON.stringify({
id: a.id,
path: a.path,
state: s,
...(a.target && { target: a.target }),
}),
)
.digest('hex');
}

function tryAction<TState = any>(
action: Action<TState>,
{
Expand All @@ -67,7 +52,8 @@ function tryAction<TState = any>(
assert(initialPlan.success);

// Generate an id for the potential node
const id = createNodeId(initialPlan.state, action);
const node = Node.of(initialPlan.state, action);
const id = node.id;

// Detect loops in the plan
if (planHasId(id, initialPlan.start)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/planner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Diff } from '../diff';
import { Target } from '../target';
import { Task } from '../task';
import { findPlan } from './findPlan';
import { PlannerConfig, Plan, Node } from './types';
import { PlannerConfig } from './types';
import { Plan, Node } from './plan';

export * from './types';
export * from './plan';

export interface Planner<TState = any> {
/**
Expand Down
86 changes: 86 additions & 0 deletions lib/planner/plan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { createHash } from 'crypto';

import { Action } from '../task';
import { PlanningStats } from './types';

/**
* A node defines a specific step in a plan.
*/
export interface Node<TState> {
/**
* Unique id for the node. This is calculated from the
* action metadata and the current runtime state expected
* by the planner. This is used for loop detection in the plan.
*/
readonly id: string;

/**
* The action to execute
*/
readonly action: Action<TState, any, any>;

/**
* The next step in the plan
*/
next: Node<TState> | null;
}

export type Plan<TState> =
| {
/**
* A plan was found
*/
success: true;

/**
* The initial step in the plan. If the start
* node is null, that means the plan is empty.
*/
start: Node<TState> | null;

/**
* The expected state at the end of the plan. This is
* probably not useful for end users, but is useful to keep
* track of intermediate steps in the planning process.
*/
state: TState;

/**
* The resulting stats of the planning process
*/
stats: PlanningStats;
}
| {
/**
* A plan could not be found
*/
success: false;

/**
* The resulting stats of the planning process
*/
stats: PlanningStats;
};

export const Node = {
of: <TState>(s: TState, a: Action<TState, any, any>): Node<TState> => {
// md5 should be good enough for this purpose
// and it's the fastest of the available options
const id = createHash('md5')
.update(
JSON.stringify({
id: a.id,
path: a.path,
state: s,
...(a.target && { target: a.target }),
}),
)
.digest('hex');

return {
id,
action: a,
next: null,
};
},
};
61 changes: 0 additions & 61 deletions lib/planner/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Action } from '../task';

/**
* Stats about the planning process
*/
Expand Down Expand Up @@ -27,62 +25,3 @@ export interface PlannerConfig {
*/
trace: (...args: any[]) => void;
}

/**
* A node defines a specific step in a plan.
*/
export interface Node<TState> {
/**
* Unique id for the node. This is calculated from the
* action metadata and the current runtime state expected
* by the planner. This is used for loop detection in the plan.
*/
readonly id: string;

/**
* The action to execute
*/
readonly action: Action<TState, any, any>;

/**
* The next step in the plan
*/
next: Node<TState> | null;
}

export type Plan<TState> =
| {
/**
* A plan was found
*/
success: true;

/**
* The initial step in the plan. If the start
* node is null, that means the plan is empty.
*/
start: Node<TState> | null;

/**
* The expected state at the end of the plan. This is
* probably not useful for end users, but is useful to keep
* track of intermediate steps in the planning process.
*/
state: TState;

/**
* The resulting stats of the planning process
*/
stats: PlanningStats;
}
| {
/**
* A plan could not be found
*/
success: false;

/**
* The resulting stats of the planning process
*/
stats: PlanningStats;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"main": "build/index.js",
"exports": {
".": "./build/index.js",
"./testing": "./build/testing/index.js"
"./testing": "./build/testing/index.js",
"./planner": "./build/planner/index.js"
},
"types": "build/index.d.ts",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion tests/composer/planner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Planner } from 'mahler';
import { Planner } from 'mahler/planner';
import { console } from '~/test-utils';

import { App } from './state';
Expand Down
2 changes: 1 addition & 1 deletion tests/orchestrator/planner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Planner } from 'mahler';
import { Planner } from 'mahler/planner';
import { console } from '~/test-utils';

import { Device } from './state';
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"mahler/testing": [
"lib/testing/index.ts"
],
"mahler/planner": [
"lib/planner/index.ts"
],
"~/test-utils": [
"tests/index.ts"
]
Expand Down